2024-02-19 19:05:48 +01:00
|
|
|
package systems
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log/slog"
|
|
|
|
|
"openquell/components"
|
|
|
|
|
. "openquell/components"
|
|
|
|
|
. "openquell/config"
|
2024-02-23 18:47:15 +01:00
|
|
|
"openquell/grid"
|
2024-02-19 19:05:48 +01:00
|
|
|
"openquell/observers"
|
2024-02-20 18:47:32 +01:00
|
|
|
"openquell/util"
|
2024-02-19 19:05:48 +01:00
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
"github.com/mlange-42/arche/ecs"
|
|
|
|
|
"github.com/mlange-42/arche/generic"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ObstacleSystem struct {
|
2024-02-23 18:47:15 +01:00
|
|
|
World *ecs.World
|
2024-02-26 12:56:12 +01:00
|
|
|
Selector *generic.Filter4[Position, Velocity, Obstacle, Renderable]
|
2024-02-23 18:47:15 +01:00
|
|
|
GridContainer *grid.GridContainer
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-23 18:47:15 +01:00
|
|
|
func NewObstacleSystem(world *ecs.World, gridcontainer *grid.GridContainer) System {
|
2024-02-19 19:05:48 +01:00
|
|
|
system := &ObstacleSystem{
|
2024-02-26 12:56:12 +01:00
|
|
|
Selector: generic.NewFilter4[Position, Velocity, Obstacle, Renderable](),
|
2024-02-23 18:47:15 +01:00
|
|
|
World: world,
|
|
|
|
|
GridContainer: gridcontainer,
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return system
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 18:47:15 +01:00
|
|
|
func (system *ObstacleSystem) Update() error {
|
2024-02-27 14:45:23 +01:00
|
|
|
observer := observers.GetGameObserver(system.World)
|
2024-02-19 19:05:48 +01:00
|
|
|
|
2024-02-27 14:45:23 +01:00
|
|
|
if observer.Lost {
|
2024-02-23 18:47:15 +01:00
|
|
|
return nil
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posID := ecs.ComponentID[components.Position](system.World)
|
|
|
|
|
veloID := ecs.ComponentID[components.Velocity](system.World)
|
|
|
|
|
|
|
|
|
|
EntitiesToRemove := []ecs.Entity{}
|
|
|
|
|
|
|
|
|
|
query := system.Selector.Query(system.World)
|
|
|
|
|
|
|
|
|
|
for query.Next() {
|
2024-02-26 12:56:12 +01:00
|
|
|
obsposition, obsvelocity, _, _ := query.Get()
|
2024-02-19 19:05:48 +01:00
|
|
|
|
2024-02-22 14:33:01 +01:00
|
|
|
// check if one player has bumped into current obstacle
|
2024-02-27 14:45:23 +01:00
|
|
|
for _, player := range observer.GetPlayers() {
|
2024-02-26 19:07:35 +01:00
|
|
|
if !system.World.Alive(player) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 19:05:48 +01:00
|
|
|
playerposition := (*Position)(system.World.Get(player, posID))
|
|
|
|
|
playervelocity := (*Velocity)(system.World.Get(player, veloID))
|
|
|
|
|
|
2024-02-21 12:50:17 +01:00
|
|
|
ok, newpos := obsposition.Intersects(playerposition, playervelocity)
|
2024-02-19 19:05:48 +01:00
|
|
|
if ok {
|
2024-02-22 14:33:01 +01:00
|
|
|
// slog.Debug("bumped into obstacle", "obstacle", obstacle)
|
2024-02-20 18:47:32 +01:00
|
|
|
|
|
|
|
|
if CheckObstacleSide(playervelocity, obsvelocity.Direction) {
|
2024-02-22 14:33:01 +01:00
|
|
|
// player died
|
2024-02-20 18:47:32 +01:00
|
|
|
EntitiesToRemove = append(EntitiesToRemove, player)
|
|
|
|
|
} else {
|
2024-02-22 14:33:01 +01:00
|
|
|
// bumped into nonlethal obstacle side, stop the
|
|
|
|
|
// player, set the obstacle in motion, if possible
|
2024-02-26 19:07:35 +01:00
|
|
|
//slog.Debug("bump not die", "originalpos", playerposition, "newpos", newpos, "obspos", obsposition)
|
2024-02-22 14:33:01 +01:00
|
|
|
obsvelocity.Set(playervelocity)
|
|
|
|
|
playervelocity.Change(Stop)
|
2024-02-20 18:47:32 +01:00
|
|
|
playerposition.Set(newpos)
|
|
|
|
|
}
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-22 14:33:01 +01:00
|
|
|
|
|
|
|
|
// check if current obstacle bumped into another obstacle
|
2024-02-27 14:45:23 +01:00
|
|
|
for _, foreign_obstacle := range observer.GetObstacles() {
|
2024-02-22 14:33:01 +01:00
|
|
|
if foreign_obstacle == query.Entity() {
|
|
|
|
|
// don't check obstacle against itself
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreign_obstacle_position := (*Position)(system.World.Get(foreign_obstacle, posID))
|
|
|
|
|
|
|
|
|
|
ok, newpos := foreign_obstacle_position.Intersects(obsposition, obsvelocity)
|
|
|
|
|
if ok {
|
|
|
|
|
//slog.Debug("bumped into foreign obstacle", "obstacle", foreign_obstacle)
|
|
|
|
|
obsposition.Set(newpos)
|
|
|
|
|
obsvelocity.ResetDirectionAndStop()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 09:38:47 +01:00
|
|
|
//system.GridContainer.Grid.CheckGridCollision(obsposition, obsvelocity)
|
2024-02-22 14:33:01 +01:00
|
|
|
// FIXME: this is the same loop as in player_system, unite the
|
|
|
|
|
// two, just iterate over all entities with pos,vel,render, dammit
|
|
|
|
|
if obsvelocity.Moving() {
|
2024-03-21 09:38:47 +01:00
|
|
|
ok, newpos := system.GridContainer.Grid.BumpEdge(obsposition, obsvelocity)
|
2024-02-22 14:33:01 +01:00
|
|
|
if ok {
|
2024-03-21 09:38:47 +01:00
|
|
|
//slog.Debug("falling off the edge", "newpos", newpos)
|
|
|
|
|
obsposition.Set(newpos)
|
|
|
|
|
} else {
|
|
|
|
|
ok, tilepos := system.GridContainer.Grid.GetSolidNeighborPosition(obsposition, obsvelocity, true)
|
|
|
|
|
if ok {
|
|
|
|
|
intersects, newpos := tilepos.Intersects(obsposition, obsvelocity)
|
|
|
|
|
if intersects {
|
|
|
|
|
// slog.Debug("collision with foreign obstacle detected", "tile",
|
|
|
|
|
// tilepos, "obs", obsposition, "new", newpos)
|
|
|
|
|
|
|
|
|
|
obsposition.Set(newpos)
|
|
|
|
|
obsvelocity.ResetDirectionAndStop()
|
|
|
|
|
}
|
2024-02-22 14:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-21 09:38:47 +01:00
|
|
|
obsposition.Move(obsvelocity)
|
|
|
|
|
}
|
2024-02-22 14:33:01 +01:00
|
|
|
}
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, entity := range EntitiesToRemove {
|
2024-02-26 19:07:35 +01:00
|
|
|
slog.Debug("remove player")
|
2024-02-19 19:05:48 +01:00
|
|
|
system.World.RemoveEntity(entity)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 14:45:23 +01:00
|
|
|
if len(observer.GetPlayers()) == 0 {
|
2024-02-26 19:07:35 +01:00
|
|
|
// lost
|
2024-02-27 14:45:23 +01:00
|
|
|
timer := observer.StopTimer
|
2024-02-19 19:05:48 +01:00
|
|
|
|
|
|
|
|
if !timer.Running {
|
|
|
|
|
timer.Start(LEVEL_END_WAIT)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 14:45:23 +01:00
|
|
|
observer.Gameover()
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
2024-02-23 18:47:15 +01:00
|
|
|
|
|
|
|
|
return nil
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (system *ObstacleSystem) Draw(screen *ebiten.Image) {
|
|
|
|
|
// write the movable tiles
|
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
|
query := system.Selector.Query(system.World)
|
|
|
|
|
|
|
|
|
|
for query.Next() {
|
2024-02-26 12:56:12 +01:00
|
|
|
pos, _, _, sprite := query.Get()
|
2024-02-19 19:05:48 +01:00
|
|
|
|
|
|
|
|
op.GeoM.Reset()
|
|
|
|
|
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
|
|
|
|
|
|
|
|
|
screen.DrawImage(sprite.Image, op)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 18:47:32 +01:00
|
|
|
// return true if obstacle weapon points into the direction the player is moving
|
|
|
|
|
func CheckObstacleSide(playervelocity *Velocity, obsdirection int) bool {
|
|
|
|
|
movingdirection := playervelocity.InvertDirection()
|
|
|
|
|
|
|
|
|
|
if movingdirection == Stop || movingdirection == obsdirection {
|
|
|
|
|
slog.Debug("Damaging obstacle collision",
|
|
|
|
|
"playerdirection", util.DirectionStr(playervelocity.Direction),
|
|
|
|
|
"obsdirection", util.DirectionStr(obsdirection),
|
|
|
|
|
"movingdirection", util.DirectionStr(movingdirection),
|
|
|
|
|
)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|