Phaser.Tilemap = function (game, key){ this.game = game; this.key = key; var data = Phaser.TilemapParser.parse(this.game, key); if (data === null ) { return ; } this.width = data.width; this.height = data.height; this.tileWidth = data.tileWidth; this.tileHeight = data.tileHeight; this.orientation = data.orientation; this.version = data.version; this.properties = data.properties; this.widthInPixels = data.widthInPixels; this.heightInPixels = data.heightInPixels; this.layers = data.layers; this.tilesets = data.tilesets; this.tiles = data.tiles; this.objects = data.objects; this.collision = data.collision; this.images = data.images; this.currentLayer = 0; this.debugMap = [] ; this._results = [] ; this._tempA = 0; this._tempB = 0; } ; Phaser.Tilemap.CSV = 0; Phaser.Tilemap.TILED_JSON = 1; Phaser.Tilemap.prototype = { create: function (name, width, height){ var data = [] ; for (var y = 0; y < height; y++ ){ data[y] = [] ; for (var x = 0; x < width; x++ ){ data[y][x] = 0; } } this.layers.push({ name: name, width: width, height: height, alpha: 1, visible: true , tileMargin: 0, tileSpacing: 0, format: Phaser.Tilemap.CSV, data: data, indexes: [] , dirty: true } ); this.currentLayer = _AN_Read_length('length', this.layers) - 1; } , addTilesetImage: function (tileset, key){ if (typeof key === 'undefined') { if (typeof tileset === 'string') { key = tileset; } else { return false ; } } if (typeof tileset === 'string') { tileset = this.getTilesetIndex(tileset); } if (this.tilesets[tileset]) { this.tilesets[tileset].image = this.game.cache.getImage(key); return true ; } return false ; } , createFromObjects: function (name, gid, key, frame, exists, autoCull, group){ if (typeof exists === 'undefined') { exists = true ; } if (typeof autoCull === 'undefined') { autoCull = true ; } if (typeof group === 'undefined') { group = this.game.world; } if (!this.objects[name]) { console.warn('Tilemap.createFromObjects: Invalid objectgroup name given: ' + name); return ; } var sprite; for (var i = 0, len = _AN_Read_length('length', this.objects[name]); i < len; i++ ){ if (this.objects[name][i].gid === gid) { sprite = group.create(this.objects[name][i].x, this.objects[name][i].y, key, frame, exists); sprite.anchor.setTo(0, 1); sprite.name = this.objects[name][i].name; sprite.visible = this.objects[name][i].visible; sprite.autoCull = autoCull; for (property in this.objects[name][i].properties){ group.set(sprite, property, this.objects[name][i].properties[property], false , false , 0); } } } } , clearPhysicsBodies: function (layer){ layer = this.getLayer(layer); var i = _AN_Read_length('length', this.layers[layer].bodies); while (i-- ){ this.layers[layer].bodies[i].destroy(); } } , generateCollisionData: function (layer, addToWorld){ layer = this.getLayer(layer); if (typeof addToWorld === 'undefined') { addToWorld = true ; } if (_AN_Read_length('length', this.layers[layer].bodies) > 0) { this.clearPhysicsBodies(layer); } this.layers[layer].bodies.length = [] ; var width = 0; var sx = 0; var sy = 0; for (var y = 0, h = this.layers[layer].height; y < h; y++ ){ width = 0; for (var x = 0, w = this.layers[layer].width; x < w; x++ ){ var tile = this.layers[layer].data[y][x]; if (tile) { right = this.getTileRight(layer, x, y); if (width === 0) { sx = tile.x * tile.width; sy = tile.y * tile.height; width = tile.width; } if (right && right.collides) { width += tile.width; } else { var body = this.game.physics.createBody(sx, sy, 0, false ); body.addRectangle(width, tile.height, width / 2, tile.height / 2, 0); if (addToWorld) { this.game.physics.addBody(body); } this.layers[layer].bodies.push(body); width = 0; } } } } return this.layers[layer].bodies; } , createCollisionObjects: function (layer, addToWorld){ if (typeof addToWorld === 'undefined') { addToWorld = true ; } var output = [] ; for (var i = 0, len = _AN_Read_length('length', this.collision[layer]); i < len; i++ ){ var object = this.collision[layer][i]; var body = this.game.physics.createBody(object.x, object.y, 0, addToWorld, { } , object.polyline); if (body) { output.push(body); } } return output; } , createLayer: function (layer, width, height, group){ if (typeof width === 'undefined') { width = this.game.width; } if (typeof height === 'undefined') { height = this.game.height; } if (typeof group === 'undefined') { group = this.game.world; } var index = layer; if (typeof layer === 'string') { index = this.getLayerIndex(layer); } if (index === null || index > _AN_Read_length('length', this.layers)) { console.warn('Tilemap.createLayer: Invalid layer ID given: ' + index); return ; } return group.add(new Phaser.TilemapLayer(this.game, this, index, width, height)); } , getIndex: function (location, name){ for (var i = 0; i < _AN_Read_length('length', _AN_Read_location('location', window)); i++ ){ if (location[i].name === name) { return i; } } return null ; } , getLayerIndex: function (name){ return this.getIndex(this.layers, name); } , getTilesetIndex: function (name){ return this.getIndex(this.tilesets, name); } , getImageIndex: function (name){ return this.getIndex(this.images, name); } , getObjectIndex: function (name){ return this.getIndex(this.objects, name); } , setTileIndexCallback: function (indexes, callback, callbackContext, layer){ layer = this.getLayer(layer); if (typeof indexes === 'number') { this.layers[layer].callbacks[indexes] = { callback: callback, callbackContext: callbackContext} ; } else { for (var i = 0, len = _AN_Read_length('length', indexes); i < len; i++ ){ this.layers[layer].callbacks[indexes[i]] = { callback: callback, callbackContext: callbackContext} ; } } } , setTileLocationCallback: function (x, y, width, height, callback, callbackContext, layer){ layer = this.getLayer(layer); this.copy(x, y, width, height, layer); if (_AN_Read_length('length', this._results) < 2) { return ; } for (var i = 1; i < _AN_Read_length('length', this._results); i++ ){ this._results[i].setCollisionCallback(callback, callbackContext); } } , setCollision: function (indexes, collides, layer){ if (typeof collides === 'undefined') { collides = true ; } layer = this.getLayer(layer); if (typeof indexes === 'number') { return this.setCollisionByIndex(indexes, collides, layer, true ); } else { for (var i = 0, len = _AN_Read_length('length', indexes); i < len; i++ ){ this.setCollisionByIndex(indexes[i], collides, layer, false ); } this.calculateFaces(layer); } } , setCollisionBetween: function (start, stop, collides, layer){ if (typeof collides === 'undefined') { collides = true ; } layer = this.getLayer(layer); if (start > stop) { return ; } for (var index = start; index <= stop; index++ ){ this.setCollisionByIndex(index, collides, layer, false ); } this.calculateFaces(layer); } , setCollisionByExclusion: function (indexes, collides, layer){ if (typeof collides === 'undefined') { collides = true ; } layer = this.getLayer(layer); for (var i = 0, len = _AN_Read_length('length', this.tiles); i < len; i++ ){ if (indexes.indexOf(i) === -1) { this.setCollisionByIndex(i, collides, layer, false ); } } this.calculateFaces(layer); } , setCollisionByIndex: function (index, collides, layer, recalculate){ if (typeof collides === 'undefined') { collides = true ; } if (typeof layer === 'undefined') { layer = this.currentLayer; } if (typeof recalculate === 'undefined') { recalculate = true ; } for (var y = 0; y < this.layers[layer].height; y++ ){ for (var x = 0; x < this.layers[layer].width; x++ ){ var tile = this.layers[layer].data[y][x]; if (tile && tile.index === index) { tile.collides = collides; tile.faceTop = collides; tile.faceBottom = collides; tile.faceLeft = collides; tile.faceRight = collides; } } } if (recalculate) { this.calculateFaces(layer); } return layer; } , getLayer: function (layer){ if (typeof layer === 'undefined') { layer = this.currentLayer; } else if (typeof layer === 'string') { layer = this.getLayerIndex(layer); } else if (layer instanceof Phaser.TilemapLayer) { layer = layer.index; } return layer; } , calculateFaces: function (layer){ var above = null ; var below = null ; var left = null ; var right = null ; for (var y = 0, h = this.layers[layer].height; y < h; y++ ){ for (var x = 0, w = this.layers[layer].width; x < w; x++ ){ var tile = this.layers[layer].data[y][x]; if (tile) { above = this.getTileAbove(layer, x, y); below = this.getTileBelow(layer, x, y); left = this.getTileLeft(layer, x, y); right = this.getTileRight(layer, x, y); if (above && above.collides) { tile.faceTop = false ; } if (below && below.collides) { tile.faceBottom = false ; } if (left && left.collides) { tile.faceLeft = false ; } if (right && right.collides) { tile.faceRight = false ; } } } } } , getTileAbove: function (layer, x, y){ if (y > 0) { return this.layers[layer].data[y - 1][x]; } return null ; } , getTileBelow: function (layer, x, y){ if (y < this.layers[layer].height - 1) { return this.layers[layer].data[y + 1][x]; } return null ; } , getTileLeft: function (layer, x, y){ if (x > 0) { return this.layers[layer].data[y][x - 1]; } return null ; } , getTileRight: function (layer, x, y){ if (x < this.layers[layer].width - 1) { return this.layers[layer].data[y][x + 1]; } return null ; } , setLayer: function (layer){ layer = this.getLayer(layer); if (this.layers[layer]) { this.currentLayer = layer; } } , putTile: function (tile, x, y, layer){ layer = this.getLayer(layer); if (x >= 0 && x < this.layers[layer].width && y >= 0 && y < this.layers[layer].height) { if (tile instanceof Phaser.Tile) { this.layers[layer].data[y][x].copy(tile); } else { this.layers[layer].data[y][x].index = tile; } this.layers[layer].dirty = true ; this.calculateFaces(layer); } } , putTileWorldXY: function (tile, x, y, tileWidth, tileHeight, layer){ layer = this.getLayer(layer); x = this.game.math.snapToFloor(x, tileWidth) / tileWidth; y = this.game.math.snapToFloor(y, tileHeight) / tileHeight; this.putTile(tile, x, y, layer); } , getTile: function (x, y, layer){ layer = this.getLayer(layer); if (x >= 0 && x < this.layers[layer].width && y >= 0 && y < this.layers[layer].height) { return this.layers[layer].data[y][x]; } } , getTileWorldXY: function (x, y, tileWidth, tileHeight, layer){ if (typeof tileWidth === 'undefined') { tileWidth = this.tileWidth; } if (typeof tileHeight === 'undefined') { tileHeight = this.tileHeight; } layer = this.getLayer(layer); x = this.game.math.snapToFloor(x, tileWidth) / tileWidth; y = this.game.math.snapToFloor(y, tileHeight) / tileHeight; return this.getTile(x, y, layer); } , copy: function (x, y, width, height, layer){ layer = this.getLayer(layer); if (!this.layers[layer]) { this._results.length = 0; return ; } if (typeof x === "undefined") { x = 0; } if (typeof y === "undefined") { y = 0; } if (typeof width === "undefined") { width = this.layers[layer].width; } if (typeof height === "undefined") { height = this.layers[layer].height; } if (x < 0) { x = 0; } if (y < 0) { y = 0; } if (width > this.layers[layer].width) { width = this.layers[layer].width; } if (height > this.layers[layer].height) { height = this.layers[layer].height; } this._results.length = 0; this._results.push({ x: x, y: y, width: width, height: height, layer: layer} ); for (var ty = y; ty < y + height; ty++ ){ for (var tx = x; tx < x + width; tx++ ){ this._results.push(this.layers[layer].data[ty][tx]); } } return this._results; } , paste: function (x, y, tileblock, layer){ if (typeof x === "undefined") { x = 0; } if (typeof y === "undefined") { y = 0; } layer = this.getLayer(layer); if (!tileblock || _AN_Read_length("length", tileblock) < 2) { return ; } var diffX = tileblock[1].x - x; var diffY = tileblock[1].y - y; for (var i = 1; i < _AN_Read_length("length", tileblock); i++ ){ this.layers[layer].data[diffY + tileblock[i].y][diffX + tileblock[i].x].copy(tileblock[i]); } this.layers[layer].dirty = true ; this.calculateFaces(layer); } , swap: function (tileA, tileB, x, y, width, height, layer){ layer = this.getLayer(layer); this.copy(x, y, width, height, layer); if (_AN_Read_length("length", this._results) < 2) { return ; } this._tempA = tileA; this._tempB = tileB; this._results.forEach(this.swapHandler, this); this.paste(x, y, this._results, layer); } , swapHandler: function (value, index){ if (value.index === this._tempA) { this._results[index].index = this._tempB; } else if (value.index === this._tempB) { this._results[index].index = this._tempA; } } , forEach: function (callback, context, x, y, width, height, layer){ layer = this.getLayer(layer); this.copy(x, y, width, height, layer); if (_AN_Read_length("length", this._results) < 2) { return ; } this._results.forEach(callback, context); this.paste(x, y, this._results, layer); } , replace: function (source, dest, x, y, width, height, layer){ layer = this.getLayer(layer); this.copy(x, y, width, height, layer); if (_AN_Read_length("length", this._results) < 2) { return ; } for (var i = 1; i < _AN_Read_length("length", this._results); i++ ){ if (this._results[i].index === source) { this._results[i].index = dest; } } this.paste(x, y, this._results, layer); } , random: function (x, y, width, height, layer){ layer = this.getLayer(layer); this.copy(x, y, width, height, layer); if (_AN_Read_length("length", this._results) < 2) { return ; } var indexes = [] ; for (var t = 1; t < _AN_Read_length("length", this._results); t++ ){ if (this._results[t].index) { var idx = this._results[t].index; if (indexes.indexOf(idx) === -1) { indexes.push(idx); } } } for (var i = 1; i < _AN_Read_length("length", this._results); i++ ){ this._results[i].index = this.game.rnd.pick(indexes); } this.paste(x, y, this._results, layer); } , shuffle: function (x, y, width, height, layer){ layer = this.getLayer(layer); this.copy(x, y, width, height, layer); if (_AN_Read_length("length", this._results) < 2) { return ; } var indexes = [] ; for (var t = 1; t < _AN_Read_length("length", this._results); t++ ){ if (this._results[t].index) { indexes.push(this._results[t].index); } } Phaser.Utils.shuffle(indexes); for (var i = 1; i < _AN_Read_length("length", this._results); i++ ){ this._results[i].index = indexes[i - 1]; } this.paste(x, y, this._results, layer); } , fill: function (index, x, y, width, height, layer){ layer = this.getLayer(layer); this.copy(x, y, width, height, layer); if (_AN_Read_length("length", this._results) < 2) { return ; } for (var i = 1; i < _AN_Read_length("length", this._results); i++ ){ this._results[i].index = index; } this.paste(x, y, this._results, layer); } , removeAllLayers: function (){ this.layers.length = 0; this.currentLayer = 0; } , dump: function (){ var txt = ''; var args = [''] ; for (var y = 0; y < this.layers[this.currentLayer].height; y++ ){ for (var x = 0; x < this.layers[this.currentLayer].width; x++ ){ txt += "%c "; if (this.layers[this.currentLayer].data[y][x] > 1) { if (this.debugMap[this.layers[this.currentLayer].data[y][x]]) { args.push("background: " + this.debugMap[this.layers[this.currentLayer].data[y][x]]); } else { args.push("background: #ffffff"); } } else { args.push("background: rgb(0, 0, 0)"); } } txt += "\n"; } args[0] = txt; console.log.apply(console, args); } , destroy: function (){ this.removeAllLayers(); this.data = [] ; this.game = null ; } } ; Phaser.Tilemap.prototype.constructor = Phaser.Tilemap;