phaser/v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js

172 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-06-09 04:00:12 +00:00
var Class = require('../../../utils/Class');
var GameObject = require('../../GameObject');
var Components = require('../../components');
var DynamicTilemapLayerRender = require('./DynamicTilemapLayerRender');
var TilemapComponents = require('../components');
2017-06-09 04:00:12 +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,
DynamicTilemapLayerRender
2017-06-09 04:00:12 +00:00
],
initialize:
function DynamicTilemapLayer (scene, tilemap, layerIndex, tileset, x, y)
2017-06-09 04:00:12 +00:00
{
GameObject.call(this, scene, 'DynamicTilemapLayer');
2017-06-09 04:00:12 +00:00
this.map = tilemap;
this.layerIndex = layerIndex;
this.layer = tilemap.layers[layerIndex];
this.tileset = tileset;
2017-06-09 04:00:12 +00:00
this.culledTiles = [];
this.setTexture(tileset.image.key);
2017-06-09 04:00:12 +00:00
this.setPosition(x, y);
this.setSizeToFrame();
this.setOrigin();
this.setSize(this.map.tileWidth * this.layer.width, this.map.tileheight * this.layer.height);
this.skipIndexZero = false;
2017-06-09 04:00:12 +00:00
},
getTotalTileCount: function ()
2017-06-22 02:19:03 +00:00
{
return this.tileArray.length;
},
getVisibleTileCount: function (camera)
{
return this.cull(camera).length;
},
cull: function (camera)
2017-06-09 04:00:12 +00:00
{
var mapData = this.layer.data;
var mapWidth = this.layer.width;
var mapHeight = this.layer.height;
2017-06-22 02:19:03 +00:00
var culledTiles = this.culledTiles;
var scrollX = camera.scrollX * this.scrollFactorX;
var scrollY = camera.scrollY * this.scrollFactorY;
var cameraW = camera.width;
var cameraH = camera.height;
culledTiles.length = 0;
for (var row = 0; row < mapHeight; ++row)
2017-06-22 02:19:03 +00:00
{
for (var col = 0; col < mapWidth; ++col)
2017-06-22 02:19:03 +00:00
{
var tile = mapData[row][col];
if (tile === null || (tile.index <= 0 && this.skipIndexZero)) { continue; }
var tileX = tile.worldX - scrollX;
var tileY = tile.worldY - scrollY;
var tileW = tile.width;
var tileH = tile.height;
var cullW = cameraW + tileW;
var cullH = cameraH + tileH;
if (tile.visible &&
tileX > -tileW && tileY > -tileH &&
tileX < cullW && tileY < cullH)
{
culledTiles.push(tile);
}
2017-06-22 02:19:03 +00:00
}
}
return culledTiles;
},
forEachTile: function (callback, context, tileX, tileY, width, height)
{
return TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, this.layer);
},
getTileAt: function (tileX, tileY, nonNull)
{
return TilemapComponents.GetTileAt(tileX, tileY, this.layer, nonNull);
},
getTilesWithin: function (tileX, tileY, width, height)
{
return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, this.layer);
},
hasTileAt: function (tileX, tileY)
{
return TilemapComponents.HasTileAt(tileX, tileY, this.layer);
}
2017-06-22 02:19:03 +00:00
// forEach: function (callback)
// {
// this.tileArray.forEach(callback);
// },
2017-06-22 02:19:03 +00:00
2017-06-26 14:08:26 +00:00
// Returns Object containing:
// {
// alpha
// frameWidth,
// frameHeight,
// frameX
// frameY
// id
// 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
// }
// getTileAt: function (x, y)
// {
// var ix = (x|0);
// var iy = (y|0);
// var tiles = this.tileArray;
// var index = iy * this.mapWidth + ix;
// if (index < tiles.length)
// {
// return tiles[index];
// }
// return null;
// },
2017-06-22 02:19:03 +00:00
// getTileAtIndex: function (index)
// {
// var tiles = this.tileArray;
// if (index < tiles.length)
// {
// return tiles[index];
// }
// return null;
// }
2017-06-09 04:00:12 +00:00
});
module.exports = DynamicTilemapLayer;