mirror of
https://github.com/wassname/phaser.git
synced 2026-07-14 01:10:16 +08:00
Extracting the correct area from a layer, debug displaying it and preparing for collision.
This commit is contained in:
@@ -100,10 +100,36 @@ Phaser.Tile = function (tileset, index, x, y, width, height) {
|
||||
*/
|
||||
this.separateY = true;
|
||||
|
||||
/**
|
||||
* @property {boolean} collisionCallback - Tilemap collision callback.
|
||||
* @default
|
||||
*/
|
||||
this.collisionCallback = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} collisionCallback - Tilemap collision callback.
|
||||
* @default
|
||||
*/
|
||||
this.collisionCallbackContext = this;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Tile.prototype = {
|
||||
|
||||
/**
|
||||
* Set callback to be called when this tilemap collides.
|
||||
*
|
||||
* @method Phaser.Tilemap.prototype.setCollisionCallback
|
||||
* @param {Function} callback - Callback function.
|
||||
* @param {object} context - Callback will be called with this context.
|
||||
*/
|
||||
setCollisionCallback: function (callback, context) {
|
||||
|
||||
this.collisionCallbackContext = context;
|
||||
this.collisionCallback = callback;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Clean up memory.
|
||||
* @method destroy
|
||||
|
||||
@@ -21,6 +21,8 @@ Phaser.Tilemap = function (game, key) {
|
||||
this.layers = [];
|
||||
}
|
||||
|
||||
console.log(this.layers);
|
||||
|
||||
this.currentLayer = 0;
|
||||
|
||||
this.debugMap = [];
|
||||
@@ -79,6 +81,23 @@ Phaser.Tilemap.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the tile located at specific position (in world coordinate) and layer (thus you give a position of a point which is within the tile).
|
||||
* @param {number} x - X position of the point in target tile.
|
||||
* @param {number} y - Y position of the point in target tile.
|
||||
* @param {number} [layer] - layer of this tile located.
|
||||
* @return {Tile} The tile with specific properties.
|
||||
*/
|
||||
getTileFromWorldXY: function (x, y, layer) {
|
||||
|
||||
if (typeof layer === "undefined") { layer = this.currentLayer; }
|
||||
|
||||
|
||||
|
||||
// return this.tiles[this.layers[layer].getTileFromWorldXY(x, y)];
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Set a specific tile with its x and y in tiles.
|
||||
* @method putTile
|
||||
@@ -95,6 +114,38 @@ Phaser.Tilemap.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Set a specific tile with its x and y in tiles.
|
||||
* @method putTileWorldXY
|
||||
* @param {number} x - X position of this tile in world coordinates.
|
||||
* @param {number} y - Y position of this tile in world coordinates.
|
||||
* @param {number} index - The index of this tile type in the core map data.
|
||||
*/
|
||||
putTileWorldXY: function (x, y, index) {
|
||||
|
||||
x = this.game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
|
||||
y = this.game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
|
||||
|
||||
if (x >= 0 && x < this.layers[this.currentLayer].width && y >= 0 && y < this.layers[this.currentLayer].height)
|
||||
{
|
||||
this.layers[this.currentLayer].data[y][x] = index;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
|
||||
// swapTile
|
||||
// fillTile
|
||||
// randomiseTiles
|
||||
// replaceTiles
|
||||
|
||||
removeAllLayers: function () {
|
||||
|
||||
this.layers.length = 0;
|
||||
this.currentLayer = 0;
|
||||
|
||||
},
|
||||
|
||||
dump: function () {
|
||||
|
||||
var txt = '';
|
||||
@@ -129,6 +180,13 @@ Phaser.Tilemap.prototype = {
|
||||
args[0] = txt;
|
||||
console.log.apply(console, args);
|
||||
|
||||
},
|
||||
|
||||
destroy: function () {
|
||||
|
||||
this.removeAllLayers();
|
||||
this.game = null;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -106,6 +106,11 @@ Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset,
|
||||
* @private
|
||||
*/
|
||||
this._ty = 0;
|
||||
|
||||
this._results = [];
|
||||
|
||||
this._tw = 0;
|
||||
this._th = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _tl - Local render loop var to help avoid gc spikes.
|
||||
@@ -144,6 +149,8 @@ Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset,
|
||||
this._y = 0;
|
||||
this._prevX = 0;
|
||||
this._prevY = 0;
|
||||
|
||||
|
||||
this.dirty = true;
|
||||
|
||||
if (tileset instanceof Phaser.Tileset || typeof tileset === 'string')
|
||||
@@ -168,7 +175,11 @@ Phaser.TilemapLayer.prototype = {
|
||||
}
|
||||
else if (typeof tileset === 'string')
|
||||
{
|
||||
this.tileset = this.game.cache.getTileset('tiles');;
|
||||
this.tileset = this.game.cache.getTileset('tiles');
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.tileWidth = this.tileset.tileWidth;
|
||||
@@ -184,9 +195,85 @@ Phaser.TilemapLayer.prototype = {
|
||||
layer = 0;
|
||||
}
|
||||
|
||||
this.tilemap = tilemap;
|
||||
this.layer = this.tilemap.layers[layer];
|
||||
this.updateMax();
|
||||
if (tilemap instanceof Phaser.Tilemap)
|
||||
{
|
||||
this.tilemap = tilemap;
|
||||
this.layer = this.tilemap.layers[layer];
|
||||
this.updateMax();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @method getTileOverlaps
|
||||
* @param {GameObject} object - Tiles you want to get that overlaps this.
|
||||
* @return {array} Array with tiles informations (each contains x, y, and the tile).
|
||||
*/
|
||||
getTiles: function (x, y, width, height, collides) {
|
||||
|
||||
if (this.tilemap === null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all)
|
||||
if (typeof collides === 'undefined') { collides = false; }
|
||||
|
||||
// Cap the values
|
||||
|
||||
if (x < 0)
|
||||
{
|
||||
x = 0;
|
||||
}
|
||||
|
||||
if (y < 0)
|
||||
{
|
||||
y = 0;
|
||||
}
|
||||
|
||||
if (width > this.widthInPixels)
|
||||
{
|
||||
width = this.widthInPixels;
|
||||
}
|
||||
|
||||
if (height > this.heightInPixels)
|
||||
{
|
||||
height = this.heightInPixels;
|
||||
}
|
||||
|
||||
// Convert the pixel values into tile coordinates
|
||||
this._tx = this.game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
|
||||
this._ty = this.game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
|
||||
this._tw = (this.game.math.snapToCeil(width, this.tileWidth) + this.tileWidth) / this.tileWidth;
|
||||
this._th = (this.game.math.snapToCeil(height, this.tileHeight) + this.tileHeight) / this.tileHeight;
|
||||
|
||||
this._results.length = 0;
|
||||
|
||||
this._results.push( { x: x, y: y, width: width, height: height, tx: this._tx, ty: this._ty, tw: this._tw, th: this._th });
|
||||
|
||||
var _index = 0;
|
||||
var _tile = null;
|
||||
|
||||
for (var wy = this._ty; wy < this._ty + this._th; wy++)
|
||||
{
|
||||
for (var wx = this._tx; wx < this._tx + this._tw; wx++)
|
||||
{
|
||||
if (this.layer.data[wy] && this.layer.data[wy][wx])
|
||||
{
|
||||
// Could combine
|
||||
_index = this.layer.data[wy][wx] - 1;
|
||||
_tile = this.tileset.getTile(_index);
|
||||
|
||||
if (collides == false || (collides && _tile.collideNone == false))
|
||||
{
|
||||
this._results.push({ left: wx * _tile.width, right: (wx * _tile.width) + _tile.width, top: wy * _tile.height, bottom: (wy * _tile.height) + _tile.height, width: _tile.width, height: _tile.height, tx: wx, ty: wy, tile: _tile });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this._results;
|
||||
|
||||
},
|
||||
|
||||
|
||||
+33
-1
@@ -36,11 +36,43 @@ Phaser.Tileset.prototype = {
|
||||
|
||||
},
|
||||
|
||||
canCollide: function (index) {
|
||||
|
||||
if (this.tiles[index])
|
||||
{
|
||||
return this.tiles[index].collideNone;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
},
|
||||
|
||||
checkTileIndex: function (index) {
|
||||
|
||||
return (this.tiles[index]);
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
setCollisionRange: function (start, stop, left, right, up, down) {
|
||||
|
||||
if (this.tiles[start] && this.tiles[stop] && start < stop)
|
||||
{
|
||||
for (var i = start; i <= stop; i++)
|
||||
{
|
||||
this.tiles[i].setCollision(left, right, up, down);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
setCollision: function (index, left, right, up, down) {
|
||||
|
||||
if (this.tiles[index])
|
||||
{
|
||||
this.tiles[index].setCollision(left, right, up, down);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user