added intermediate popup after win/loose, refresh level setup, fixes

This commit is contained in:
2024-02-19 19:05:48 +01:00
parent e12af87fb7
commit fd570216f0
16 changed files with 322 additions and 20 deletions

View File

@@ -30,6 +30,7 @@ func NewCollectibleSystem(world *ecs.World) *CollectibleSystem {
func (system *CollectibleSystem) Update() {
playerobserver := observers.GetPlayerObserver(system.World)
gameobserver := observers.GetGameObserver(system.World)
posID := ecs.ComponentID[components.Position](system.World)
veloID := ecs.ComponentID[components.Velocity](system.World)
@@ -40,7 +41,7 @@ func (system *CollectibleSystem) Update() {
query := system.Selector.Query(system.World)
numcollectibles := query.Count()
if numcollectibles == 0 {
if numcollectibles == 0 || gameobserver.Lost {
query.Close()
return
}

117
systems/obstacle_system.go Normal file
View File

@@ -0,0 +1,117 @@
package systems
import (
"log/slog"
"openquell/assets"
"openquell/components"
. "openquell/components"
"openquell/config"
. "openquell/config"
"openquell/observers"
"github.com/hajimehoshi/ebiten/v2"
"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/arche/generic"
)
type ObstacleSystem struct {
World *ecs.World
Selector *generic.Filter3[Position, Obstacle, Renderable]
}
func NewObstacleSystem(world *ecs.World) *ObstacleSystem {
system := &ObstacleSystem{
Selector: generic.NewFilter3[Position, Obstacle, Renderable](),
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() {
obsposition, obstacle, _ := query.Get()
for player := range playerobserver.Entities {
playerposition := (*Position)(system.World.Get(player, posID))
playervelocity := (*Velocity)(system.World.Get(player, veloID))
ok, _ := playerposition.Intersects(obsposition, playervelocity)
if ok {
slog.Debug("bumped into obstacle", "obstacle", obstacle)
EntitiesToRemove = append(EntitiesToRemove, player)
gameover = true
}
}
}
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() {
pos, _, sprite := query.Get()
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,
)
timer.Start(config.PARTICLE_LOOPWAIT)
}

View File

@@ -15,6 +15,7 @@ type PlayerSystem struct {
Selector *generic.Filter5[Position, Velocity, Player, Renderable, Speed]
Particle *ParticleSystem
Collectible *CollectibleSystem
Obstacle *ObstacleSystem
Grid *GridSystem
}
@@ -23,6 +24,7 @@ func NewPlayerSystem(world *ecs.World, grid *GridSystem) *PlayerSystem {
Selector: generic.NewFilter5[Position, Velocity, Player, Renderable, Speed](),
Particle: NewParticleSystem(world, grid.Tilesize),
Collectible: NewCollectibleSystem(world),
Obstacle: NewObstacleSystem(world),
Grid: grid,
World: world,
}
@@ -75,6 +77,7 @@ func (system PlayerSystem) Update() error {
system.Particle.Update()
system.Collectible.Update()
system.Obstacle.Update()
return nil
}
@@ -93,6 +96,7 @@ func (system *PlayerSystem) Draw(screen *ebiten.Image) {
screen.DrawImage(sprite.Image, op)
}
system.Obstacle.Draw(screen)
system.Collectible.Draw(screen)
system.Particle.Draw(screen)
}