Skip to content

Commit a76a653

Browse files
committed
CSV tilemap parser & dummy tiled json parser
1 parent 35ecba7 commit a76a653

6 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var GenerateEmptyMapData = function (tileWidth, tileHeight, width, height)
2+
{
3+
return {
4+
width: (width !== undefined && width !== null) ? width : 0,
5+
height: (height !== undefined && height !== null) ? height : 0,
6+
tileWidth: (tileWidth !== undefined && tileWidth !== null) ? tileWidth : 0,
7+
tileHeight: (tileHeight !== undefined && tileHeight !== null) ? tileHeight : 0,
8+
orientation: 'orthogonal',
9+
version: '1',
10+
properties: {},
11+
widthInPixels: 0,
12+
heightInPixels: 0,
13+
layers: [
14+
{
15+
name: 'layer',
16+
x: 0,
17+
y: 0,
18+
width: 0,
19+
height: 0,
20+
widthInPixels: 0,
21+
heightInPixels: 0,
22+
alpha: 1,
23+
visible: true,
24+
properties: {},
25+
indexes: [],
26+
callbacks: [],
27+
bodies: [],
28+
data: []
29+
}
30+
],
31+
images: [],
32+
objects: {},
33+
collision: {},
34+
tilesets: [],
35+
tiles: []
36+
};
37+
};
38+
39+
module.exports = GenerateEmptyMapData;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var Formats = require('./Formats');
2+
var Parsers = require('./parsers');
3+
4+
var Parse = function (key, map, tileWidth, tileHeight, width, height)
5+
{
6+
var newMap;
7+
8+
switch(map.format)
9+
{
10+
case (Formats.TILEMAP_CSV):
11+
newMap = Parsers.ParseCSV(key, map.data, tileWidth, tileHeight, width, height);
12+
break;
13+
case (Formats.TILEMAP_TILED_JSON):
14+
newMap = Parsers.ParseTiledJSON(key, map.data);
15+
break;
16+
default:
17+
console.warn('Unrecognized tilemap data format: ' + map.format);
18+
newMap = null;
19+
}
20+
21+
return newMap;
22+
};
23+
24+
module.exports = Parse;
25+

v3/src/gameobjects/tilemap/Tile.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var Class = require('../../utils/Class');
2+
3+
// Dummy Tile class
4+
// Todo: merge dynamic/tile into this
5+
6+
var Tile = new Class({
7+
8+
initialize:
9+
10+
function Tile (layer, index, x, y, width, height)
11+
{
12+
this.layer = layer;
13+
this.index = index;
14+
this.x = x;
15+
this.y = y;
16+
this.width = width;
17+
this.height = height;
18+
}
19+
20+
});
21+
22+
module.exports = Tile;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var GenerateEmptyMapData = require('../GenerateEmptyMapData');
2+
var Formats = require('../Formats');
3+
var Tile = require('../Tile');
4+
5+
var ParseCSV = function (key, data, tileWidth, tileHeight)
6+
{
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+
}
30+
31+
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;
45+
46+
return map;
47+
};
48+
49+
module.exports = ParseCSV;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var GenerateEmptyMapData = require('../GenerateEmptyMapData');
2+
3+
var ParseJSONTiled = function (json)
4+
{
5+
// Dummy map for now
6+
return GenerateEmptyMapData();
7+
};
8+
9+
module.exports = ParseJSONTiled;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
3+
ParseCSV: require('./ParseCSV'),
4+
ParseTiledJSON: require('./ParseTiledJSON')
5+
6+
};

0 commit comments

Comments
 (0)