-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
67 lines (50 loc) · 1.36 KB
/
game.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
import Renderer from "./renderer.js";
const space_bar = 32;
const right_arrow = 39;
class Game {
constructor(canvas) {
this.renderer = new Renderer(canvas);
this.canvas = canvas;
this.renderer.init();
this.last_frame = Date.now();
this.current_frame = this.last_frame;
// TODO: Frame delay currently ignored
// this.frame_delay_s = 1.0 / 30.0;
this.resolution_factor = 2;
requestAnimationFrame(this.frame);
}
frame = () => {
// Frame timing
this.current_frame = Date.now();
let frame_delta = this.current_frame - this.last_frame;
// TODO
// Input handling
// Logic update
// Render
this.renderer.render(frame_delta);
this.last_frame = this.current_frame;
requestAnimationFrame(this.frame);
}
onKeyDown = (ev) => {
if(ev.keyCode === space_bar) {
};
if(ev.keyCode === right_arrow) {
};
}
onKeyUp = (ev) => {
}
onWindowSize = () => {
if( !this.canvas ) {
return;
}
const w = window.innerWidth;
const h = window.innerHeight;
// TODO: For now I've hacked in a resolution reduction here,
// most development is being done on an intel gpu :/
this.canvas.style.width = w;
this.canvas.style.height = h;
this.canvas.width = w / this.resolution_factor;
this.canvas.height = h / this.resolution_factor;
}
}
export default Game;