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
- 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

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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,

View File

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