-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameState.lua
More file actions
425 lines (331 loc) · 10.8 KB
/
gameState.lua
File metadata and controls
425 lines (331 loc) · 10.8 KB
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
local gameStatePlay = {}
function gameStatePlay:enter()
end
function gameStatePlay:update(dt)
-- check input
if not gameState.gridEntity.fall then
-- rotate
if PlayerControl.player1Control:testTrigger("use") and gameState.gridEntity:rotate() then
game.rotateSound:stop()
game.rotateSound:play()
end
if PlayerControl.player1Control:testTrigger("left") and gameState.gridEntity:moveLeft() then
game.moveSound:stop()
game.moveSound:play()
end
if PlayerControl.player1Control:testTrigger("right") and gameState.gridEntity:moveRight() then
game.moveSound:stop()
game.moveSound:play()
end
if PlayerControl.player1Control:testTrigger("attack") and gameState.gridEntity:moveDown() then
game.fallSound:stop()
game.fallSound:play()
end
end
-- move tetromino
local bottom, lineCount = gameState.gridEntity:updateTetromino(dt)
if bottom then
if lineCount > 0 then
-- update score
local points = 40
if lineCount == 2 then
points = 100
elseif lineCount == 3 then
points = 300
elseif lineCount == 4 then
points = 1200
end
gameState.gridEntity.score = gameState.gridEntity.score + points * (gameState.gridEntity.level + 1)
-- update level
local levelUp = false
local newLevel = math.min(math.max(math.floor(gameState.gridEntity.lineCount / 20), gameState.gridEntity.level), 10)
if gameState.gridEntity.level < newLevel then
gameState.gridEntity.fallTime = levelsFallTime[newLevel]
gameState.gridEntity.level = newLevel
--game.levelUpSound:rewind()
game.levelUpSound:play()
else
--game.lineSound:rewind()
game.lineSound:play()
end
end
-- update score, line, etc
gameState.scoreText.text = gameState.gridEntity.score
gameState.linesText.text = gameState.gridEntity.lineCount
gameState.levelText.text = "Level " .. gameState.gridEntity.level
-- generate a new tetromino
gameState:generateTetromino(gameState.nextTetromino.index)
-- are we stuck?
if not gameState.gridEntity:canMoveDown(gameState.gridEntity.tetromino) then
-- Game over!
gameState.fsm:changeState(ThreadState.new(gameOverThread))
end
end
if PlayerControl.player1Control:testTrigger("menu_back") then
gameState.fsm:changeState(inGameMenuState)
end
end
function gameStatePlay:exit()
end
newHighscoreState = {}
newHighscoreState.music = love.audio.newSource("Music/highscore.xm", "stream")
newHighscoreState.music:setLooping(true)
function newHighscoreState:enter()
game:fadeIn()
newHighscoreState.music:play()
self.view = Entity.new(0, 0)
game.scene:addChild(self.view)
self.view:addChild(Text.new("New Highscore!", 0, 35, 320, "center"))
self.view:addChild(Text.new(self.highscore, 0, 55, 320, "center"))
self.view:addChild(Text.new("Enter your name", 0, 80, 320, "center"))
self.view:addChild(Sprite.new(love.graphics.newImage("Gfx/text_field.png"), nil, 95, 96))
self.nameField = Text.new("", 0, 100, 320, "center")
self.view:addChild(self.nameField)
self.name = ""
self.timer = 0
end
function newHighscoreState:textinput(text)
self.name = self.name .. text
end
function newHighscoreState:update(dt)
-- blink cursor
self.timer = self.timer + dt
if (math.floor(self.timer * 5)) % 2 == 0 then
self.nameField.text = self.name .. "_"
else
self.nameField.text = self.name .. "µ"
end
if PlayerControl.player1Control:testTrigger("text_del") then
self.name = self.name:sub(1, -2)
end
if PlayerControl.player1Control:testTrigger("menu_valid") then
-- insert in score
table.insert(game.highscores, {name=self.name, score=self.highscore})
-- sort
table.sort(game.highscores, function(k1, k2) return k1.score > k2.score end )
-- remove last entry
table.remove(game.highscores, #game.highscores)
-- save highscore
love.filesystem.write("highscores.lua", serialize(game.highscores))
game.fsm:changeState(ThreadState.new(backToMainMenuThread))
end
end
function newHighscoreState:exit()
game:fadeOut()
newHighscoreState.music:stop()
game.scene:removeChild(self.view)
end
function gameOverThread()
-- copy last tetromino
gameState.gridEntity:copyTetromino(gameState.gridEntity.tetromino)
-- and don't display it
gameState.gridEntity:removeChild(gameState.gridEntity.tetromino)
-- stop music
game.levelMusic:stop()
-- play lost music
game.lostMusic:play()
-- show game over
gameState.gameOverText:animateTo(0, 85, 2048)
-- fill the grid
for y = 19,0, -1 do
for x = 0,9 do
gameState.gridEntity.grid[y * 10 + x] = 7
end
wait(0.016)
end
wait(1)
game:fadeOut()
wait(0.3)
-- new highscore?
if gameState.gridEntity.score > game.highscores[10].score then
-- display new highscore input menu
newHighscoreState.highscore = gameState.gridEntity.score
game.fsm:changeState(newHighscoreState)
else
game.fsm:changeState(menuState)
end
end
function gameReadyThread()
-- first show instruction
-- fade in
game:fadeIn()
wait(0.2)
-- wait for an input
while not PlayerControl.player1Control:testTrigger("menu_valid") do
coroutine.yield()
end
game.menuValidSound:stop()
game.menuValidSound:play()
-- let's go!
gameState.view:animateTo(0, 0)
gameState.instructionView:animateTo(-640, 0)
-- setup text
gameState.gameReadyText:moveTo(640, 85)
gameState.gameReadyText.text = "Get Ready!"
-- wait for fade
wait(0.2)
-- show "get ready"
gameState.gameReadyText:animateTo(0, 85, 2048)
wait(1)
-- hide get ready
gameState.gameReadyText:animateTo(-640, 85, 2048)
wait(0.4)
-- show 1
gameState.gameReadyText:moveTo(640, 85)
gameState.gameReadyText.text = "1"
gameState.gameReadyText:animateTo(0, 85, 2048)
wait(0.4)
game.ready1Sound:play()
wait(0.4)
gameState.gameReadyText:animateTo(-640, 85, 2048)
-- show 2
gameState.gameReadyText:moveTo(640, 85)
gameState.gameReadyText.text = "2"
gameState.gameReadyText:animateTo(0, 85, 2048)
wait(0.4)
game.ready1Sound:play()
wait(0.4)
gameState.gameReadyText:animateTo(-640, 85, 2048)
-- show 3
gameState.gameReadyText:moveTo(640, 85)
gameState.gameReadyText.text = "3"
gameState.gameReadyText:animateTo(0, 85, 2048)
wait(0.4)
game.ready2Sound:play()
wait(0.4)
gameState.gameReadyText:animateTo(-640, 85, 2048)
game.levelMusic:play()
gameState.fsm:changeState(gameStatePlay)
end
function backToMainMenuThread()
-- fade out
game:fadeOut()
wait(0.3)
game.fsm:changeState(menuState)
end
inGameMenuState = { idx = 0 }
function inGameMenuState:enter()
if not self.window then
self.window = Sprite.new(game.menuWindowImage, nil, 60, -360 + 40)
game.scene:addChild(self.window)
self.cancelButton = Button.new("Cancel", 15, 70)
self.exitButton = Button.new("Exit", 105, 70)
self.window:addChild(self.exitButton)
self.window:addChild(self.cancelButton)
self.window:addChild(Text.new("Are you sure you want to go back to main menu?", 20, 30, 160, "center"))
else
-- move window on top
game.scene:removeChild(self.window)
game.scene:addChild(self.window)
end
self.idx = 0
game.menuCancelSound:stop()
game.menuCancelSound:play()
self.window:animateTo(60, 40, 2048)
end
function inGameMenuState:update(dt)
if PlayerControl.player1Control:testTrigger("left") and self.idx > 0 then
self.idx = 0
game.menuChangeSound:stop()
game.menuChangeSound:play()
end
if PlayerControl.player1Control:testTrigger("right") and self.idx < 1 then
self.idx = 1
game.menuChangeSound:stop()
game.menuChangeSound:play()
end
if PlayerControl.player1Control:testTrigger("menu_valid") then
if self.idx == 0 then
gameState.fsm:changeState(gameStatePlay)
game.menuValidSound:stop()
game.menuValidSound:play()
else
-- stop music
game.levelMusic:stop()
gameState.fsm:changeState(ThreadState.new(backToMainMenuThread))
game.menuCancelSound:stop()
game.menuCancelSound:play()
end
end
self.cancelButton.active = self.idx == 0
self.exitButton.active = self.idx == 1
end
function inGameMenuState:exit()
self.window:animateTo(60, -360 + 40, 2048)
end
local gameState = {}
gameState.mode = "classic"
function gameState:enter()
self.view = Entity.new(640, 0)
game.scene:addChild(self.view)
-- add element to the scene
self.scorePanel = Entity.new(640,0)
self.view:addChild(self.scorePanel)
self.levelText = Text.new("Level " .. self.level, 0, 10)
self.scorePanel:addChild(self.levelText)
self.highscoreText = Text.new("Highscore" .. game.highscores[9].score, 0, 30)
self.scorePanel:addChild(self.highscoreText)
self.scoreText = Text.new("0", 0, 40)
self.scorePanel:addChild(self.scoreText)
self.scorePanel:addChild(Text.new("Line", 0, 60))
self.linesText = Text.new("0", 0, 70)
self.scorePanel:addChild(self.linesText)
self.nextTetrominoPanel = Entity.new(-640 + 78, 10)
self.view:addChild(self.nextTetrominoPanel)
self.nextTetrominoPanel:addChild(Text.new("Next", 0, 0, 32, "left"))
self.nextTetromino = TetrominoEntity.new(0, 20, 0, 0)
self.nextTetrominoPanel:addChild(self.nextTetromino)
self.gridBezel = Sprite.new(game.bezelImage, nil, 117, 367)
self.view:addChild(self.gridBezel)
self.gridEntity = TetrominoGrid.new(3, 3)
self.gridBezel:addChild(self.gridEntity)
self.gridEntity.level = self.level
self.gridEntity.fallTime = levelsFallTime[self.level]
self.scorePanel:animateTo(210, 0, 2048)
self.nextTetrominoPanel:animateTo(78, 10, 2048)
self.gridBezel:animateTo(117, 7, 2048)
self.gameReadyText = Text.new("Get Ready!", 640, 85, 320, "center")
self.view:addChild(self.gameReadyText)
self.gameOverText = Text.new("Game Over", 640, 85, 320, "center")
self.view:addChild(self.gameOverText)
self.instructionView = Entity.new(0, 0)
game.scene:addChild(self.instructionView)
self.instructionView:addChild(Text.new("How to play", 0, 40, 320, "center"))
self.instructionView:addChild(Text.new("Move", 40, 70, 80, "right"))
self.instructionView:addChild(Text.new("Rotate", 40, 95, 80, "right"))
self.instructionView:addChild(Text.new("Fall", 40, 120, 80, "right"))
self.instructionView:addChild(Sprite.new(love.graphics.newImage("Gfx/controls.png"), nil, 140, 62))
local startBtn = Button.new("Start", 120, 150)
startBtn.active = true
self.instructionView:addChild(startBtn)
if self.mode == "challenge" then
-- fill with some "noise"
for y = 0, 3 + self.level / 4 do
for x = 0,9 do
local r = love.math.random()
if r < 0.25 then
self.gridEntity.grid[(19 - y) * 10 + x] = love.math.random(7)
end
end
end
end
self:generateTetromino(love.math.random(7) - 1)
self.fsm = FSM(ThreadState.new(gameReadyThread))
end
function gameState:exit()
-- clean game scene
game.scene:removeChild(self.view)
game.scene:removeChild(self.instructionView)
end
-- generate a next tetromino and
function gameState:generateTetromino(idx)
self.gridEntity.tetromino.index = idx
self.gridEntity.tetromino.orientation = 0
self.gridEntity.tetromino:moveToGrid(3, 0)
-- next one
self.nextTetromino.index = love.math.random(7) - 1
end
function gameState:update(dt)
self.fsm:update(dt)
end
return gameState