-
Notifications
You must be signed in to change notification settings - Fork 9
/
enemy.lua
214 lines (189 loc) · 4.68 KB
/
enemy.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
local G = love.graphics
Enemy = Object:New {
quad_generator = Player.quad_generator,
list = {},
ang = 0,
flash = 0,
score = 0,
frame_length = 4,
alive = true,
hit_by_energy_blast = false,
}
function Enemy:init(rand, x, y)
table.insert(self.list, self)
self.rand = rand
self.trans_model = {}
self.x = x
self.y = y
self.tick = 0
self.entered_screen = false
transform(self)
end
function Enemy:hit(damage)
sound.play("hit", self.x, self.y)
self.flash = 3
self.shield = self.shield - damage
if self.shield <= 0 then
self.alive = false
game.player.score = game.player.score + self.score
end
end
function Enemy:kill()
end
function Enemy:die()
end
function Enemy:update()
if not self.alive then
makeExplosion(self.x, self.y)
self:die()
return "kill"
end
-- in screen?
if not self.entered_screen
and self.x < 428 and self.x > -428
and self.y < 328 and self.y > -328 then
self.entered_screen = true
end
if self.entered_screen and (self.y < -328 or self.y > 328)
or self.x > 428 or self.x < -428
or self.y > 500 then return "kill" end
if self.flash > 0 then self.flash = self.flash - 1 end
self.tick = self.tick + 1
local player = game.player
if not self.hit_by_energy_blast then
local blast = player.energy_blast
if blast.alive then
local x, y = naivePolygonCircleCollision(self.trans_model, blast.x, blast.y, blast.radius)
if x then
self:hit(blast.damage)
self.hit_by_energy_blast = true
end
end
end
self:subUpdate()
end
function Enemy:draw()
if self.flash > 0 then
self.quads.batch:setColor(1, 1, 1, 0.5)
else
self.quads.batch:setColor(1, 1, 1)
end
self:subDraw()
end
function Enemy:subDraw()
self.quads.batch:add(self.quads[math.floor(self.tick / self.frame_length) % #self.quads + 1],
self.x, self.y, -self.ang, 4, 4, self.size / 2, self.size / 2)
-- G.polygon("line", self.trans_model)
end
-- bullet ----------------------------------------------------------------------
Bullet = Object:New {
quad_generator = Laser.quad_generator,
list = {},
frame_length = 4,
size = 8,
ang = 0,
}
function Bullet:init(x, y, vx, vy)
table.insert(self.list, self)
self.trans_model = {}
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.tick = 0
end
function Bullet:makeSparks(x, y)
local c = self.color
for i = 1, 10 do BulletParticle(x, y, {c[1] * 0.6, c[2] * 0.6, c[3] * 0.6}) end
end
function Bullet:update()
self.tick = self.tick + 1
for i = 1, 2 do
self.x = self.x + self.vx / 2
self.y = self.y + self.vy / 2
transform(self)
if self.x > 405 or self.x < -405
or self.y > 305 or self.y < -305 then
return "kill"
end
local d, n, w = game.walls:checkCollision(self.trans_model)
if d > 0 then
sound.play("miss", w[1], w[2])
self:makeSparks(w[1], w[2])
return "kill"
end
local player = game.player
local blast = player.energy_blast
if blast.alive then
local x, y = naivePolygonCircleCollision(self.trans_model, blast.x, blast.y, blast.radius)
if x then
sound.play("miss", x, y)
self:makeSparks(x, y)
return "kill"
end
end
if player.alive then
if player.field_active then
local d, n, w = polygonCollision(self.trans_model, player.field.trans_model)
if d > 0 then
self:makeSparks(w[1], w[2])
return "kill"
end
elseif player.invincible == 0 then
local d, n, w = polygonCollision(self.trans_model, player.trans_model)
if d > 0 then
player:hit()
self:makeSparks(w[1], w[2])
return "kill"
end
end
for _, ball in ipairs(player.balls) do
if ball.alive then
if player.field_active then
local d, n, w = polygonCollision(self.trans_model, ball.field.trans_model)
if d > 0 then
self:makeSparks(w[1], w[2])
return "kill"
end
else
local d, n, w = polygonCollision(self.trans_model, ball.trans_model)
if d > 0 then
ball:hit()
self:makeSparks(w[1], w[2])
return "kill"
end
end
end
end
end
end
end
function Bullet:draw()
self.quads.batch:add(self.quads[math.floor(self.tick / self.frame_length) % #self.quads + 1],
self.x, self.y, -self.ang, 4, 4, self.size / 2, self.size / 2)
-- G.polygon("line", self.trans_model)
end
BulletParticle = SparkParticle:New {
friction = 0.9,
}
function BulletParticle:init(x, y, color)
self:super(x, y)
self.color = color
end
PlasmaBullet = Bullet:New {
color = { 1, 0.14, 0.14 },
model = { 4, 4, 4, -4, -4, -4, -4, 4, },
frame_length = 2,
size = 7,
}
PlasmaBullet:InitQuads("media/plasma_bullet.png")
initPolygonRadius(PlasmaBullet.model)
-- more enemies
require "blockade_enemy"
require "cannon_enemy"
require "ring_enemy"
require "rocket_enemy"
require "saucer_enemy"
require "spider_enemy"
require "square_enemy"
require "twister_enemy"