-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenemy.moon
More file actions
408 lines (302 loc) · 8.47 KB
/
enemy.moon
File metadata and controls
408 lines (302 loc) · 8.47 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
import graphics from love
export ^
class MoveSequence extends Sequence
@extend {
move: (thing, x,y, time) ->
vx = x/time
vy = y/time
while time > 0
dt = coroutine.yield!
time -= dt
thing\set_direction Vec2d vx, vy if thing.set_direction
real_dt = if time < 0 then dt + time else dt
cx, cy = thing\fit_move real_dt * vx, real_dt * vy
vx = -vx if cx
vy = -vy if cy
thing\set_direction! if thing.set_direction
coroutine.yield "more", -time if time < 0
shake: (thing, total_time, mx=5, my=5, speed=10, decay_to=0) ->
ox, oy = thing.ox, thing.oy
time = total_time
while time > 0
time -= coroutine.yield!
decay = math.min(math.max(time, decay_to), 1)
dx = decay * mx * math.sin(time*10*speed)
dy = decay * my * math.cos(time*10*speed)
thing.ox = ox + dx
thing.oy = oy + dy
thing.ox, thing.oy = ox, oy
coroutine.yield "more", -time if time < 0
charge: (thing, dir, speed=200, decay=0.5) ->
thing.velocity = dir\normalized! * speed
Sequence.default_scope.tween thing.velocity, decay, [1]: 0, [2]: 0
thing\set_direction dir if thing.set_direction
}
class Enemy extends Entity
watch_class self
blood_color: {}
w: 6
h: 5
ox: 0, oy: 0
alive: true
life: 15
stun_time: 0.3
immune: false
__tostring: => ("<Slime %s, %s>")\format @box\pos!
new: (...) =>
super ...
@make_ai!
make_ai: => error "implement me"
draw: =>
@hit\before! if @hit
@anim\draw @box.x + @ox, @box.y + @oy
@hit\after! if @hit
update: (dt) =>
@ai\update dt if @box\touches_box @world.game.viewport
@anim\update dt
if @hit
@hit = nil unless @hit\update dt
super dt
@life > 0 or @hit
hurt_player: (player) => player\take_hit self
take_hit: (weapon) =>
return if @hit or @life < 0
return if @immune
sfx\play "enemy_is_hit"
damage = weapon\calc_damage self
x,y = @box\center!
@world.high_particles\add NumberParticle damage, x,y
@world.particles\add with BloodEmitter @world, x,y
.blood_color = @blood_color
@life -= damage
if @life < 0
sfx\play "enemy_die"
@hit = join_effect Flash!, Fade!
else
@hit = Flash!
px, py = weapon.player.box\center!
if @stun_time > 0
@velocity = Vec2d(x - px, y - py) * 5
@ai = Sequence ->
tween @velocity, @stun_time, [1]: 0, [2]: 0
@make_ai!
module "enemies", package.seeall
-- wanders around
class GreenSlime extends Enemy
ox: -3, oy: -6
life: 15
blood_color: {267,244,129}
new: (...) =>
super ...
@sprite = with Spriter imgfy"img/sprite.png", 10, 13
.ox = 60
@anim = @sprite\seq {0, 1}, 0.2
make_ai: =>
@ai = MoveSequence ->
wait 0.5
dx, dy = unpack Vec2d.random 10
move self, dx, dy, 1.0
wait 0.5
again!
class Bullet extends Entity
watch_class self
ox: -3, oy: -3
size: 2
@id = 0
__tostring: => "<Bullet " .. tostring(@@id) .. ">"
new: (@world, x, y, @anim, @velocity) =>
@@id += 1
@box = Box x,y, @size, @size
update: (dt) =>
@anim\update dt
colx, coly = super dt
not @hit and not colx and not coly
draw: =>
blend = graphics.getBlendMode!
graphics.setBlendMode "multiplicative"
@anim\draw @box.x + @ox, @box.y + @oy
graphics.setBlendMode blend
hurt_player: (player) =>
@hit = true
player\take_hit self
class BounceBullet extends Bullet
time: 1.3
update: (dt) =>
@time -= dt
@anim\update dt
colx, coly = Entity.update self, dt
@velocity[1] = -@velocity[1] if colx
@velocity[2] = -@velocity[2] if coly
not @hit and @time > 0
draw: =>
-- fade out
if @time < 0.3
r,g,b,a = graphics.getColor!
graphics.setColor r,g,b, 255 * @time / 0.3
super!
graphics.setColor r,g,b,a
else
super!
-- charges
class BlueSlime extends Enemy
ox: -3, oy: -6
life: 25
blood_color: {65,141,255}
new: (...) =>
super ...
@sprite = with Spriter "img/sprite.png", 10, 13
.ox = 80
@anim = @sprite\seq {0, 1}, 0.2
make_ai: =>
@ai = MoveSequence ->
wait 0.5
player = @world.game.player
vec = @box\vector_to player.box
if vec\len! < 55
shake self, 0.8, 2, 1
@velocity = vec\normalized! * 200
tween @velocity, 0.5, [1]: 0, [2]: 0
else
dx, dy = unpack Vec2d.random 10
move self, dx, dy, 1.0
wait 0.5
again!
class RedSlime extends Enemy
ox: -3, oy: -6
bullet_cls: Bullet
life: 35
blood_color: {255,200,200}
bullet_frames: { "101,28,8,9", "111,29,8,9" }
new: (...) =>
super ...
@sprite = with Spriter imgfy"img/sprite.png", 10, 13
.ox = 100
@anim = @sprite\seq {0, 1}, 0.2
@bullet_sprite = Spriter "img/sprite.png"
shoot: (vec, silent=false) =>
anim = @bullet_sprite\seq @bullet_frames, 0.1
x,y = @box\center!
vel = vec\normalized! * 50
sfx\play "single_shot" unless silent
@world.entities\add self.bullet_cls @world, x,y, anim, vel
spray: (vec) =>
sfx\play "spread_shot"
for deg=0,360,30
@shoot Vec2d.from_angle deg
make_ai: =>
@ai = MoveSequence ->
wait 0.5
player = @world.game.player
vec = @box\vector_to player.box
len = vec\len!
if math.random() < 0.5 and len < 65
if len < 40
shake self, 1.5, 2, 1
@spray!
wait 0.5
else
@shoot vec
else
dx, dy = unpack Vec2d.random 10
move self, dx, dy, 1.0
wait 0.5
again!
class BadRedSlime extends RedSlime
bullet_cls: BounceBullet
class MadDog extends Enemy
life: 35
new: (...) =>
super ...
with Spriter"img/sprite.png"
stand_left_right = { "60,65,17,12", "60,78,17,12" }
stand_up_down = { "82,52,5,13", "82,65,5,13" }
left_right = { "60,65,17,12", "60,52,17,12" }
up_down = { "82,52,5,13", "93,52,5,13" }
@anim = StateAnim "stand_left", {
stand_down: \seq stand_up_down, 0.25, true, true
stand_up: \seq stand_up_down, 0.25
stand_right: \seq stand_left_right, 0.25, true
stand_left: \seq stand_left_right, 0.25
walk_down: \seq up_down, 0.25, false, true
walk_up: \seq up_down, 0.25
walk_right: \seq left_right, 0.25, true
walk_left: \seq left_right, 0.25
}
set_direction: (other_vel) =>
v = @velocity
v += other_vel if other_vel
@anim\set_state @direction_name "left", v
if @last_direction == "left" or @last_direction == "right"
@ox, @oy = -5, -2
else
@ox, @oy = 0, -3
make_ai: =>
@ai = MoveSequence ->
wait 0.5
player = @world.game.player
vec = @box\vector_to player.box
if math.random() < 0.3
shake self, 0.3, 2, 1
wait 1.0
elseif vec\len! < 60
shake self, 1.0, 1, 1
charge self, vec, 150, 0.3
wait 0.1
charge self, @box\vector_to(player.box), 150, 0.3
else
dx, dy = unpack Vec2d.random 25
move self, dx, dy, 0.6
again!
class HugeSlime extends RedSlime
life: 334
ox: -20, oy: -15
w: 35, h: 30
blood_color: {267,244,129}
bullet_frames: { "81,28,8,9", "91,29,8,9" }
stun_time: 0.0
new: (...) =>
super ...
with Spriter "img/slime.png"
@anim = \seq {"0,0,70,51", "0,52,70,51"}, 0.25
@bullet_sprite = Spriter "img/sprite.png"
update: (dt) =>
@box = Box @box.x, @box.y, @w, @h
super dt
take_hit: (...) =>
super ...
if @life < 0
@world\goto_next_level!
make_ai: =>
@ai = MoveSequence ->
player = @world.game.player
wait 0.5
r = math.random()
if r < 0.1
-- mega attack
@immune = true
shake self, 1.5, 3, 3
@spray!
charge self, @box\vector_to(player.box), 150, 0.3
charge self, @box\vector_to(player.box), 250, 0.4
@immune = false
wait 1.0
elseif r < 0.3
for i=1,4
@shoot @box\vector_to player.box
wait 0.3
wait 1.0
elseif r < 0.5
@shoot @box\vector_to player.box
wait 0.4
@spray!
wait 0.5
elseif r < 0.8
shake self, 0.6, 1, 1
charge self, @box\vector_to(player.box), 200, 0.4
wait 0.5
dx, dy = if r < 0.5
unpack @box\vector_to(player.box)\normalized! * 15
else
unpack Vec2d.random 15
move self, dx, dy, 0.8
again!