-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
284 lines (247 loc) · 6.92 KB
/
player.lua
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
player = new_type(1)
player.hit_x = 1
player.hit_w = 5
player.hit_h = 6
player.hit_y = 2
player.t_jump_grace = 0
player.t_var_jump = 0
player.jumping = false
player.can_switch = true
player.t_bounce = 0
player.state = 0
--[[
hazard types:
1 - general hazard
2 - up-spike
3 - down-spike
4 - right-spike
5 - left-spike
]]
player.hazard_table = {
[1] = function(self) return true end,
[2] = function(self) return self.speed_y >= 0 end,
[3] = function(self) return self.speed_y <= 0 end,
[4] = function(self) return self.speed_x <= 0 end,
[5] = function(self) return self.speed_x >= 0 end,
}
function player:hazard_check(ox, oy)
local ox = ox or 0
local oy = oy or 0
for o in all(objects) do
if o.hazard and self:overlaps(o, ox, oy) and self.hazard_table[o.hazard](self) then
return true
end
end
return false
end
function player:correction_check(ox, oy)
return not self:hazard_check(ox, oy)
end
function player:on_collide_x(dir)
if dir == input_x and self:corner_correct(dir, 0, 2 * g_dir, -1) then
return false
end
return object.on_collide_x(self, dir)
end
function player:on_collide_y(dir)
if dir * g_dir < 0 and input_jump and self:corner_correct(0, -g_dir, 2, input_x) then
return true
end
return object.on_collide_y(self, dir)
end
function player:jump()
consume_jump_press()
self.jumping = true
self.speed_y = -6.2 * g_dir
self.remainder_y = 0
self.speed_x += 0.32 * input_x
self.remainder_x = 0
self.t_jump_grace = 0
self:move_y(self.jump_grace_y - self.y)
sfx(8)
end
function player:bounce_check(obj)
return self.speed_y * g_dir >= 0 and abs(obj.speed_y) < 1 and
((g_dir == 1 and self.y - self.speed_y < obj.y + obj.speed_y - 7) or
(g_dir == -1 and self.y - self.speed_y > obj.y + obj.speed_y + 7))
end
function player:spring()
self.jumping = true
self.t_jump_grace = 0
self.springboard:spring(self)
self:leave_springboard()
if input_jump then sfx(24)
else sfx(25) end
end
function player:leave_springboard()
self.state = 0
self.springboard.spr = 42
self.springboard = nil
end
function player:switch()
consume_switch_press()
g_dir *= -1
self.hit_y = max(0, g_dir * 2)
self.can_switch = false
self.t_jump_grace = 0
sfx(9)
end
function player:die()
sfx(12)
level_load = 30
end
function player:init()
set_camera_target(self.x)
snap_camera()
end
--[[
player states:
0 - normal
1 - springboard bounce
2 - finished
]]
function player:update()
local on_ground = self.speed_y == 0 and self:check_solid(0, g_dir)
if on_ground then
self.t_jump_grace = 4
self.jump_grace_y = self.y
if not self.was_on_ground and not self.can_switch then
self.can_switch = true
end
else
self.t_jump_grace = max(self.t_jump_grace - 1)
end
if self.state == 0 then
-- normal state
-- running
local accel = 0.1
if on_ground then
accel = 0.45
elseif input_x != 0 then
accel = 0.35
end
self.speed_x = approach(self.speed_x, 2.1 * input_x , accel)
-- facing
if input_x != 0 then
self.facing = input_x
end
-- gravity
if not on_ground then
local gravity = self.speed_y * g_dir < 0 and 0.72 or 0.54
self.speed_y = approach(self.speed_y, 5.4 * g_dir, gravity)
end
-- variable jumping
if self.jumping then
if self.speed_y * g_dir >= 0 then
self.jumping = false
elseif not input_jump then
self.speed_y *= 0.5
self.jumping = false
end
end
-- jumping
if input_jump_pressed > 0 then
if self.t_jump_grace > 0 then
self:jump()
end
end
elseif self.state == 1 then
-- springboard bounce state
self.t_bounce += 1
self.springboard.spr = 43
if self.t_bounce < 6 then
local at_y = approach(self.y, self.springboard.y - 2 * g_dir, 1.5)
self:move_y(at_y - self.y)
elseif self.t_bounce == 8 then
self:spring()
end
elseif self.state == 2 then
self.delay -= 1
if self.delay == 0 then
goto_level(level_index + 1)
if level_index != 4 then
music(-1)
sfx(14)
end
end
return
end
-- switch gravity
if self.can_switch and input_switch_pressed > 0 then
self:switch()
end
-- apply
self:move_x(self.speed_x)
self:move_y(self.speed_y)
-- object interactions
for o in all(objects) do
if o.base == crumble and not o.breaking then
-- crumble
if self:overlaps(o, 0, g_dir) then
o.breaking = true
sfx(10)
end
elseif o.base == springboard and self:overlaps(o) and self.state != 1 and self:bounce_check(o) then
-- springboard
self.speed_x = 0
self.speed_y = 0
self.springboard = o
self.t_bounce = 0
self.state = 1
self:move_y(o.y - 8 * g_dir - self.y)
self.can_switch = true
elseif o.base == smasher and self:overlaps(o) then
-- smasher
self:die()
return
elseif o.base == mover then
-- mover
if self:overlaps(o, 0, g_dir) then
if self.speed_y == 0 and self.mover != o then
o.state = 1
self.mover = o
o.player = self
end
elseif self.mover == o then
self.mover.player = nil
self.mover = nil
end
elseif o.base == checkpoint and o.id != level_checkpoint and self:overlaps(o) then
-- checkpoint
level_checkpoint = o.id
sfx(11)
end
end
-- death
if (self.y + self.hit_y - 16 > level_top() * 8 and g_dir == 1) or
(self.y + self.hit_y + self.hit_h - 1 + 16 < level_bottom() * 8 and g_dir == -1) or
self:hazard_check() then
self:die()
return
end
-- bounds
if self.x + self.hit_x < 0 then
self.x = -self.hit_x
object.on_collide_x(self)
elseif self.x + 1 > 1024 then
self.state = 2
self.delay = 5
end
self.was_on_ground = on_ground
-- camera
set_camera_target(self.x)
-- animation
if not on_ground and self.state != 1 then
self.spr = 2
elseif input_x != 0 then
self.spr += 0.25
if self.spr >= 3 then
self.spr = 1
end
else
self.spr = 1
end
end
function player:draw()
spr(self.spr, self.x, self.y, 1, 1, self.facing != 1, g_dir != 1)
end