Skip to content

Commit 90e8906

Browse files
committed
Do not increment game tick on pause
1 parent 83c7960 commit 90e8906

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

pkg/game/game.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ func NewGame() *Game {
3434

3535
// Update updates a game by one tick.
3636
func (g *Game) Update() error {
37-
g.state.Tick++
37+
if !g.state.Paused {
38+
g.state.Tick++
39+
}
3840

3941
switch g.state.Screen {
4042
case domain.ScreenControlSelection:

pkg/game/game_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package game
2+
3+
import (
4+
"testing"
5+
6+
"github.com/morozov/river-raid-ebiten/pkg/domain"
7+
)
8+
9+
// TestTickFrozenWhenPaused verifies that GameState.Tick does not advance while
10+
// the game is paused, matching the original where the main loop is blocked by the
11+
// interrupt handler during pause and state_tick never increments.
12+
func TestTickFrozenWhenPaused(t *testing.T) {
13+
g := NewGame()
14+
g.state.Screen = domain.ScreenGameplay
15+
g.state.Paused = true
16+
before := g.state.Tick
17+
18+
if err := g.Update(); err != nil {
19+
t.Fatalf("Update returned error: %v", err)
20+
}
21+
22+
if g.state.Tick != before {
23+
t.Errorf("Tick advanced during pause: got %d, want %d", g.state.Tick, before)
24+
}
25+
}
26+
27+
// TestTickAdvancesWhenNotPaused verifies that GameState.Tick increments by one
28+
// per Update call during normal (non-paused) operation.
29+
func TestTickAdvancesWhenNotPaused(t *testing.T) {
30+
g := NewGame()
31+
g.state.Screen = domain.ScreenControlSelection
32+
before := g.state.Tick
33+
34+
if err := g.Update(); err != nil {
35+
t.Fatalf("Update returned error: %v", err)
36+
}
37+
38+
if g.state.Tick != before+1 {
39+
t.Errorf("Tick = %d, want %d", g.state.Tick, before+1)
40+
}
41+
}

0 commit comments

Comments
 (0)