-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_scene_controller.go
93 lines (82 loc) · 2.33 KB
/
game_scene_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bytes"
"log"
"reflect"
"sync"
"time"
)
type GameSceneController struct {
sync.Mutex
readyState GameSceneControllerState
countdown2State GameSceneControllerState
countdown3State GameSceneControllerState
countdown1State GameSceneControllerState
playingState GameSceneControllerState
finishedState GameSceneControllerState
gameScene *GameScene
currentState GameSceneControllerState
reusableTimer *time.Timer
gameServerSessions GameServerSessions
onFinishedCallback func()
}
func (gsc *GameSceneController) communicate(s *GameServerSession, command []byte) {
gsc.Lock()
defer gsc.Unlock()
switch {
case bytes.Contains(command, tapCommand):
gsc.currentState.tap(s)
default:
}
}
func (gsc *GameSceneController) disconnect(session *GameServerSession) {
gsc.Lock()
defer gsc.Unlock()
gsc.currentState.disconnect(session)
}
func (gsc *GameSceneController) countdown() {
gsc.Lock()
defer gsc.Unlock()
gsc.currentState.countdown()
}
func (gsc *GameSceneController) timeout() {
gsc.Lock()
defer gsc.Unlock()
gsc.currentState.timeout()
}
func (gsc *GameSceneController) next(gscs GameSceneControllerState) {
log.Println("next", reflect.TypeOf(gscs))
leaver, ok := gsc.currentState.(stateLeaver)
if ok {
leaver.onStateLeave()
}
enter, ok := gscs.(stateEnterer)
if ok {
enter.onStateEnter()
}
gsc.currentState = gscs
}
func NewGameSceneController(lobby map[*GameServerSession]struct{}, callback func([]string)) *GameSceneController {
usernames := make([]string, 0, 2)
playerNameToSession := make(map[string]*GameServerSession, 2)
for gss := range lobby {
playerNameToSession[gss.Username] = gss
usernames = append(usernames, gss.Username)
}
gsc := &GameSceneController{
gameServerSessions: playerNameToSession,
onFinishedCallback: func() {
callback(usernames)
},
}
timer := NewPausableAfterFunc(30*time.Second, gsc.timeout)
gsc.gameScene = NewGameScene(timer)
gsc.readyState = &gameSceneControllerStateReady{gsc: gsc}
gsc.countdown3State = &gameSceneControllerCountdown3{gsc: gsc}
gsc.countdown2State = &gameSceneControllerCountdown2{gsc: gsc}
gsc.countdown1State = &gameSceneControllerCountdown1{gsc: gsc}
gsc.playingState = &gameSceneControllerStatePlaying{gsc: gsc}
gsc.finishedState = &gameSceneControllerStateFinished{gsc: gsc}
gsc.next(gsc.readyState)
return gsc
}