added stop timer to add a delay after level end

This commit is contained in:
Thomas von Dein 2024-02-13 18:15:52 +01:00
parent 5a288035b3
commit c9f8783521
6 changed files with 48 additions and 19 deletions

View File

@ -4,6 +4,9 @@
- ignore comments in lvl files - ignore comments in lvl files
- check when sphere bounces from one end to the other endlessly w/o - 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 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: - Grid Observer:
https://github.com/mlange-42/arche/issues/374 https://github.com/mlange-42/arche/issues/374

View File

@ -1,41 +1,48 @@
package components package components
import ( import (
"math/rand"
"time" "time"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
type Timer struct { type Timer struct {
currentTicks int CurrentTicks int
targetTicks int TargetTicks int
Running bool
Id int
} }
func NewTimer(d time.Duration) *Timer { func NewTimer() *Timer {
return &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 min := int(1000 / ebiten.TPS()) // 16.66ms == 1 tick
useD := d
if int(d.Milliseconds()) < min { dur := duration
useD = time.Duration(1000/ebiten.TPS()) * time.Millisecond 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() { func (timer *Timer) Update() {
if t.currentTicks < t.targetTicks { if (timer.CurrentTicks < timer.TargetTicks) && timer.Running {
t.currentTicks++ timer.CurrentTicks++
} }
} }
func (t *Timer) IsReady() bool { func (timer *Timer) IsReady() bool {
return t.currentTicks >= t.targetTicks return (timer.CurrentTicks >= timer.TargetTicks) && timer.Running
} }
func (t *Timer) Reset() { func (timer *Timer) Reset() {
t.currentTicks = 0 timer.CurrentTicks = 0
timer.TargetTicks = 0
timer.Running = false
} }

View File

@ -12,3 +12,4 @@ const (
const PLAYERSPEED int = 4 const PLAYERSPEED int = 4
const PARTICLE_LOOPWAIT time.Duration = 250 * time.Millisecond const PARTICLE_LOOPWAIT time.Duration = 250 * time.Millisecond
const LEVEL_END_WAIT time.Duration = 500 * time.Millisecond

View File

@ -40,10 +40,21 @@ func NewGame(width, height, cellsize, startlevel int, startscene int) *Game {
} }
func (game *Game) Update() error { 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 { for _, scene := range game.Scenes {
scene.Update() scene.Update()
} }
timer.Update()
return nil return nil
} }

View File

@ -1,6 +1,8 @@
package observers package observers
import ( import (
"openquell/components"
"github.com/mlange-42/arche/ecs" "github.com/mlange-42/arche/ecs"
"github.com/mlange-42/arche/generic" "github.com/mlange-42/arche/generic"
) )
@ -8,11 +10,14 @@ import (
// Used for global game state // Used for global game state
type GameObserver struct { type GameObserver struct {
CurrentLevel, Width, Height, Cellsize, Score int 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 { func NewGameObserver(world *ecs.World, startlevel, width, height, cellsize int) *GameObserver {
observer := &GameObserver{ observer := &GameObserver{
CurrentLevel: startlevel, CurrentLevel: startlevel,
StopTimer: &components.Timer{},
Width: width, Width: width,
Height: height, Height: height,
Cellsize: cellsize, Cellsize: cellsize,

View File

@ -6,6 +6,7 @@ import (
"openquell/components" "openquell/components"
. "openquell/components" . "openquell/components"
"openquell/config" "openquell/config"
. "openquell/config"
"openquell/observers" "openquell/observers"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@ -70,10 +71,11 @@ func (system *CollectibleSystem) Update() {
} }
if numcollectibles == 0 { if numcollectibles == 0 {
// winner, winner, chicken dinner! // winner, winner, chicken dinner!
game := observers.GetGameObserver(system.World) timer := observers.GetGameObserver(system.World).StopTimer
game.Score++ if !timer.Running {
game.CurrentLevel++ timer.Start(LEVEL_END_WAIT)
}
} }
} }