From c9f87835219955fadd9e5ade836686c2c48ee0db Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 13 Feb 2024 18:15:52 +0100 Subject: [PATCH] added stop timer to add a delay after level end --- TODO.md | 3 +++ components/timer.go | 37 +++++++++++++++++++++-------------- config/static.go | 1 + game/game.go | 11 +++++++++++ observers/game_observer.go | 5 +++++ systems/collectible_system.go | 10 ++++++---- 6 files changed, 48 insertions(+), 19 deletions(-) diff --git a/TODO.md b/TODO.md index 8db425f..3ba1245 100644 --- a/TODO.md +++ b/TODO.md @@ -4,6 +4,9 @@ - ignore comments in lvl files - check when sphere bounces from one end to the other endlessly w/o any solids in between. this is a game lock and equals game over +- Start the game end timer and add a score FIXME: put the actual max + reachable score into the level definition along with the minimum + steps required to reach it, also add a step counter - Grid Observer: https://github.com/mlange-42/arche/issues/374 diff --git a/components/timer.go b/components/timer.go index 413eaa2..38f7ffc 100644 --- a/components/timer.go +++ b/components/timer.go @@ -1,41 +1,48 @@ package components import ( + "math/rand" "time" "github.com/hajimehoshi/ebiten/v2" ) type Timer struct { - currentTicks int - targetTicks int + CurrentTicks int + TargetTicks int + Running bool + Id int } -func NewTimer(d time.Duration) *Timer { +func NewTimer() *Timer { return &Timer{} } -func (timer *Timer) Start(d time.Duration) { +func (timer *Timer) Start(duration time.Duration) { min := int(1000 / ebiten.TPS()) // 16.66ms == 1 tick - useD := d - if int(d.Milliseconds()) < min { - useD = time.Duration(1000/ebiten.TPS()) * time.Millisecond + dur := duration + if int(duration.Milliseconds()) < min { + dur = time.Duration(1000/ebiten.TPS()) * time.Millisecond } - timer.targetTicks = int(useD.Milliseconds()) / min // id d=50, then 50 / 16.6 = + timer.TargetTicks = int(dur.Milliseconds()) / min // id d=50, then 50 / 16.6 = + timer.Running = true + timer.Id = rand.Int() } -func (t *Timer) Update() { - if t.currentTicks < t.targetTicks { - t.currentTicks++ +func (timer *Timer) Update() { + if (timer.CurrentTicks < timer.TargetTicks) && timer.Running { + timer.CurrentTicks++ } } -func (t *Timer) IsReady() bool { - return t.currentTicks >= t.targetTicks +func (timer *Timer) IsReady() bool { + return (timer.CurrentTicks >= timer.TargetTicks) && timer.Running } -func (t *Timer) Reset() { - t.currentTicks = 0 +func (timer *Timer) Reset() { + timer.CurrentTicks = 0 + timer.TargetTicks = 0 + timer.Running = false } diff --git a/config/static.go b/config/static.go index 4cc76cd..580e1a1 100644 --- a/config/static.go +++ b/config/static.go @@ -12,3 +12,4 @@ const ( const PLAYERSPEED int = 4 const PARTICLE_LOOPWAIT time.Duration = 250 * time.Millisecond +const LEVEL_END_WAIT time.Duration = 500 * time.Millisecond diff --git a/game/game.go b/game/game.go index ef47297..2acb0ce 100644 --- a/game/game.go +++ b/game/game.go @@ -40,10 +40,21 @@ func NewGame(width, height, cellsize, startlevel int, startscene int) *Game { } func (game *Game) Update() error { + gameobserver := observers.GetGameObserver(game.World) + timer := gameobserver.StopTimer + + if timer.IsReady() { + timer.Reset() + gameobserver.CurrentLevel++ + gameobserver.Score++ // FIXME: use level.Score(), see TODO + } + for _, scene := range game.Scenes { scene.Update() } + timer.Update() + return nil } diff --git a/observers/game_observer.go b/observers/game_observer.go index 8de76c1..75ecce2 100644 --- a/observers/game_observer.go +++ b/observers/game_observer.go @@ -1,6 +1,8 @@ package observers import ( + "openquell/components" + "github.com/mlange-42/arche/ecs" "github.com/mlange-42/arche/generic" ) @@ -8,11 +10,14 @@ import ( // Used for global game state type GameObserver struct { CurrentLevel, Width, Height, Cellsize, Score int + StopTimer *components.Timer + Lost bool // set to true if player is struck or something, by default: win! } func NewGameObserver(world *ecs.World, startlevel, width, height, cellsize int) *GameObserver { observer := &GameObserver{ CurrentLevel: startlevel, + StopTimer: &components.Timer{}, Width: width, Height: height, Cellsize: cellsize, diff --git a/systems/collectible_system.go b/systems/collectible_system.go index 3c58dea..f1f0cb8 100644 --- a/systems/collectible_system.go +++ b/systems/collectible_system.go @@ -6,6 +6,7 @@ import ( "openquell/components" . "openquell/components" "openquell/config" + . "openquell/config" "openquell/observers" "github.com/hajimehoshi/ebiten/v2" @@ -70,10 +71,11 @@ func (system *CollectibleSystem) Update() { } if numcollectibles == 0 { - // winner, winner, chicken dinner! - game := observers.GetGameObserver(system.World) - game.Score++ - game.CurrentLevel++ + // winner, winner, chicken dinner! + timer := observers.GetGameObserver(system.World).StopTimer + if !timer.Running { + timer.Start(LEVEL_END_WAIT) + } } }