-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverworld.js
51 lines (42 loc) · 1.54 KB
/
Overworld.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
class Overworld {
constructor(config) {
this.element = config.element;
this.canvas = this.element.querySelector(".game-canvas");
this.ctx = this.canvas.getContext("2d");
this.map = null;
}
startGameLoop() {
const step = () => {
//clear canvas
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
//establish camera view
const cameraPerson = this.map.gameObjects.hero;
//update all objects
Object.values(this.map.gameObjects).forEach(object => {
object.update({
arrow: this.directionInput.direction,
map: this.map,
});
})
//draw lower layer
this.map.drawLowerImage(this.ctx, cameraPerson);
//draw game objects
Object.values(this.map.gameObjects).forEach(object => {
object.sprite.draw(this.ctx, cameraPerson);
})
//draw upper layer
this.map.drawUpperImage(this.ctx, cameraPerson)
requestAnimationFrame(() => {
step();
})
}
step();
}
init() {
//can start on new map by changing 'DemoRoom' property to new map locaiton
this.map = new OverworldMap(window.OverworldMaps.DemoRoom)
this.directionInput = new DirectionInput();
this.directionInput.init();
this.startGameLoop();
}
}