2017-06-09 04:00:12 +00:00
|
|
|
var Class = require('../../../utils/Class');
|
|
|
|
var GameObject = require('../../GameObject');
|
2017-07-04 00:59:31 +00:00
|
|
|
var Components = require('../../components');
|
2017-11-09 18:18:23 +00:00
|
|
|
var DynamicTilemapLayerRender = require('./DynamicTilemapLayerRender');
|
2017-06-09 04:00:12 +00:00
|
|
|
var Tile = require('./Tile');
|
|
|
|
|
2017-11-09 18:18:23 +00:00
|
|
|
var DynamicTilemapLayer = new Class({
|
2017-06-09 04:00:12 +00:00
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
|
|
|
Components.Flip,
|
|
|
|
Components.GetBounds,
|
|
|
|
Components.Origin,
|
|
|
|
Components.RenderTarget,
|
|
|
|
Components.ScaleMode,
|
|
|
|
Components.Size,
|
|
|
|
Components.Texture,
|
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
2017-06-22 02:19:03 +00:00
|
|
|
Components.ScrollFactor,
|
2017-11-09 18:18:23 +00:00
|
|
|
DynamicTilemapLayerRender
|
2017-06-09 04:00:12 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
function DynamicTilemapLayer (scene, tilemap, layerIndex, tileset, x, y)
|
2017-06-09 04:00:12 +00:00
|
|
|
{
|
2017-11-09 18:18:23 +00:00
|
|
|
GameObject.call(this, scene, 'DynamicTilemapLayer');
|
2017-06-09 04:00:12 +00:00
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
this.map = tilemap;
|
|
|
|
this.layerIndex = layerIndex;
|
|
|
|
this.layer = tilemap.layers[layerIndex];
|
|
|
|
this.tileset = tileset;
|
2017-11-03 16:52:57 +00:00
|
|
|
|
2017-06-09 04:00:12 +00:00
|
|
|
this.tileArray = [];
|
|
|
|
this.culledTiles = [];
|
2017-11-03 16:52:57 +00:00
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
this.setTexture(tileset.image.key);
|
2017-06-09 04:00:12 +00:00
|
|
|
this.setPosition(x, y);
|
|
|
|
this.setSizeToFrame();
|
|
|
|
this.setOrigin();
|
2017-11-11 16:38:52 +00:00
|
|
|
this.setSize(this.map.tileWidth * this.layer.width, this.map.tileheight * this.layer.height);
|
2017-11-03 16:52:57 +00:00
|
|
|
|
|
|
|
this.skipIndexZero = false;
|
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
this.buildTilemap();
|
2017-06-09 04:00:12 +00:00
|
|
|
},
|
|
|
|
|
2017-06-26 12:16:27 +00:00
|
|
|
getTotalTileCount: function ()
|
2017-06-22 02:19:03 +00:00
|
|
|
{
|
|
|
|
return this.tileArray.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
getVisibleTileCount: function (camera)
|
|
|
|
{
|
|
|
|
return this.cull(camera).length;
|
|
|
|
},
|
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
buildTilemap: function ()
|
2017-06-09 04:00:12 +00:00
|
|
|
{
|
|
|
|
var tileArray = this.tileArray;
|
2017-11-11 16:38:52 +00:00
|
|
|
var mapData = this.layer.data;
|
|
|
|
var tileWidth = this.map.tileWidth;
|
|
|
|
var tileHeight = this.map.tileHeight;
|
|
|
|
var tileset = this.tileset;
|
2017-06-09 04:00:12 +00:00
|
|
|
var width = this.texture.source[0].width;
|
|
|
|
var height = this.texture.source[0].height;
|
2017-11-11 16:38:52 +00:00
|
|
|
var mapWidth = this.layer.width;
|
|
|
|
var mapHeight = this.layer.height;
|
2017-06-09 04:00:12 +00:00
|
|
|
|
|
|
|
tileArray.length = 0;
|
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
for (var row = 0; row < mapHeight; ++row)
|
2017-06-09 04:00:12 +00:00
|
|
|
{
|
2017-11-11 16:38:52 +00:00
|
|
|
for (var col = 0; col < mapWidth; ++col)
|
2017-06-09 04:00:12 +00:00
|
|
|
{
|
2017-11-11 16:38:52 +00:00
|
|
|
var tileIndex = mapData[row][col].index;
|
|
|
|
if (tileIndex <= 0 && this.skipIndexZero) { continue; }
|
|
|
|
|
|
|
|
var texCoords = tileset.getTileTextureCoordinates(tileIndex);
|
|
|
|
if (texCoords === null) { continue; }
|
2017-06-27 21:22:39 +00:00
|
|
|
|
2017-06-09 04:00:12 +00:00
|
|
|
tileArray.push(new Tile({
|
2017-11-11 16:38:52 +00:00
|
|
|
index: tileIndex,
|
|
|
|
id: tileIndex,
|
|
|
|
x: col * tileWidth,
|
|
|
|
y: row * tileHeight,
|
2017-06-09 04:00:12 +00:00
|
|
|
width: tileWidth,
|
|
|
|
height: tileHeight,
|
2017-11-11 16:38:52 +00:00
|
|
|
frameX: texCoords.x,
|
|
|
|
frameY: texCoords.y,
|
2017-06-22 02:19:03 +00:00
|
|
|
frameWidth: tileWidth,
|
|
|
|
frameHeight: tileHeight,
|
|
|
|
textureWidth: width,
|
2017-11-11 16:38:52 +00:00
|
|
|
textureHeight: height
|
2017-06-09 04:00:12 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-06-22 02:19:03 +00:00
|
|
|
cull: function (camera)
|
|
|
|
{
|
|
|
|
var culledTiles = this.culledTiles;
|
|
|
|
var tiles = this.tileArray;
|
|
|
|
var length = tiles.length;
|
|
|
|
var scrollX = camera.scrollX * this.scrollFactorX;
|
|
|
|
var scrollY = camera.scrollY * this.scrollFactorY;
|
|
|
|
var cameraW = camera.width;
|
|
|
|
var cameraH = camera.height;
|
|
|
|
|
|
|
|
culledTiles.length = 0;
|
2017-06-26 12:16:27 +00:00
|
|
|
|
2017-06-22 02:19:03 +00:00
|
|
|
for (var index = 0; index < length; ++index)
|
|
|
|
{
|
|
|
|
var tile = tiles[index];
|
|
|
|
var tileX = tile.x - scrollX;
|
|
|
|
var tileY = tile.y - scrollY;
|
|
|
|
var tileW = tile.width;
|
|
|
|
var tileH = tile.height;
|
|
|
|
var cullW = cameraW + tileW;
|
|
|
|
var cullH = cameraH + tileH;
|
|
|
|
|
|
|
|
if (tile.visible &&
|
2017-07-07 01:17:27 +00:00
|
|
|
tileX > -tileW && tileY > -tileH &&
|
2017-06-22 02:19:03 +00:00
|
|
|
tileX < cullW && tileY < cullH)
|
|
|
|
{
|
|
|
|
culledTiles.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return culledTiles;
|
|
|
|
},
|
|
|
|
|
|
|
|
forEach: function (callback)
|
2017-06-09 04:00:12 +00:00
|
|
|
{
|
2017-06-22 02:19:03 +00:00
|
|
|
this.tileArray.forEach(callback);
|
|
|
|
},
|
|
|
|
|
2017-06-26 14:08:26 +00:00
|
|
|
// Returns Object containing:
|
|
|
|
// {
|
|
|
|
// alpha
|
|
|
|
// frameWidth,
|
|
|
|
// frameHeight,
|
|
|
|
// frameX
|
|
|
|
// frameY
|
|
|
|
// id
|
2017-11-03 16:52:57 +00:00
|
|
|
// index = the tile in the tileset to render
|
2017-06-26 14:08:26 +00:00
|
|
|
// textureWidth = tileset texture size
|
|
|
|
// textureHeight
|
|
|
|
// tint
|
|
|
|
// visible
|
|
|
|
// width
|
|
|
|
// x
|
|
|
|
// y
|
|
|
|
// }
|
|
|
|
|
2017-06-22 02:19:03 +00:00
|
|
|
getTileAt: function (x, y)
|
|
|
|
{
|
|
|
|
var ix = (x|0);
|
|
|
|
var iy = (y|0);
|
|
|
|
var tiles = this.tileArray;
|
|
|
|
var index = iy * this.mapWidth + ix;
|
2017-06-26 12:16:27 +00:00
|
|
|
|
2017-06-22 02:19:03 +00:00
|
|
|
if (index < tiles.length)
|
|
|
|
{
|
|
|
|
return tiles[index];
|
|
|
|
}
|
2017-06-26 12:16:27 +00:00
|
|
|
|
2017-06-22 02:19:03 +00:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
getTileAtIndex: function (index)
|
|
|
|
{
|
|
|
|
var tiles = this.tileArray;
|
2017-06-26 12:16:27 +00:00
|
|
|
|
2017-06-22 02:19:03 +00:00
|
|
|
if (index < tiles.length)
|
|
|
|
{
|
|
|
|
return tiles[index];
|
|
|
|
}
|
2017-06-26 12:16:27 +00:00
|
|
|
|
|
|
|
return null;
|
2017-06-09 04:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-11-09 18:18:23 +00:00
|
|
|
module.exports = DynamicTilemapLayer;
|