-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientGame.js
More file actions
78 lines (67 loc) · 1.69 KB
/
ClientGame.js
File metadata and controls
78 lines (67 loc) · 1.69 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
import ClientEngine from './ClientEngine';
import ClientWorld from './ClientWorld';
import sprites from '../configs/sprites';
import levelCfg from '../configs/world.json';
import gameObjects from '../configs/gameObjects.json';
class ClientGame {
constructor(cfg) {
Object.assign(this, {
cfg,
gameObjects,
player: null,
});
this.engine = this.createEngine();
this.initEngine();
this.world = this.createWorld();
}
setPlayer(player) {
this.player = player;
}
createEngine() {
return new ClientEngine(document.getElementById(this.cfg.tagId));
}
createWorld() {
return new ClientWorld(this, this.engine, levelCfg);
}
initEngine() {
this.engine.loadSprites(sprites).then(() => {
this.world.init();
this.engine.on('render', (_, time) => {
this.world.render(time);
});
this.engine.start();
this.initKeys();
});
}
initKeys() {
const canMovePlayer = (cell) => cell.findObjectsByType('grass').length;
this.engine.input.onKey({
ArrowLeft: (keydown) => {
if (keydown) {
this.player.moveByCellCoord(-1, 0, canMovePlayer);
}
},
ArrowRight: (keydown) => {
if (keydown) {
this.player.moveByCellCoord(1, 0, canMovePlayer);
}
},
ArrowUp: (keydown) => {
if (keydown) {
this.player.moveByCellCoord(0, -1, canMovePlayer);
}
},
ArrowDown: (keydown) => {
if (keydown) {
this.player.moveByCellCoord(0, 1, canMovePlayer);
}
},
});
}
static init(cfg) {
if (!ClientGame.game) {
ClientGame.game = new ClientGame(cfg);
}
}
}
export default ClientGame;