/
enemies.moon
248 lines (174 loc) · 4.87 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
{ graphics: g } = love
import insert from table
export *
-- 3 point bezier
bez3 = (x1, y1, x2, y2, x3, y3, t) ->
tt = 1 - t
x = tt * (tt * x1 + t * x2) + t * (tt * x2 + t * x3)
y = tt * (tt * y1 + t * y2) + t * (tt * y2 + t * y3)
x,y
-- derivative of 3 point bez
bez3_prime = (x1, y1, x2, y2, x3, y3, t) ->
tt = 1 - t
x = 2 * tt * (x2 - x1) + 2 * t * (x3 - x2)
y = 2 * tt * (y2 - y1) + 2 * t * (y3 - y2)
x,y
reverse = (t) ->
return for i=#t,1,-1 do t[i]
class Thing
speed: 200
new: (@x, @y) =>
@flip = false
update: (dt) =>
{:fx, :fy, :tx, :ty, :cx, :cy} = @
if tx and fx
before = @time
@time += dt * @tscale
if @time > 1
@x, @y = @tx, @ty
@tx, @ty = nil, nil
else
-- wow this is lame
ax, ay = bez3 fx, fy, cx, cy, tx, ty, before
bx, by = bez3 fx, fy, cx, cy, tx, ty, @time
dx = bx - ax
dy = by - ay
@x += dx
@y += dy
draw: =>
g.point @x ,@y
move_to: (@tx, @ty) =>
@time = 0
@fx, @fy = @x, @y
move = Vec2d(@tx - @fx, @ty - @fy)
move_len = move\len!
control_len = 1 + move_len / 4
dir = move\normalized!\cross!
dir = dir\flip! if @flip
@flip = not @flip
@cx = (@tx + @fx) / 2 + dir[1] * control_len
@cy = (@ty + @fy) / 2 + dir[2] * control_len
@tscale = 1 / move_len * @speed
-- stores enemies in the entity list in the world
class EnemySpawner extends Sequence
new: (@world, num_enemies=3) =>
super ->
for i=1, num_enemies
targets = PathEnemy\gen_targets @world, 2
if math.random! < 0.5
targets = reverse targets
{ix, iy} = table.remove targets, 1
-- @world.entities\add Enemy 0, 100, Vec2d(100, 0)
@world.entities\add PathEnemy ix, iy, targets
wait 2.0
draw: =>
class Enemy extends Box
is_enemy: true
w: 30
h: 30
life: 100
color: { 209, 100, 121 }
lazy_value @, "sprite", -> Player.sprite
new: (@x, @y, @vel=Vec2d(0,0), @accel=Vec2d(0,0))=>
shoot: (world, dir) =>
sfx\play "enemy_shoot"
dir = (Vec2d(world.player.box\center!) - Vec2d(@center!))\normalized!
world.entities\add EnemyBullet dir, @x, @y
take_hit: (thing, world) =>
if thing.is_bullet and not thing.is_enemy_bullet
thing.life = 0
@life -= 34
spray_dir = thing.vel\normalized!
world.particles\add Sparks spray_dir, world, thing\center!
@damage_seq = Sequence ->
@shake_time = 1
@rand = math.random!
tween @, 0.5, shake_time: 0
@shake_time = nil
if @life > 0
sfx\play "enemy_hit"
else
sfx\play "enemy_die"
world.player.score += math.random 101, 127
update: (dt, world) =>
@vel\adjust unpack @accel * dt
@damage_seq\update dt if @damage_seq
@move unpack @vel * dt
@life > 0
draw: =>
g.setColor 255,255,255
if t = @shake_time
g.push!
g.translate 5 * t * math.sin((t*10 + @rand)*5),
5 * t * math.cos((t*11 + @rand)*5)
o = (1 - t) * 255
g.setColor 255, o, o
else
g.setColor 255, 255, 255
cx, cy = @center!
@sprite\draw "273,19,30,25", cx, cy, 0, 2, 2, 15,12
if @shake_time
g.pop!
Sequence.default_scope.bezier_move_to = (thing, tx, ty, speed) ->
speed or= thing.speed
fx = thing.x
fy = thing.y
move = Vec2d tx - fx, ty - fy
move_len = move\len!
control_len = 1 + move_len / 4
dir = move\normalized!\cross!
dir = dir\flip! if math.random! > 0.5
cx = (tx + fx) / 2 + dir[1] * control_len
cy = (ty + fy) / 2 + dir[2] * control_len
tscale = 1 / move_len * speed
time = 0
remaining = 0
before_x, before_y = fx, fy
while time < 1
dt = coroutine.yield!
time += dt * tscale
if time > 1
remaining = (1 - time) / tscale
time = 1
-- wow this is lame
nx, ny = bez3 fx, fy, cx, cy, tx, ty, smoothstep 0, 1, time
dx = nx - before_x
dy = ny - before_y
before_x = nx
before_y = ny
thing.x += dx
thing.y += dy
if remaining > 0
coroutine.yield "more", remaining
class PathEnemy extends Enemy
@gen_targets: (world, num_targets=2) =>
max_height = world.box.h * 0.4
chunk = world.box.w / num_targets
min_x = 0
targets = {
{ world.box.x - 100, rand(-200, max_height) }
}
for i=1,num_targets
table.insert targets, {
rand min_x, min_x + chunk
rand 0, max_height
}
min_x += chunk
targets[#targets + 1] = {
world.box.w + 200, rand(-200, max_height)
}
targets
speed: 200
new: (@x, @y, @targets) =>
@speed = rand 150, 250
@seq = Sequence ->
for i, {x,y} in ipairs @targets
bezier_move_to @, x, y
unless i == #@targets
@shoot @world
wait rand 0.3, 0.7
update: (dt, world) =>
@world = world
@damage_seq\update dt if @damage_seq
@seq\update(dt) and @life > 0
nil