2017-11-11 16:38:52 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-11-29 04:40:48 +00:00
|
|
|
var Extend = require('../../utils/object/Extend');
|
2017-11-17 19:16:39 +00:00
|
|
|
var LayerData = require('./mapdata/LayerData');
|
2017-11-11 16:38:52 +00:00
|
|
|
var StaticTilemapLayer = require('./staticlayer/StaticTilemapLayer.js');
|
|
|
|
var DynamicTilemapLayer = require('./dynamiclayer/DynamicTilemapLayer.js');
|
|
|
|
var Tileset = require('./Tileset');
|
|
|
|
var Formats = require('./Formats');
|
2017-11-15 19:50:56 +00:00
|
|
|
var TilemapComponents = require('./components');
|
2017-11-16 17:44:24 +00:00
|
|
|
var Tile = require('./Tile');
|
2017-11-29 04:40:48 +00:00
|
|
|
var Rotate = require('../../math/Rotate');
|
|
|
|
var DegToRad = require('../../math/DegToRad');
|
2017-11-11 16:38:52 +00:00
|
|
|
|
|
|
|
var Tilemap = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-11-17 21:55:12 +00:00
|
|
|
function Tilemap (scene, mapData)
|
2017-11-11 16:38:52 +00:00
|
|
|
{
|
|
|
|
this.scene = scene;
|
|
|
|
|
|
|
|
this.tilesets = [];
|
|
|
|
this.tileWidth = mapData.tileWidth;
|
|
|
|
this.tileHeight = mapData.tileHeight;
|
|
|
|
|
|
|
|
this.width = mapData.width;
|
|
|
|
this.height = mapData.height;
|
|
|
|
this.orientation = mapData.orientation;
|
|
|
|
this.format = mapData.format;
|
|
|
|
this.version = mapData.version;
|
|
|
|
this.properties = mapData.properties;
|
|
|
|
this.widthInPixels = mapData.widthInPixels;
|
|
|
|
this.heightInPixels = mapData.heightInPixels;
|
2017-11-24 14:21:09 +00:00
|
|
|
this.imageCollections = mapData.imageCollections;
|
2017-11-24 15:15:02 +00:00
|
|
|
this.images = mapData.images;
|
2017-11-24 14:23:40 +00:00
|
|
|
this.collision = mapData.collision;
|
2017-11-11 16:38:52 +00:00
|
|
|
this.layers = mapData.layers;
|
|
|
|
this.tilesets = mapData.tilesets;
|
|
|
|
this.tiles = mapData.tiles;
|
|
|
|
this.objects = mapData.objects;
|
2017-11-15 21:05:11 +00:00
|
|
|
this.currentLayerIndex = 0;
|
2017-11-11 16:38:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addTilesetImage: function (tilesetName, key, tileWidth, tileHeight, tileMargin, tileSpacing, gid)
|
|
|
|
{
|
|
|
|
if (tilesetName === undefined) { return null; }
|
|
|
|
if (key === undefined || key === null) { key = tilesetName; }
|
|
|
|
|
|
|
|
if (!this.scene.sys.textures.exists(key))
|
|
|
|
{
|
|
|
|
console.warn('Invalid image key given for tileset: "' + key + '"');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var texture = this.scene.sys.textures.get(key);
|
|
|
|
|
|
|
|
var index = this.getTilesetIndex(tilesetName);
|
|
|
|
|
2017-11-17 21:49:32 +00:00
|
|
|
if (index === null && this.format === Formats.TILEMAP_TILED_JSON)
|
2017-11-11 16:38:52 +00:00
|
|
|
{
|
|
|
|
console.warn('No data found in the JSON tilemap from Tiled matching the tileset name: "' + tilesetName + '"');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.tilesets[index])
|
|
|
|
{
|
2017-11-18 21:40:27 +00:00
|
|
|
this.tilesets[index].setTileSize(tileWidth, tileHeight);
|
|
|
|
this.tilesets[index].setSpacing(tileMargin, tileSpacing);
|
2017-11-11 16:38:52 +00:00
|
|
|
this.tilesets[index].setImage(texture);
|
|
|
|
return this.tilesets[index];
|
|
|
|
}
|
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
if (tileWidth === undefined) { tileWidth = this.tileWidth; }
|
|
|
|
if (tileHeight === undefined) { tileHeight = this.tileHeight; }
|
2017-11-18 21:40:27 +00:00
|
|
|
if (tileMargin === undefined) { tileMargin = 0; }
|
|
|
|
if (tileSpacing === undefined) { tileSpacing = 0; }
|
|
|
|
if (gid === undefined) { gid = 0; }
|
2017-11-11 16:38:52 +00:00
|
|
|
|
2017-11-18 21:40:27 +00:00
|
|
|
var tileset = new Tileset(tilesetName, gid, tileWidth, tileHeight, tileMargin, tileSpacing, {});
|
|
|
|
tileset.setImage(texture);
|
|
|
|
this.tilesets.push(tileset);
|
2017-11-11 16:38:52 +00:00
|
|
|
return tileset;
|
|
|
|
},
|
|
|
|
|
2017-11-16 17:44:24 +00:00
|
|
|
// Creates & selects
|
2017-11-17 01:08:58 +00:00
|
|
|
createBlankDynamicLayer: function (name, tileset, x, y, width, height, tileWidth, tileHeight)
|
|
|
|
{
|
2017-11-29 18:52:34 +00:00
|
|
|
if (tileWidth === undefined) { tileWidth = tileset.tileWidth; }
|
|
|
|
if (tileHeight === undefined) { tileHeight = tileset.tileHeight; }
|
2017-11-16 17:44:24 +00:00
|
|
|
if (width === undefined) { width = this.width; }
|
|
|
|
if (height === undefined) { height = this.height; }
|
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
|
|
|
|
var index = this.getLayerIndex(name);
|
|
|
|
|
|
|
|
if (index !== null)
|
|
|
|
{
|
|
|
|
console.warn('Cannot create blank layer: layer with matching name already exists ' + name);
|
2017-11-17 01:08:58 +00:00
|
|
|
return null;
|
2017-11-16 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 19:16:39 +00:00
|
|
|
var layerData = new LayerData({
|
|
|
|
name: name,
|
|
|
|
tileWidth: tileWidth,
|
|
|
|
tileHeight: tileHeight,
|
|
|
|
width: width,
|
|
|
|
height: height
|
|
|
|
});
|
2017-11-16 17:44:24 +00:00
|
|
|
|
|
|
|
var row;
|
|
|
|
for (var tileY = 0; tileY < height; tileY++)
|
|
|
|
{
|
|
|
|
row = [];
|
|
|
|
for (var tileX = 0; tileX < width; tileX++)
|
|
|
|
{
|
2017-11-17 19:16:39 +00:00
|
|
|
row.push(new Tile(layerData, -1, tileX, tileY, tileWidth, tileHeight));
|
2017-11-16 17:44:24 +00:00
|
|
|
}
|
2017-11-17 19:16:39 +00:00
|
|
|
layerData.data.push(row);
|
2017-11-16 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 19:16:39 +00:00
|
|
|
this.layers.push(layerData);
|
2017-11-16 17:44:24 +00:00
|
|
|
this.currentLayerIndex = this.layers.length - 1;
|
|
|
|
|
|
|
|
var dynamicLayer = new DynamicTilemapLayer(this.scene, this, this.currentLayerIndex, tileset, x, y);
|
|
|
|
this.scene.sys.displayList.add(dynamicLayer);
|
2017-11-17 21:55:12 +00:00
|
|
|
|
2017-11-16 17:44:24 +00:00
|
|
|
return dynamicLayer;
|
|
|
|
},
|
|
|
|
|
2017-11-29 04:40:48 +00:00
|
|
|
/**
|
|
|
|
* Creates a Sprite for every object matching the given gid in the map data. All properties from
|
|
|
|
* the map data objectgroup are copied into the `spriteConfig`, so you can use this as an easy
|
|
|
|
* way to configure Sprite properties from within the map editor. For example giving an object a
|
|
|
|
* property of alpha: 0.5 in the map editor will duplicate that when the Sprite is created.
|
|
|
|
*
|
|
|
|
* @param {string} name - The name of the object layer (from Tiled) to create Sprites from.
|
2017-11-29 21:07:56 +00:00
|
|
|
* @param {integer|string} id - Either the id (object), gid (tile object) or name (object or
|
|
|
|
* tile object) from Tiled. Ids are unique in Tiled, but a gid is shared by all tile objects
|
|
|
|
* with the same graphic. The same name can be used on multiple objects.
|
2017-11-29 04:40:48 +00:00
|
|
|
* @param {object} spriteConfig - The config object to pass into the Sprite creator (i.e.
|
|
|
|
* scene.make.sprite).
|
|
|
|
* @param {Scene} [scene=the scene the map is within] - The Scene to create the Sprites within.
|
2017-11-29 21:07:56 +00:00
|
|
|
* @return {Sprite[]} An array of the Sprites that were created.
|
2017-11-29 04:40:48 +00:00
|
|
|
*/
|
|
|
|
createFromObjects: function (name, id, spriteConfig, scene)
|
|
|
|
{
|
|
|
|
if (spriteConfig === undefined) { spriteConfig = {}; }
|
|
|
|
if (scene === undefined) { scene = this.scene; }
|
|
|
|
|
|
|
|
if (!this.objects[name])
|
|
|
|
{
|
|
|
|
console.warn('Cannot create from object. Invalid objectgroup name given: ' + name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-29 13:35:26 +00:00
|
|
|
var sprites = [];
|
|
|
|
|
2017-11-29 04:40:48 +00:00
|
|
|
for (var i = 0; i < this.objects[name].length; i++)
|
|
|
|
{
|
|
|
|
var found = false;
|
|
|
|
var obj = this.objects[name][i];
|
|
|
|
|
|
|
|
if (obj.gid !== undefined && typeof id === 'number' && obj.gid === id ||
|
|
|
|
obj.id !== undefined && typeof id === 'number' && obj.id === id ||
|
|
|
|
obj.name !== undefined && typeof id === 'string' && obj.name === id)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
Extend(spriteConfig, obj.properties);
|
|
|
|
|
|
|
|
spriteConfig.x = obj.x;
|
|
|
|
spriteConfig.y = obj.y;
|
|
|
|
|
|
|
|
var sprite = this.scene.make.sprite(spriteConfig);
|
|
|
|
|
|
|
|
sprite.name = obj.name;
|
|
|
|
|
|
|
|
if (obj.width) { sprite.displayWidth = obj.width; }
|
|
|
|
if (obj.height) { sprite.displayHeight = obj.height; }
|
|
|
|
|
2017-11-29 13:25:04 +00:00
|
|
|
// Origin is (0, 1) in Tiled, so find the offset that matches the Sprite's origin.
|
|
|
|
var offset = {
|
|
|
|
x: sprite.originX * sprite.displayWidth,
|
|
|
|
y: (sprite.originY - 1) * sprite.displayHeight
|
|
|
|
};
|
2017-11-29 04:40:48 +00:00
|
|
|
|
2017-11-29 13:25:04 +00:00
|
|
|
// If the object is rotated, then the origin offset also needs to be rotated.
|
2017-11-29 04:40:48 +00:00
|
|
|
if (obj.rotation)
|
|
|
|
{
|
|
|
|
var angle = DegToRad(obj.rotation);
|
|
|
|
Rotate(offset, angle);
|
|
|
|
sprite.rotation = angle;
|
|
|
|
}
|
|
|
|
|
2017-11-29 13:25:04 +00:00
|
|
|
sprite.x += offset.x;
|
|
|
|
sprite.y += offset.y;
|
|
|
|
|
2017-11-29 04:40:48 +00:00
|
|
|
if (obj.flippedHorizontal !== undefined || obj.flippedVertical !== undefined)
|
|
|
|
{
|
|
|
|
sprite.setFlip(obj.flippedHorizontal, obj.flippedVertical);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!obj.visible) { sprite.visible = false; }
|
2017-11-29 13:35:26 +00:00
|
|
|
|
|
|
|
sprites.push(sprite);
|
2017-11-29 04:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 13:35:26 +00:00
|
|
|
return sprites;
|
2017-11-29 04:40:48 +00:00
|
|
|
},
|
|
|
|
|
2017-11-29 17:59:25 +00:00
|
|
|
// Creates & selects, uses layer offset if x,y undefined
|
2017-11-11 16:38:52 +00:00
|
|
|
createStaticLayer: function (layerID, tileset, x, y)
|
|
|
|
{
|
2017-11-15 19:50:56 +00:00
|
|
|
var index = this.getLayerIndex(layerID);
|
2017-11-11 16:38:52 +00:00
|
|
|
|
2017-11-15 19:50:56 +00:00
|
|
|
if (index === null)
|
2017-11-11 16:38:52 +00:00
|
|
|
{
|
2017-11-17 01:55:17 +00:00
|
|
|
console.warn('Cannot create tilemap layer, invalid layer ID given: ' + layerID);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
var layerData = this.layers[index];
|
|
|
|
|
2017-11-17 01:55:17 +00:00
|
|
|
// Check for an associated static or dynamic tilemap layer
|
2017-11-29 18:52:34 +00:00
|
|
|
if (layerData.tilemapLayer)
|
2017-11-17 01:55:17 +00:00
|
|
|
{
|
|
|
|
console.warn('Cannot create static tilemap layer since a static or dynamic tilemap layer exists for layer ID:' + layerID);
|
2017-11-17 01:08:58 +00:00
|
|
|
return null;
|
2017-11-11 16:38:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 17:44:24 +00:00
|
|
|
this.currentLayerIndex = index;
|
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
// Make sure that all the LayerData & the tiles have the correct tile size. They usually
|
|
|
|
// are, but wouldn't match if you try to load a 2x or 4x res tileset when the map was made
|
|
|
|
// with a 1x res tileset.
|
|
|
|
if (layerData.tileWidth !== tileset.tileWidth || layerData.tileHeight !== tileset.tileHeight)
|
|
|
|
{
|
|
|
|
this.setLayerTileSize(tileset.tileWidth, tileset.tileHeight, index);
|
|
|
|
}
|
|
|
|
|
2017-11-29 17:59:25 +00:00
|
|
|
// Default the x/y position to match Tiled layer offset, if it exists.
|
|
|
|
if (x === undefined && this.layers[index].x) { x = this.layers[index].x; }
|
|
|
|
if (y === undefined && this.layers[index].y) { y = this.layers[index].y; }
|
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
var layer = new StaticTilemapLayer(this.scene, this, index, tileset, x, y);
|
|
|
|
this.scene.sys.displayList.add(layer);
|
2017-11-29 18:52:34 +00:00
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
return layer;
|
|
|
|
},
|
|
|
|
|
2017-11-29 17:59:25 +00:00
|
|
|
// Creates & selects, uses layer offset if x,y undefined
|
2017-11-11 16:38:52 +00:00
|
|
|
createDynamicLayer: function (layerID, tileset, x, y)
|
|
|
|
{
|
2017-11-15 19:50:56 +00:00
|
|
|
var index = this.getLayerIndex(layerID);
|
2017-11-11 16:38:52 +00:00
|
|
|
|
2017-11-15 19:50:56 +00:00
|
|
|
if (index === null)
|
2017-11-11 16:38:52 +00:00
|
|
|
{
|
2017-11-17 01:55:17 +00:00
|
|
|
console.warn('Cannot create tilemap layer, invalid layer ID given: ' + layerID);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
var layerData = this.layers[index];
|
|
|
|
|
2017-11-17 01:55:17 +00:00
|
|
|
// Check for an associated static or dynamic tilemap layer
|
2017-11-29 18:52:34 +00:00
|
|
|
if (layerData.tilemapLayer)
|
2017-11-17 01:55:17 +00:00
|
|
|
{
|
|
|
|
console.warn('Cannot create dynamic tilemap layer since a static or dynamic tilemap layer exists for layer ID:' + layerID);
|
2017-11-17 01:08:58 +00:00
|
|
|
return null;
|
2017-11-11 16:38:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 17:44:24 +00:00
|
|
|
this.currentLayerIndex = index;
|
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
// Make sure that all the LayerData & the tiles have the correct tile size. They usually
|
|
|
|
// are, but wouldn't match if you try to load a 2x or 4x res tileset when the map was made
|
|
|
|
// with a 1x res tileset.
|
|
|
|
if (layerData.tileWidth !== tileset.tileWidth || layerData.tileHeight !== tileset.tileHeight)
|
|
|
|
{
|
|
|
|
this.setLayerTileSize(tileset.tileWidth, tileset.tileHeight, index);
|
|
|
|
}
|
|
|
|
|
2017-11-29 17:59:25 +00:00
|
|
|
// Default the x/y position to match Tiled layer offset, if it exists.
|
|
|
|
if (x === undefined && this.layers[index].x) { x = this.layers[index].x; }
|
|
|
|
if (y === undefined && this.layers[index].y) { y = this.layers[index].y; }
|
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
var layer = new DynamicTilemapLayer(this.scene, this, index, tileset, x, y);
|
|
|
|
this.scene.sys.displayList.add(layer);
|
2017-11-29 18:52:34 +00:00
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
return layer;
|
2017-11-15 19:50:56 +00:00
|
|
|
},
|
|
|
|
|
2017-11-29 17:36:36 +00:00
|
|
|
convertLayerToStatic: function (layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
|
|
|
|
|
|
|
var dynamicLayer = layer.tilemapLayer;
|
|
|
|
|
|
|
|
if (!dynamicLayer || !(dynamicLayer instanceof DynamicTilemapLayer))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var staticLayer = new StaticTilemapLayer(dynamicLayer.scene, dynamicLayer.map,
|
|
|
|
dynamicLayer.layerIndex, dynamicLayer.tileset, dynamicLayer.x, dynamicLayer.y);
|
|
|
|
this.scene.sys.displayList.add(staticLayer);
|
|
|
|
|
|
|
|
dynamicLayer.destroy();
|
|
|
|
|
|
|
|
return staticLayer;
|
|
|
|
},
|
|
|
|
|
2017-11-26 14:58:38 +00:00
|
|
|
copy: function (srcTileX, srcTileY, width, height, destTileX, destTileY, recalculateFaces, layer)
|
2017-11-15 22:36:41 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-17 01:58:35 +00:00
|
|
|
if (this._isStaticCall(layer, 'copy')) { return this; }
|
2017-11-16 02:06:07 +00:00
|
|
|
if (layer !== null)
|
|
|
|
{
|
2017-11-26 14:58:38 +00:00
|
|
|
TilemapComponents.Copy(srcTileX, srcTileY, width, height, destTileX, destTileY, recalculateFaces, layer);
|
2017-11-16 02:06:07 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-29 02:49:24 +00:00
|
|
|
createFromTiles: function (indexes, replacements, spriteConfig, scene, camera, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
|
|
|
return TilemapComponents.CreateFromTiles(indexes, replacements, spriteConfig, scene, camera, layer);
|
|
|
|
},
|
|
|
|
|
2017-11-17 01:09:28 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.layers.length = 0;
|
|
|
|
this.tilesets.length = 0;
|
|
|
|
this.tiles.length = 0;
|
|
|
|
this.objects.length = 0;
|
|
|
|
this.scene = undefined;
|
|
|
|
},
|
|
|
|
|
2017-11-26 15:19:57 +00:00
|
|
|
fill: function (index, tileX, tileY, width, height, recalculateFaces, layer)
|
2017-11-16 02:06:07 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-17 01:58:35 +00:00
|
|
|
if (this._isStaticCall(layer, 'fill')) { return this; }
|
2017-11-16 02:06:07 +00:00
|
|
|
if (layer !== null)
|
|
|
|
{
|
2017-11-26 15:19:57 +00:00
|
|
|
TilemapComponents.Fill(index, tileX, tileY, width, height, recalculateFaces, layer);
|
2017-11-16 02:06:07 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2017-11-15 22:36:41 +00:00
|
|
|
|
2017-11-29 02:49:24 +00:00
|
|
|
filterTiles: function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
|
|
|
return TilemapComponents.FilterTiles(callback, context, tileX, tileY, width, height, filteringOptions, layer);
|
|
|
|
},
|
|
|
|
|
2017-11-17 01:08:58 +00:00
|
|
|
findByIndex: function (findIndex, skip, reverse, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
|
|
|
return TilemapComponents.FindByIndex(findIndex, skip, reverse, layer);
|
|
|
|
},
|
|
|
|
|
2017-11-26 00:03:21 +00:00
|
|
|
forEachTile: function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
|
2017-11-16 02:06:07 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer !== null)
|
|
|
|
{
|
2017-11-26 00:03:21 +00:00
|
|
|
TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, filteringOptions, layer);
|
2017-11-16 02:06:07 +00:00
|
|
|
}
|
|
|
|
return this;
|
2017-11-15 22:36:41 +00:00
|
|
|
},
|
|
|
|
|
2017-11-24 15:15:02 +00:00
|
|
|
getImageIndex: function (name)
|
|
|
|
{
|
|
|
|
return this.getIndex(this.images, name);
|
|
|
|
},
|
|
|
|
|
2017-11-15 20:55:26 +00:00
|
|
|
getIndex: function (location, name)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < location.length; i++)
|
|
|
|
{
|
|
|
|
if (location[i].name === name)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
getLayer: function (layer)
|
|
|
|
{
|
|
|
|
var index = this.getLayerIndex(layer);
|
|
|
|
return index !== null ? this.layers[index] : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
getLayerIndex: function (layer)
|
|
|
|
{
|
|
|
|
if (layer === undefined)
|
|
|
|
{
|
2017-11-15 21:05:11 +00:00
|
|
|
return this.currentLayerIndex;
|
2017-11-15 20:55:26 +00:00
|
|
|
}
|
|
|
|
else if (typeof layer === 'string')
|
|
|
|
{
|
|
|
|
return this.getLayerIndexByName(layer);
|
|
|
|
}
|
|
|
|
else if (typeof layer === 'number' && layer < this.layers.length)
|
|
|
|
{
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
else if (layer instanceof StaticTilemapLayer || layer instanceof DynamicTilemapLayer)
|
|
|
|
{
|
|
|
|
return layer.layerIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getLayerIndexByName: function (name)
|
|
|
|
{
|
|
|
|
return this.getIndex(this.layers, name);
|
|
|
|
},
|
|
|
|
|
2017-11-16 19:09:07 +00:00
|
|
|
getTileAt: function (tileX, tileY, nonNull, layer)
|
2017-11-15 19:50:56 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
2017-11-16 19:09:07 +00:00
|
|
|
return TilemapComponents.GetTileAt(tileX, tileY, nonNull, layer);
|
2017-11-15 20:55:26 +00:00
|
|
|
},
|
|
|
|
|
2017-11-17 13:58:33 +00:00
|
|
|
getTileAtWorldXY: function (worldX, worldY, nonNull, camera, layer)
|
2017-11-16 19:27:52 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
2017-11-17 13:58:33 +00:00
|
|
|
return TilemapComponents.GetTileAtWorldXY(worldX, worldY, nonNull, camera, layer);
|
2017-11-16 19:27:52 +00:00
|
|
|
},
|
|
|
|
|
2017-11-26 00:03:21 +00:00
|
|
|
getTilesWithin: function (tileX, tileY, width, height, filteringOptions, layer)
|
2017-11-15 22:36:41 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
2017-11-26 00:03:21 +00:00
|
|
|
return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, filteringOptions, layer);
|
2017-11-15 22:36:41 +00:00
|
|
|
},
|
|
|
|
|
2017-11-26 13:55:44 +00:00
|
|
|
getTilesWithinShape: function (shape, filteringOptions, camera, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
|
|
|
return TilemapComponents.GetTilesWithinShape(shape, filteringOptions, camera, layer);
|
|
|
|
},
|
|
|
|
|
2017-11-26 00:03:21 +00:00
|
|
|
getTilesWithinWorldXY: function (worldX, worldY, width, height, filteringOptions, camera, layer)
|
2017-11-25 14:42:19 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
2017-11-26 00:03:21 +00:00
|
|
|
return TilemapComponents.GetTilesWithinWorldXY(worldX, worldY, width, height, filteringOptions, camera, layer);
|
2017-11-25 14:42:19 +00:00
|
|
|
},
|
|
|
|
|
2017-11-15 20:55:26 +00:00
|
|
|
getTilesetIndex: function (name)
|
|
|
|
{
|
|
|
|
return this.getIndex(this.tilesets, name);
|
|
|
|
},
|
|
|
|
|
2017-11-15 21:28:15 +00:00
|
|
|
hasTileAt: function (tileX, tileY, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
|
|
|
return TilemapComponents.HasTileAt(tileX, tileY, layer);
|
|
|
|
},
|
|
|
|
|
2017-11-17 13:58:33 +00:00
|
|
|
hasTileAtWorldXY: function (worldX, worldY, camera, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
|
|
|
return TilemapComponents.HasTileAtWorldXY(worldX, worldY, camera, layer);
|
|
|
|
},
|
|
|
|
|
2017-11-15 20:55:26 +00:00
|
|
|
layer: {
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-11-15 21:05:11 +00:00
|
|
|
return this.layers[this.currentLayerIndex];
|
2017-11-15 20:55:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (layer)
|
|
|
|
{
|
|
|
|
this.setLayer(layer);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-11-22 01:18:34 +00:00
|
|
|
putTileAt: function (tile, tileX, tileY, recalculateFaces, layer)
|
2017-11-16 19:09:07 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-18 21:41:26 +00:00
|
|
|
if (this._isStaticCall(layer, 'putTileAt')) { return null; }
|
2017-11-16 19:09:07 +00:00
|
|
|
if (layer === null) { return null; }
|
2017-11-22 01:18:34 +00:00
|
|
|
return TilemapComponents.PutTileAt(tile, tileX, tileY, recalculateFaces, layer);
|
2017-11-16 19:09:07 +00:00
|
|
|
},
|
|
|
|
|
2017-11-22 01:18:34 +00:00
|
|
|
putTileAtWorldXY: function (tile, worldX, worldY, recalculateFaces, camera, layer)
|
2017-11-17 13:58:33 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-18 21:41:26 +00:00
|
|
|
if (this._isStaticCall(layer, 'putTileAtWorldXY')) { return null; }
|
2017-11-17 13:58:33 +00:00
|
|
|
if (layer === null) { return null; }
|
2017-11-22 01:18:34 +00:00
|
|
|
return TilemapComponents.PutTileAtWorldXY(tile, worldX, worldY, recalculateFaces, camera, layer);
|
2017-11-17 13:58:33 +00:00
|
|
|
},
|
|
|
|
|
2017-11-29 19:22:30 +00:00
|
|
|
randomize: function (tileX, tileY, width, height, indexes, layer)
|
2017-11-16 02:06:07 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-17 01:58:35 +00:00
|
|
|
if (this._isStaticCall(layer, 'randomize')) { return this; }
|
2017-11-16 02:06:07 +00:00
|
|
|
if (layer !== null)
|
|
|
|
{
|
2017-11-29 19:22:30 +00:00
|
|
|
TilemapComponents.Randomize(tileX, tileY, width, height, indexes, layer);
|
2017-11-16 02:06:07 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-22 01:18:34 +00:00
|
|
|
calculateFacesWithin: function (tileX, tileY, width, height, layer)
|
2017-11-21 02:06:49 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return this; }
|
2017-11-22 01:18:34 +00:00
|
|
|
TilemapComponents.CalculateFacesWithin(tileX, tileY, width, height, layer);
|
2017-11-21 02:06:49 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-17 01:09:28 +00:00
|
|
|
removeAllLayers: function ()
|
|
|
|
{
|
|
|
|
this.layers.length = 0;
|
|
|
|
this.currentLayerIndex = 0;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-22 01:18:34 +00:00
|
|
|
removeTileAt: function (tileX, tileY, replaceWithNull, recalculateFaces, layer)
|
2017-11-16 19:09:07 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-18 21:41:26 +00:00
|
|
|
if (this._isStaticCall(layer, 'removeTileAt')) { return null; }
|
2017-11-16 19:09:07 +00:00
|
|
|
if (layer === null) { return null; }
|
2017-11-22 01:18:34 +00:00
|
|
|
return TilemapComponents.RemoveTileAt(tileX, tileY, replaceWithNull, recalculateFaces, layer);
|
2017-11-16 19:09:07 +00:00
|
|
|
},
|
|
|
|
|
2017-11-22 01:18:34 +00:00
|
|
|
removeTileAtWorldXY: function (worldX, worldY, replaceWithNull, recalculateFaces, camera, layer)
|
2017-11-17 13:58:33 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-18 21:41:26 +00:00
|
|
|
if (this._isStaticCall(layer, 'removeTileAtWorldXY')) { return null; }
|
2017-11-17 13:58:33 +00:00
|
|
|
if (layer === null) { return null; }
|
2017-11-22 01:18:34 +00:00
|
|
|
return TilemapComponents.RemoveTileAtWorldXY(worldX, worldY, replaceWithNull, recalculateFaces, camera, layer);
|
2017-11-17 13:58:33 +00:00
|
|
|
},
|
|
|
|
|
2017-11-29 15:05:53 +00:00
|
|
|
renderDebug: function (graphics, styleConfig, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return this; }
|
|
|
|
TilemapComponents.RenderDebug(graphics, styleConfig, layer);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-17 01:08:58 +00:00
|
|
|
replaceByIndex: function (findIndex, newIndex, tileX, tileY, width, height, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-17 01:58:35 +00:00
|
|
|
if (this._isStaticCall(layer, 'replaceByIndex')) { return this; }
|
2017-11-17 01:08:58 +00:00
|
|
|
if (layer !== null)
|
|
|
|
{
|
|
|
|
TilemapComponents.ReplaceByIndex(findIndex, newIndex, tileX, tileY, width, height, layer);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-21 02:06:49 +00:00
|
|
|
setCollision: function (indexes, collides, recalculateFaces, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return this; }
|
|
|
|
TilemapComponents.SetCollision(indexes, collides, recalculateFaces, layer);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCollisionBetween: function (start, stop, collides, recalculateFaces, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return this; }
|
|
|
|
TilemapComponents.SetCollisionBetween(start, stop, collides, recalculateFaces, layer);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCollisionByExclusion: function (indexes, collides, recalculateFaces, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return this; }
|
|
|
|
TilemapComponents.SetCollisionByExclusion(indexes, collides, recalculateFaces, layer);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-29 13:53:04 +00:00
|
|
|
setTileIndexCallback: function (indexes, callback, callbackContext, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return this; }
|
|
|
|
TilemapComponents.SetTileIndexCallback(indexes, callback, callbackContext, layer);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-29 14:20:24 +00:00
|
|
|
setTileLocationCallback: function (tileX, tileY, width, height, callback, callbackContext, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return this; }
|
|
|
|
TilemapComponents.SetTileLocationCallback(tileX, tileY, width, height, callback, callbackContext, layer);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-11-15 20:55:26 +00:00
|
|
|
setLayer: function (layer)
|
|
|
|
{
|
|
|
|
var index = this.getLayerIndex(layer);
|
|
|
|
if (index !== null)
|
|
|
|
{
|
2017-11-15 21:05:11 +00:00
|
|
|
this.currentLayerIndex = index;
|
2017-11-15 20:55:26 +00:00
|
|
|
}
|
|
|
|
return this;
|
2017-11-16 02:06:07 +00:00
|
|
|
},
|
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
setBaseTileSize: function (tileWidth, tileHeight)
|
2017-11-17 01:09:28 +00:00
|
|
|
{
|
|
|
|
this.tileWidth = tileWidth;
|
|
|
|
this.tileHeight = tileHeight;
|
|
|
|
this.widthInPixels = this.width * tileWidth;
|
|
|
|
this.heightInPixels = this.height * tileHeight;
|
2017-11-29 18:52:34 +00:00
|
|
|
},
|
2017-11-18 21:40:27 +00:00
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
// Sets the tile size for a given layer
|
|
|
|
setLayerTileSize: function (tileWidth, tileHeight, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return this; }
|
|
|
|
|
|
|
|
layer.tileWidth = tileWidth;
|
|
|
|
layer.tileHeight = tileHeight;
|
2017-11-18 21:40:27 +00:00
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
var mapData = layer.data;
|
|
|
|
var mapWidth = layer.width;
|
|
|
|
var mapHeight = layer.height;
|
2017-11-18 21:40:27 +00:00
|
|
|
|
2017-11-29 18:52:34 +00:00
|
|
|
for (var row = 0; row < mapHeight; ++row)
|
|
|
|
{
|
|
|
|
for (var col = 0; col < mapWidth; ++col)
|
2017-11-18 21:40:27 +00:00
|
|
|
{
|
2017-11-29 18:52:34 +00:00
|
|
|
var tile = mapData[row][col];
|
|
|
|
if (tile !== null) { tile.setSize(tileWidth, tileHeight); }
|
2017-11-18 21:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
2017-11-17 01:09:28 +00:00
|
|
|
},
|
|
|
|
|
2017-11-16 02:06:07 +00:00
|
|
|
shuffle: function (tileX, tileY, width, height, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-17 01:58:35 +00:00
|
|
|
if (this._isStaticCall(layer, 'shuffle')) { return this; }
|
2017-11-16 02:06:07 +00:00
|
|
|
if (layer !== null)
|
|
|
|
{
|
|
|
|
TilemapComponents.Shuffle(tileX, tileY, width, height, layer);
|
|
|
|
}
|
|
|
|
return this;
|
2017-11-17 01:08:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
swapByIndex: function (indexA, indexB, tileX, tileY, width, height, layer)
|
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
2017-11-17 01:58:35 +00:00
|
|
|
if (this._isStaticCall(layer, 'swapByIndex')) { return this; }
|
2017-11-17 01:08:58 +00:00
|
|
|
if (layer !== null)
|
|
|
|
{
|
|
|
|
TilemapComponents.SwapByIndex(indexA, indexB, tileX, tileY, width, height, layer);
|
|
|
|
}
|
|
|
|
return this;
|
2017-11-17 01:58:35 +00:00
|
|
|
},
|
|
|
|
|
2017-11-25 13:08:06 +00:00
|
|
|
worldToTileX: function (worldX, snapToFloor, camera, layer)
|
2017-11-17 02:36:45 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
2017-11-25 13:08:06 +00:00
|
|
|
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, layer);
|
2017-11-17 02:36:45 +00:00
|
|
|
},
|
|
|
|
|
2017-11-25 13:08:06 +00:00
|
|
|
worldToTileY: function (worldY, snapToFloor, camera, layer)
|
2017-11-17 02:36:45 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
2017-11-25 13:08:06 +00:00
|
|
|
return TilemapComponents.WorldToTileY(worldY, snapToFloor, camera, layer);
|
2017-11-17 02:36:45 +00:00
|
|
|
},
|
|
|
|
|
2017-11-25 13:08:06 +00:00
|
|
|
worldToTileXY: function (worldX, worldY, snapToFloor, point, camera, layer)
|
2017-11-17 02:36:45 +00:00
|
|
|
{
|
|
|
|
layer = this.getLayer(layer);
|
|
|
|
if (layer === null) { return null; }
|
2017-11-25 13:08:06 +00:00
|
|
|
return TilemapComponents.WorldToTileXY(worldX, worldY, snapToFloor, point, camera, layer);
|
2017-11-17 02:36:45 +00:00
|
|
|
},
|
|
|
|
|
2017-11-17 01:58:35 +00:00
|
|
|
_isStaticCall: function (layer, functionName)
|
|
|
|
{
|
|
|
|
if (layer.tilemapLayer instanceof StaticTilemapLayer)
|
|
|
|
{
|
|
|
|
console.warn(functionName + ': You cannot change the tiles in a static tilemap layer');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-11 16:38:52 +00:00
|
|
|
}
|
2017-11-15 20:55:26 +00:00
|
|
|
|
2017-11-11 16:38:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Tilemap;
|