-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.js
410 lines (345 loc) · 12.1 KB
/
player.js
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
407
408
409
410
let player_x = 9
let player_y = 5
let player_y_nowrap = 0
let player_z = 3
let player_natural_z = 1
let player_natural_z_deviation = 2
let player_speed = 1.6
let player_x_speed = 1.8
let player_iframes = 0 // ++ and --. we may have iframes for 2 reasons
let player_inertia_x = 0.1
let player_inertia_y = 0.2
let player_inertia_z = 0.1
let player_far_from_ground_control = 0.4
let default_mov_y = 1.2
let prev_mov_x = 0
let prev_mov_y = 0.6
let prev_mov_z = 0
let frames_in_natural_z = 0
let player_lunge_forward_as_a_result_of_hitting_a_wall_speed = 5
// The player has no collision while jumping and inside the track
let player_should_collide = () =>
!(player_z > 6 && player_x > 2 && player_x < map_len_x - 2) && !PLAYER_NO_COLLIDE && !ENDING_CUTSCENE
let sideways_force_from_turns_gradual = gradually_change(0, 0.05)
let updatePlayer = () => {
if (player_iframes) return
let mov_y = default_mov_y
let mov_x = 0
let mov_z = 0
if (keys.KeyW) mov_y += 1
// if (keys.s) mov_y -= .5 // brakes
if (keys.KeyA) mov_x -= 1
if (keys.KeyD) mov_x += 1
// jump
let z_deviation = player_z - player_natural_z;
if (keys.Space && frames_in_natural_z > 5) {
mov_z = 3
frames_in_natural_z = 0
} else {
// mov_z needs gravity+inertia
if (player_z < 0.05) {
player_z = abs(player_z);
mov_z = abs(prev_mov_z) // go up pls
prev_mov_z = abs(prev_mov_z) // go up pls
frames_in_natural_z = 0
}
else if (abs(z_deviation) > player_natural_z_deviation) {
mov_z = -z_deviation * .2
frames_in_natural_z = 0
} else {
mov_z = -z_deviation * 0.1
frames_in_natural_z++
}
mov_z = lerp(prev_mov_z, mov_z, player_inertia_z)
}
if (mov_x && mov_y) {
mov_x *= (FORTY_FIVE_DEG_DIST + 1) / 2
mov_y *= (FORTY_FIVE_DEG_DIST + 1) / 2
}
// Inertia
mov_x = lerp(prev_mov_x, mov_x, player_inertia_x)
mov_y = lerp(prev_mov_y, mov_y, player_inertia_y)
prev_mov_x = mov_x
prev_mov_y = mov_y
prev_mov_z = mov_z
let sideways_force_from_turns = sideways_force_from_turns_gradual(CURRENT_TURN * .8)
mov_x -= sideways_force_from_turns * prev_mov_y
let difficulty_speed_increase = lerp(1, 2, DIFFICULTY);
mov_y *= difficulty_speed_increase
// try new position to see how we feel
mov_x = (mov_x * player_x_speed) + player_x
mov_y = (mov_y * player_speed) + player_y
mov_z = mov_z + player_z
// Physics: the world wraps around
mov_x = wrap_around(0, map_len_x - 1, mov_x)
mov_y = wrap_around(0, map_len_y - 1, mov_y)
let has_collided
if (
mov_x > 0 && mov_y > 0
&& mov_x < map_len_x && mov_y < map_len_y
&& (
player_should_collide()
? !(has_collided = (
map_collide_line_segment(player_x, player_y, mov_x, mov_y)
))
: true
)
) {
player_x = mov_x
player_y = mov_y
player_y_nowrap += prev_mov_y
player_z = mov_z
}
// Bounce the player to the center of the track
if (has_collided) {
player_iframes++
// barrel roll into the center of the track
(async () => {
let end = game_now() + 1000
while (game_now() < end) {
prev_mov_x = 0
player_x = mov_x = lerp(player_x, map_len_x / 2, 0.3)
player_y += player_lunge_forward_as_a_result_of_hitting_a_wall_speed
player_y_nowrap += player_lunge_forward_as_a_result_of_hitting_a_wall_speed
prev_mov_y = delayed_mov_y = player_speed + 0.4 // simulate high speed for other componets of the game
mov_y = player_speed + 0.2 // simulate high speed for other componets of the game
player_y = wrap_around(0, map_len_y - 1, player_y)
await sleep(FRAME_DELTA_MS)
}
player_iframes--
})()
}
}
let hovering = 0
let burning = 0
let delayed_mov_x = prev_mov_x
let delayed_mov_y = prev_mov_y
let delayed_mov_z = prev_mov_z
let shaking = 0
let shaking_intensity_period = 0
let gradual_shadow_alpha = gradually_change(1, 0.8)
let gradual_current_turn = gradually_change(0)
let draw_hull = ([tip, botLeft, botRight, top], color_left, color_right, color_highlight, color_back, lineWidth) => {
let drawTriangle = (color, lineColor, a, b, c) => {
ctx.fillStyle = color
ctx.beginPath()
ctx.moveTo(...a)
ctx.lineTo(...b)
ctx.lineTo(...c)
ctx.lineTo(...a)
ctx.fill()
ctx.lineWidth = lineWidth
ctx.strokeStyle = lineColor
ctx.beginPath()
ctx.moveTo(...a)
ctx.lineTo(...b)
ctx.lineTo(...c)
ctx.lineTo(...a)
ctx.stroke()
ctx.lineWidth = 1
}
// right triangle is facing us, draw it in front
if (top[0] < tip[0]) {
// left
drawTriangle(color_left, color_highlight, tip, top, botLeft)
// right
drawTriangle(color_right, color_highlight, tip, top, botRight)
} else {
// right
drawTriangle(color_right, color_highlight, tip, top, botRight)
// left
drawTriangle(color_left, color_highlight, tip, top, botLeft)
}
// back
drawTriangle(color_back, 'brown', top, botLeft, botRight)
}
let shrink_hull = (hull_coords, scale_x, scale_y) => {
let midpoint_x=0, midpoint_y=0;
hull_coords.forEach(([x, y]) => {
midpoint_x += x
midpoint_y += y
})
midpoint_x /= hull_coords.length
midpoint_y /= hull_coords.length
hull_coords.forEach((cx) => {
let at_origin = cx[0] - midpoint_x;
at_origin *= scale_x
cx[0] = midpoint_x + at_origin;
at_origin = cx[1] - midpoint_y;
at_origin *= scale_y
cx[1] = midpoint_y + at_origin;
})
}
let move_hull = (hull_coords, move_x, move_y) => {
return hull_coords.forEach((cx) => {
cx[0] = (cx[0] + move_x) | 0
return cx[1] = (cx[1] + move_y) | 0
})
}
let create_hull = (center_x, center_y, prev_mov_x, global_turn) => {
let tip = [center_x + 1, center_y - 15]
let botLeft = [center_x - 30, center_y + 20]
let botRight = [center_x + 30, center_y + 20]
let top = [center_x - 1, center_y - 2]
let t = global_turn
tip[0] += t * 20
botLeft[1] -= t * 5
botRight[1] += t * 5
// Wiggle as we move X
tip[0] += prev_mov_x * 32
tip[1] += abs(prev_mov_x) * 7
top[0] -= prev_mov_x * 5 * 2
top[1] -= abs(prev_mov_x) * 5
if (prev_mov_x < 0) {
// when turning left
botLeft[0] -= prev_mov_x * 25
botRight[1] += prev_mov_x * 25
botLeft[1] -= prev_mov_x * 25
}
if (prev_mov_x > 0) {
botRight[0] -= prev_mov_x * 25
botLeft[1] -= prev_mov_x * 25
botRight[1] += prev_mov_x * 25
}
return [tip, botLeft, botRight, top]
}
let ending_cutscene_player_closer_gradual = gradually_change(0, 0.01)
let drawPlayer = () => {
// shake our guy with a sin()
shaking = shaking += 8
shaking_intensity_period += 0.1
hovering += abs(abs(prev_mov_y) - abs(prev_mov_x)) * 0.1
// burn intensity is 2 components, one is how much harder we're accelerating, and the other is how much we're accelerating in general
let burn_intensity = clamp(0, 10,
(clamp(0, 1, prev_mov_y - default_mov_y) * 4)
+ (clamp(0, 1, prev_mov_y - delayed_mov_y) * 6)
) / 10 // 0..=1
burning += burn_intensity
let HULL = create_hull(halfWidth, canvasHeight - 30, prev_mov_x, gradual_current_turn(CURRENT_TURN))
let [tip, botLeft, botRight, top] = HULL
// for sfx
delayed_mov_x = lerp(delayed_mov_x, prev_mov_x, 0.1)
delayed_mov_y = lerp(delayed_mov_y, prev_mov_y, 0.1)
delayed_mov_z = lerp(delayed_mov_z, prev_mov_z, 0.1)
// Lower tip as we move faster
tip[1] += (prev_mov_y - default_mov_y) * 5
// Buttclench as we move faster
botLeft[0] += (delayed_mov_y - default_mov_y) * 8
botRight[0] -= (delayed_mov_y - default_mov_y) * 8
// shake the tip when we go fast
let how_fast_to_shake = 1 + (delayed_mov_y - prev_mov_y) * 2
let how_much_to_shake = clamp(0, 1, remap(default_mov_y + 0.3, player_speed, 0, 1, delayed_mov_y)) * 0.5 * (sin(shaking_intensity_period) + 0.8)/2
tip[0] += cos(shaking * how_fast_to_shake + 10) * how_much_to_shake
tip[1] -= cos(shaking * how_fast_to_shake + 10) * how_much_to_shake * .5
// Raise the tip, lower the bottom, when we jump
tip[1] -= prev_mov_z * 5
top[1] += prev_mov_z * 8
botLeft[1] += prev_mov_z * 4
botRight[1] += prev_mov_z * 4
// Lower us when moving X, raise us when moving Y
for (let coords of HULL) {
let vertical_offset = -(abs(delayed_mov_x) * 2) + (((delayed_mov_y - default_mov_y)) * 9)
coords[1] += vertical_offset
// Periodic hovering, but only if offset isn't too harsh
coords[1] -= lerp(0, sin(hovering) * 4, inv_lerp(0, 4, abs(vertical_offset)))
// Shake our player when they go fast like sonic
coords[0] += sin(shaking * how_fast_to_shake) * how_much_to_shake
// Some rounding+noise
coords[0] = (round(coords[0] * (5/6))) * (6/5)
coords[1] = (round(coords[1] * (5/6))) * (6/5)
}
let ending_cutscene_scale = ending_cutscene_player_closer_gradual(ENDING_CUTSCENE)
if (ending_cutscene_scale > 0.01) {
// tip down, top up
HULL[0][1] -= ending_cutscene_scale * -4
HULL[3][1] -= ending_cutscene_scale * 2
ending_cutscene_scale **= 2
shrink_hull(HULL, (ending_cutscene_scale * 1.2) + 1, (ending_cutscene_scale * 1.2) + 1)
move_hull(HULL, 0, ending_cutscene_scale * -40)
}
// Cast a shadow
ctx.fillStyle = 'rgba(44,0,44,0.8)'
ctx.filter = `blur(${4}px)`
ctx.globalAlpha = gradual_shadow_alpha(+(player_z < 2))
ctx.beginPath()
ctx.moveTo(tip[0], lerp(tip[1], canvasHeight, 0.8) - 10)
ctx.lineTo(botLeft[0], lerp(botLeft[1], canvasHeight, 0.8) + 3)
ctx.lineTo(botRight[0], lerp(botRight[1], canvasHeight, 0.8) + 3)
ctx.fill()
ctx.filter = `none`
ctx.globalAlpha = 1
let colorOverride
let colorOverride2
if (player_iframes) {
if (sin(GAME_TIME_SECS * 30) < 0) return
colorOverride = 'yellow'
colorOverride2 = 'red'
if (sin(GAME_TIME_SECS * 30) > 0.8) {
colorOverride = 'red'
colorOverride2 = 'yellow'
}
}
ctx.filter = `brightness(${COLOR_player_brightness})`
move_hull(HULL, (canvasWidth / 20) * current_turn_gradual(CURRENT_TURN), abs(current_turn_gradual(CURRENT_TURN)) * -2)
draw_hull(HULL, colorOverride||'green', colorOverride||'gray', colorOverride2||'purple', colorOverride||'black', 1.5)
draw_hull_burninators(HULL, false, 1, burn_intensity)
ctx.filter = `none`
}
let current_turn_gradual = gradually_change(0, 0.01)
// Draws the burninators. Destroys hull data
let draw_hull_burninators = ([tip, botLeft, botRight, top], just_one, scale, burn_intensity) => {
// Go a third of the hull's height down
let bottom_y = (botLeft[1] + botRight[1]) / 2
top[1] += abs(top[1] - bottom_y) / 2
let burnNumber = f => {
for (let i = 0.1; i < 1; i+=0.1) if (f > i) f *= 1.1
return f * 0.15
}
// BURNINATOR
let drawBurninator = (is_main) => {
if (burn_intensity > 0.5 && is_main) {
ctx.fillStyle = 'yellow'
ctx.globalAlpha = burn_intensity > 0.5
? sin(burning * 1.5) > (random() - 0.5)
: (10 * burnNumber(burn_intensity + random())) ** 2
ctx.beginPath()
ctx.arc(...top, burnNumber(burn_intensity + random()) * 20 * scale, 0, TAU)
ctx.fill()
ctx.globalAlpha *= 0.6
ctx.filter = 'blur(15px)'
ctx.fillRect(top[0] - 20, top[1] - 10, 40 * scale, 20 + 20 * burnNumber(burn_intensity) * scale)
ctx.filter = 'none'
ctx.globalAlpha = 1
}
top[1] -= 1
ctx.fillStyle = 'orange'
ctx.globalAlpha = burn_intensity > 0.5
? sin(burning * 1.5) > (random() - 0.5)
: (10 * burn_intensity) ** 2
ctx.beginPath()
ctx.arc(...top, burnNumber(burn_intensity + random()) * 14 * scale, 0, TAU)
ctx.fill()
ctx.globalAlpha = 1
top[1] -= 1
ctx.fillStyle = prev_mov_y > default_mov_y ? 'red' : 'purple'
ctx.globalAlpha = burnNumber(burn_intensity + random()) > 0.5
? sin(burning * 1.5) > (random() - 0.5)
: (10 * burnNumber(burn_intensity + random())) ** 2
ctx.beginPath()
ctx.arc(...top, burnNumber(burn_intensity + random()) * 10 * scale, 0, TAU)
ctx.fill()
ctx.globalAlpha = 1
}
drawBurninator(true)
if (just_one) return
// SMALLER BOTTOM BURNERS
burn_intensity *= .1
top[1] += 10 * scale
let center = [...top]
// BOTTOM LEFT BURNER
top = lerp_vec(center, botLeft, .5)
drawBurninator(false)
// BOTTOM RIGHT BURNER
top = lerp_vec(center, botRight, .5)
drawBurninator(false)
}