-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
254 lines (219 loc) · 5.89 KB
/
Copy pathplayer.lua
File metadata and controls
254 lines (219 loc) · 5.89 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
local M = {
}
DIR = {
RIGHT = 1,
LEFT = -1
}
local ACTION_BTN = {
spread = input.BTN1,
focus = input.BTN2,
bomb = input.BTN3,
}
local BULLET_SPEED = 600 -- px/s
local MAX_PLAYER_BULLETS = 12
local SPREAD_DEG = 6
local SPEED_DEFAULT = 140 -- px/s
local SPEED_SLOW = 80 -- px/s
-- returns nil if there are no available bullets, intentional to limit number of
-- active player bullets and encourage getting closer
local function find_available_bullet_idx(bullets)
local idx = nil
for i = 1, #bullets do
if not bullets[i].alive then
idx = i
break
end
end
return idx
end
-- creates a pool of bullets
local function init_bullets()
local bullets = {}
for i = 1, MAX_PLAYER_BULLETS do
bullets[i] = Bullet.fire(0, 0, { x = BULLET_SPEED, y = 0 }, Bullet.kind.PLAYER)
bullets[i].alive = false
end
return bullets
end
function M.init()
local p = {
invincible = false,
alive = true,
x = 32,
y = usagi.GAME_H / 2 - SPR_SIZE / 2,
speed = 120, -- px/s
fire_delay = 0.1, -- s
fire_cooldown = 0, -- s
fire_armed = false, -- requires BTN1 release before first shot, so a press that triggered scene entry doesn't auto-fire
bullets = init_bullets(),
chip_count = 0,
}
-- uncomment to make invincible in dev mode
-- if usagi.IS_DEV then
-- p.invincible = true
-- p.chip_count = Bomb.CHIP_COST
-- end
return p
end
local function fire(x, y, a)
local vel = { x = math.cos(a) * BULLET_SPEED, y = math.sin(a) * BULLET_SPEED }
return Bullet.fire(x, y, vel, Bullet.kind.PLAYER)
end
-- attempts to trigger the player's bomb if the player has enough chips; return
-- a boolean for whether or not the bomb was triggered
local function try_bomb(p)
if M.bombable(p) then
sfx.play("bomb")
table.insert(State.bombs, Bomb.init(p.x + SPR_SIZE / 2, p.y + SPR_SIZE / 2))
p.chip_count = 0
effect.screen_shake(0.3, 2)
return true
else
return false
end
end
-- whether or not the player is firing bullets
function M.firing()
return input.held(ACTION_BTN.spread) or input.held(ACTION_BTN.focus)
end
function M.update(dt, p)
assert(p, "player `p` is nil when it shouldn't be")
if not p.alive then
return
end
if p.fire_cooldown > 0 then
p.fire_cooldown -= dt
end
local is_focus = input.held(ACTION_BTN.focus)
if is_focus then
p.speed = SPEED_SLOW
else
p.speed = SPEED_DEFAULT
end
local delta = { x = 0, y = 0 }
if input.held(input.DOWN) then
delta.y = 1
elseif input.held(input.UP) then
delta.y = -1
end
if input.held(input.LEFT) then
delta.x = -1
elseif input.held(input.RIGHT) then
delta.x = 1
end
if input.pressed(ACTION_BTN.bomb) then
try_bomb(p)
end
if usagi.IS_DEV then
if input.key_pressed(input.KEY_1) then
p.invincible = not p.invincible
end
end
if not p.fire_armed and not input.held(ACTION_BTN.spread) and not input.held(ACTION_BTN.focus) then
p.fire_armed = true
end
if p.fire_armed and p.fire_cooldown <= 0 and (input.held(ACTION_BTN.spread) or is_focus) then
sfx.play("player_shoot_" .. math.random(1, 4))
p.fire_cooldown = p.fire_delay
local x = p.x + 10
local spread_deg = SPREAD_DEG
if is_focus then
spread_deg = 0
end
local bullet_idx = find_available_bullet_idx(p.bullets)
if bullet_idx then
p.bullets[bullet_idx] = fire(x, p.y + 2, math.rad(-spread_deg))
end
local bullet_idx_2 = find_available_bullet_idx(p.bullets)
if bullet_idx_2 then
p.bullets[bullet_idx_2] = fire(x, p.y + 8, math.rad(0))
end
local bullet_idx_3 = find_available_bullet_idx(p.bullets)
if bullet_idx_3 then
p.bullets[bullet_idx_3] = fire(x, p.y + 14, math.rad(spread_deg))
end
end
delta = util.vec_normalize(delta)
p.x += delta.x * p.speed * dt
p.x = util.clamp(p.x, 0, usagi.GAME_W - SPR_SIZE)
p.y += delta.y * p.speed * dt
p.y = util.clamp(p.y, 0, usagi.GAME_H - SPR_SIZE)
for i = 1, #p.bullets do
local b = Bullet.update(dt, p.bullets[i])
end
end
function M.draw(dt, p)
if not p.alive then
return
end
-- eyes
-- gfx.rect_fill(p.x + SPR_SIZE - 7, p.y - 2, 6, 6, gfx.COLOR_DARK_GREEN)
-- gfx.rect_fill(p.x + SPR_SIZE - 5, p.y - 1, 3, 3, gfx.COLOR_BLACK)
-- gfx.rect_fill(p.x + SPR_SIZE - 14, p.y - 2, 6, 6, gfx.COLOR_DARK_GREEN)
-- gfx.rect_fill(p.x + SPR_SIZE - 12, p.y - 1, 3, 3, gfx.COLOR_BLACK)
-- -- body
-- gfx.rect_fill(p.x, p.y + 4, SPR_SIZE, SPR_SIZE - 3, gfx.COLOR_DARK_GREEN)
-- gfx.rect_fill(p.x + 6, p.y + 6, SPR_SIZE - 12, SPR_SIZE - 12, gfx.COLOR_PINK)
gfx.spr(1, p.x, p.y)
-- local hit = M.hit_circ(p)
-- gfx.circ_fill(hit.x + 1, hit.y, hit.r + 1, gfx.COLOR_PINK)
if usagi.IS_DEV then
if p.invincible then
gfx.text("invincible", p.x - 18, p.y + SPR_SIZE, gfx.COLOR_RED)
end
if State.draw_debug then
local hitc = M.hit_circ(p)
gfx.circ(hitc.x, hitc.y, hitc.r, gfx.COLOR_RED)
end
end
for i = 1, #p.bullets do
Bullet.draw(p.bullets[i])
end
end
function M.hit(p)
if p.invincible then
return
end
sfx.play("player_death")
if try_bomb(p) then
effect.screen_shake(0.25, 3)
effect.slow_mo(0.2, 0.2)
else
p.alive = false
effect.screen_shake(0.5, 5)
effect.slow_mo(0.4, 0.3)
effect.flash(0.08, gfx.COLOR_WHITE)
local cx = p.x + SPR_SIZE / 2
local cy = p.y + SPR_SIZE / 2
Explosion.spawn(cx, cy, 28)
Pixels.spawn(cx, cy, 18, gfx.COLOR_DARK_GREEN)
Pixels.spawn(cx, cy, 10, gfx.COLOR_PINK)
end
end
function M.hit_circ(p)
local c = M.center(p)
return {
x = c.x,
y = c.y,
r = 2
}
end
function M.center(p)
return {
x = p.x + SPR_SIZE / 2,
y = p.y + SPR_SIZE / 2,
}
end
function M.collect_circ(p)
local c = M.center(p)
return {
x = c.x,
y = c.y,
r = 8
}
end
-- returns whether or not the player has enough chips to trigger bomb
function M.bombable(p)
return p.chip_count >= Bomb.CHIP_COST
end
return M