Skip to content

Commit b8e8d9d

Browse files
committed
Made data structure for maps and layers more explicit via classes & default props
1 parent 7a2b970 commit b8e8d9d

7 files changed

Lines changed: 141 additions & 119 deletions

File tree

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Class = require('../../utils/Class');
2-
var GenerateEmptyMapData = require('./emptymap/GenerateEmptyMapData');
3-
var GenerateEmptyMapLayer = require('./emptymap/GenerateEmptyMapLayer');
2+
var MapData = require('./mapdata/MapData');
3+
var LayerData = require('./mapdata/LayerData');
44
var StaticTilemapLayer = require('./staticlayer/StaticTilemapLayer.js');
55
var DynamicTilemapLayer = require('./dynamiclayer/DynamicTilemapLayer.js');
66
var Tileset = require('./Tileset');
@@ -18,7 +18,12 @@ var Tilemap = new Class({
1818

1919
if (mapData === null)
2020
{
21-
mapData = GenerateEmptyMapData(tileWidth, tileHeight, width, height);
21+
mapData = new MapData({
22+
tileWidth: tileWidth,
23+
tileHeight: tileHeight,
24+
width: width,
25+
height: height
26+
});
2227
}
2328

2429
this.tilesets = [];
@@ -111,20 +116,26 @@ var Tilemap = new Class({
111116
return null;
112117
}
113118

114-
var layer = GenerateEmptyMapLayer(name, tileWidth, tileHeight, width, height);
119+
var layerData = new LayerData({
120+
name: name,
121+
tileWidth: tileWidth,
122+
tileHeight: tileHeight,
123+
width: width,
124+
height: height
125+
});
115126

116127
var row;
117128
for (var tileY = 0; tileY < height; tileY++)
118129
{
119130
row = [];
120131
for (var tileX = 0; tileX < width; tileX++)
121132
{
122-
row.push(new Tile(layer, -1, tileX, tileY, tileWidth, tileHeight));
133+
row.push(new Tile(layerData, -1, tileX, tileY, tileWidth, tileHeight));
123134
}
124-
layer.data.push(row);
135+
layerData.data.push(row);
125136
}
126137

127-
this.layers.push(layer);
138+
this.layers.push(layerData);
128139
this.currentLayerIndex = this.layers.length - 1;
129140

130141
// TODO: decide about v2 trimming to game width/height

v3/src/gameobjects/tilemap/emptymap/GenerateEmptyMapData.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

v3/src/gameobjects/tilemap/emptymap/GenerateEmptyMapLayer.js

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var Class = require('../../../utils/Class');
2+
var GetFastValue = require('../../../utils/object/GetFastValue');
3+
4+
var LayerData = new Class({
5+
6+
initialize:
7+
8+
function MapLayerData (config)
9+
{
10+
if (config === undefined) { config = {}; }
11+
12+
this.name = GetFastValue(config, 'name', 'layer');
13+
this.x = GetFastValue(config, 'x', 0);
14+
this.y = GetFastValue(config, 'y', 0);
15+
this.width = GetFastValue(config, 'width', 0);
16+
this.height = GetFastValue(config, 'height', 0);
17+
this.tileWidth = GetFastValue(config, 'tileWidth', 0);
18+
this.tileHeight = GetFastValue(config, 'tileHeight', 0);
19+
this.widthInPixels = GetFastValue(config, 'widthInPixels', this.width * this.tileWidth);
20+
this.heightInPixels = GetFastValue(config, 'heightInPixels', this.height * this.tileHeight);
21+
this.alpha = GetFastValue(config, 'alpha', 1);
22+
this.visible = GetFastValue(config, 'visible', true);
23+
this.properties = GetFastValue(config, 'properties', {});
24+
this.indexes = GetFastValue(config, 'indexes', []);
25+
this.callbacks = GetFastValue(config, 'callbacks', []);
26+
this.bodies = GetFastValue(config, 'bodies', []);
27+
this.data = GetFastValue(config, 'data', []);
28+
this.tilemapLayer = GetFastValue(config, 'tilemapLayer', null);
29+
}
30+
31+
});
32+
33+
module.exports = LayerData;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var Class = require('../../../utils/Class');
2+
var GetFastValue = require('../../../utils/object/GetFastValue');
3+
4+
var MapData = new Class({
5+
6+
initialize:
7+
8+
function MapData (config)
9+
{
10+
if (config === undefined) { config = {}; }
11+
12+
this.name = GetFastValue(config, 'name', 'map');
13+
this.width = GetFastValue(config, 'width', 0);
14+
this.height = GetFastValue(config, 'height', 0);
15+
this.tileWidth = GetFastValue(config, 'tileWidth', 0);
16+
this.tileHeight = GetFastValue(config, 'tileHeight', 0);
17+
this.widthInPixels = GetFastValue(config, 'widthInPixels', this.width * this.tileWidth);
18+
this.heightInPixels = GetFastValue(config, 'heightInPixels', this.height * this.tileHeight);
19+
this.format = GetFastValue(config, 'format', null);
20+
this.orientation = GetFastValue(config, 'orientation', 'orthogonal');
21+
this.version = GetFastValue(config, 'version', '1');
22+
this.properties = GetFastValue(config, 'properties', {});
23+
this.layers = GetFastValue(config, 'layers', []);
24+
this.images = GetFastValue(config, 'images', []);
25+
this.objects = GetFastValue(config, 'objects', {});
26+
this.collision = GetFastValue(config, 'collision', {});
27+
this.tilesets = GetFastValue(config, 'tilesets', []);
28+
this.tiles = GetFastValue(config, 'tiles', []);
29+
}
30+
31+
});
32+
33+
module.exports = MapData;
Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
1-
var GenerateEmptyMapData = require('../emptymap/GenerateEmptyMapData');
1+
var MapData = require('../mapdata/MapData');
2+
var LayerData = require('../mapdata/LayerData');
23
var Formats = require('../Formats');
34
var Tile = require('../Tile');
45

56
var Parse2DArray = function (key, data, tileWidth, tileHeight, insertNull)
67
{
7-
var map = GenerateEmptyMapData(Formats.TILEMAP_2D_ARRAY, key, tileWidth, tileHeight);
8-
9-
var output = [];
8+
var layerData = new LayerData({
9+
tileWidth: tileWidth,
10+
tileHeight: tileHeight
11+
});
12+
13+
var mapData = new MapData({
14+
name: key,
15+
tileWidth: tileWidth,
16+
tileHeight: tileHeight,
17+
format: Formats.TILEMAP_2D_ARRAY,
18+
layers: [ layerData ]
19+
});
20+
21+
var tiles = [];
1022
var height = data.length;
1123
var width = 0;
1224

1325
for (var y = 0; y < data.length; y++)
1426
{
15-
output[y] = [];
27+
tiles[y] = [];
1628
var row = data[y];
1729

1830
for (var x = 0; x < row.length; x++)
1931
{
2032
var tileIndex = parseInt(row[x], 10);
2133

22-
if (Number.isNaN(tileIndex))
34+
if (Number.isNaN(tileIndex) || tileIndex === -1)
2335
{
24-
output[y][x] = insertNull
36+
tiles[y][x] = insertNull
2537
? null
26-
: new Tile(map.layers[0], -1, x, y, tileWidth, tileHeight);
38+
: new Tile(layerData, -1, x, y, tileWidth, tileHeight);
2739
}
2840
else
2941
{
30-
output[y][x] = new Tile(map.layers[0], tileIndex, x, y, tileWidth, tileHeight);
42+
tiles[y][x] = new Tile(layerData, tileIndex, x, y, tileWidth, tileHeight);
3143
}
3244
}
3345

@@ -37,20 +49,13 @@ var Parse2DArray = function (key, data, tileWidth, tileHeight, insertNull)
3749
}
3850
}
3951

40-
map.width = width;
41-
map.height = height;
42-
map.tileWidth = tileWidth;
43-
map.tileHeight = tileHeight;
44-
map.widthInPixels = width * tileWidth;
45-
map.heightInPixels = height * tileHeight;
46-
47-
map.layers[0].width = width;
48-
map.layers[0].height = height;
49-
map.layers[0].widthInPixels = map.widthInPixels;
50-
map.layers[0].heightInPixels = map.heightInPixels;
51-
map.layers[0].data = output;
52+
mapData.width = layerData.width = width;
53+
mapData.height = layerData.height = height;
54+
mapData.widthInPixels = layerData.widthInPixels = width * tileWidth;
55+
mapData.heightInPixels = layerData.heightInPixels = height * tileHeight;
56+
layerData.data = tiles;
5257

53-
return map;
58+
return mapData;
5459
};
5560

5661
module.exports = Parse2DArray;

0 commit comments

Comments
 (0)