Skip to content

Commit 2e68f25

Browse files
committed
Added Weltmeister support to Loader & Tilemap API
1 parent 72a14df commit 2e68f25

5 files changed

Lines changed: 149 additions & 1 deletion

File tree

src/gameobjects/tilemap/parsers/Parse.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var Parse2DArray = require('./Parse2DArray');
33
var ParseCSV = require('./ParseCSV');
44
var ParseTiledJSON = require('./parsetiledjson/');
5+
var ParseWeltmister = require('./parseweltmeister/');
56
var Formats = require('../Formats');
67

78
/**
@@ -39,6 +40,9 @@ var Parse = function (name, mapFormat, data, tileWidth, tileHeight, insertNull)
3940
case (Formats.TILED_JSON):
4041
newMap = ParseTiledJSON(name, data, insertNull);
4142
break;
43+
case (Formats.WELTMEISTER):
44+
newMap = ParseWeltmister(name, data, insertNull);
45+
break;
4246
default:
4347
console.warn('Unrecognized tilemap data format: ' + mapFormat);
4448
newMap = null;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var LayerData = require('../../mapdata/LayerData');
2+
var Tile = require('../../Tile');
3+
4+
var ParseTileLayers = function (json, insertNull)
5+
{
6+
var tileLayers = [];
7+
8+
for (var i = 0; i < json.layer.length; i++)
9+
{
10+
var layer = json.layer[i];
11+
12+
var layerData = new LayerData({
13+
name: layer.name,
14+
width: layer.width,
15+
height: layer.height,
16+
tileWidth: layer.tilesize,
17+
tileHeight: layer.tilesize,
18+
visible: layer.visible === 1
19+
});
20+
21+
var row = [];
22+
var tileGrid = [];
23+
24+
// Loop through the data field in the JSON. This is a 2D array containing the tile indexes,
25+
// one after the other. The indexes are relative to the tileset that contains the tile.
26+
for (var y = 0; y < layer.data.length; y++)
27+
{
28+
for (var x = 0; x < layer.data[y].length; x++)
29+
{
30+
// In Weltmeister, 0 = no tile, but the Tilemap API expects -1 = no tile.
31+
var index = layer.data[y][x] - 1;
32+
33+
var tile;
34+
35+
if (index > -1)
36+
{
37+
tile = new Tile(layerData, index, x, y, layer.tilesize, layer.tilesize);
38+
}
39+
else
40+
{
41+
tile = insertNull
42+
? null
43+
: new Tile(layerData, -1, x, y, layer.tilesize, layer.tilesize);
44+
}
45+
46+
row.push(tile);
47+
}
48+
49+
tileGrid.push(row);
50+
row = [];
51+
}
52+
53+
layerData.data = tileGrid;
54+
55+
tileLayers.push(layerData);
56+
}
57+
58+
return tileLayers;
59+
};
60+
61+
module.exports = ParseTileLayers;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Tileset = require('../../Tileset');
2+
3+
var ParseTilesets = function (json)
4+
{
5+
var tilesets = [];
6+
var tilesetsNames = [];
7+
8+
for (var i = 0; i < json.layer.length; i++)
9+
{
10+
var layer = json.layer[i];
11+
12+
// A relative filepath to the source image (within Weltmeister) is used for the name
13+
var tilesetName = layer.tilesetName;
14+
15+
// Only add unique tilesets that have a valid name. Collision layers will have a blank name.
16+
if (tilesetName !== '' && tilesetsNames.indexOf(tilesetName) === -1)
17+
{
18+
tilesetsNames.push(tilesetName);
19+
20+
// Tiles are stored with an ID relative to the tileset, rather than a globally unique ID
21+
// across all tilesets. Also, tilesets in Weltmeister have no margin or padding.
22+
tilesets.push(new Tileset(tilesetName, 0, layer.tilesize, layer.tilesize, 0, 0));
23+
}
24+
}
25+
26+
return tilesets;
27+
};
28+
29+
module.exports = ParseTilesets;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var Formats = require('../../Formats');
2+
var MapData = require('../../mapdata/MapData');
3+
var ParseTilesets = require('./ParseTilesets');
4+
var ParseTileLayers = require('./ParseTileLayers');
5+
6+
/**
7+
* Parses a Weltmeister JSON object into a new MapData object.
8+
*
9+
* @param {string} name - The name of the tilemap, used to set the name on the MapData.
10+
* @param {object} json - The Weltmeister JSON object.
11+
* @param {boolean} insertNull - Controls how empty tiles, tiles with an index of -1, in the map
12+
* data are handled. If `true`, empty locations will get a value of `null`. If `false`, empty
13+
* location will get a Tile object with an index of -1. If you've a large sparsely populated map and
14+
* the tile data doesn't need to change then setting this value to `true` will help with memory
15+
* consumption. However if your map is small or you need to update the tiles dynamically, then leave
16+
* the default value set.
17+
*/
18+
var ParseWeltmeister = function (name, json, insertNull)
19+
{
20+
if (json.layer.length === 0)
21+
{
22+
console.warn('No layers found in the Weltmeister map: ' + name);
23+
return null;
24+
}
25+
26+
var width = 0;
27+
var height = 0;
28+
for (var i = 0; i < json.layer.length; i++)
29+
{
30+
if (json.layer.width > width) { width = json.layer.width; }
31+
if (json.layer.height > height) { height = json.layer.height; }
32+
}
33+
34+
var mapData = new MapData({
35+
width: width,
36+
height: height,
37+
name: name,
38+
tileWidth: json.layer[0].tilesize,
39+
tileHeight: json.layer[0].tilesize,
40+
format: Formats.WELTMEISTER
41+
});
42+
43+
mapData.layers = ParseTileLayers(json, insertNull);
44+
mapData.tilesets = ParseTilesets(json);
45+
46+
return mapData;
47+
};
48+
49+
module.exports = ParseWeltmeister;

src/loader/Loader.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,16 @@ var Loader = new Class({
133133
return TilemapCSVFile.create(this, key, url, TILEMAP_FORMATS.CSV, xhrSettings);
134134
},
135135

136-
tilemapJSON: function (key, url, xhrSettings)
136+
tilemapTiledJSON: function (key, url, xhrSettings)
137137
{
138138
return TilemapJSONFile.create(this, key, url, TILEMAP_FORMATS.TILED_JSON, xhrSettings);
139139
},
140140

141+
tilemapWeltmeister: function (key, url, xhrSettings)
142+
{
143+
return TilemapJSONFile.create(this, key, url, TILEMAP_FORMATS.WELTMEISTER, xhrSettings);
144+
},
145+
141146
// ---------------------------------------------------
142147
// Multi-File Loaders
143148
// ---------------------------------------------------

0 commit comments

Comments
 (0)