-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_game.go
113 lines (100 loc) · 2.04 KB
/
system_game.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package spin
type gameSystem struct {
eng *Engine
config *Config
game *GameVars
}
func registerGameSystem(eng *Engine) {
s := &gameSystem{
eng: eng,
config: &eng.Config,
game: GetGameVars(eng),
}
eng.RegisterActionHandler(s)
}
func (s gameSystem) HandleAction(action Action) {
switch act := action.(type) {
// case AddBall:
// s.addBall(act)
case AddPlayer:
s.addPlayer(act)
case AdvanceGame:
s.advanceGame(act)
case AwardScore:
s.awardScore(act)
case SetScore:
s.setScore(act)
}
}
// func (s gameSystem) addBall(act AddBall) {
// if s.game.BallsInPlay >= s.config.NumBalls {
// return
// }
// s.game.BallsInPlay += 1
// s.eng.Post(BallAddedEvent{BallsInPlay: s.game.BallsInPlay})
// }
func (s *gameSystem) addPlayer(act AddPlayer) {
game := s.game
if game.Ball > 1 {
return
}
if game.NumPlayers == game.MaxPlayers {
return
}
game.NumPlayers += 1
s.eng.Post(PlayerAddedEvent{Player: game.NumPlayers})
}
func (s *gameSystem) advanceGame(act AdvanceGame) {
g := s.game
if g.NumPlayers == 0 {
return
}
if g.Ball == 0 {
for i := 1; i < g.NumPlayers; i++ {
player := GetPlayerVarsFor(s.eng, i)
player.Score = 0
}
g.Ball = 1
g.Player = 1
g.BallActive = true
s.eng.Post(StartOfBallEvent{Player: 1, Ball: 1})
return
}
if g.BallActive {
s.eng.Post(EndOfBallEvent{Player: g.Player, Ball: g.Ball})
g.BallActive = false
return
}
if g.Ball == g.BallsPerGame && g.Player == g.NumPlayers {
g.Ball = 0
g.Player = 0
g.NumPlayers = 0
s.eng.Post(EndOfGameEvent{})
return
}
shootAgain := false
if g.ExtraBalls > 0 {
g.ExtraBalls -= 1
shootAgain = true
} else {
g.Player += 1
if g.Player > g.NumPlayers {
g.Player = 1
g.Ball += 1
}
}
g.BallActive = true
s.eng.Post(StartOfBallEvent{
Player: g.Player,
Ball: g.Ball,
ShootAgain: shootAgain,
})
}
func (s *gameSystem) awardScore(act AwardScore) {
player := GetPlayerVars(s.eng)
player.Score += act.Val
}
func (s *gameSystem) setScore(act SetScore) {
player := GetPlayerVars(s.eng)
player.Score = act.Val
}