game: countdown to play state

This commit is contained in:
Settel 2021-08-15 17:12:24 +02:00
parent 24d828d420
commit 7ccef35f3d
5 changed files with 67 additions and 27 deletions

View File

@ -13,7 +13,6 @@ func (gm *Game) changeGameState(expectedState, newState, newPhase string) error
}
gm.state = newState
gm.phase = newPhase
gm.eng.Update()
return nil
}
@ -29,7 +28,6 @@ func (gm *Game) changeGamePhase(expectedState, expectedPhase, newPhase string) e
return fmt.Errorf("game phase \"%s\" != expected phase \"%s\"", gm.phase, expectedPhase)
}
gm.phase = newPhase
gm.eng.Update()
return nil
}

View File

@ -40,6 +40,13 @@ func (gm *Game) GetId() string {
return gm.id
}
func (gm *Game) GetState() string {
gm.mu.Lock()
defer gm.mu.Unlock()
return gm.state
}
func (gm *Game) GetName() string {
gm.mu.Lock()
defer gm.mu.Unlock()

View File

@ -0,0 +1,8 @@
package game
func (gm *Game) notifyClients() {
gm.mu.Lock()
defer gm.mu.Unlock()
gm.eng.Update()
}

View File

@ -0,0 +1,43 @@
package game
import (
"fmt"
"time"
)
func (gm *Game) runCountdown() {
phases := []string{
"Ready?",
"3",
"2",
"1",
"Go!",
"",
}
err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0])
if err != nil {
fmt.Println(err)
return
}
gm.notifyClients()
time.Sleep(2000 * time.Millisecond)
for i := 1; i < len(phases); i++ {
err = gm.changeGamePhase(STATE_READY_SET, phases[i-1], phases[i])
if err != nil {
fmt.Println(err)
return
}
gm.notifyClients()
time.Sleep(1500 * time.Millisecond)
}
err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE)
if err != nil {
fmt.Println(err)
return
}
gm.runRound()
}

View File

@ -2,43 +2,27 @@ package game
import (
"fmt"
"time"
)
func (gm *Game) StartGame() {
go gm.startGameSub()
// go gm.runCountdown()
go gm.runRound()
}
func (gm *Game) startGameSub() {
phases := []string{
"5",
"4",
"3",
"2",
"1",
"Go!",
func (gm *Game) runRound() {
state := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY {
fmt.Println(fmt.Errorf("expected state \"IDLE\" | \"PLAY\" != \"%s\"", state))
return
}
err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0])
err := gm.changeGameState(STATE_ANY, STATE_PLAY, PHASE_NONE)
if err != nil {
fmt.Println(err)
return
}
for i := 1; i < len(phases); i++ {
time.Sleep(1500 * time.Millisecond)
err = gm.changeGamePhase(STATE_READY_SET, phases[i-1], phases[i])
if err != nil {
fmt.Println(err)
return
}
}
time.Sleep(3 * time.Second)
err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE)
if err != nil {
fmt.Println(err)
return
}
gm.notifyClients()
}
func (gm *Game) ResetGame() {