refactored recursive systems out of player_system => game/levels.go

This commit is contained in:
2024-02-23 18:47:15 +01:00
parent c93070883a
commit ab07bc23e3
11 changed files with 198 additions and 183 deletions

82
grid/collider.go Normal file
View File

@@ -0,0 +1,82 @@
package grid
import (
"openquell/components"
. "openquell/config"
)
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)
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
}

9
grid/container.go Normal file
View File

@@ -0,0 +1,9 @@
package grid
type GridContainer struct {
Grid *Grid
}
func (container *GridContainer) SetGrid(grid *Grid) {
container.Grid = grid
}

View File

@@ -13,12 +13,13 @@ import (
)
type Grid struct {
World *ecs.World
Width int
Height int
Size int
Tilesize int
Map map[image.Point]*assets.Tile
World *ecs.World
Width int
Height int
Size int
Tilesize int
TilesX, TilesY int
Map map[image.Point]*assets.Tile
}
// FIXME: put component addition into extra callable function, to be called
@@ -123,6 +124,8 @@ func NewGrid(world *ecs.World,
Height: height,
Map: mapslice,
World: world,
TilesX: width / tilesize,
TilesY: height / tilesize,
}
}