-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.go
286 lines (239 loc) · 6.07 KB
/
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"time"
)
type Game struct {
ui *UIHandler
input *InputHandler
players *Players
activeEvents *[]Event
keyDisp chan StateEvent
ticker *time.Ticker
state GameState
lastState GameState
started bool
aiActive bool
}
type GameSettings struct {
BgHidden bool
}
// Create a new game instance initialised and ready to go
func CreateGame(options *GameSettings) (*Game, error) {
g := Game{}
if err := g.Init(options); err != nil {
return nil, err
}
return &g, nil
}
// Initialize the game. Must be called on new game instance
func (g *Game) Init(optns *GameSettings) error {
var err error
defer func() {
if err != nil {
g.End()
}
}()
// screen init
if g.ui, err = NewUIHandler(optns.BgHidden); err != nil {
return err
}
// input handler init
if g.input, err = NewInputHandler(g.getKeys); err != nil {
return err
}
// players init
w, h := g.ui.screen.Size()
g.players = newPlayers(w, h, padding)
// input channels and key/event state init
g.activeEvents = new([]Event)
g.keyDisp = make(chan StateEvent)
// ticker init according to framerate variable
g.ticker = time.NewTicker(1000000 / framerate * time.Microsecond)
// mark the inital game state
g.state = gameStateStarting
// signal that everything is ok
return nil
}
// Start the game. Must be called after initialization
func (g *Game) Loop() {
// make sure that end cleanup function is executed
defer g.End()
// start the keyboard input listeners
go g.input.Listen(g.keyDisp)
// set the main menu game state
g.SetState(gameStateMainMenu)
// if the game is not ending or ended
for g.state != gameStateEnding && g.state != gameStateEnded {
select {
case <-g.ticker.C:
// draw the gui and update the terminal.
// this MUST be the only place where these methods are called.
g.drawGUI()
g.updateTerminal()
case e := <-g.keyDisp:
g.dispatchEvent(e)
}
}
}
// Break the game loop. End cleanup function should invoke when the loop breaks
func (g *Game) EndLoop() {
g.SetState(gameStateEnding)
}
// Start the game
func (g *Game) Start() {
if !g.started {
// reset the ball from the start menu
g.ui.ball.Reset()
g.SetState(gameStateInGame)
g.started = true
}
}
// End the game. Must be called when game ends or if g.Init fails for cleanup.
func (g *Game) End() {
// needs fix: leftover q in terminal when game ends
// check for nil is required in case the game ends with an error and g.ui.screen is not set,
// in that case it would panic with nil pointer dereference. If it doesnt exist,
// then there is no need to clean it up.
g.ui.Destroy()
}
// Reset the game
func (g *Game) Reset() {
g.ui.ball.Reset()
for _, p := range g.players.GetAll() {
p.Reset()
}
}
func (g *Game) SetState(s GameState) {
g.lastState = g.state
g.state = s
}
// Universal pause toggler
func (g *Game) __tpause(state GameState) {
if !g.started && state == gameStatePaused {
return
}
if g.state == state {
g.SetState(g.lastState)
} else {
g.SetState(state)
}
}
// Toggle pause
func (g *Game) togglePause() {
g.__tpause(gameStatePaused)
}
// Toggle hard pause
func (g *Game) toggleHardPause() {
g.__tpause(gameStateHardPaused)
}
// Switch next theme and update the terminal
func (g *Game) switchTheme() {
g.ui.theme.Switch()
}
// Toggle background on/off and update the terminal
func (g *Game) toggleBackground() {
g.ui.theme.ToggleBg()
}
func (g *Game) toggleAI() {
g.aiActive = !g.aiActive
}
// Gets the current active key map.
func (g *Game) getKeys() *KeyEventMap {
stuff := screenData[g.state].Keys
// if the game is hard paused, preserve the event key map,
// but block their effects elsewhere
if g.state == gameStateHardPaused {
stuff = screenData[g.lastState].Keys
}
return &stuff
}
// Gets the current active legend key map.
func (g *Game) getLegendKeys() *KeyEventMap {
stuff := screenData[g.state].Keys
return &stuff
}
// Gets the current active legend key map.
func (g *Game) getLegendType() string {
return screenData[g.state].LegendType
}
// Move player 1 char higher. If player is at the edge, do nothing.
func (g *Game) movePlayerUp(p *Player) {
// if the platform is at the top edge, do nothing
if _, h := p.Coords(); h < 1 {
return
}
p.GoUp()
}
// Move player 1 char lower. If the player is at the edge, do nothing.
func (g *Game) movePlayerDown(p *Player) {
_, sh := g.ui.ScreenSize()
_, ph := p.Coords()
// if the platform is at the bottom edge, do nothing
if ph > sh-p.GetHeight()-1 {
return
}
p.GoDown()
}
// Move selected legend item up.
func (g *Game) moveMenuSelectedUp() {
g.ui.MoveMenuSelectedUp(g.state)
}
// Move selected legend item down.
func (g *Game) moveMenuSelectedDown() {
g.ui.MoveMenuSelectedDown(g.state)
}
func (g *Game) doSelectedMenuAction() {
// TODO
g.ui.GetSelectedMenuAction(g.state)
}
func filterEvent(e Event, old []Event) []Event {
var newEvents []Event
for _, event := range old {
if e.Name != event.Name {
newEvents = append(newEvents, event)
}
}
return newEvents
}
// Triggers an action based on the event an key that have been passed.
func (g *Game) dispatchEvent(e StateEvent) {
// filter the key from the slice if it is in there
switch e.State {
case eventStateStarting:
// in case the event existed in the array, remove it
newEvents := filterEvent(e.Event, *g.activeEvents)
// update only if there are less than 5 chars and key is pressed down
if len(*g.activeEvents) < 5 {
newEvents = append(newEvents, e.Event)
}
// update the old array
g.activeEvents = &newEvents
case eventStateEnding:
*g.activeEvents = filterEvent(e.Event, *g.activeEvents)
case eventStatePulse:
g.dispatch(e.Event)
}
}
// Calls an action according to the event.
func (g *Game) dispatch(e Event) {
switch e {
case eventDestroy:
g.EndLoop()
case eventStart:
g.Start()
case eventTogglePause:
g.togglePause()
case eventReset:
g.Reset()
case eventSwitchTheme:
g.switchTheme()
case eventMenuUp:
g.moveMenuSelectedUp()
case eventMenuDown:
g.moveMenuSelectedDown()
case eventMenuSelect:
g.doSelectedMenuAction()
case eventToggleAI:
g.toggleAI()
}
}