- fixed bumpedge test for obstacles - fixed obstacle crash when going to the south or east - added wasm builder
111 lines
2.4 KiB
Go
111 lines
2.4 KiB
Go
package grid
|
|
|
|
import (
|
|
"openquell/components"
|
|
. "openquell/config"
|
|
)
|
|
|
|
// FIXME: make available everywhere
|
|
func (grid *Grid) CheckGridCollision(
|
|
playerposition *components.Position,
|
|
velocity *components.Velocity) {
|
|
|
|
if velocity.Moving() {
|
|
ok, newpos := grid.BumpEdge(playerposition, velocity)
|
|
if ok {
|
|
//slog.Debug("falling off the edge", "newpos", newpos)
|
|
playerposition.Set(newpos)
|
|
} else {
|
|
ok, tilepos := grid.GetSolidNeighborPosition(playerposition, velocity, true)
|
|
if ok {
|
|
// slog.Debug("HaveSolidNeighbor", "ok", ok, "tilepos",
|
|
// tilepos.Point(), "playerpos", playerposition)
|
|
intersects, newpos := tilepos.Intersects(playerposition, velocity)
|
|
if intersects {
|
|
playerposition.Set(newpos)
|
|
velocity.Change(Stop)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (grid *Grid) GetSolidNeighborPosition(
|
|
position *components.Position,
|
|
velocity *components.Velocity,
|
|
solid bool) (bool, *components.Position) {
|
|
|
|
if !solid {
|
|
return false, nil
|
|
}
|
|
|
|
// set to true if we are already on the last tile in the current
|
|
// direction, i.e. on the edge of the grid
|
|
edge := true
|
|
neighborpos := position.Point()
|
|
|
|
switch velocity.Direction {
|
|
case East:
|
|
if neighborpos.X < grid.TilesX {
|
|
neighborpos.X++
|
|
edge = false
|
|
}
|
|
case West:
|
|
if neighborpos.X > 0 {
|
|
neighborpos.X--
|
|
edge = false
|
|
}
|
|
case South:
|
|
if neighborpos.Y < grid.TilesY {
|
|
neighborpos.Y++
|
|
edge = false
|
|
}
|
|
case North:
|
|
if neighborpos.Y > 0 {
|
|
neighborpos.Y--
|
|
edge = false
|
|
}
|
|
}
|
|
|
|
newpos := components.NewPosition(neighborpos, grid.Tilesize)
|
|
|
|
// slog.Debug("SolidNeighbor?", "player", position.Point(),
|
|
// "neighbor", neighborpos, "edge", edge, "neighbor-solid",
|
|
// grid.Map[neighborpos].Solid, "newpos", newpos.Point())
|
|
if !edge && grid.Map[neighborpos].Solid {
|
|
return true, newpos
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
func (grid *Grid) BumpEdge(
|
|
pos *components.Position,
|
|
velocity *components.Velocity) (bool, *components.Position) {
|
|
|
|
x := pos.X + velocity.Data.X
|
|
y := pos.Y + velocity.Data.Y
|
|
|
|
if x < 0 || x > grid.Width-grid.Tilesize || y < 0 || y > grid.Height-grid.Tilesize {
|
|
newpos := &components.Position{}
|
|
X := pos.X
|
|
Y := pos.Y
|
|
|
|
switch velocity.Direction {
|
|
case East:
|
|
X = 0
|
|
case West:
|
|
X = grid.Width - grid.Tilesize
|
|
case South:
|
|
Y = 0
|
|
case North:
|
|
Y = grid.Height - grid.Tilesize
|
|
}
|
|
|
|
newpos.Update(X, Y, grid.Tilesize)
|
|
return true, newpos
|
|
}
|
|
|
|
return false, nil
|
|
}
|