Skip to content

Commit b5fb0b8

Browse files
committed
Refactor in order to update TilemapCreator & TilemapFactory
1 parent f82217e commit b5fb0b8

4 files changed

Lines changed: 68 additions & 86 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var Formats = require('./Formats');
2+
var Parse = require('./parsers/Parse');
3+
var MapData = require('./mapdata/MapData');
4+
var Tilemap = require('./Tilemap');
5+
6+
// Parse from key or data and create a Tilemap. If neither is given, make an empty Tilemap.
7+
var ParseToTilemap = function (scene, key, tileWidth, tileHeight, width, height, data, insertNull)
8+
{
9+
if (tileWidth === undefined) { tileWidth = 32; }
10+
if (tileHeight === undefined) { tileHeight = 32; }
11+
if (width === undefined) { width = 10; }
12+
if (height === undefined) { height = 10; }
13+
if (insertNull === undefined) { insertNull = false; }
14+
15+
var mapData = null;
16+
17+
if (Array.isArray(data))
18+
{
19+
var name = key !== undefined ? key : 'map';
20+
mapData = Parse(name, Formats.TILEMAP_2D_ARRAY, data, tileWidth, tileHeight, insertNull);
21+
}
22+
else if (key !== undefined)
23+
{
24+
var tilemapData = scene.cache.tilemap.get(key);
25+
26+
if (!tilemapData)
27+
{
28+
console.warn('No map data found for key ' + key);
29+
}
30+
else
31+
{
32+
mapData = Parse(key, tilemapData.format, tilemapData.data, tileWidth, tileHeight, insertNull);
33+
}
34+
}
35+
36+
if (mapData === null)
37+
{
38+
mapData = new MapData({
39+
tileWidth: tileWidth,
40+
tileHeight: tileHeight,
41+
width: width,
42+
height: height
43+
});
44+
}
45+
46+
return new Tilemap(scene, mapData);
47+
};
48+
49+
module.exports = ParseToTilemap;
50+

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,10 @@ var Tilemap = new Class({
1212

1313
initialize:
1414

15-
function Tilemap (scene, mapData, tileWidth, tileHeight, width, height)
15+
function Tilemap (scene, mapData)
1616
{
1717
this.scene = scene;
1818

19-
if (mapData === null)
20-
{
21-
mapData = new MapData({
22-
tileWidth: tileWidth,
23-
tileHeight: tileHeight,
24-
width: width,
25-
height: height
26-
});
27-
}
28-
2919
this.tilesets = [];
3020
this.tileWidth = mapData.tileWidth;
3121
this.tileHeight = mapData.tileHeight;
@@ -142,6 +132,7 @@ var Tilemap = new Class({
142132

143133
var dynamicLayer = new DynamicTilemapLayer(this.scene, this, this.currentLayerIndex, tileset, x, y);
144134
this.scene.sys.displayList.add(dynamicLayer);
135+
145136
return dynamicLayer;
146137
},
147138

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
var GameObjectCreator = require('../../scene/plugins/GameObjectCreator');
2-
var GetFastValue = require('../../utils/object/GetFastValue');
3-
var Tilemap = require('./Tilemap');
4-
var Parse = require('./Parse');
5-
var Formats = require('./Formats');
2+
var ParseToTilemap = require('./ParseToTilemap');
63

74
// When registering a factory function 'this' refers to the GameObjectCreator context.
85

96
GameObjectCreator.register('tilemap', function (config)
107
{
118
// config {
12-
// key: null, (string|number)
9+
// key: undefined, (string|number),
1310
// tileWidth: 32,
1411
// tileHeight: 32,
1512
// width: 10,
@@ -18,32 +15,6 @@ GameObjectCreator.register('tilemap', function (config)
1815
// insertNull: false
1916
// }
2017

21-
var key = GetFastValue(config, 'key', null);
22-
var tileWidth = GetFastValue(config, 'tileWidth', 32);
23-
var tileHeight = GetFastValue(config, 'tileHeight', 32);
24-
var width = GetFastValue(config, 'width', 10);
25-
var height = GetFastValue(config, 'height', 10);
26-
var insertNull = GetFastValue(config, 'insertNull', false);
27-
var data = GetFastValue(config, 'data', null);
28-
29-
var parsedData = null;
30-
if (key === null && Array.isArray(data))
31-
{
32-
parsedData = Parse(key, Formats.TILEMAP_2D_ARRAY, data, tileWidth, tileHeight, insertNull);
33-
}
34-
else if (key !== null)
35-
{
36-
var tilemapData = this.scene.cache.tilemap.get(key);
37-
38-
if (!tilemapData)
39-
{
40-
console.warn('No map data found for key ' + key);
41-
}
42-
else
43-
{
44-
parsedData = Parse(key, tilemapData.format, tilemapData.data, tileWidth, tileHeight, insertNull);
45-
}
46-
}
47-
48-
return new Tilemap(this.scene, parsedData, tileWidth, tileHeight, width, height);
18+
var c = config !== undefined ? config : {};
19+
return ParseToTilemap(this.scene, c.key, c.tileWidth, c.tileHeight, c.width, c.height, c.data, c.insertNull);
4920
});
Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
var GameObjectFactory = require('../../scene/plugins/GameObjectFactory');
2-
var GetFastValue = require('../../utils/object/GetFastValue');
3-
var Tilemap = require('./Tilemap');
4-
var Parse = require('./Parse');
5-
var Formats = require('./Formats');
2+
var ParseToTilemap = require('./ParseToTilemap');
63

74

85
// When registering a factory function 'this' refers to the GameObjectFactory context.
@@ -13,44 +10,17 @@ var Formats = require('./Formats');
1310
// this.displayList - a reference to the Display List the Scene owns
1411
// this.updateList - a reference to the Update List the Scene owns
1512

16-
GameObjectFactory.register('tilemap', function (config)
13+
GameObjectFactory.register('tilemap', function (key, tileWidth, tileHeight, width, height, data, insertNull)
1714
{
18-
// config {
19-
// key: null, (string|number)
20-
// tileWidth: 32,
21-
// tileHeight: 32,
22-
// width: 10,
23-
// height: 10,
24-
// data: null (2D array of tile indices),
25-
// insertNull: false
26-
// }
15+
// Allow users to specify null as default parameter, but convert it to undefined to match what
16+
// the creator function passed to the parser.
17+
if (key === null) { key = undefined; }
18+
if (tileWidth === null) { tileWidth = undefined; }
19+
if (tileHeight === null) { tileHeight = undefined; }
20+
if (width === null) { width = undefined; }
21+
if (height === null) { height = undefined; }
22+
if (data === null) { data = undefined; }
23+
if (insertNull === null) { insertNull = undefined; }
2724

28-
var key = GetFastValue(config, 'key', null);
29-
var tileWidth = GetFastValue(config, 'tileWidth', 32);
30-
var tileHeight = GetFastValue(config, 'tileHeight', 32);
31-
var width = GetFastValue(config, 'width', 10);
32-
var height = GetFastValue(config, 'height', 10);
33-
var insertNull = GetFastValue(config, 'insertNull', false);
34-
var data = GetFastValue(config, 'data', null);
35-
36-
var parsedData = null;
37-
if (key === null && Array.isArray(data))
38-
{
39-
parsedData = Parse(key, Formats.TILEMAP_2D_ARRAY, data, tileWidth, tileHeight, insertNull);
40-
}
41-
else if (key !== null)
42-
{
43-
var tilemapData = this.scene.cache.tilemap.get(key);
44-
45-
if (!tilemapData)
46-
{
47-
console.warn('No map data found for key ' + key);
48-
}
49-
else
50-
{
51-
parsedData = Parse(key, tilemapData.format, tilemapData.data, tileWidth, tileHeight, insertNull);
52-
}
53-
}
54-
55-
return new Tilemap(this.scene, parsedData, tileWidth, tileHeight, width, height);
25+
return ParseToTilemap(this.scene, key, tileWidth, tileHeight, width, height, data, insertNull);
5626
});

0 commit comments

Comments
 (0)