Skip to content

Commit ca6985e

Browse files
committed
Removed lots of redundant code and tidied up bad formatting.
1 parent 7645e77 commit ca6985e

1 file changed

Lines changed: 75 additions & 82 deletions

File tree

src/tilemap/TilemapParser.js

Lines changed: 75 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -140,58 +140,41 @@ Phaser.TilemapParser = {
140140
*/
141141
getEmptyData: function (tileWidth, tileHeight, width, height) {
142142

143-
var map = {};
144-
145-
map.width = 0;
146-
map.height = 0;
147-
map.tileWidth = 0;
148-
map.tileHeight = 0;
149-
150-
if (typeof tileWidth !== 'undefined' && tileWidth !== null) { map.tileWidth = tileWidth; }
151-
if (typeof tileHeight !== 'undefined' && tileHeight !== null) { map.tileHeight = tileHeight; }
152-
if (typeof width !== 'undefined' && width !== null) { map.width = width; }
153-
if (typeof height !== 'undefined' && height !== null) { map.height = height; }
154-
155-
map.orientation = 'orthogonal';
156-
map.version = '1';
157-
map.properties = {};
158-
map.widthInPixels = 0;
159-
map.heightInPixels = 0;
160-
161-
var layers = [];
162-
163-
var layer = {
164-
165-
name: 'layer',
166-
x: 0,
167-
y: 0,
168-
width: 0,
169-
height: 0,
143+
return {
144+
width: (width !== undefined && width !== null) ? width : 0,
145+
height: (height !== undefined && height !== null) ? height : 0,
146+
tileWidth: (tileWidth !== undefined && tileWidth !== null) ? tileWidth : 0,
147+
tileHeight: (tileHeight !== undefined && tileHeight !== null) ? tileHeight : 0,
148+
orientation: 'orthogonal',
149+
version: '1',
150+
properties: {},
170151
widthInPixels: 0,
171152
heightInPixels: 0,
172-
alpha: 1,
173-
visible: true,
174-
properties: {},
175-
indexes: [],
176-
callbacks: [],
177-
bodies: [],
178-
data: []
179-
153+
layers: [
154+
{
155+
name: 'layer',
156+
x: 0,
157+
y: 0,
158+
width: 0,
159+
height: 0,
160+
widthInPixels: 0,
161+
heightInPixels: 0,
162+
alpha: 1,
163+
visible: true,
164+
properties: {},
165+
indexes: [],
166+
callbacks: [],
167+
bodies: [],
168+
data: []
169+
}
170+
],
171+
images: [],
172+
objects: {},
173+
collision: {},
174+
tilesets: [],
175+
tiles: []
180176
};
181177

182-
// fill with nulls?
183-
184-
layers.push(layer);
185-
186-
map.layers = layers;
187-
map.images = [];
188-
map.objects = {};
189-
map.collision = {};
190-
map.tilesets = [];
191-
map.tiles = [];
192-
193-
return map;
194-
195178
},
196179

197180
/**
@@ -209,18 +192,18 @@ Phaser.TilemapParser = {
209192
}
210193

211194
// Map data will consist of: layers, objects, images, tilesets, sizes
212-
var map = {};
213-
214-
map.width = json.width;
215-
map.height = json.height;
216-
map.tileWidth = json.tilewidth;
217-
map.tileHeight = json.tileheight;
218-
map.orientation = json.orientation;
219-
map.format = Phaser.Tilemap.TILED_JSON;
220-
map.version = json.version;
221-
map.properties = json.properties;
222-
map.widthInPixels = map.width * map.tileWidth;
223-
map.heightInPixels = map.height * map.tileHeight;
195+
var map = {
196+
width: json.width,
197+
height: json.height,
198+
tileWidth: json.tilewidth,
199+
tileHeight: json.tileheight,
200+
orientation: json.orientation,
201+
format: Phaser.Tilemap.TILED_JSON,
202+
version: json.version,
203+
properties: json.properties,
204+
widthInPixels: json.width * json.tileWidth,
205+
heightInPixels: json.height * json.tileHeight
206+
};
224207

225208
// Tile Layers
226209
var layers = [];
@@ -236,27 +219,35 @@ Phaser.TilemapParser = {
236219

237220
// Base64 decode data if necessary
238221
// NOTE: uncompressed base64 only.
239-
if (!curl.compression && curl.encoding && curl.encoding === "base64") {
240-
var binaryString = window.atob(curl.data);
222+
223+
if (!curl.compression && curl.encoding && curl.encoding === 'base64')
224+
{
225+
var binaryString = window.atob(curl.data);
241226
var len = binaryString.length;
242-
var bytes = new Array( len );
227+
var bytes = new Array(len);
228+
243229
// Interpret binaryString as an array of bytes representing
244230
// little-endian encoded uint32 values.
245-
for (var j = 0; j < len; j+=4) {
246-
bytes[j/4] = (binaryString.charCodeAt(j) |
247-
binaryString.charCodeAt(j+1) << 8 |
248-
binaryString.charCodeAt(j+2) << 16 |
249-
binaryString.charCodeAt(j+3) << 24) >>> 0;
231+
for (var j = 0; j < len; j+=4)
232+
{
233+
bytes[j / 4] = (
234+
binaryString.charCodeAt(j) |
235+
binaryString.charCodeAt(j + 1) << 8 |
236+
binaryString.charCodeAt(j + 2) << 16 |
237+
binaryString.charCodeAt(j + 3) << 24
238+
) >>> 0;
250239
}
240+
251241
curl.data = bytes;
242+
252243
delete curl.encoding;
253244
}
254-
else if(curl.compression){
245+
else if (curl.compression)
246+
{
255247
console.warn('TilemapParser.parseTiledJSON - Layer compression is unsupported, skipping layer \'' + curl.name + '\'');
256248
continue;
257249
}
258250

259-
260251
var layer = {
261252

262253
name: curl.name,
@@ -326,28 +317,34 @@ Phaser.TilemapParser = {
326317
switch (flippedVal)
327318
{
328319
case 5:
329-
rotation = Math.PI/2;
320+
rotation = Math.PI / 2;
330321
break;
322+
331323
case 6:
332324
rotation = Math.PI;
333325
break;
326+
334327
case 3:
335-
rotation = 3*Math.PI/2;
328+
rotation = 3 * Math.PI / 2;
336329
break;
330+
337331
case 4:
338332
rotation = 0;
339333
flipped = true;
340334
break;
335+
341336
case 7:
342-
rotation = Math.PI/2;
337+
rotation = Math.PI / 2;
343338
flipped = true;
344339
break;
340+
345341
case 2:
346342
rotation = Math.PI;
347343
flipped = true;
348344
break;
345+
349346
case 1:
350-
rotation = 3*Math.PI/2;
347+
rotation = 3 * Math.PI / 2;
351348
flipped = true;
352349
break;
353350
}
@@ -385,7 +382,6 @@ Phaser.TilemapParser = {
385382
layer.data = output;
386383

387384
layers.push(layer);
388-
389385
}
390386

391387
map.layers = layers;
@@ -558,15 +554,14 @@ Phaser.TilemapParser = {
558554
// polygon
559555
else if (curo.objects[v].polygon)
560556
{
561-
var object = slice(curo.objects[v],
562-
["name", "type", "x", "y", "visible", "rotation", "properties" ]);
557+
var object = slice(curo.objects[v], ['name', 'type', 'x', 'y', 'visible', 'rotation', 'properties']);
563558

564559
// Parse the polygon into an array
565560
object.polygon = [];
566561

567562
for (var p = 0; p < curo.objects[v].polygon.length; p++)
568563
{
569-
object.polygon.push([ curo.objects[v].polygon[p].x, curo.objects[v].polygon[p].y ]);
564+
object.polygon.push([curo.objects[v].polygon[p].x, curo.objects[v].polygon[p].y]);
570565
}
571566

572567
objects[curo.name].push(object);
@@ -575,15 +570,13 @@ Phaser.TilemapParser = {
575570
// ellipse
576571
else if (curo.objects[v].ellipse)
577572
{
578-
var object = slice(curo.objects[v],
579-
["name", "type", "ellipse", "x", "y", "width", "height", "visible", "rotation", "properties" ]);
573+
var object = slice(curo.objects[v], ['name', 'type', 'ellipse', 'x', 'y', 'width', 'height', 'visible', 'rotation', 'properties']);
580574
objects[curo.name].push(object);
581575
}
582576
// otherwise it's a rectangle
583577
else
584578
{
585-
var object = slice(curo.objects[v],
586-
["name", "type", "x", "y", "width", "height", "visible", "rotation", "properties" ]);
579+
var object = slice(curo.objects[v], ['name', 'type', 'x', 'y', 'width', 'height', 'visible', 'rotation', 'properties']);
587580
object.rectangle = true;
588581
objects[curo.name].push(object);
589582
}

0 commit comments

Comments
 (0)