Skip to content

Commit beb652a

Browse files
committed
Merge branch 'master' of https://github.com/photonstorm/phaser
2 parents 1637272 + 780bd76 commit beb652a

6 files changed

Lines changed: 69 additions & 46 deletions

File tree

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22

3-
TILEMAP_CSV: '0',
4-
TILEMAP_TILED_JSON: '1'
3+
TILEMAP_CSV: 0,
4+
TILEMAP_TILED_JSON: 1,
5+
TILEMAP_2D_ARRAY: 2
56

67
};

v3/src/gameobjects/tilemap/GenerateEmptyMapData.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
var GenerateEmptyMapData = function (tileWidth, tileHeight, width, height)
1+
var GenerateEmptyMapData = function (format, name, tileWidth, tileHeight, width, height)
22
{
33
return {
44
width: (width !== undefined && width !== null) ? width : 0,
55
height: (height !== undefined && height !== null) ? height : 0,
66
tileWidth: (tileWidth !== undefined && tileWidth !== null) ? tileWidth : 0,
77
tileHeight: (tileHeight !== undefined && tileHeight !== null) ? tileHeight : 0,
8+
name: (name !== undefined && name !== null) ? name : '',
9+
format: format,
810
orientation: 'orthogonal',
911
version: '1',
1012
properties: {},

v3/src/gameobjects/tilemap/Parse.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
var Formats = require('./Formats');
22
var Parsers = require('./parsers');
33

4-
var Parse = function (key, map, tileWidth, tileHeight, width, height)
4+
var Parse = function (key, mapFormat, mapData, tileWidth, tileHeight, width, height)
55
{
66
var newMap;
77

8-
switch(map.format)
8+
switch(mapFormat)
99
{
10+
case (Formats.TILEMAP_2D_ARRAY):
11+
newMap = Parsers.Parse2DArray(key, mapData, tileWidth, tileHeight, width, height);
12+
break;
1013
case (Formats.TILEMAP_CSV):
11-
newMap = Parsers.ParseCSV(key, map.data, tileWidth, tileHeight, width, height);
14+
newMap = Parsers.ParseCSV(key, mapData, tileWidth, tileHeight, width, height);
1215
break;
1316
case (Formats.TILEMAP_TILED_JSON):
14-
newMap = Parsers.ParseTiledJSON(key, map.data);
17+
newMap = Parsers.ParseTiledJSON(key, mapData);
1518
break;
1619
default:
17-
console.warn('Unrecognized tilemap data format: ' + map.format);
20+
console.warn('Unrecognized tilemap data format: ' + mapFormat);
1821
newMap = null;
1922
}
2023

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var GenerateEmptyMapData = require('../GenerateEmptyMapData');
2+
var Formats = require('../Formats');
3+
var Tile = require('../Tile');
4+
5+
var Parse2DArray = function (key, data, tileWidth, tileHeight)
6+
{
7+
var map = GenerateEmptyMapData(Formats.TILEMAP_2D_ARRAY, key, tileWidth, tileHeight);
8+
9+
var output = [];
10+
var height = data.length;
11+
var width = 0;
12+
13+
for (var y = 0; y < data.length; y++)
14+
{
15+
output[y] = [];
16+
var row = data[y];
17+
18+
for (var x = 0; x < row.length; x++)
19+
{
20+
var tileIndex = parseInt(row[x], 10);
21+
if (Number.isNaN(tileIndex)) { tileIndex = -1; }
22+
23+
output[y][x] = new Tile(map.layers[0], tileIndex, x, y, tileWidth, tileHeight);
24+
}
25+
26+
if (width === 0)
27+
{
28+
width = row.length;
29+
}
30+
}
31+
32+
map.width = width;
33+
map.height = height;
34+
map.tileWidth = tileWidth;
35+
map.tileHeight = tileHeight;
36+
map.widthInPixels = width * tileWidth;
37+
map.heightInPixels = height * tileHeight;
38+
39+
map.layers[0].width = width;
40+
map.layers[0].height = height;
41+
map.layers[0].widthInPixels = map.widthInPixels;
42+
map.layers[0].heightInPixels = map.heightInPixels;
43+
map.layers[0].data = output;
44+
45+
return map;
46+
};
47+
48+
module.exports = Parse2DArray;

v3/src/gameobjects/tilemap/parsers/ParseCSV.js

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,15 @@
1-
var GenerateEmptyMapData = require('../GenerateEmptyMapData');
21
var Formats = require('../Formats');
3-
var Tile = require('../Tile');
2+
var Parse2DArray = require('./Parse2DArray');
43

54
var ParseCSV = function (key, data, tileWidth, tileHeight)
65
{
7-
var map = GenerateEmptyMapData(tileWidth, tileHeight);
8-
9-
var output = [];
10-
var rows = data.trim().split('\n');
11-
var height = rows.length;
12-
var width = 0;
13-
14-
for (var y = 0; y < rows.length; y++)
15-
{
16-
output[y] = [];
17-
18-
var column = rows[y].split(',');
19-
20-
for (var x = 0; x < column.length; x++)
21-
{
22-
output[y][x] = new Tile(map.layers[0], parseInt(column[x], 10), x, y, tileWidth, tileHeight);
23-
}
24-
25-
if (width === 0)
26-
{
27-
width = column.length;
28-
}
29-
}
6+
var array2D = data
7+
.trim()
8+
.split('\n')
9+
.map(function (row) { return row.split(','); });
3010

11+
var map = Parse2DArray(key, array2D, tileWidth, tileHeight);
3112
map.format = Formats.TILEMAP_CSV;
32-
map.name = key;
33-
map.width = width;
34-
map.height = height;
35-
map.tileWidth = tileWidth;
36-
map.tileHeight = tileHeight;
37-
map.widthInPixels = width * tileWidth;
38-
map.heightInPixels = height * tileHeight;
39-
40-
map.layers[0].width = width;
41-
map.layers[0].height = height;
42-
map.layers[0].widthInPixels = map.widthInPixels;
43-
map.layers[0].heightInPixels = map.heightInPixels;
44-
map.layers[0].data = output;
4513

4614
return map;
4715
};

v3/src/gameobjects/tilemap/parsers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22

3+
Parse2DArray: require('./Parse2DArray'),
34
ParseCSV: require('./ParseCSV'),
45
ParseTiledJSON: require('./ParseTiledJSON')
56

0 commit comments

Comments
 (0)