-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.lua
More file actions
263 lines (239 loc) · 9.94 KB
/
Copy pathenemy.lua
File metadata and controls
263 lines (239 loc) · 9.94 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
local Pattern = require("pattern") -- NOTE: required before enemy since enemy relies upon it
local M = {}
local HIT_FLASH_TIME = 0.1 -- s
local DEATH_PIXEL_COUNT = 24
local FLY_IN_SPEED = 160 -- px/s
local ARRIVE_DIST = 1 -- px
M.kind = {
popcorn = {
name = "Popcorn",
color = gfx.COLOR_INDIGO,
hp = 20,
r = 7,
chips = 6,
death_sfx = "popcorn_death",
sequence = {
{ fn = Pattern.fire_aimed, params = {}, count = 6, delay = 0.07, start_gap = 0.8 },
}
},
kernel = {
name = "Kernel",
color = gfx.COLOR_DARK_PURPLE,
hp = 28,
r = 9,
chips = 8,
death_sfx = "popcorn_death",
sequence = {
{ fn = Pattern.fire_aimed, params = { speed = 220, kind = Bullet.kind.ENEMY_SMALL }, count = 6, delay = 0.02, start_gap = 0.6 },
{ fn = Pattern.fire_aimed, params = { speed = 220, kind = Bullet.kind.ENEMY_SMALL }, count = 6, delay = 0.02, start_gap = 0.8 },
}
},
shotgun = {
name = "Shotgun",
color = gfx.COLOR_DARK_GRAY,
hp = 40,
r = 10,
chips = 10,
death_sfx = "popcorn_death",
sequence = {
{ fn = Pattern.fire_fan, params = { n = 3, speed = 100, kind = Bullet.kind.ENEMY_SMALL }, count = 3, delay = 0.06, start_gap = 0.8 },
{ fn = Pattern.fire_fan, params = { n = 4, speed = 100, kind = Bullet.kind.ENEMY_SMALL }, count = 3, delay = 0.06, start_gap = 0.8 },
{ fn = Pattern.fire_fan, params = { n = 8, spread = math.pi / 4, speed = 100, kind = Bullet.kind.ENEMY_SMALL }, count = 3, delay = 0.06, start_gap = 0.8 },
}
},
flower = {
name = "Flower",
color = gfx.COLOR_DARK_GREEN,
hp = 80,
r = 12,
chips = 16,
death_sfx = "popcorn_death",
sequence = {
{ fn = Pattern.fire_ring, params = { n = 12, speed = 80 }, count = 1, delay = 0.08, start_gap = 1 },
{ fn = Pattern.fire_ring, params = { n = 20, speed = 80 }, count = 4, delay = 0.08, start_gap = 1 },
{ fn = Pattern.fire_ring, params = { n = 20, speed = 80 }, count = 8, delay = 0.08, start_gap = 1 },
{ fn = Pattern.fire_ring, params = { n = 32, speed = 80 }, count = 10, delay = 0.08, start_gap = 1.5 },
{ fn = Pattern.fire_ring, params = { n = 52, speed = 80 }, count = 1, delay = 0.08, start_gap = 0.5 },
{ fn = Pattern.fire_ring, params = { n = 48, speed = 80 }, count = 1, delay = 0.08, start_gap = 0.5 },
}
},
midboss = {
name = "Midboss",
color = gfx.COLOR_RED,
hp = 200,
r = 16,
chips = 20,
death_sfx = "boss_death",
sequence = {
{ fn = Pattern.fire_aimed, params = { speed = 240, kind = Bullet.kind.ENEMY_SMALL }, count = 10, delay = 0.03, start_gap = 1.5 },
{ fn = Pattern.fire_aimed, params = { speed = 240, kind = Bullet.kind.ENEMY_SMALL }, count = 10, delay = 0.03, start_gap = 0.5 },
{ fn = Pattern.fire_ring, params = { n = 12, speed = 180, kind = Bullet.kind.ENEMY_BIG }, count = 5, delay = 0.08, start_gap = 1 },
{ fn = Pattern.fire_spiral, params = { n = 6, spin = 0.15 }, count = 10, delay = 0.04, start_gap = 2 },
{ fn = Pattern.fire_ring, params = { n = 40 }, count = 1, delay = 0.08, start_gap = 0.5 },
}
},
boss = {
name = "Boss",
color = gfx.COLOR_RED,
hp = 400,
r = 24,
chips = 100,
death_sfx = "boss_death",
alt_seq_hp_thres = 100,
sequence = {
{ fn = Pattern.fire_aimed, params = { speed = 240, kind = Bullet.kind.ENEMY_SMALL }, count = 10, delay = 0.03, start_gap = 0.5 },
{ fn = Pattern.fire_aimed, params = { speed = 120, kind = Bullet.kind.ENEMY_LARGE }, count = 20, delay = 0.05, start_gap = 0.7 },
{ fn = Pattern.fire_spiral, params = { n = 12, spin = 0.40, speed = 120 }, count = 12, delay = 0.04, start_gap = 1.0 },
{ fn = Pattern.fire_wall, params = { n = 6, base_angle = math.pi - 0.5, gap_n = 4, gap_shift = -3, spread = math.pi / 3, speed = 100 }, count = 16, delay = 0.15, start_gap = 1.0 },
{ fn = Pattern.fire_aimed, params = { speed = 240, kind = Bullet.kind.ENEMY_SMALL }, count = 10, delay = 0.03, start_gap = 0.5 },
{ fn = Pattern.fire_wall, params = { n = 6, base_angle = math.pi + 0.5, gap_n = 4, gap_shift = 3, spread = math.pi / 4, speed = 100 }, count = 16, delay = 0.15, start_gap = 1.0 },
{ fn = Pattern.fire_aimed, params = { speed = 240, kind = Bullet.kind.ENEMY_SMALL }, count = 10, delay = 0.05, start_gap = 0.5 },
{ fn = Pattern.fire_ring, params = { n = 52 }, count = 1, delay = 0.08, start_gap = 0.5 },
{ fn = Pattern.fire_ring, params = { n = 42 }, count = 1, delay = 0.08, start_gap = 0.5 },
{ fn = Pattern.fire_spiral, params = { n = 12, spin = 0.40, speed = 120 }, count = 12, delay = 0.04, start_gap = 1.0 },
{ fn = Pattern.fire_spiral, params = { n = 10, spin = 0.20, speed = 60, kind = Bullet.kind.ENEMY_BIG }, count = 12, delay = 0.04, start_gap = 0.3 },
{ fn = Pattern.fire_spiral, params = { n = 10, spin = 0.20, speed = 60, kind = Bullet.kind.ENEMY_BIG }, count = 12, delay = 0.04, start_gap = 0.3 },
{ fn = Pattern.fire_spiral, params = { n = 10, spin = 0.20, speed = 60, kind = Bullet.kind.ENEMY_BIG }, count = 12, delay = 0.04, start_gap = 0.3 },
{ fn = Pattern.fire_wall, params = { n = 6, base_angle = math.pi, gap_n = 4, gap_shift = 3, spread = math.pi / 4, speed = 100 }, count = 16, delay = 0.15, start_gap = 3.0 },
},
alt_sequence = {
{ fn = Pattern.fire_aimed, params = { speed = 260, kind = Bullet.kind.ENEMY_SMALL }, count = 20, delay = 0.03, start_gap = 0.3 },
{ fn = Pattern.fire_aimed, params = { speed = 260, kind = Bullet.kind.ENEMY_SMALL }, count = 20, delay = 0.03, start_gap = 0.3 },
{ fn = Pattern.fire_spiral, params = { n = 14, spin = 0.50, speed = 180 }, count = 12, delay = 0.04, start_gap = 0.5 },
{ fn = Pattern.fire_wall, params = { n = 6, gap_n = 4, gap_shift = -3, spread = math.pi / 3 }, count = 12, delay = 0.15, start_gap = 0.5 },
}
}
}
function M.init(kind, x, y, dest)
local sequence = kind.sequence
local first_phase = sequence[1]
return {
name = kind.name,
color = kind.color,
hp = kind.hp,
alive = true,
x = x,
y = y,
r = kind.r,
hit_timer = 0,
bullets = {},
chips = kind.chips,
fire_countdown = first_phase.start_gap, -- counts down until next shot; secs
spiral_angle = 0,
sequence = sequence,
alt_sequence = kind.alt_sequence,
alt_seq_hp_thres = kind.alt_seq_hp_thres,
alt_seq_active = false,
sequence_idx = 1,
pattern_shots_remaining = first_phase.count,
dest = dest,
arrived = false,
}
end
function M.hit(e, dmg)
dmg = dmg or 1
e.hit_timer = HIT_FLASH_TIME
e.hp -= dmg
-- shrink enemies as they take damage, with a minimum radius of 6px
e.r -= 0.10
e.r = math.max(e.r, 6)
if e.hp <= 0 then
sfx.play(e.death_sfx or "popcorn_death")
e.alive = false
Pixels.spawn(e.x, e.y, DEATH_PIXEL_COUNT, gfx.COLOR_RED)
assert(e.chips, "nil chips for e")
Chip.drop(e.chips, e.x, e.y)
end
end
function M.activate_alt_sequence(e)
print("alt seq activated!")
e.sequence_idx = 1
e.alt_seq_active = true
e.sequence = e.alt_sequence
end
local function advance_phase(e)
e.sequence_idx += 1
if e.sequence_idx > #e.sequence then
e.sequence_idx = 1
end
if e.alt_sequence and e.hp <= e.alt_seq_hp_thres and not e.alt_seq_active then
M.activate_alt_sequence(e)
end
local phase = e.sequence[e.sequence_idx]
assert(phase, "expected non-nil phase when advancing sequence")
if phase.fn == Pattern.fire_spiral then
e.spiral_angle = 0
end
e.fire_countdown = phase.start_gap
e.pattern_shots_remaining = phase.count
end
local function fire_bullet(e, p)
assert(p, "expected non-nil player `p`")
local phase = e.sequence[e.sequence_idx]
phase.fn(e, p, phase.params)
e.pattern_shots_remaining -= 1
if e.pattern_shots_remaining > 0 then
e.fire_countdown = phase.delay
else
advance_phase(e)
end
end
function M.update(dt, e, p)
if e.hit_timer > 0 then
e.hit_timer -= dt
end
if e.alive then
if not e.arrived then
local dx = e.dest.x - e.x
local dy = e.dest.y - e.y
local dist = math.sqrt(dx * dx + dy * dy)
local step = FLY_IN_SPEED * dt
if dist <= step + ARRIVE_DIST then
e.x = e.dest.x
e.y = e.dest.y
e.arrived = true
else
e.x = e.x + (dx / dist) * step
e.y = e.y + (dy / dist) * step
end
else
if e.fire_countdown > 0 then
e.fire_countdown -= dt
end
if e.fire_countdown <= 0 then
fire_bullet(e, p)
end
end
for i = #e.bullets, 1, -1 do
local b = e.bullets[i]
Bullet.update(dt, b)
if not b.alive then
table.remove(e.bullets, i)
end
end
end
end
function M.draw(e)
if not e.alive then
return
end
if e.hit_timer > 0 then
gfx.circ_fill(e.x, e.y, e.r, gfx.COLOR_ORANGE)
else
gfx.circ_fill(e.x, e.y, e.r, e.color)
gfx.circ_fill(e.x, e.y, 4, gfx.COLOR_WHITE)
gfx.circ_fill(e.x, e.y, 2, e.color)
if e.alt_seq_active then
gfx.circ_fill(e.x, e.y, e.r, gfx.COLOR_GREEN)
end
end
if usagi.IS_DEV then
if State.draw_debug then
gfx.circ(e.x, e.y, e.r, gfx.COLOR_RED)
end
end
for i = 1, #e.bullets do
Bullet.draw(e.bullets[i])
end
end
return M