forked from mozilla/BrowserQuest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessmap.js
More file actions
303 lines (276 loc) · 9.96 KB
/
processmap.js
File metadata and controls
303 lines (276 loc) · 9.96 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
var Log = require('log'),
_ = require('underscore'),
log = new Log(Log.DEBUG),
Types = require("../../shared/js/gametypes");
var map,
mode,
collidingTiles = {},
staticEntities = {},
mobsFirstgid;
module.exports = function processMap(json, options) {
var self = this,
Tiled = json.map,
layerIndex = 0,
tileIndex = 0,
tilesetFilepath = "";
map = {
width: 0,
height: 0,
collisions: [],
doors: [],
checkpoints: []
};
mode = options.mode;
if(mode === "client") {
map.data = [];
map.high = [];
map.animated = {};
map.blocking = [];
map.plateau = [];
map.musicAreas = [];
}
if(mode === "server") {
map.roamingAreas = [];
map.chestAreas = [];
map.staticChests = [];
map.staticEntities = {};
}
log.info("Processing map info...");
map.width = Tiled.width;
map.height = Tiled.height;
map.tilesize = Tiled.tilewidth;
// Tile properties (collision, z-index, animation length...)
var tileProperties;
var handleProp = function(property, id) {
if(property.name === "c") {
collidingTiles[id] = true;
}
if(mode === "client") {
if(property.name === "v") {
map.high.push(id);
}
if(property.name === "length") {
if(!map.animated[id]) {
map.animated[id] = {};
}
map.animated[id].l = property.value;
}
if(property.name === "delay") {
if(!map.animated[id]) {
map.animated[id] = {};
}
map.animated[id].d = property.value;
}
}
}
if(Tiled.tileset instanceof Array) {
_.each(Tiled.tileset, function(tileset) {
if(tileset.name === "tilesheet") {
log.info("Processing terrain properties...");
tileProperties = tileset.tile;
for(var i=0; i < tileProperties.length; i += 1) {
var property = tileProperties[i].properties.property;
var tilePropertyId = tileProperties[i].id + 1;
if(property instanceof Array) {
for(var pi=0; pi < property.length; pi += 1) {
handleProp(property[pi], tilePropertyId);
}
} else {
handleProp(property, tilePropertyId);
}
}
}
else if(tileset.name === "Mobs" && mode === "server") {
log.info("Processing static entity properties...");
mobsFirstgid = tileset.firstgid;
_.each(tileset.tile, function(p) {
var property = p.properties.property,
id = p.id + 1;
if(property.name === "type") {
staticEntities[id] = property.value;
}
});
}
});
} else {
log.error("A tileset is missing");
}
for(var i=0; i < Tiled.objectgroup.length; i += 1) {
var group = Tiled.objectgroup[i];
if(group.name === 'doors') {
var doors = group.object;
log.info("Processing doors...");
for(var j=0; j < doors.length; j += 1) {
map.doors[j] = {
x: doors[j].x / map.tilesize,
y: doors[j].y / map.tilesize,
p: (doors[j].type === 'portal') ? 1 : 0,
}
var doorprops = doors[j].properties.property;
for(var k=0; k < doorprops.length; k += 1) {
map.doors[j]['t'+doorprops[k].name] = doorprops[k].value;
}
}
}
}
// Object layers
_.each(Tiled.objectgroup, function(objectlayer) {
if(objectlayer.name === "roaming" && mode === "server") {
log.info("Processing roaming areas...");
var areas = objectlayer.object;
for(var i=0; i < areas.length; i += 1) {
if(areas[i].properties) {
var nb = areas[i].properties.property.value;
}
map.roamingAreas[i] = { id: i,
x: areas[i].x / 16,
y: areas[i].y / 16,
width: areas[i].width / 16,
height: areas[i].height / 16,
type: areas[i].type,
nb: nb
};
}
}
else if(objectlayer.name === "chestareas" && mode === "server") {
log.info("Processing chest areas...");
_.each(objectlayer.object, function(area) {
var chestArea = {
x: area.x / map.tilesize,
y: area.y / map.tilesize,
w: area.width / map.tilesize,
h: area.height / map.tilesize
};
_.each(area.properties.property, function(prop) {
if(prop.name === 'items') {
chestArea['i'] = _.map(prop.value.split(','), function(name) {
return Types.getKindFromString(name);
});
} else {
chestArea['t'+prop.name] = prop.value;
}
});
map.chestAreas.push(chestArea);
});
}
else if(objectlayer.name === "chests" && mode === "server") {
log.info("Processing static chests...");
_.each(objectlayer.object, function(chest) {
var items = chest.properties.property.value;
var newChest = {
x: chest.x / map.tilesize,
y: chest.y / map.tilesize,
i: _.map(items.split(','), function(name) {
return Types.getKindFromString(name);
})
};
map.staticChests.push(newChest);
});
}
else if(objectlayer.name === "music" && mode === "client") {
log.info("Processing music areas...");
_.each(objectlayer.object, function(music) {
var musicArea = {
x: music.x / map.tilesize,
y: music.y / map.tilesize,
w: music.width / map.tilesize,
h: music.height / map.tilesize,
id: music.properties.property.value
};
map.musicAreas.push(musicArea);
});
}
else if(objectlayer.name === "checkpoints") {
log.info("Processing check points...");
var count = 0;
_.each(objectlayer.object, function(checkpoint) {
var cp = {
id: ++count,
x: checkpoint.x / map.tilesize,
y: checkpoint.y / map.tilesize,
w: checkpoint.width / map.tilesize,
h: checkpoint.height / map.tilesize
};
if(mode === "server") {
cp.s = checkpoint.type ? 1 : 0;
}
map.checkpoints.push(cp);
});
}
});
// Layers
if(Tiled.layer instanceof Array) {
for(var i=Tiled.layer.length - 1; i > 0; i -= 1) {
processLayer(Tiled.layer[i]);
}
} else {
processLayer(Tiled.layer);
}
if(mode === "client") {
// Set all undefined tiles to 0
for(var i=0, max=map.data.length; i < max; i+=1) {
if(!map.data[i]) {
map.data[i] = 0;
}
}
}
return map;
};
var processLayer = function processLayer(layer) {
if(mode === "server") {
// Mobs
if(layer.name === "entities") {
log.info("Processing positions of static entities ...");
var tiles = layer.data.tile;
for(var j=0; j < tiles.length; j += 1) {
var gid = tiles[j].gid - mobsFirstgid + 1;
if(gid && gid > 0) {
map.staticEntities[j] = staticEntities[gid];
}
}
}
}
var tiles = layer.data.tile;
if(mode === "client" && layer.name === "blocking") {
log.info("Processing blocking tiles...");
for(var i=0; i < tiles.length; i += 1) {
var gid = tiles[i].gid;
if(gid && gid > 0) {
map.blocking.push(i);
}
}
}
else if(mode === "client" && layer.name === "plateau") {
log.info("Processing plateau tiles...");
for(var i=0; i < tiles.length; i += 1) {
var gid = tiles[i].gid;
if(gid && gid > 0) {
map.plateau.push(i);
}
}
}
else if(layer.visible !== 0 && layer.name !== "entities") {
log.info("Processing layer: "+ layer.name);
for(var j=0; j < tiles.length; j += 1) {
var gid = tiles[j].gid;
if(mode === "client") {
// Set tile gid in the tilesheet
if(gid > 0) {
if(map.data[j] === undefined) {
map.data[j] = gid;
}
else if(map.data[j] instanceof Array) {
map.data[j].unshift(gid);
}
else {
map.data[j] = [gid, map.data[j]];
}
}
}
// Colliding tiles
if(gid in collidingTiles) {
map.collisions.push(j);
}
}
}
}