/
enemies.moon
361 lines (270 loc) · 6.27 KB
/
enemies.moon
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
{graphics: g} = love
import randomNormal from love.math
import ExplosionEmitter from require "emitters"
class EnemyBullet extends Entity
is_enemy_bullet: true
w: 4
h: 4
speed: 200
ox: 6
oy: 6
lazy sprite: => Spriter "images/sprites.png", 16, 16
new: (@x,@y, dir=Vec2d(-1, 0)) =>
@vel = @speed * dir
on_stuck: =>
@alive = false
draw: =>
@sprite\draw 6, @x - @ox, @y - @oy
super {0, 100, 255}
update: (dt, world)=>
super dt, world
@alive
class Enemy extends Entity
mixin HasEffects
is_enemy: true
core_color: { 255, 50, 50 }
is_powered: false
score: 37
radius: 7
inner_radius: 3
speed: 10
w: 10
h: 10
time: 0
new: (...) =>
super ...
@alive = true
@vel = Vec2d -@speed, 0
@seqs = DrawList!
@effects = EffectList!
@effects\add PopinEffect 0.2
@effects\add FadeInEffect 0.2
if @ai = @make_ai!
@seqs\add @ai
make_ai: =>
update: (dt, @world) =>
@move unpack @vel * dt
@time += dt * (@is_powered and 2 or 1)
@seqs\update dt
@alive = false if @x < -100
@alive
draw: =>
-- hack for effects mixin
@draw_inner!
draw_inner: =>
error "replace me"
die: =>
return unless @world
@world.hud\add_score @score
@dying = true
@world.particles\add ExplosionEmitter @world, @center!
@effects\add BlowOutEffect 0.2, -> @alive = false
AUDIO\play "enemy_die"
if @is_powered
import Powerup from require "player"
@world.entities\add Powerup @center!
take_hit: (thing, world) =>
return if @dying
if thing.is_player
thing\die!
if thing.is_bullet
@die!
thing.take_hit and thing\take_hit @, world
draw_hitbox: =>
return unless DEBUG
COLOR\push 255,0,0, 100
g.rectangle "fill", @unpack!
COLOR\pop!
draw_core: =>
cx, cy = @center!
if @is_powered
g.push!
g.translate cx, cy
scale = 1.5 + math.sin(@time * 2) / 2
g.scale scale, scale
g.rotate -@time
g.translate -cx, -cy
COLOR\push @core_color
g.rectangle "fill", cx - @inner_radius / 2, cy - @inner_radius / 2,
@inner_radius, @inner_radius
COLOR\pop!
if @is_powered
g.pop!
make_ai: =>
do return
Sequence ->
wait 2 + randomNormal!
if love.math.random! > 0.5
-- @shoot!
@shoot_sweep!
again!
-- points to player
shoot_dir: =>
target = Vec2d @world.player\center!
src = Vec2d @center!
(target - src)\normalized!
add_bullet: (...) =>
AUDIO\play "enemy_shoot"
cx, cy = @center!
@world.bullets\add EnemyBullet cx, cy, ...
shoot: =>
@add_bullet @shoot_dir!
shoot_sweep: (bullets=5, degs=40, delay=0.2) =>
rads = math.rad degs
@shooting = @seqs\add Sequence ->
dir = @shoot_dir!
half = rads/2
spin_dir = pick_one(-1, 1)
spin_step = rads/bullets
dir = dir\rotate -spin_dir * half
for i=1,bullets
@add_bullet dir
dir = dir\rotate spin_step * spin_dir
wait delay
class Drone extends Enemy
speed: 100
draw_inner: =>
@draw_core!
x, y = @center!
g.push!
g.translate x,y
g.rotate @time
g.circle "line", 0, 0, @radius, 6
g.pop!
@draw_hitbox!
class Shooter extends Enemy
draw_inner: =>
bar_l = 15
bar_w = 5
@draw_core!
ox = bar_l/2
oy = (bar_w + 2)
g.push!
cx, cy = @center!
cx -= 0.5
cy -= 0.5
g.translate cx, cy
g.rotate @time
g.translate -ox, -oy
g.rectangle "line", 0, 0, bar_l, bar_w
g.rectangle "line", 0, bar_w + 3, bar_l, bar_w
g.pop!
@draw_hitbox!
make_ai: =>
do return
Sequence ->
wait 2 + randomNormal!
if love.math.random! > 0.9
@shoot!
else
@shoot_sweep!
wait 2 + randomNormal!
again!
class Charger extends Enemy
aggression: 1
time: 0
radius: 4
update: (dt, ...) =>
super dt, ...
draw_inner: =>
@draw_core!
g.push!
cx, cy = @center!
cx -= 0.5
cy -= 0.5
g.translate cx, cy
g.rotate @time
pos = Vec2d(0, -1) * (@radius * 1.5)
for i=1,3
g.circle "line", pos[1], pos[2], @radius, 5
pos = pos\rotate math.pi*2/3
g.pop!
@draw_hitbox!
class Boss extends Enemy
is_powered: true
open: 0 -- from 0 to 1, opens mouth for when shooting
update: (dt, ...) =>
@vel = Vec2d 0,0
super dt, ...
draw_inner: =>
@draw_core!
@draw_hitbox!
g.push!
g.translate @center!
oy = 5
ox = 5
len = 30
wing = 12
rot = math.sin(@time) / 10 + @open * 0.6
-- top
g.push!
g.rotate rot
g.polygon "line", -len + ox, -oy,
ox, -oy - wing,
ox, -oy
g.pop!
-- bottom
g.push!
g.rotate -rot
g.polygon "line", -len + ox, oy,
ox, oy + wing,
ox, oy
g.pop!
g.pop!
class Spawner extends Sequence
speed: 200
sight: 600
is_spawner: true
active: false
new: (...) =>
super ...
@range = Box 0,0, @sight, 80
@range.x = @x - @range.w
@vel = Vec2d(-1, 0) * @speed
draw: =>
if DEBUG and @world
COLOR\push 0,100,255
g.setPointSize 5
g.point @x, @y
g.rectangle "line", @range\unpack!
COLOR\pop!
update: (dt, @world) =>
@x += dt * @vel[1]
@range.x = @x - @range.w
if not @active
for touching in *world.collider\get_touching @range
continue unless touching.is_player
world\first_enemies!
@active = true
if @active
return super dt, world
true
class ChainSpawner extends Spawner
count: 5
spacing: 23
new: (@world, @x, @y) =>
enemy_cls = Drone
super ->
time_offset = love.math.random! * math.pi * 2
for i=0,@count - 1
ox = enemy_cls.w / 2
oy = enemy_cls.h / 2
enemy = enemy_cls @x + i * @spacing - ox,
@y + @spacing * math.sin(i * math.pi / 5 + time_offset) / 2 - oy
if i == @count - 1
enemy.is_powered = true
@world.entities\add enemy
wait 0.1
class SingleSpawner extends Spawner
new: (@world, @enemy_cls, @x, @y) =>
super ->
@add_enemy!
wait 0.1 -- lame hack
add_enemy: =>
enemy_cls = Shooter
@world.entities\add enemy_cls @x, @y
{
:Enemy, :EnemyBullet
:Drone, :Shooter, :Charger, :Boss
:Spawner, :ChainSpawner, :SingleSpawner
}