Skip to content

Commit 8d4733c

Browse files
committed
Commit current state of development before trying something different (see last note in the progress doc). Will revert to previous commits if this attempt fails.
1 parent 835df12 commit 8d4733c

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

docs/_phaser_tilemap_GL_progress.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,9 @@ Some notes for later:
150150

151151
So, I can't create new TilemapLayer objects in TilemapParser because it will preempt Tilemap.createLayer. Or maybe I can, these TilemapLayer objects will be internal (hidden)...? There aren't any later calls in the Examples which I can extend or adapt, so that might have to be the solution.
152152

153+
day seven:
154+
155+
Read through the above and took a good look at the code. It's a messy solution in this context.
156+
A better idea... parse the map as previously, on return to Tilemap create layers for each 'unique' entry in tilesets array.
157+
This avoids the circular calls to and from tilemap and parse, allows map to have a populated data member after parse, and still creates the required new layers before the example calls createLayer.
158+
(Check if this process can be added to createLayer?)

src/tilemap/Tilemap.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Phaser.Tilemap = function (game, key, tileWidth, tileHeight, width, height) {
3434
*/
3535
this.key = key;
3636

37-
var data = Phaser.TilemapParser.parse(this.game, key, tileWidth, tileHeight, width, height);
37+
var data = Phaser.TilemapParser.parse(this.game, key, tileWidth, tileHeight, width, height, this);
3838
this.data = data;
3939

4040
if (data === null)
@@ -568,6 +568,8 @@ Phaser.Tilemap.prototype = {
568568

569569
// Add Buffer support for the left of the canvas
570570

571+
console.log("Tilemap.createLayer", layer, width, height);
572+
571573
if (width === undefined) { width = this.game.width; }
572574
if (height === undefined) { height = this.game.height; }
573575
if (group === undefined) { group = this.game.world; }
@@ -579,7 +581,9 @@ Phaser.Tilemap.prototype = {
579581
index = this.getLayerIndex(layer);
580582
}
581583

582-
if (index === null || index > this.layers.length)
584+
// createLayer can be called by TilemapParser.parseTiledJSON so this.layers is undefined because we called parse to populate it
585+
// TODO: this creates an ugly circularity in the class relationship, parse/tilemap should be reworked to be cleaner
586+
if (index === null || (this.layers && index > this.layers.length))
583587
{
584588
console.warn('Tilemap.createLayer: Invalid layer ID given: ' + index);
585589
return;

src/tilemap/TilemapParser.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Phaser.TilemapParser = {
2626
INSERT_NULL: false,
2727

2828
/**
29-
* Parse tilemap data from the cache and creates a Tilemap object.
29+
* Parse tilemap data from the cache and creates data for a Tilemap object.
3030
*
3131
* @method Phaser.TilemapParser.parse
3232
* @param {Phaser.Game} game - Game reference to the currently running game.
@@ -35,14 +35,16 @@ Phaser.TilemapParser = {
3535
* @param {number} [tileHeight=32] - The pixel height of a single map tile. If using CSV data you must specify this. Not required if using Tiled map data.
3636
* @param {number} [width=10] - The width of the map in tiles. If this map is created from Tiled or CSV data you don't need to specify this.
3737
* @param {number} [height=10] - The height of the map in tiles. If this map is created from Tiled or CSV data you don't need to specify this.
38+
* @param {Phaser.Tilemap} [tileMap=null] - The Phaser.Tilemap this data is created for (used for createLayer to add new layers when mixed tilesets are used).
3839
* @return {object} The parsed map object.
3940
*/
40-
parse: function (game, key, tileWidth, tileHeight, width, height) {
41+
parse: function (game, key, tileWidth, tileHeight, width, height, tileMap) {
4142

4243
if (tileWidth === undefined) { tileWidth = 32; }
4344
if (tileHeight === undefined) { tileHeight = 32; }
4445
if (width === undefined) { width = 10; }
4546
if (height === undefined) { height = 10; }
47+
if (tileMap === undefined) { tileMap = null; }
4648

4749
if (key === undefined)
4850
{
@@ -64,7 +66,7 @@ Phaser.TilemapParser = {
6466
}
6567
else if (!map.format || map.format === Phaser.Tilemap.TILED_JSON)
6668
{
67-
return this.parseTiledJSON(map.data);
69+
return this.parseTiledJSON(map.data, tileMap);
6870
}
6971
}
7072
else
@@ -198,9 +200,10 @@ Phaser.TilemapParser = {
198200
* Parses a Tiled JSON file into valid map data.
199201
* @method Phaser.TilemapParser.parseJSON
200202
* @param {object} json - The JSON map data.
203+
* @param {Phaser.Tilemap} [tileMap=null] - The Phaser.Tilemap object which has the createLayer functionality.
201204
* @return {object} Generated and parsed map data.
202205
*/
203-
parseTiledJSON: function (json) {
206+
parseTiledJSON: function (json, tileMap) {
204207

205208
if (json.orientation !== 'orthogonal')
206209
{
@@ -646,12 +649,15 @@ Phaser.TilemapParser = {
646649
var tile;
647650
var sid;
648651
var set;
652+
var setLayers = [];
649653

650-
// go through each of the map layers
654+
// go through each of the map data layers
651655
for (var i = 0; i < map.layers.length; i++)
652656
{
653657
layer = map.layers[i];
654658

659+
set = null;
660+
655661
// rows of tiles
656662
for (var j = 0; j < layer.data.length; j++)
657663
{
@@ -670,14 +676,28 @@ Phaser.TilemapParser = {
670676
// find the relevant tileset
671677

672678
sid = map.tiles[tile.index][2];
679+
680+
// if this is a different tileset to the one already being used by this layer...
681+
// TODO: check if the tileset has changed size, we can probably skip this if it's identical
682+
if ( set && set.name !== map.tilesets[sid].name )
683+
{
684+
// if there's no layer for this tileset, create a clone of this layer and attach the tileset to it
685+
if ( !setLayers[set.name] )
686+
{
687+
setLayers[set.name] = tileMap.createLayer();
688+
}
689+
// TODO: add the tile to the layer which holds this tileset, and make it an invalid tile in this layer
690+
}
673691
set = map.tilesets[sid];
674692

693+
675694
// if that tile type has any properties, add them to the tile object
676695

677696
if (set.tileProperties && set.tileProperties[tile.index - set.firstgid])
678697
{
679698
tile.properties = Phaser.Utils.mixin(set.tileProperties[tile.index - set.firstgid], tile.properties);
680699
}
700+
681701
}
682702
}
683703
}

0 commit comments

Comments
 (0)