-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameMap.js
187 lines (147 loc) · 4.65 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const REACH_LIMIT = 1000;
class GameMap extends EventListener {
constructor(options = {}) {
super();
const {
dimensions,
objects
} = options;
this.dimensions = dimensions;
this.objects = objects;
this.spawn = null;
}
getLivingEntities() {
return this.objects.filter(object => object instanceof LivingEntity);
}
getMonsters() {
return this.objects.filter(object => object instanceof Monster);
}
getArrows() {
return this.objects.filter(object => object instanceof Arrow);
}
getWalls() {
return this.objects.filter(object => object instanceof Wall);
}
getExits() {
return this.objects.filter(object => object instanceof Exit);
}
getPlayer() {
return this.objects.find(object => object instanceof Player);
}
getObject(position) {
if(!(position instanceof Vector)) throw new Error("Position must be a Vector");
return this.objects.find(object => object.position.isEqual(position));
}
addObject(object) {
if(!(object instanceof MapObject)) throw new Error("Cannot move non-map object");
this.dispatchEvent("objectadd", {object}, event => {
this.objects.push(object);
this.dispatchEvent("update");
});
}
moveObjectTo(object, position) {
if(!(object instanceof MapObject)) throw new Error("Cannot move non-map object");
if(!(position instanceof Vector)) throw new Error("Position must be a Vector");
const targetObject = this.getObject(position);
const move = () => {
this.dispatchEvent("move", {
from: object,
to: targetObject
}, event => {
object.position = position;
this.dispatchEvent("update");
});
};
if(targetObject) {
this.dispatchEvent("collision", {
collider1: object,
collider2: targetObject
}, event => {
move();
});
} else {
move();
}
}
moveObjectBy(object, direction) {
if(!(object instanceof MapObject)) throw new Error("Cannot move non-map object");
if(!(direction instanceof Vector)) throw new Error("Position must be a Vector");
const newPosition = object.position.copy().add(direction);
this.moveObjectTo(object, newPosition);
}
destroyObject(object) {
if(!(object instanceof MapObject)) throw new Error("Cannot move non-map object");
this.dispatchEvent("destroy", {object}, event => {
this.objects.splice(this.objects.indexOf(object), 1);
this.dispatchEvent("update");
});
}
find(position, callback) {
if(!(position instanceof Vector)) throw new Error("Position must be a Vector");
if(typeof callback !== "function") throw new Error("Callback must be a function");
const location = {
position: position,
path: [],
found: false
};
const visited = [];
const queue = [location];
//Queue based Flood-fill algorithm
while(queue.length > 0) {
const currentLocation = queue.shift();
const object = this.getObject(currentLocation.position);
if(object instanceof Wall) continue;
const called = object && callback(object);
if(!object || !called || called == 0) {
for(const direction of DIRECTIONS) {
const newPosition = currentLocation.position.copy().add(direction);
const newPath = currentLocation.path.slice();
const newLocation = {
position: newPosition,
path: newPath,
found: false
};
newPath.push(newPosition);
if(!visited.some(e => e.isEqual(newPosition))) {
queue.push(newLocation);
visited.push(newLocation.position);
}
}
} else if(called == 1) {
currentLocation.found = true;
return currentLocation;
}
}
return location;
}
search(position, type) {
if(!(position instanceof Vector)) throw new Error("Position must be a Vector");
if(!Utils.subclassOf(type, MapObject)) throw new Error("Type must be a MapObject");
return this.find(position, object => object instanceof type);
}
getReachable(position, type) {
if(!(position instanceof Vector)) throw new Error("Position must be a Vector");
if(!Utils.subclassOf(type, MapObject)) throw new Error("Type must be a MapObject");
const found = [];
let len = 0;
let i = 0;
do {
this.find(position, object => {
const isType = object instanceof type;
if(isType && !found.includes(object)) found.push(object);
if(isType) return -1;
return 0;
});
} while((len != found.length) && (len = found.length) && (i++ < REACH_LIMIT));
return found;
}
clone() {
const objects = this.objects.map(object => object.clone());
const map = new GameMap({
dimensions: this.dimensions.copy(),
objects: objects
});
map.spawn = this.spawn;
return map;
}
}