-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamemap.js
133 lines (116 loc) · 3.51 KB
/
gamemap.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
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
class GameMap{
/**
* @param {GameManager} gameManager ゲームマネージャー
* @param {string} mapPath マップファイルのパス
*/
constructor(gameManager){
this.gameManager = gameManager;
this.add = this.add.bind(this);
this.requireFree = this.requireFree.bind(this);
this.objectFree = this.objectFree.bind(this);
this.process = this.process.bind(this);
this.draw = this.draw.bind(this);
this.MAP_WIDTH = 800;
this.MAP_HEIGHT = 600;
this.mapPath = "";
this.isGoal = false;
this.goalImage = new Image();
this.goalImage.src = "data/object/goal-text.png";
this.collision = new Collision(this.gameManager);
this.objectList = [];
this.freeList = [];
this.frame = 0;
}
/**
* マップにオブジェクトを追加する
* @param {CanvasObject} obj 追加するオブジェクト
*/
add(obj){
this.objectList.push(obj);
}
/**
* マップにオブジェクトの削除を要請する
* @param {CanvasObject} obj 削除するオブジェクト
*/
requireFree(obj){
if(!obj)return;
this.freeList.push(obj);
}
/**
* requireFreeで渡されたオブジェクトを削除する
*/
objectFree(){
this.objectList = this.objectList.filter((element)=>{
for(let freeObj of this.freeList){
if(freeObj == element){
return false;
}
}
return true;
});
this.freeList = [];
}
goal(){
this.isGoal = true;
}
/**
* マップに登録されているすべてのオブジェクトを1フレーム分動作させる
* @param {number} delta 前フレームから経過した時間(ミリ秒)
*/
process(delta){
this.frame++;
if(this.isGoal)return;
if(this.gameManager.input.has_down("r")){
this.gameManager.changeMap(this.mapPath);
}
this.collision.tick();
for(let obj of this.objectList){
obj.process(delta);
}
this.objectFree();
}
draw(context){
context.beginPath();
context.clearRect(0, 0, this.MAP_WIDTH, this.MAP_HEIGHT);
//描画順に並び替える
this.objectList.sort((a,b)=>{
if(a.layer < b.layer){
return -1;
}else if(a.layer > b.layer){
return 1;
}else{
return 0;
}
});
for(let obj of this.objectList){
obj.draw(context);
}
if(this.isGoal){
context.drawImage(this.goalImage, 200, 200);
}
context.stroke();
}
loadMap(data, path){
this.MAP_WIDTH = data.MAP_WIDTH;
this.MAP_HEIGHT = data.MAP_HEIGHT;
this.mapPath = path;
this.frame = data.frame;
this.objectList = [];
for(let obj of data.objectList){
let createFunc = objectManager.getCreateFunc(obj.objectName);
this.objectList.push(new createFunc(this.gameManager).loadObject(obj));
}
return this;
}
saveObjectList(){
let data = {};
data.MAP_WIDTH = this.MAP_WIDTH;
data.MAP_HEIGHT = this.MAP_HEIGHT;
data.frame = this.frame;
data.objectList = [];
for(let obj of this.objectList){
data.objectList.push(obj.saveObjectList());
}
return data;
}
}