/
game.go
430 lines (317 loc) · 7.88 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package game
import (
"encoding/json"
"fmt"
"time"
)
// Game --------------------------------------------------------------------------------------------
type Game struct {
// The 10x22 grid of history, the first two lines should be hidden in visualization
grid GameGrid
// Five position array of tetrominoes, where index
// 0: Current dropping tetromino
// 1: Coming up 1
// 2: Coming up 2
// 3: Coming up 3
// -- 4: On hold (TODO)
Tetrominoes [5]Tetromino
factory TetrominoFactory
// Base offset of dropping tetromino
// Can be outside of grid if current tetromino sprite has blank rows/columns
Position Vector
GhostPosition Vector
// Rotation of dropping tetromino
// Should be >0 and <4
Rotation int8
Score uint64
Level uint64
Lines uint64
// Internal stuff below
Nickname string
keyStates *map[string]interface{}
dataChannel chan string
timerReal time.Time
timerModified time.Time
}
func NewGame(nickname string, dataChannel chan string) Game {
game := Game{}
// Set nickname of this game
game.Nickname = nickname
// Reset stuff
game.Score = 0
game.Level = 0
game.grid.Clear()
// Initialize upcoming tetrominos
game.Tetrominoes[1] = game.factory.Next()
game.Tetrominoes[2] = game.factory.Next()
game.Tetrominoes[3] = game.factory.Next()
// Set current
game.NextTetromino()
// Set up outgoing data channel
game.dataChannel = dataChannel
// Initial game update
game.dataChannel <- game.updateGrid()
return game
}
func (game *Game) updateGame() string {
// Marshal gamefield and player
b, err := json.Marshal(game)
if err != nil {
fmt.Println("error:", err)
}
return string(b)
}
func (game *Game) updateGrid() string {
// Marshal gamefield and player
b, err := json.Marshal(game.grid)
if err != nil {
fmt.Println("error:", err)
}
return string(b)
}
func (game *Game) Changed() {
currentSprite := game.Tetrominoes[0].Sprites[game.Rotation].Data
bogusPosition := game.Position
bogusPosition.Y += 1
// Drop til it stop
for game.validMove(currentSprite, bogusPosition) {
game.GhostPosition = bogusPosition
bogusPosition.Y += 1
}
game.dataChannel <- game.updateGame()
}
func (game *Game) GetGrid() GameGrid {
return game.grid
}
func (game *Game) MoveX(d int32) bool {
game.resetTimerIfLanded()
currentSprite := game.Tetrominoes[0].Sprites[game.Rotation].Data
bogusPosition := game.Position
bogusPosition.X += d
if game.validMove(currentSprite, bogusPosition) {
// Seem ok, apply position
game.Position = bogusPosition
game.Changed()
return true
}
return false
}
func (game *Game) SetKeyStates(kss *map[string]interface{}) {
game.keyStates = kss
}
func (game *Game) SetKey(event map[string]interface{}) {
key, keyOk := event["key"]
if !keyOk {
// Key "key" did not exist, bail out
return
}
state, stateOk := event["state"]
if !stateOk {
// Key "state" did not exist, bail out
return
}
// Check if key was pressed (hence not released)
if state.(bool) {
// Do immidiate actions
switch key {
case "right":
game.MoveX(1)
case "left":
game.MoveX(-1)
case "drop":
game.Drop()
case "rotCW":
game.Rotate(1)
case "rotCCW":
game.Rotate(-1)
case "down":
game.MoveDown()
}
}
// ToDo: Keep states
}
func (game *Game) MoveDown() bool {
currentSprite := game.Tetrominoes[0].Sprites[game.Rotation].Data
bogusPosition := game.Position
bogusPosition.Y += 1
game.resetTimerIfLanded()
if game.validMove(currentSprite, bogusPosition) {
// Seem ok, apply position
game.Position = bogusPosition
game.Changed()
return true
} else {
return false
}
}
func (game *Game) hasLanded() bool {
currentSprite := game.Tetrominoes[0].Sprites[game.Rotation].Data
bogusPosition := game.Position
bogusPosition.Y += 1
if game.validMove(currentSprite, bogusPosition) {
return false
} else {
return true
}
}
func (game *Game) Drop() {
currentSprite := game.Tetrominoes[0].Sprites[game.Rotation].Data
bogusPosition := game.Position
bogusPosition.Y += 1
var dropOffset uint32 = 0
// Drop til it stops
for game.validMove(currentSprite, bogusPosition) {
game.Position = bogusPosition
dropOffset += 1
bogusPosition.Y += 1
}
game.AddScore(uint64(dropOffset*2), false)
game.Lockdown()
}
func (game *Game) resetTimerIfLanded() {
if game.hasLanded() {
// Allow stuffing around for 2 seconds
if time.Since(game.timerReal) < 2*time.Second {
game.timerModified = time.Now()
}
}
}
func (game *Game) Rotate(d int8) {
game.resetTimerIfLanded()
bogusRotation := game.Rotation
bogusRotation += d
if bogusRotation < 0 {
bogusRotation = 4 - 1
} else if bogusRotation > 4-1 {
bogusRotation = 0
}
currentSprite := game.Tetrominoes[0].Sprites[bogusRotation].Data
// Check if the desired rotation was a valid move
if game.validMove(currentSprite, game.Position) {
// Seem ok, apply position
game.Rotation = bogusRotation
game.Changed()
return
}
// Try wall kick left
bogusPosition := game.Position
bogusPosition.X -= 1
if game.validMove(currentSprite, bogusPosition) {
game.Rotation = bogusRotation
game.Position = bogusPosition
game.Changed()
return
}
// Try wall kick right
bogusPosition = game.Position
bogusPosition.X += 1
if game.validMove(currentSprite, bogusPosition) {
game.Rotation = bogusRotation
game.Position = bogusPosition
game.Changed()
return
}
// Try double kick left
bogusPosition = game.Position
bogusPosition.X -= 2
if game.validMove(currentSprite, bogusPosition) {
game.Rotation = bogusRotation
game.Position = bogusPosition
game.Changed()
return
}
// Try double kick right
bogusPosition = game.Position
bogusPosition.X += 2
if game.validMove(currentSprite, bogusPosition) {
game.Rotation = bogusRotation
game.Position = bogusPosition
game.Changed()
return
}
}
func (game *Game) validMove(s []Vector, p Vector) bool {
for _, v := range s {
// Check that there is space below
targetX := v.X + p.X
targetY := v.Y + p.Y
// Check X
outOfRange := targetX < 0 || targetX > 10-1
// Check Y
outOfRange = outOfRange || targetY < 0 || targetY > 22-1 || game.grid.Data[targetX+targetY*10] > 0
if outOfRange {
return false
}
}
return true
}
// Game methods
func (game *Game) Iterate() bool {
if time.Since(game.timerModified) > game.iterateDelayMs()*time.Millisecond {
// Reset timer
game.timerReal = time.Now()
game.timerModified = time.Now()
if !game.MoveDown() {
return game.Lockdown()
}
}
return true
}
func (game *Game) iterateDelayMs() time.Duration {
// Reduce 10 ms for each level
delay := 500 - 20*game.Level
if delay < 120 {
delay = 120
}
return time.Duration(delay)
}
func (game *Game) NextTetromino() {
game.Position = Vector{3, 0}
game.Rotation = 0
game.Tetrominoes[0] = game.Tetrominoes[1]
game.Tetrominoes[1] = game.Tetrominoes[2]
game.Tetrominoes[2] = game.Tetrominoes[3]
game.Tetrominoes[3] = game.factory.Next()
}
func (game *Game) AddScore(baseScore uint64, levelBoost bool) {
if levelBoost {
game.Score += baseScore * (game.Level + 1)
} else {
game.Score += baseScore
}
}
func (game *Game) Lockdown() bool {
var clearedRows int32
clearedRows = game.grid.ApplySprite(game.Tetrominoes[0].Sprites[game.Rotation].Data, game.Position, game.Tetrominoes[0].Type)
// End condition ?
if clearedRows == -1 {
return false
}
// Count cleared lines
game.Lines += uint64(clearedRows)
// Add score for cleared rows
switch clearedRows {
case 1:
game.AddScore(40, true)
case 2:
game.AddScore(40*2*2, true) // x2
case 3:
game.AddScore(40*3*4, true) // x4
case 4:
game.AddScore(40*4*8, true) // x8
}
// Time to level up?
if game.Lines > (game.Level+1)*5 {
game.Level += 1
}
// Spawn new tetromino!
game.NextTetromino()
// Reset drop button (PANIC safe?)
if game.keyStates != nil {
(*game.keyStates)["down"] = false
}
// Notify that stuf has changed
game.Changed()
game.dataChannel <- game.updateGrid()
return true
}