-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.html
362 lines (305 loc) · 10.4 KB
/
demo.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>js13k gameplay test</title>
<style>
* {
box-sizing: border-box;
}
canvas {
/* border: 3px solid red; */
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: -5;
}
</style>
</head>
<body>
<canvas id="c">
</canvas>
<script>
'use strict';
/** @type {HTMLCanvasElement} */
const canv = document.getElementById('c');
const ctx = canv.getContext('2d');
let width, height;
let elapsed_time = 0;
let player_x = 0;
let cam_x = 0;
let player_y = 0;
let grav_direction = 1;
let player_y_velocity = 0;
let obstacle_generation_x = 600;
let forward_speed = 110;
let difficulty = 1; // [0, 1]
const PLAYER_RADIUS = 20;
const SIDE_BARS_WIDTH = 22;
const BAR_INSCRIPTION_PERIOD = 30;
const obstacles = [];
function lerp(start, end, x) {
return start + Math.max(0, Math.min(1, x)) * (end - start)
}
function recalculate_sizing() {
height = 768;
width = height * canv.clientWidth / canv.clientHeight;
canv.width = width;
canv.height = height;
}
window.addEventListener('resize', recalculate_sizing);
recalculate_sizing();
player_y = height / 2;
function line(x, y, dx, dy) {
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + dx, y + dy);
ctx.stroke();
}
function render() {
ctx.clearRect(0, 0, width, height);
obstacles.forEach(o => {
if(o.inactive) return;
ctx.save();
ctx.fillStyle = o.c;
ctx.fillRect(o.x - cam_x, o.y, o.w, o.h);
ctx.restore();
});
ctx.fillRect(player_x - PLAYER_RADIUS - cam_x, player_y - PLAYER_RADIUS, PLAYER_RADIUS * 2, PLAYER_RADIUS * 2);
function render_side_bars(is_top) {
const height_offset = is_top ? SIDE_BARS_WIDTH : height - 2 * SIDE_BARS_WIDTH;
line(0, height_offset, width, 0);
line(0, height_offset + SIDE_BARS_WIDTH, width, 0);
for(let x = -5; x <= width + BAR_INSCRIPTION_PERIOD; x += BAR_INSCRIPTION_PERIOD) {
line(x - ((cam_x) % BAR_INSCRIPTION_PERIOD), height_offset, (is_top ? 1 : -1) * SIDE_BARS_WIDTH, SIDE_BARS_WIDTH);
}
}
render_side_bars(true);
render_side_bars(false);
ctx.textBaseline = 'hanging';
ctx.textAlign = 'start';
ctx.font = '48px sans-serif';
ctx.fillText(`Score: ${score}`, 20, SIDE_BARS_WIDTH * 3);
}
function do_two_boxes_collide(b1, b2) {
return b1.x < b2.x + b2.w && b1.x + b1.w > b2.x && b1.y < b2.y + b2.h && b1.y + b1.h > b2.y;
}
function do_death() {
render();
setTimeout(() => {
alert('Oh well...');
window.location.reload();
}, 10);
throw Error('ya bad');
}
var score = 0;
function got_coin(obstacle) {
obstacle.inactive = true;
score += 1;
}
function invert_obstacle_y(o) {
o.y = height - o.h - o.y;
return o;
}
function update(dt) {
player_x += dt * forward_speed;
cam_x = player_x - 400;
player_y_velocity += dt * grav_direction * 150;
player_y += player_y_velocity * dt;
let latch_y = 2 * SIDE_BARS_WIDTH + PLAYER_RADIUS;
if(player_y < latch_y) {
player_y = latch_y;
player_y_velocity = 0;
}
if(player_y > height - latch_y) {
player_y = height - latch_y;
player_y_velocity = 0;
}
const player_box = {
x: player_x - PLAYER_RADIUS,
y: player_y - PLAYER_RADIUS,
w: PLAYER_RADIUS * 2,
h: PLAYER_RADIUS * 2
};
obstacles.forEach(o => {
if(!o.inactive && o.do_collision && do_two_boxes_collide(player_box, o)) {
o.do_collision(o);
}
});
while(obstacles.length) {
if(!obstacles[0].inactive && obstacles[0].x + obstacles[0].w - cam_x >= 0) {
break;
}
// debugger;
obstacles.shift();
}
while(obstacle_generation_x < cam_x + width + 100) {
const decision = Math.random();
const new_obstacles = [];
if(decision < 0.3) {
// TYPE 1: A bunch of still(?) meteorites which requires active dodging
// Floor is occasionally dead
// TODO: Do the floor, and carve out from the obstacles a path to ensure
// that it's actuall possible
obstacle_generation_x += 120;
const start_of_obstacle_x = obstacle_generation_x;
let target_x = obstacle_generation_x + lerp(8000, 1600, Math.random());
while(obstacle_generation_x < target_x) {
const half_height_range = lerp(
height / 8, height / 2,
(obstacle_generation_x - start_of_obstacle_x) / 1000
);
const new_y = lerp(2 * SIDE_BARS_WIDTH, half_height_range, Math.random());
const new_obstacle = {
x: obstacle_generation_x,
y: new_y,
w: 20,
h: 20,
c: '#F00',
do_collision: do_death
};
if(new_y > height / 3 && Math.random() > 0.7) {
new_obstacle.c = '#0F0';
new_obstacle.do_collision = got_coin;
}
new_obstacles.push(new_obstacle);
if(Math.random() > 0.5) {
invert_obstacle_y(new_obstacles[new_obstacles.length - 1]);
}
obstacle_generation_x += lerp(60, 80, Math.random());
}
obstacle_generation_x += 120;
} else if(decision < 0.6) {
// Random middle separator
obstacle_generation_x += 180;
new_obstacles.push({
x: obstacle_generation_x,
y: height / 2 - 200,
w: 80,
h: 400,
c: '#F00',
do_collision: do_death
});
obstacle_generation_x += 180;
} else if(decision < 0.9) {
// Type 2: Pillars of doom
// let current_intended_pos = 0; // -1 = top, 0 = middle, 1 = bottom
const start_of_obstacle_x = (obstacle_generation_x += 120);
let target_x = obstacle_generation_x + lerp(8000, 1600, Math.random());
while(obstacle_generation_x < target_x) {
const sub_decision = Math.random();
if(sub_decision < 0.2) {
// Middle
obstacle_generation_x += 150;
const new_obstacle = {
x: obstacle_generation_x,
y: SIDE_BARS_WIDTH * 2,
w: 80,
h: 200,
c: '#F00',
do_collision: do_death
};
new_obstacles.push(new_obstacle);
new_obstacles.push(invert_obstacle_y(Object.assign({}, new_obstacle)));
if(Math.random() > 0.8) {
new_obstacles.push({
x: obstacle_generation_x + 40 - 10,
y: height / 2 - 10,
w: 20,
h: 20,
c: '#0F0',
do_collision: got_coin
});
}
obstacle_generation_x += 250;
} else if(sub_decision < 0.6) {
// Poking out column
obstacle_generation_x += lerp(300, 200, difficulty);
let poking_out_column = {
x: obstacle_generation_x,
y: SIDE_BARS_WIDTH * 2,
w: 80,
h: 400,
c: '#F00',
do_collision: do_death
};
let grazer, coin;
if(Math.random() > 0.5) {
// Coin plus a grazer
grazer = {
x: obstacle_generation_x,
y: SIDE_BARS_WIDTH * 2,
w: 80,
h: 20,
c: '#F00',
do_collision: do_death
};
invert_obstacle_y(grazer);
coin = {
x: obstacle_generation_x + 40 - 10,
y: SIDE_BARS_WIDTH * 2 + 400 + (height - SIDE_BARS_WIDTH * 4 - 400 - 20) / 2,
w: 20,
h: 20,
c: '#0F0',
do_collision: got_coin
};
// invert_obstacle_y(coin);
}
if(Math.random() > 0.5) {
invert_obstacle_y(poking_out_column);
if(grazer) invert_obstacle_y(grazer);
if(coin) invert_obstacle_y(coin);
}
new_obstacles.push(poking_out_column);
if(grazer) new_obstacles.push(grazer);
if(coin) new_obstacles.push(coin);
obstacle_generation_x += lerp(lerp(400, 500, Math.random()), 275, difficulty);;
}
}
} else {
// do nothing for a bit
obstacle_generation_x += 100;
}
new_obstacles.sort((a, b) => a.x - b.x);
if(Math.random() > 0.5) {
new_obstacles.forEach(o => invert_obstacle_y(o));
}
obstacles.push(...new_obstacles);
}
}
let last_frame_time = new Date();
function frame() {
let this_frame_time = new Date();
let dt = Math.min((this_frame_time - last_frame_time) / 1000, 1 / 15);
elapsed_time += dt;
dt *= 1;
const SUBSTEPS = 10;
for(let substep = 0; substep < SUBSTEPS; ++substep) {
update(dt / SUBSTEPS);
}
render();
requestAnimationFrame(frame);
}
frame();
function do_grav_switch() {
grav_direction *= -1;
player_y_velocity /= 3;
}
window.addEventListener('keydown', e => {
console.log(e);
if(e.code == 'Space') {
do_grav_switch();
}
});
window.addEventListener('touchstart', e => {
console.log(e);
do_grav_switch();
});
</script>
</body>
</html>