Added in BuildFromConfig support for Static and Dynamic Tilemaps.

This commit is contained in:
photonstorm 2017-06-26 13:16:27 +01:00
parent fe835266fd
commit c5242004c4
7 changed files with 71 additions and 12 deletions

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: '1146c000-5865-11e7-a80a-994e778896fe'
build: '2e773640-5a69-11e7-ad23-414a67d4aeba'
};
module.exports = CHECKSUM;

View file

@ -0,0 +1,26 @@
var Tilemap = require('./Tilemap');
var GetValue = require('../../../utils/object/GetValue');
var BuildGameObject = require('../../BuildGameObject');
var BuildFromConfig = function (state, config)
{
var mapData = GetValue(config, 'map.data', null);
var mapWidth = GetValue(config, 'map.width', 1);
var mapHeight = GetValue(config, 'map.height', 1);
var x = GetValue(config, 'x', 0);
var y = GetValue(config, 'y', 0);
var tileWidth = GetValue(config, 'tile.width', 16);
var tileHeight = GetValue(config, 'tile.height', 16);
var tileTexture = GetValue(config, 'tile.texture', null);
var tileFrame = GetValue(config, 'tile.frame', null);
var map = new Tilemap(state, mapData, x, y, tileWidth, tileHeight, mapWidth, mapHeight, tileTexture, tileFrame);
BuildGameObject(state, map, config);
return map;
};
module.exports = BuildFromConfig;

View file

@ -46,7 +46,7 @@ var Tilemap = new Class({
this.buildTilemap();
},
getTotalTileCount: function ()
getTotalTileCount: function ()
{
return this.tileArray.length;
},
@ -56,11 +56,11 @@ var Tilemap = new Class({
return this.cull(camera).length;
},
buildTilemap: function ()
buildTilemap: function ()
{
var tileArray = this.tileArray;
var mapData = this.mapData;
var frame = this.frame;
// var frame = this.frame;
var tileWidth = this.tileWidth;
var tileHeight = this.tileHeight;
var width = this.texture.source[0].width;
@ -76,8 +76,8 @@ var Tilemap = new Class({
for (var x = 0; x < mapWidth; ++x)
{
var tileId = mapData[y * mapWidth + x];
var halfTileWidth = (tileWidth) * 0.5;
var halfTileHeight = (tileHeight) * 0.5;
var halfTileWidth = (tileWidth) * 0.5;
var halfTileHeight = (tileHeight) * 0.5;
var rectx = (((tileId % setWidth)|0) * tileWidth) + halfTileWidth;
var recty = (((tileId / setWidth)|0) * tileHeight) + halfTileHeight;
var tx = x * tileWidth;
@ -112,6 +112,7 @@ var Tilemap = new Class({
var cameraH = camera.height;
culledTiles.length = 0;
for (var index = 0; index < length; ++index)
{
var tile = tiles[index];
@ -144,21 +145,25 @@ var Tilemap = new Class({
var iy = (y|0);
var tiles = this.tileArray;
var index = iy * this.mapWidth + ix;
if (index < tiles.length)
{
return tiles[index];
}
return null;
},
getTileAtIndex: function (index)
{
var tiles = this.tileArray;
if (index < tiles.length)
{
return tiles[index];
}
return null;
return null;
}
});

View file

@ -1,5 +1,6 @@
var Tilemap = require('./Tilemap');
var BuildFromConfig = require('./BuildFromConfig');
var FactoryContainer = require('../../../gameobjects/FactoryContainer');
var TilemapFactory = {
@ -11,9 +12,9 @@ var TilemapFactory = {
return this.children.add(new Tilemap(this.state, mapData, x, y, tileWidth, tileHeight, mapWidth, mapHeight, texture, frame));
},
make: function (mapData, x, y, tileWidth, tileHeight, mapWidth, mapHeight, texture, frame)
make: function (config)
{
return new Tilemap(this.state, mapData, x, y, tileWidth, tileHeight, mapWidth, mapHeight, texture, frame);
return BuildFromConfig(this.state, config);
}
};

View file

@ -27,7 +27,7 @@ var TilemapWebGLRenderer = function (renderer, gameObject, interpolationPercenta
texture,
x + tile.x, y + tile.y, tile.width, tile.height, alpha * tile.alpha, tile.tint,
scrollFactorX, scrollFactorY,
textureWidth, textureHeight,
textureWidth, textureHeight,
tile.frameX, tile.frameY, tile.frameWidth, tile.frameHeight,
camera,
renderTarget

View file

@ -0,0 +1,26 @@
var StaticTilemap = require('./StaticTilemap');
var GetValue = require('../../../utils/object/GetValue');
var BuildGameObject = require('../../BuildGameObject');
var BuildFromConfig = function (state, config)
{
var mapData = GetValue(config, 'map.data', null);
var mapWidth = GetValue(config, 'map.width', 1);
var mapHeight = GetValue(config, 'map.height', 1);
var x = GetValue(config, 'x', 0);
var y = GetValue(config, 'y', 0);
var tileWidth = GetValue(config, 'tile.width', 16);
var tileHeight = GetValue(config, 'tile.height', 16);
var tileTexture = GetValue(config, 'tile.texture', null);
var tileFrame = GetValue(config, 'tile.frame', null);
var map = new StaticTilemap(state, mapData, x, y, tileWidth, tileHeight, mapWidth, mapHeight, tileTexture, tileFrame);
BuildGameObject(state, map, config);
return map;
};
module.exports = BuildFromConfig;

View file

@ -1,5 +1,6 @@
var StaticTilemap = require('./StaticTilemap');
var BuildFromConfig = require('./BuildFromConfig');
var FactoryContainer = require('../../../gameobjects/FactoryContainer');
var StaticTilemapFactory = {
@ -11,9 +12,9 @@ var StaticTilemapFactory = {
return this.children.add(new StaticTilemap(this.state, mapData, x, y, tileWidth, tileHeight, mapWidth, mapHeight, texture, frame));
},
make: function (mapData, x, y, tileWidth, tileHeight, mapWidth, mapHeight, texture, frame)
make: function (config)
{
return new StaticTilemap(this.state, mapData, x, y, tileWidth, tileHeight, mapWidth, mapHeight, texture, frame);
return BuildFromConfig(this.state, config);
}
};