-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
383 lines (353 loc) · 10.5 KB
/
run.js
File metadata and controls
383 lines (353 loc) · 10.5 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
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
// Run
//TODO
//fillstyle fix
//restart on mistake
//
// initialize canvas and keyboard
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
// var player;
// Handle keyboard controls
var keysDown = {};
// Check for keys pressed where key represents the keycode captured
addEventListener("keydown", function (key) {
keysDown[key.keyCode] = true;
}, false);
addEventListener("keyup", function (key) {
delete keysDown[key.keyCode];
}, false);
const WORLD_SHIFT = 0.5
const FPS = 15
// const FPS = 25
var stop = false
var frameCount = 0
// var $results = $("#results");
var fpsInterval, startTime, now, then, elapsed
// create player
const player = {
width: 20,
height: 20,
color: "black",
x: 0,
y: 0,
dx: 0,
dy: 0,
speedX: 0.5,
speedY: -15,
onGround: false,
onDanger: false,
onBorder: false,
on_ground(ground) {
const y_next = this.y + this.dy
next_bottom = y_next + this.height
bottom = this.y + this.height
left = this.x
right = this.x + this.width
if ((next_bottom >= ground.y)
&& (bottom <= ground.y)
&& (right >= ground.x)
&& (left <= ground.x + ground.width)) {
this.y = ground.y - this.height
this.dy = 0
this.onGround = true
} else {
this.onGround = false;
}
return this.onGround
},
on_wall(wall) {
const x_next = this.x + this.dx
const y_next = this.y + this.dy
const next_top = y_next
const next_bottom = y_next + this.height
const next_left = x_next
const next_right = x_next + this.width
const top = this.y
const bottom = this.y + this.height
const left = this.x
const right = this.x + this.width
const wall_top = wall.y
const wall_bottom = wall.y + wall.height
const wall_left = wall.x
const wall_right = wall.x + wall.width
if ((next_right >= wall_left)
&& (right <= wall_left)
&& ((bottom >= wall_top) || next_bottom >= wall_top)
&& ((top <= wall_bottom) || next_top <= wall_bottom)) {
this.x = wall_left - this.width
this.dx = 0
} else
if ((top >= wall_bottom)
&& (next_top <= wall_bottom)
&& (right >= wall_left)
&& (left <= wall_right)) {
this.y = wall_bottom
this.dy = 0
} else
if ((next_left <= wall_right)
&& (left >= wall_right)
&& ((bottom >= wall_top) || next_bottom >= wall_top)
&& ((top <= wall_bottom) || next_top <= wall_bottom)) {
this.x = wall_right
this.dx = 0
} else
if ((bottom <= wall_top)
&& (next_bottom >= wall_top)
&& (right >= wall_left)
&& (left <= wall_right)) {
this.y = wall_top - this.height
this.dy = 0
this.onGround = true
}
},
on_danger(danger) {
if ((this.x + this.width >= danger.x) && (this.x <= danger.x + danger.width)
&& (this.y + this.height >= danger.y) && (this.y <= danger.y + danger.height)) {
this.onDanger = true
} else {
this.onDanger = false
}
return this.onDanger
},
on_border() {
// canvas.width
if (this.y + this.height > 400
| this.x < 0 | this.x + this.width > 600) {
this.onBorder = true
} else {
this.onBorder = false
}
return this.onBorder
},
update(grounds, dangers, walls) {
let do_restart = false
if (37 in keysDown) { // Player is holding left key
this.dx -= this.speedX;
}
if (39 in keysDown) { // Player is holding right key
this.dx += this.speedX;
}
if (32 in keysDown && this.onGround) { // Player is holding spacebar
this.dy = this.speedY
}
// apply gravity drag and move player
this.dy += world.gravity
this.dy *= world.drag
this.dx *= this.onGround ? world.groundDrag : world.drag
// check if on ground
this.onGround = grounds.some(ground => this.on_ground(ground))
// check if on wall
walls.forEach(wall => this.on_wall(wall))
// console.log(this.dx)
this.x += this.dx
this.y += this.dy
// check if on danger space
this.onDanger = dangers.some(danger => this.on_danger(danger))
this.onBorder = this.on_border()
if (this.onDanger == true || this.onBorder == true) {
let do_restart = true
return do_restart
}
return do_restart
},
draw() {
drawRect(this.x, this.y, this.width, this.height, this.color)
},
start() {
// this.x = ctx.canvas.width / 2 - this.width / 2;
this.x = 100
this.y = 250 - this.height;
// this.y = 150
this.onGround = true;
this.dx = 0;
this.dy = 0;
}
}
// world rules
const world = {
gravity: 0.99,
drag: 0.9,
groundDrag: 0.85,
// ground: 250
}
// rectangle entity
class Block {
constructor(x, y, width, height, color) {
this.x = x
this.y = y
this.width = width
this.height = height
this.color = color
}
generate() {
drawRect(this.x, this.y, this.width, this.height, this.color)
}
}
// walkable ground
class Ground {
constructor(x, y, width, height, color) {
this.x = x
this.y = y
this.width = width
this.height = height
this.color = color
}
generate() {
drawRect(this.x, this.y, this.width, this.height, this.color)
}
// getAll() {
// return this.allInstances
// }
}
// dangerous areas
class Danger {
constructor(x, y, width, height, color) {
this.x = x
this.y = y
this.width = width
this.height = height
this.color = color
}
generate() {
drawRect(this.x, this.y, this.width, this.height, this.color)
}
}
// draw the board
function drawBoard(grounds, dangers, walls) {
ctx.font = "24px Times New Roman";
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.fillText("Run", canvas.width / 2, 20);
// drawRect(1, 250, 600, 2, "blue")
grounds.forEach(ground => ground.generate())
dangers.forEach(danger => danger.generate())
walls.forEach(wall => wall.generate())
// ground1.generate()
// ground2.generate()
// ground3.generate()
// ground4.generate()
// ground5.generate()
// ground6.generate()
// bad1.generate("b1a")
// drawRect(0,0,10,10,"blue")
}
// draw rectangle with parameters
function drawRect(x, y, width, height, color) {
ctx.beginPath()
ctx.rect(x, y, width, height)
ctx.fillStyle = color
// ctx.fillRect(x, y, width, height);
ctx.fill()
ctx.closePath()
}
// Erase old state after update
function reset() {
ctx.clearRect(0,0,canvas.width,canvas.height);
}
// restart
function restart() {
const g1 = new Block(100, 250, 300, 2, "blue")
const g2 = new Block(300, 220, 40, 2, "blue")
const g3 = new Block(250, 180, 20, 2, "blue")
const g4 = new Block(300, 140, 40, 2, "blue")
const g5 = new Block(360, 100, 20, 2, "blue")
const g6 = new Block(520, 180, 60, 2, "blue")
const g7 = new Block(520, 350, 20, 2, "blue")
const g8 = new Block(620, 370, 40, 2, "blue")
const g9 = new Block(700, 330, 10, 2, "blue")
const g10 = new Block(640, 290, 10, 2, "blue")
const g11 = new Block(700, 250, 10, 2, "blue")
const g12 = new Block(640, 210, 10, 2, "blue")
const g13 = new Block(700, 170, 10, 2, "blue")
const g14 = new Block(640, 130, 10, 2, "blue")
const d1 = new Block(450, 250, 60, 150, "red")
const w1 = new Block(610, 120, 2, 170, "black")
// const wall2 = new Block(200, 100, 50, 120, "black")
const grounds = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12,
g13, g14]
const dangers = [d1]
const walls = [w1]
player.start()
console.log(grounds)
console.log(dangers)
console.log(walls)
main(grounds, dangers, walls)
return grounds, dangers, walls
}
// shift all items -1 x
function viewShift(player, grounds, dangers, walls) {
function shift(entity) {
entity.x -= WORLD_SHIFT
}
player.x -= WORLD_SHIFT
grounds.map(ground => shift(ground))
dangers.map(danger => shift(danger))
walls.map(wall => shift(wall))
}
function startAnimating(fps, grounds, dangers, walls) {
fpsInterval = 1000 / fps;
then = Date.now();
startTime = then;
animate(grounds, dangers, walls);
}
function animate(grounds, dangers, walls) {
// requestAnimationFrame(animate(grounds, dangers, walls))
requestAnimationFrame(() => animate(grounds, dangers, walls))
now = Date.now()
elapsed = (now - then)
console.log("now", now)
console.log("then", then)
console.log(now - then)
console.log("elapsed", elapsed)
console.log("interval", fpsInterval)
if (elapsed > fpsInterval) {
console.log("IF")
then = now = (elapsed % fpsInterval)
// requestAnimationFrame(() => main(grounds, dangers, walls))
player.draw()
drawBoard(grounds, dangers, walls)
}
// reset()
main(grounds, dangers, walls)
}
function animation(grounds, dangers, walls) {
player.draw()
drawBoard(grounds, dangers, walls)
}
function main(grounds, dangers, walls) {
reset()
viewShift(player, grounds, dangers, walls)
const do_restart = player.update(grounds, dangers, walls)
if (do_restart) {
return restart()
}
player.draw()
drawBoard(grounds, dangers, walls)
requestAnimationFrame(() => main(grounds, dangers, walls))
// startAnimating(15, grounds, dangers, walls)
// animate(grounds, dangers, walls)
}
restart()
// startAnimating(15, grounds, dangers, walls)
// var w = window;
// requestAnimationFrame = w.requestAnimationFrame ||
// w.webkitRequestAnimationFrame || w.msRequestAnimationFrame ||
// w.mozRequestAnimationFrame;
// player.start()
// main()
// window.focus()
// // gravity
// function gravity(entity){
// gravityValue = 0.05;
// gravitySpeed = 0;
// x = entity.x
// y = entity.y
// // function update(){ }
// function newPos(){
// gravitySpeed += gravityValue;
// x = entity.x + entity.speedX
// y = entity.y + entity.speedY + gravitySpeed
// }
// return x, y
// }