-
Notifications
You must be signed in to change notification settings - Fork 9
/
player.lua
489 lines (427 loc) · 11.2 KB
/
player.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
local G = love.graphics
local quad_generator = QuadGenerator(200)
BallField = Object:New {
quad_generator = quad_generator,
size = 16,
model = { 8, 16, 16, 8, 16, -8, 8, -16, -8, -16, -16, -8, -16, 8, -8, 16, },
}
BallField:InitQuads("media/ball_field.png")
initPolygonRadius(BallField.model)
function BallField:init()
self.trans_model = {}
end
Ball = Object:New {
quad_generator = quad_generator,
size = 8,
model = { 6, 6, 6, -6, -6, -6, -6, 6, },
}
Ball:InitQuads("media/ball.png")
initPolygonRadius(Ball.model)
function Ball:init(player, dir)
self.player = player
self.dir = dir
self.ox = 28 * dir
self.oy = 8
self.alive = false
self.trans_model = {}
self.field = BallField()
end
function Ball:activate()
self.alive = true
self.x = self.player.x
self.y = self.player.y
self.glide = 0
end
function Ball:shoot(side_shoot)
if not self.alive then return end
if side_shoot then
SmallLaser(self.x - 16 * self.dir, self.y, 4 * self.dir, -0.4)
else
SmallLaser(self.x, self.y + 16, 0.4 * self.dir, -4)
end
end
function Ball:hit()
if not self.alive then return end
sound.play("hit", self.x, self.y)
self.alive = false
for i = 1, 10 do
ExplosionSparkParticle(self.x, self.y)
end
local sm = SmokeParticle(self.x, self.y)
sm.dx = 0
sm.dy = 0
sm.ttl = 8
end
function Ball:update()
if not self.alive then return end
local dx = self.player.x + self.ox - self.x
local dy = self.player.y + self.oy - self.y
if self.glide < 0.3 then
self.glide = self.glide + 0.02
end
self.x = self.x + dx * self.glide
self.y = self.y + dy * self.glide
transform(self)
self.field.x = self.x
self.field.y = self.y
transform(self.field)
-- collision with wall
local d, n, w = game.walls:checkCollision(self.trans_model, true)
if d > 0 then
self:hit()
return
end
-- collision with enemies
for _, e in ipairs(Enemy.list) do
local d, n, w = polygonCollision(self.trans_model, e.trans_model)
if d > 0 then
e:hit(1)
self:hit()
return
end
end
end
function Ball:drawField()
if not self.alive then return end
local q1 = math.floor( self.player.tick / 4) % #self.field.quads + 1
local q2 = math.floor((self.player.tick + 3) / 4) % #self.field.quads + 1
self.quads.batch:setColor(0, 0.31, 0.31)
self.quads.batch:add(self.field.quads[q1], self.x, self.y, 0, 4, 4, 8, 8)
self.quads.batch:setColor(0.16 + math.sin(self.player.tick / 2) * 0.15, 0.31, 0.31)
self.quads.batch:add(self.field.quads[q2], self.x, self.y, 0, 4, 4, 8, 8)
end
function Ball:draw()
if not self.alive then return end
local f = math.floor(self.player.tick / 4) % #self.quads + 1
self.quads.batch:setColor(1, 1, 1)
self.quads.batch:add(self.quads[f], self.x, self.y, 0, 4 * self.dir, 4, 4, 4)
end
Player = Object:New {
quad_generator = quad_generator,
size = 16,
img = G.newImage("media/player.png"),
model = { 16, 16, 16, 0, 4, -12, -4, -12, -16, 0, -16, 16, },
field = Object:New {
size = 16,
img = G.newImage("media/field.png"),
model = { 16, 24, 24, 16, 24, 0, 4, -20, -4, -20, -24, 0, -24, 16, -16, 24, },
trans_model = {},
}
}
Player.field.quad_generator = Player.quad_generator
Player:InitQuads("media/player.png")
Player.field:InitQuads("media/field.png")
initPolygonRadius(Player.model)
initPolygonRadius(Player.field.model)
function Player:init()
self.trans_model = {}
self.balls = { Ball(self, -1), Ball(self, 1) }
self.energy_blast = EnergyBlast()
self.field_sound = sound.newLoopSource("field")
end
function Player:reset()
self.tick = 0
self.x = 0
self.y = 350
self.shield = 3
self.max_shield = self.shield
self.score = 0
self.energy = 0
self.max_energy = 30
self.field_active = false
self.speed_boost = 0
self.alive = true
self.invincible = 0
self.shoot_delay = 0
self.side_shoot = false
self.blast = 0
self.blast_x = 0
self.blast_y = 0
self.flash = 0
self.balls[1].alive = false
self.balls[2].alive = false
self.energy_blast.alive = false
-- transform(self)
end
function Player:hit(d, n, w, e)
sound.play("hit", self.x, self.y)
if DEBUG then return end
-- collision
if d then
self.x = self.x + n[1] * d
self.y = self.y + n[2] * d
-- instant death
if self.y > 284 and (not e or e.alive) then
self.invincible = 0
self.shield = 0
end
self.blast_x = n[1] * 7
self.blast_y = n[2] * 7
self.blast = 15
transform(self)
for i = 1, 10 do
ExplosionSparkParticle(w[1], w[2])
end
end
-- damage
if self.invincible == 0 then
self.invincible = 120
self.flash = 5
self.shield = self.shield - 1
if self.shield <= 0 then
self.alive = false
self.field_sound:stop()
self.balls[1]:hit()
self.balls[2]:hit()
makeExplosion(self.x, self.y)
end
end
end
function Player:update(input)
self.energy_blast:update()
if not self.alive then return end
self.tick = self.tick + 1
if self.flash > 0 then self.flash = self.flash - 1 end
self.blast_x = self.blast_x * 0.85
self.blast_y = self.blast_y * 0.85
-- calc speed
local speed = 0
if self.blast > 0 then
self.blast = self.blast - 1
else
-- speed = 3 + self.speed_boost * 0.2
speed = (self.speed_boost + 9) ^ 0.5
if self.shoot_delay > 0 or input.a then
speed = speed * 0.5
end
end
-- move
if self.tick < 60 then
self.y = self.y - 3
else
self.x = self.x + self.blast_x + input.dx * speed
self.y = self.y + self.blast_y + input.dy * speed
if self.x > 384 then self.x = 384 end
if self.x <-384 then self.x =-384 end
if self.y > 284 then self.y = 284 end
if self.y <-284 then self.y =-284 end
end
-- collision with walls
transform(self)
local d, n, w = game.walls:checkCollision(self.trans_model, true)
if d > 0 then
n[1] = -n[1]
n[2] = -n[2]
self:hit(d, n, w)
end
-- balls
self.balls[1]:update()
self.balls[2]:update()
if self.tick < 60 then return end
-- shoot
if input.dy > 0 then
self.side_shoot = false
elseif input.dy < 0 then
self.side_shoot = true
end
if input.a and self.shoot_delay == 0 and self.blast == 0 then
sound.play("laser", self.x, self.y)
self.shoot_delay = 10
Laser(self.x, self.y - 4)
self.balls[1]:shoot(self.side_shoot)
self.balls[2]:shoot(self.side_shoot)
end
if self.shoot_delay > 0 then
self.shoot_delay = self.shoot_delay - 1
end
-- field
if self.field_active then
self.energy = self.energy - 0.075
if self.energy < 0 then
self.energy = 0
self.field_active = false
self.field_sound:stop()
end
-- energy blast
if input.b and not self.input_b then
self.energy_blast:activate(self.x, self.y)
self.energy = 0
self.field_active = false
self.field_sound:stop()
end
elseif input.b and self.energy >= self.max_energy then
self.field_active = true
self.field_sound:play()
end
self.field_sound:setPosition(self.x, self.y, 0)
local p = self.energy / self.max_energy
self.field_sound:setPitch(1 - (p - 0.95) ^ 8)
self.input_b = input.b
self.field.x = self.x
self.field.y = self.y
transform(self.field)
-- collision with enemies
for _, e in ipairs(Enemy.list) do
local d, n, w = polygonCollision(self.trans_model, e.trans_model)
if d > 0 then
e:hit(1)
self:hit(d, n, w, e)
end
end
if self.invincible > 0 then
self.invincible = self.invincible - 1
end
end
function Player:draw()
self.energy_blast:draw()
if not self.alive then return end
local batch = self.quads.batch
-- field
if self.field_active then
self.balls[1]:drawField()
self.balls[2]:drawField()
local q1 = math.floor( self.tick / 4) % #self.field.quads + 1
local q2 = math.floor((self.tick + 3) / 4) % #self.field.quads + 1
batch:setColor(0, 0.31, 0.31)
batch:add(self.field.quads[q1], self.x, self.y, 0, 4, 4, 8, 8)
batch:setColor(0.16 + math.sin(self.tick / 2) * 0.15, 0.31, 0.31)
batch:add(self.field.quads[q2], self.x, self.y, 0, 4, 4, 8, 8)
end
-- balls
self.balls[1]:draw()
self.balls[2]:draw()
if self.invincible % 8 >= 4 then return end
if self.flash > 0 then
batch:setColor(1, 1, 1, 0.5)
else
batch:setColor(1, 1, 1)
end
batch:add(self.quads[1 + math.floor(self.tick / 8 % 2)],
self.x, self.y, 0, 4, 4, 8, 8)
-- if self.trans_model[1] then G.polygon("line", self.trans_model) end
-- if self.field.trans_model[1] then G.polygon("line", self.field.trans_model) end
end
Laser = Object:New {
quad_generator = QuadGenerator(200),
list = {},
model = { -2, 10, 2, 10, 2, -10, -2, -10, },
damage = 1
}
Laser:InitQuads("media/laser.png")
initPolygonRadius(Laser.model)
function Laser:init(x, y, vx, vy)
table.insert(self.list, self)
self.x = x
self.y = y
self.vx = vx or 0
self.vy = vy or -4
self.ang = math.atan2(self.vx, self.vy)
self.trans_model = {}
end
function Laser:update()
for i = 1, 4 do
self.x = self.x + self.vx
self.y = self.y + self.vy
if self.y < -310 or self.y > 310
or self.x < -410 or self.x > 410 then return "kill" end
transform(self)
local d, n, w = game.walls:checkCollision(self.trans_model, true)
if d > 0 then
sound.play("miss", w[1], w[2])
for i = 1, 10 do
LaserParticle(w[1], w[2])
end
return "kill"
end
for _, e in ipairs(Enemy.list) do
local d, n, w = polygonCollision(e.trans_model, self.trans_model)
if d > 0 then
e:hit(self.damage)
for i = 1, 10 do
LaserParticle(w[1], w[2])
end
return "kill"
end
end
end
end
function Laser:draw()
self.quads.batch:add(self.quads[1], self.x, self.y, -self.ang, 2, 2, 6, 6)
end
SmallLaser = Laser:New {
model = { -2, 5, 2, 5, 2, -5, -2, -5, },
damage = 0.5
}
SmallLaser:InitQuads("media/small_laser.png")
initPolygonRadius(SmallLaser.model)
function Laser:draw()
self.quads.batch:add(self.quads[1], self.x, self.y, -self.ang, 2, 2, 6, 6)
end
EnergyBlast = Object:New {
canvas = G.newCanvas(100, 100),
shader = G.newShader([[
uniform sampler2D table;
uniform float r;
uniform float s;
vec4 effect(vec4 col, sampler2D tex, vec2 tex_coords, vec2 screen_coords) {
float d = distance(vec2(50.0, 50.0), screen_coords);
float x = 0.0;
if (d > r) return vec4(0.0);
if (d < s) {
x = texture2D(table, vec2((s - d) * 0.1 , 0.0)).x;
return vec4(0.0, 1.0, 1.0, 0.6) * x;
}
if (d > r - 1.0) return vec4(1.0, 1.0, 1.0, 1.0);
return vec4(0.0, 1.0, 1.0, 0.6);
}
]]),
}
local data = love.image.newImageData(8, 1)
data:setPixel(0, 0, 0, 0, 0, 0)
data:setPixel(1, 0, 255, 255, 255, 255)
data:setPixel(2, 0, 255, 255, 255, 255)
data:setPixel(3, 0, 0, 0, 0, 0)
data:setPixel(4, 0, 0, 0, 0, 0)
data:setPixel(5, 0, 255, 255, 255, 255)
data:setPixel(6, 0, 0, 0, 0, 0)
EnergyBlast.img = G.newImage(data)
function EnergyBlast:activate(x, y)
self.shader:send("table", self.img)
sound.play("blast", self.x, self.y)
self.damage = 4
self.alive = true
self.x = x
self.y = y
self.level = 0
self.radius = 0
self:update()
end
function EnergyBlast:update()
if not self.alive then return end
self.y = self.y + game.walls.speed
self.level = self.level + 0.025
self.r = (1 - 2 ^ (-4 * self.level)) * 40
self.s = (1 - 2 ^ (-1.8 * self.level)) * 60
self.radius = self.r * 4
if self.level >= 1.2 then
self.alive = false
end
end
function EnergyBlast:draw()
if not self.alive then return end
self.canvas:renderTo(function()
G.clear(0, 0, 0, 0)
G.push()
G.origin()
self.shader:send("r", self.r)
self.shader:send("s", self.s)
local s = G.getShader()
G.setShader(self.shader)
G.rectangle("fill", 0, 0, 128, 128)
G.setShader(s)
G.pop()
end)
G.setColor(1, 1, 1, 0.78)
G.draw(self.canvas, self.x, self.y, 0, 4, 4, 50, 50)
G.setColor(1, 1, 1)
end