-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgame.lua
496 lines (420 loc) · 10.8 KB
/
game.lua
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
local G = love.graphics
flash_shader = G.newShader([[
vec4 effect(vec4 col, sampler2D tex, vec2 tex_coords, vec2 screen_coords) {
vec4 tc = texture2D(tex, tex_coords);
tc = vec4(tc.xyz * col.xyz, tc.w);
return tc + vec4(vec3(1.0, 1.0, 1.0) - tc.rgb, 0.0) * (1.0 - col.w);
}]])
Game = Object:New {}
Game.health_img = G.newImage("media/health.png")
Game.health_quads = makeQuads(16, 8, 8)
Game.canvas = G.newCanvas()
function Game:init()
self.stars = Stars()
self.walls = Walls()
self.player = Player()
end
function Game:playBack(demo)
self:reset(demo.seed)
self.record = demo.record
self.is_demo = true
end
function Game:makeRG()
return makeRandomGenerator(self.rand.int(0xffffff))
end
function Game:start(seed, input)
self.input = input
self:reset(seed)
end
function Game:reset(seed)
self.seed = seed
self.rand = makeRandomGenerator(self.seed)
self.record = {}
self.is_demo = false
self.tick = 0
self.outro = 0
self.blend = 1
self.action = false
self.pause = false
self.wall_rows = 0
self.blockades = 0
self.player:reset()
self.stars:reset(self:makeRG())
self.walls:reset(self:makeRG())
Boom.list = {}
Particle.list = {}
Laser.list = {}
Enemy.list = {}
Bullet.list = {}
Item.list = {}
self.flame_account = 0
self.heart_account = 0
self.saucer_delay = 6000
self.spawn_break_counter = 0
-- DEBUG
-- for i = 1, 720 do self.walls:generate() end
-- CannonEnemy(self:makeRG(), 0, 0, "left")
-- CannonEnemy(self:makeRG(), 50, -100, "up")
-- MoneyItem(0, 0)
-- SpeedItem(0, -100)
-- SpeedItem(0, -200)
-- BallItem(0, 100)
-- SaucerEnemy(self:makeRG(), 100, -150)
-- makeEnergyItem(0, -150, self.rand, 40)
end
function Game:trySpawnFlame(x, y, s)
self.flame_account = self.flame_account + (1 or s)
if self.flame_account >= 10 then
self.flame_account = self.flame_account - 10
SpeedItem(x, y)
end
end
function Game:trySpawnHeart(x, y, s)
self.heart_account = self.heart_account + (s or 1)
if self.heart_account >= 15
and (not self.player.balls[1].alive or not self.player.balls[2].alive) then
self.heart_account = self.heart_account - 15
BallItem(x, y)
return
end
if self.heart_account >= 25 then
self.heart_account = self.heart_account - 25
if self.player.shield < self.player.max_shield then
HealthItem(x, y)
else
MoneyItem(x, y)
end
end
end
function Game:next_wall_row()
self.wall_rows = self.wall_rows + 1
if self.wall_rows >= 100 then
self.wall_rows = self.wall_rows - 100
self.blockades = self.blockades + 1
end
if self.blockades > 0 then
local data = self.walls.data
local r = data[#data]
local suitable = true
for x, c in ipairs(r) do
if c == 0 then
local cl = r[x - 1] or 1
local cr = r[x + 1] or 1
if 2 <= cl and cl <= 5
or 2 <= cr and cr <= 5 then
suitable = false
break
end
end
end
if suitable then
self.blockades = self.blockades - 1
local prev
for ix, c in ipairs(r) do
if c == 0 then
r[ix] = -2
local x, y = self.walls:getTilePosition(ix, #data)
block = BlockadeEnemy(self:makeRG(), x, y, r, ix)
if prev then
block.neighbors[1] = prev
prev.neighbors[2] = block
end
prev = block
else
prev = nil
end
end
end
end
end
function Game:setPause(p)
if not self.is_demo then
self.pause = p
sound.pauseLoopSources(self.pause)
end
end
function Game:update()
-- DEBUG
-- love.timer.sleep(0.05)
if self.action ~= "BACK" and (Input:gotAnyPressed("back")
or self.is_demo and (Input:gotAnyPressed("start") or Input:gotAnyPressed("a"))) then
sound.play("back")
self.action = "BACK"
end
if not self.action then
-- blend
if self.blend > 0 then
self.blend = self.blend - 0.1
end
-- pause
if Input:gotAnyPressed("start") then
sound.play("pause")
self:setPause(not self.pause)
else
if self.pause and self.input:gotPressed("a") then
sound.play("pause")
self:setPause(false)
end
end
if self.pause then return end
else
if self.blend < 1 then
self.blend = self.blend + 0.1
end
if self.blend >= 1 then
if self.action == "BACK" then
state = menu
sound.stopLoopSources()
menu:swapState("main")
end
end
end
self.tick = self.tick + 1
-- input
local input = {}
if self.is_demo then
local i = self.record[self.tick] or {}
input.dx = i[1] or 0
input.dy = i[2] or 0
input.a = i[3] == 1
input.b = i[4] == 1
if not self.record[self.tick] then self.action = "BACK" end
else
input = self.input.state
local ri = {
input.dx,
input.dy,
input.a and 1 or 0,
input.b and 1 or 0,
}
for i = #ri, 1, -1 do
if ri[i+1] then break end
if ri[i] == 0 then ri[i] = nil end
end
self.record[self.tick] = ri
end
-- update entities
self.stars:update(self.walls.speed)
self.walls:update()
updateList(Particle.list)
updateList(Boom.list)
self.player:update(input)
updateList(Enemy.list)
updateList(Laser.list)
updateList(Bullet.list)
updateList(Item.list)
-- TODO: spawn enemies better
if self.saucer_delay > 0 then self.saucer_delay = self.saucer_delay - 1 end
if self.rand.float(-5, 1) > 0.99996^(self.tick/10 + 1000 + self.tick % 1000 * 3)
or self.spawn_break_counter > 70 then
self.spawn_break_counter = 0
local data = self.walls.data
local j = #data - 4
local wall_spot = {}
local spot = {}
for i, c in ipairs(data[j]) do
if c == 0 then
table.insert(spot, { i, j })
if data[j][i-1] == 1 then
table.insert(wall_spot, { i, j, "left" })
elseif data[j][i+1] == 1 then
table.insert(wall_spot, { i, j, "right" })
elseif data[j+1][i] == 1 then
table.insert(wall_spot, { i, j, "up" })
elseif data[j-1][i] == 1 then
table.insert(wall_spot, { i, j, "down" })
end
end
end
local side_spot = {}
for i = 9, #data - 4 do
if data[i ][1] == 0
and data[i + 1][1] == 0
and data[i + 2][1] == 0 then
table.insert(side_spot, { 1, i + 1, 0 })
end
if data[i ][26] == 0
and data[i + 1][26] == 0
and data[i + 2][26] == 0 then
table.insert(side_spot, { 26, i + 1, math.pi })
end
end
local r = self.rand.int(1, 9)
if #spot > 0 and r <= 4 then
local t = spot[self.rand.int(1, #spot)]
data[t[2]][t[1]] = -1
local x, y = self.walls:getTilePosition(unpack(t))
if r == 1 then
SquareEnemy(self:makeRG(), x, y)
elseif r < 4 then
RingEnemy(self:makeRG(), x, y)
end
end
-- wall enemies
if #wall_spot > 0 and r >= 5 and r <= 7 then
local t = wall_spot[self.rand.int(1, #wall_spot)]
data[t[2]][t[1]] = -1
local x, y = self.walls:getTilePosition(t[1], t[2])
if r == 5 then
RocketEnemy(self:makeRG(), x, y, t[3])
elseif r == 6 then
CannonEnemy(self:makeRG(), x, y, t[3])
else
SpiderEnemy(self:makeRG(), x, y, t[3])
end
end
-- twister
if #side_spot > 0 and r == 8 and self.rand.int(0, 3) == 0 then
local t = side_spot[self.rand.int(1, #side_spot)]
for iy = t[2] - 9, t[2] + 9 do
if data[iy] and data[iy][t[1]] == 0 then
data[iy][t[1]] = -1
end
end
local x, y = self.walls:getTilePosition(t[1], t[2])
x = x + (bool[x > 0] - bool[x < 0]) * 16
TwisterSpawn(self:makeRG(), x, y, t[3])
end
-- saucer
if #spot > 0 and r == 9 and self.tick % 3 == 0 and self.saucer_delay <= 0 then
self.saucer_delay = 3000 - self.tick * 0.02
local t = spot[self.rand.int(1, #spot)]
data[t[2]][t[1]] = -1
local x, y = self.walls:getTilePosition(t[1], t[2])
SaucerEnemy(self:makeRG(), x, y)
end
else
self.spawn_break_counter = self.spawn_break_counter + 1
end
-- game over
if not self.player.alive then
self.outro = self.outro + 1
if self.outro > 250 then
state = menu
sound.stopLoopSources()
if self.is_demo then
menu:swapState("main")
else
menu:gameOver(self)
end
end
end
end
quake = 0
function Game:draw()
quake = quake * 0.95
if quake < 1 then quake = 0 end
local quake_x = math.random(-quake, quake)
local quake_y = math.random(-quake, quake)
G.scale(G.getWidth() / 800, G.getHeight() / 600)
G.translate(400, 300)
G.translate(math.floor(quake_x), math.floor(quake_y))
if DEBUG then
G.scale(0.25)
G.translate(0, 650)
end
-- bump texture
Boom.canvas:renderTo(function()
G.clear(0, 0, 0, 1)
G.setColor(1, 1, 1)
G.setBlendMode("add")
Boom:DrawAll()
G.setBlendMode("alpha")
end)
-- background stuff
self.canvas:renderTo(function()
G.clear(0, 0, 0, 1)
self.stars:draw()
Particle:DrawBatch("back")
G.setColor(1, 1, 1)
Item:DrawBatch("back")
Player.quad_generator.batch:clear()
Enemy:DrawAll()
self.player:draw()
G.setShader(flash_shader)
G.draw(Player.quad_generator.batch)
G.setShader()
-- draw lasers and bullets
G.setColor(1, 1, 1)
Laser.quad_generator.batch:clear()
Bullet:DrawAll()
Laser:DrawAll()
G.draw(Laser.quad_generator.batch)
self.walls:draw()
end)
-- apply bump
G.origin()
G.setColor(1, 1, 1)
Boom.shader:send("bump", Boom.canvas)
G.setShader(Boom.shader)
G.setBlendMode("replace")
G.draw(self.canvas)
G.setBlendMode("alpha")
G.setShader()
-- G.setBlendMode("replace")
-- G.draw(self.canvas)
-- G.setBlendMode("add")
-- G.draw(Boom.canvas)
-- G.setBlendMode("alpha")
-- foreground stuff
G.scale(G.getWidth() / 800, G.getHeight() / 600)
G.translate(400, 300)
G.translate(math.floor(quake_x), math.floor(quake_y))
if DEBUG then
G.scale(0.25)
G.translate(0, 650)
G.rectangle("line", -400, -300, 800, 600)
end
Particle:DrawBatch("front")
Item:DrawBatch("front")
-- hud
-- score
G.origin()
G.scale(G.getWidth() / 800, G.getHeight() / 600)
G.setColor(1, 1, 1)
font:print(("%07d"):format(self.player.score), 800 - 6 * 4 * 7 - 8, 0)
-- hearts
for i = 1, self.player.max_shield do
local f = 1
if i > self.player.shield
or (self.player.shield == 1 and self.tick % 32 < 8) then
f = 2
end
G.draw(self.health_img, self.health_quads[f], 8 + (i - 1) * 32, 4, 0, 4)
end
-- energy
local m = self.player.max_energy
local e = self.player.energy
G.setColor(0.235, 0.235, 0.235, 0.392)
G.rectangle("fill", 400-m*2, 8, 4*m, 4)
G.setColor(0, 0.784, 0.784)
if self.player.field_active then
if self.tick % 8 < 4 then
G.setColor(0, 0.498, 0.498)
end
else
if self.player.energy == self.player.max_energy and self.tick % 8 < 4 then
G.setColor(1, 1, 1)
end
end
G.rectangle("fill", 400-m*2, 8, 4*e, 4)
-- pause
if self.pause then
G.setColor(1, 1, 1)
font:printCentered("PAUSE", 398, 300 - 20)
end
-- generate border image
-- if self.walls.row_counter % 18 == 0 and self.walls.offset == 0 then
-- local background = love.image.newImageData(800, 576)
-- background:paste(G.newScreenshot(), 0, 0, 0, 0, 800, 576)
-- background:encode("png", ("%03d.png"):format(self.walls.row_counter))
-- end
local blend = self.blend
if not self.player.alive and self.outro > 200 then
blend = math.max(blend, math.min(1, (self.outro - 200) / 50))
end
if blend > 0 then
G.setColor(0, 0, 0, blend)
G.rectangle("fill", 0, 0, 800, 600)
end
end