2024-02-19 19:05:48 +01:00
|
|
|
package systems
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log/slog"
|
|
|
|
|
"openquell/assets"
|
|
|
|
|
"openquell/components"
|
|
|
|
|
. "openquell/components"
|
|
|
|
|
. "openquell/config"
|
|
|
|
|
"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-20 18:47:32 +01:00
|
|
|
World *ecs.World
|
|
|
|
|
Selector *generic.Filter4[Position, Velocity, Obstacle, Renderable]
|
|
|
|
|
PreviousFreePos *components.Position
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewObstacleSystem(world *ecs.World) *ObstacleSystem {
|
|
|
|
|
system := &ObstacleSystem{
|
2024-02-20 18:47:32 +01:00
|
|
|
Selector: generic.NewFilter4[Position, Velocity, Obstacle, Renderable](),
|
2024-02-19 19:05:48 +01:00
|
|
|
World: world,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return system
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (system *ObstacleSystem) Update() {
|
|
|
|
|
playerobserver := observers.GetPlayerObserver(system.World)
|
|
|
|
|
gameobserver := observers.GetGameObserver(system.World)
|
|
|
|
|
|
|
|
|
|
if gameobserver.Lost {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posID := ecs.ComponentID[components.Position](system.World)
|
|
|
|
|
veloID := ecs.ComponentID[components.Velocity](system.World)
|
|
|
|
|
|
|
|
|
|
EntitiesToRemove := []ecs.Entity{}
|
|
|
|
|
|
|
|
|
|
query := system.Selector.Query(system.World)
|
|
|
|
|
gameover := false
|
|
|
|
|
|
|
|
|
|
for query.Next() {
|
2024-02-20 18:47:32 +01:00
|
|
|
obsposition, obsvelocity, obstacle, _ := query.Get()
|
2024-02-19 19:05:48 +01:00
|
|
|
|
|
|
|
|
for player := range playerobserver.Entities {
|
|
|
|
|
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 {
|
|
|
|
|
slog.Debug("bumped into obstacle", "obstacle", obstacle)
|
2024-02-20 18:47:32 +01:00
|
|
|
|
|
|
|
|
if CheckObstacleSide(playervelocity, obsvelocity.Direction) {
|
|
|
|
|
EntitiesToRemove = append(EntitiesToRemove, player)
|
|
|
|
|
gameover = true
|
|
|
|
|
} else {
|
|
|
|
|
playervelocity.Change(Stop)
|
|
|
|
|
slog.Debug("bump not die", "originalpos", playerposition)
|
|
|
|
|
playerposition.Set(newpos)
|
|
|
|
|
slog.Debug("bump not die", "newpos", newpos)
|
|
|
|
|
}
|
2024-02-19 19:05:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, entity := range EntitiesToRemove {
|
|
|
|
|
system.World.RemoveEntity(entity)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if gameover {
|
|
|
|
|
// winner, winner, chicken dinner!
|
|
|
|
|
timer := gameobserver.StopTimer
|
|
|
|
|
|
|
|
|
|
if !timer.Running {
|
|
|
|
|
timer.Start(LEVEL_END_WAIT)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gameobserver.Gameover()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-20 18:47:32 +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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (system *ObstacleSystem) AddParticle(position *components.Position) {
|
|
|
|
|
particleobserver := observers.GetParticleObserver(system.World)
|
|
|
|
|
|
|
|
|
|
ptmapper := generic.NewMap3[
|
|
|
|
|
components.Position,
|
|
|
|
|
components.Particle,
|
|
|
|
|
components.Timer,
|
|
|
|
|
](system.World)
|
|
|
|
|
|
|
|
|
|
entity := ptmapper.New()
|
|
|
|
|
pos, particle, timer := ptmapper.Get(entity)
|
|
|
|
|
particleobserver.AddEntity(entity)
|
|
|
|
|
|
|
|
|
|
particle.Index = assets.Tiles['*'].Particle
|
|
|
|
|
particle.Particles = assets.Tiles['*'].Particles
|
|
|
|
|
|
|
|
|
|
pos.Update(
|
|
|
|
|
position.X-(16), // FIXME: use global tilesize!
|
|
|
|
|
position.Y-(16),
|
|
|
|
|
64,
|
|
|
|
|
)
|
|
|
|
|
|
2024-02-20 18:47:32 +01:00
|
|
|
timer.Start(PARTICLE_LOOPWAIT)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|