-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamemanager.js
48 lines (46 loc) · 1.46 KB
/
gamemanager.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
/**
* ゲームをまとめる
*/
class GameManager{
constructor(){
this.start = this.start.bind(this);
this.changeMap = this.changeMap.bind(this);
this.gameLoop = this.gameLoop.bind(this);
this.mapEditMode = false;
this.prev_time = 0;
}
start(){
this.changeMap("/data/map/map.json");
}
changeMap(mapPath){
this.mapEditMode = false;
this.input = new Input();
this.map = new GameMap(this);
$.getJSON(mapPath)
.done((json)=>{
this.map.loadMap(json, mapPath);
requestAnimationFrame(this.gameLoop);
});
}
/**
* この関数を呼び出してゲームを始める
* この関数を呼び出しても制御は戻る
*/
gameLoop(){
if(!this.map)return false;
//キャラがぶっ飛んでいかないように前フレームから1秒以上立っていた場合prev_timeを現在時間にする
if(performance.now() - this.prev_time > 1000){
this.prev_time = performance.now();
}
let delta = performance.now() - this.prev_time;
this.prev_time = performance.now();
this.map.process(delta);
this.map.draw(document.getElementById('canvas').getContext('2d'));
requestAnimationFrame(this.gameLoop);
}
startMapEdit(){
this.mapEditMode = true;
this.input = new Input();
this.map = new MapEdit(this);
}
}