diff --git a/Phaser/GameMath.ts b/Phaser/GameMath.ts
index 6ec75b79..ab93bfd7 100644
--- a/Phaser/GameMath.ts
+++ b/Phaser/GameMath.ts
@@ -855,29 +855,27 @@ module Phaser {
/**
* Fetch a random entry from the given array.
* Will return null if random selection is missing, or array has no entries.
- * G.getRandom() is deterministic and safe for use with replays/recordings.
- * HOWEVER, U.getRandom() is NOT deterministic and unsafe for use with replays/recordings.
*
- * @param Objects An array of objects.
- * @param StartIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
- * @param Length Optional restriction on the number of values you want to randomly select from.
+ * @param objects An array of objects.
+ * @param startIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
+ * @param length Optional restriction on the number of values you want to randomly select from.
*
* @return The random object that was selected.
*/
- public getRandom(Objects, StartIndex: number = 0, Length: number = 0) {
+ public getRandom(objects, startIndex: number = 0, length: number = 0) {
- if (Objects != null)
+ if (objects != null)
{
- var l: number = Length;
+ var l: number = length;
- if ((l == 0) || (l > Objects.length - StartIndex))
+ if ((l == 0) || (l > objects.length - startIndex))
{
- l = Objects.length - StartIndex;
+ l = objects.length - startIndex;
}
if (l > 0)
{
- return Objects[StartIndex + Math.floor(Math.random() * l)];
+ return objects[startIndex + Math.floor(Math.random() * l)];
}
}
diff --git a/Phaser/gameobjects/Tilemap.ts b/Phaser/gameobjects/Tilemap.ts
index 9e24c3c5..9a346705 100644
--- a/Phaser/gameobjects/Tilemap.ts
+++ b/Phaser/gameobjects/Tilemap.ts
@@ -282,10 +282,14 @@ module Phaser {
}
+ public putTile(x: number, y: number, index: number, layer?: number = 0) {
+
+ this.layers[layer].putTile(x, y, index);
+
+ }
+
// Set current layer
// Set layer order?
- // Get block of tiles
- // Swap tiles around
// Delete tiles of certain type
// Erase tiles
diff --git a/Phaser/system/TilemapLayer.ts b/Phaser/system/TilemapLayer.ts
index 95657e81..c4e15101 100644
--- a/Phaser/system/TilemapLayer.ts
+++ b/Phaser/system/TilemapLayer.ts
@@ -75,6 +75,102 @@ module Phaser {
public tileMargin: number = 0;
public tileSpacing: number = 0;
+ public putTile(x: number, y: number, index: number) {
+
+ x = this._game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
+ y = this._game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
+
+ if (y >= 0 && y < this.mapData.length)
+ {
+ if (x >= 0 && x < this.mapData[y].length)
+ {
+ this.mapData[y][x] = index;
+ }
+ }
+
+ }
+
+ public swapTile(tileA: number, tileB: number, x?: number = 0, y?: number = 0, width?: number = this.widthInTiles, height?: number = this.heightInTiles) {
+
+ this.getTempBlock(x, y, width, height);
+
+ for (var r = 0; r < this._tempTileBlock.length; r++)
+ {
+ // First sweep marking tileA as needing a new index
+ if (this._tempTileBlock[r].tile.index == tileA)
+ {
+ this._tempTileBlock[r].newIndex = true;
+ }
+
+ // In the same pass we can swap tileB to tileA
+ if (this._tempTileBlock[r].tile.index == tileB)
+ {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileA;
+ }
+ }
+
+ for (var r = 0; r < this._tempTileBlock.length; r++)
+ {
+ // And now swap our newIndex tiles for tileB
+ if (this._tempTileBlock[r].newIndex == true)
+ {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileB;
+ }
+ }
+
+ }
+
+ public fillTile(index: number, x?: number = 0, y?: number = 0, width?: number = this.widthInTiles, height?: number = this.heightInTiles) {
+
+ this.getTempBlock(x, y, width, height);
+
+ for (var r = 0; r < this._tempTileBlock.length; r++)
+ {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = index;
+ }
+
+ }
+
+ public randomiseTiles(tiles: number[], x?: number = 0, y?: number = 0, width?: number = this.widthInTiles, height?: number = this.heightInTiles) {
+
+ this.getTempBlock(x, y, width, height);
+
+ for (var r = 0; r < this._tempTileBlock.length; r++)
+ {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = this._game.math.getRandom(tiles);
+ }
+
+ }
+
+ public replaceTile(tileA: number, tileB: number, x?: number = 0, y?: number = 0, width?: number = this.widthInTiles, height?: number = this.heightInTiles) {
+
+ this.getTempBlock(x, y, width, height);
+
+ for (var r = 0; r < this._tempTileBlock.length; r++)
+ {
+ if (this._tempTileBlock[r].tile.index == tileA)
+ {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileB;
+ }
+ }
+
+ }
+
+ public getTileBlock(x: number, y: number, width: number, height: number) {
+
+ var output = [];
+
+ this.getTempBlock(x, y, width, height);
+
+ for (var r = 0; r < this._tempTileBlock.length; r++)
+ {
+ output.push({ x: this._tempTileBlock[r].x, y: this._tempTileBlock[r].y, tile: this._tempTileBlock[r].tile });
+ }
+
+ return output;
+
+ }
+
public getTileFromWorldXY(x: number, y: number): number {
x = this._game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
@@ -99,7 +195,9 @@ module Phaser {
this._tempTileH = (this._game.math.snapToCeil(object.bounds.height, this.tileHeight) + this.tileHeight) / this.tileHeight;
// Loop through the tiles we've got and check overlaps accordingly (the results are stored in this._tempTileBlock)
- this.getTileBlock(this._tempTileX, this._tempTileY, this._tempTileW, this._tempTileH);
+
+ this._tempBlockResults = [];
+ this.getTempBlock(this._tempTileX, this._tempTileY, this._tempTileW, this._tempTileH, true);
Collision.TILE_OVERLAP = false;
@@ -115,7 +213,7 @@ module Phaser {
}
- public getTileBlock(x: number, y: number, width: number, height: number) {
+ private getTempBlock(x: number, y: number, width: number, height: number, collisionOnly?: bool = false) {
if (x < 0)
{
@@ -138,16 +236,25 @@ module Phaser {
}
this._tempTileBlock = [];
- this._tempBlockResults = [];
for (var ty = y; ty < y + height; ty++)
{
for (var tx = x; tx < x + width; tx++)
{
- // We only want to consider the tile for checking if you can actually collide with it
- if (this.mapData[ty] && this.mapData[ty][tx] && this._parent.tiles[this.mapData[ty][tx]].allowCollisions != Collision.NONE)
+ if (collisionOnly)
{
- this._tempTileBlock.push({ x: tx, y: ty, tile: this._parent.tiles[this.mapData[ty][tx]] });
+ // We only want to consider the tile for checking if you can actually collide with it
+ if (this.mapData[ty] && this.mapData[ty][tx] && this._parent.tiles[this.mapData[ty][tx]].allowCollisions != Collision.NONE)
+ {
+ this._tempTileBlock.push({ x: tx, y: ty, tile: this._parent.tiles[this.mapData[ty][tx]] });
+ }
+ }
+ else
+ {
+ if (this.mapData[ty] && this.mapData[ty][tx])
+ {
+ this._tempTileBlock.push({ x: tx, y: ty, tile: this._parent.tiles[this.mapData[ty][tx]] });
+ }
}
}
}
@@ -194,8 +301,6 @@ module Phaser {
this.boundsInTiles.setTo(0, 0, this.widthInTiles, this.heightInTiles);
- //console.log('layer bounds', this.boundsInTiles);
-
}
public parseTileOffsets():number {
diff --git a/README.md b/README.md
index c25b7c06..36129d0c 100644
--- a/README.md
+++ b/README.md
@@ -41,8 +41,14 @@ V0.9.5
* Tilemap.collide now optionally takes callback and context parameters which are used if collision occurs.
* Added Tilemap.collisionCallback and Tilemap.collisionCallbackContext so you can set them once and not re-set them on every call to collide.
* Collision.separateTile now has 2 extra parameters: separateX and separateY. If true the object will be separated on overlap, otherwise just the overlap boolean result is returned.
-* Added Tile.separateX and Tile.separateY (both true by default), if true an object will be separated from the tile on overlap. Set to false if you don't want a tile to stop an object from moving, you just want it to return collision data to your callback.
+* Added Tile.separateX and Tile.separateY (both true by default). Set to false if you don't want a tile to stop an object from moving, you just want it to return collision data to your callback.
* Added Tilemap.getTileByIndex(value) to access a specific type of tile, rather than by its map index.
+* Added TilemapLayer.putTile(x,y,index) - allows you to insert new tile data into the map layer (create your own tile editor!).
+* TilemapLayer.getTileBlock now returns a unique Array of map data, not just a reference to the temporary block array
+* Added TilemapLayer.swapTile - scans the given region of the map for all instances of tileA and swaps them for tileB, and vice versa.
+* Added TilemapLayer.replaceTile - scans the given region of the map and replaces all instances of tileA with tileB. tileB is left unaffected.
+* Added TilemapLayer.fillTiles - fills the given region of the map with the tile specified.
+* Added TilemapLayer.randomiseTiles - fills the given region of the map with a random tile from the list specified.
diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj
index c68b56e8..8ed65d1a 100644
--- a/Tests/Tests.csproj
+++ b/Tests/Tests.csproj
@@ -159,12 +159,40 @@
collision.ts
+
+
+ fill tiles.ts
+
get tile.ts
+
+
+
+ map draw.ts
+
+
+ put tile.ts
+
+
+
+
+ random tiles.ts
+
+
+ replace tiles.ts
+
small map.ts
+
+
+
+ sprite draw tiles.ts
+
+
+ swap tiles.ts
+
bounce.ts
diff --git a/Tests/assets/maps/map draw.tmx b/Tests/assets/maps/map draw.tmx
new file mode 100644
index 00000000..2572504d
--- /dev/null
+++ b/Tests/assets/maps/map draw.tmx
@@ -0,0 +1,11 @@
+
+
diff --git a/Tests/assets/maps/mapdraw.json b/Tests/assets/maps/mapdraw.json
new file mode 100644
index 00000000..a363e3aa
--- /dev/null
+++ b/Tests/assets/maps/mapdraw.json
@@ -0,0 +1,39 @@
+{ "height":40,
+ "layers":[
+ {
+ "data":[21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 21, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 21, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 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, 0, 0, 0, 0, 21, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 60, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 60, 79, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 79, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78],
+ "height":40,
+ "name":"Tile Layer 1",
+ "opacity":1,
+ "type":"tilelayer",
+ "visible":true,
+ "width":50,
+ "x":0,
+ "y":0
+ }],
+ "orientation":"orthogonal",
+ "properties":
+ {
+
+ },
+ "tileheight":16,
+ "tilesets":[
+ {
+ "firstgid":1,
+ "image":"..\/..\/..\/..\/kiwi-lite\/Test Suite\/assets\/tiles\/platformer_tiles.png",
+ "imageheight":96,
+ "imagewidth":304,
+ "margin":0,
+ "name":"platformer_tiles",
+ "properties":
+ {
+
+ },
+ "spacing":0,
+ "tileheight":16,
+ "tilewidth":16
+ }],
+ "tilewidth":16,
+ "version":1,
+ "width":50
+}
\ No newline at end of file
diff --git a/Tests/phaser.js b/Tests/phaser.js
index 4f038bfb..a32484be 100644
--- a/Tests/phaser.js
+++ b/Tests/phaser.js
@@ -6012,25 +6012,23 @@ var Phaser;
GameMath.prototype.getRandom = /**
* Fetch a random entry from the given array.
* Will return null if random selection is missing, or array has no entries.
- * G.getRandom() is deterministic and safe for use with replays/recordings.
- * HOWEVER, U.getRandom() is NOT deterministic and unsafe for use with replays/recordings.
*
- * @param Objects An array of objects.
- * @param StartIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
- * @param Length Optional restriction on the number of values you want to randomly select from.
+ * @param objects An array of objects.
+ * @param startIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
+ * @param length Optional restriction on the number of values you want to randomly select from.
*
* @return The random object that was selected.
*/
- function (Objects, StartIndex, Length) {
- if (typeof StartIndex === "undefined") { StartIndex = 0; }
- if (typeof Length === "undefined") { Length = 0; }
- if(Objects != null) {
- var l = Length;
- if((l == 0) || (l > Objects.length - StartIndex)) {
- l = Objects.length - StartIndex;
+ function (objects, startIndex, length) {
+ if (typeof startIndex === "undefined") { startIndex = 0; }
+ if (typeof length === "undefined") { length = 0; }
+ if(objects != null) {
+ var l = length;
+ if((l == 0) || (l > objects.length - startIndex)) {
+ l = objects.length - startIndex;
}
if(l > 0) {
- return Objects[StartIndex + Math.floor(Math.random() * l)];
+ return objects[startIndex + Math.floor(Math.random() * l)];
}
}
return null;
@@ -11158,6 +11156,82 @@ var Phaser;
this._tempTileBlock = [];
this._texture = this._game.cache.getImage(key);
}
+ TilemapLayer.prototype.putTile = 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(y >= 0 && y < this.mapData.length) {
+ if(x >= 0 && x < this.mapData[y].length) {
+ this.mapData[y][x] = index;
+ }
+ }
+ };
+ TilemapLayer.prototype.swapTile = function (tileA, tileB, x, y, width, height) {
+ if (typeof x === "undefined") { x = 0; }
+ if (typeof y === "undefined") { y = 0; }
+ if (typeof width === "undefined") { width = this.widthInTiles; }
+ if (typeof height === "undefined") { height = this.heightInTiles; }
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ // First sweep marking tileA as needing a new index
+ if(this._tempTileBlock[r].tile.index == tileA) {
+ this._tempTileBlock[r].newIndex = true;
+ }
+ // In the same pass we can swap tileB to tileA
+ if(this._tempTileBlock[r].tile.index == tileB) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileA;
+ }
+ }
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ // And now swap our newIndex tiles for tileB
+ if(this._tempTileBlock[r].newIndex == true) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileB;
+ }
+ }
+ };
+ TilemapLayer.prototype.fillTile = function (index, x, y, width, height) {
+ if (typeof x === "undefined") { x = 0; }
+ if (typeof y === "undefined") { y = 0; }
+ if (typeof width === "undefined") { width = this.widthInTiles; }
+ if (typeof height === "undefined") { height = this.heightInTiles; }
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = index;
+ }
+ };
+ TilemapLayer.prototype.randomiseTiles = function (tiles, x, y, width, height) {
+ if (typeof x === "undefined") { x = 0; }
+ if (typeof y === "undefined") { y = 0; }
+ if (typeof width === "undefined") { width = this.widthInTiles; }
+ if (typeof height === "undefined") { height = this.heightInTiles; }
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = this._game.math.getRandom(tiles);
+ }
+ };
+ TilemapLayer.prototype.replaceTile = function (tileA, tileB, x, y, width, height) {
+ if (typeof x === "undefined") { x = 0; }
+ if (typeof y === "undefined") { y = 0; }
+ if (typeof width === "undefined") { width = this.widthInTiles; }
+ if (typeof height === "undefined") { height = this.heightInTiles; }
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ if(this._tempTileBlock[r].tile.index == tileA) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileB;
+ }
+ }
+ };
+ TilemapLayer.prototype.getTileBlock = function (x, y, width, height) {
+ var output = [];
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ output.push({
+ x: this._tempTileBlock[r].x,
+ y: this._tempTileBlock[r].y,
+ tile: this._tempTileBlock[r].tile
+ });
+ }
+ return output;
+ };
TilemapLayer.prototype.getTileFromWorldXY = function (x, y) {
x = this._game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
y = this._game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
@@ -11174,7 +11248,8 @@ var Phaser;
this._tempTileW = (this._game.math.snapToCeil(object.bounds.width, this.tileWidth) + this.tileWidth) / this.tileWidth;
this._tempTileH = (this._game.math.snapToCeil(object.bounds.height, this.tileHeight) + this.tileHeight) / this.tileHeight;
// Loop through the tiles we've got and check overlaps accordingly (the results are stored in this._tempTileBlock)
- this.getTileBlock(this._tempTileX, this._tempTileY, this._tempTileW, this._tempTileH);
+ this._tempBlockResults = [];
+ this.getTempBlock(this._tempTileX, this._tempTileY, this._tempTileW, this._tempTileH, true);
Phaser.Collision.TILE_OVERLAP = false;
for(var r = 0; r < this._tempTileBlock.length; r++) {
if(Phaser.Collision.separateTile(object, this._tempTileBlock[r].x * this.tileWidth, this._tempTileBlock[r].y * this.tileHeight, this.tileWidth, this.tileHeight, this._tempTileBlock[r].tile.mass, this._tempTileBlock[r].tile.collideLeft, this._tempTileBlock[r].tile.collideRight, this._tempTileBlock[r].tile.collideUp, this._tempTileBlock[r].tile.collideDown, this._tempTileBlock[r].tile.separateX, this._tempTileBlock[r].tile.separateY) == true) {
@@ -11187,7 +11262,8 @@ var Phaser;
}
return this._tempBlockResults;
};
- TilemapLayer.prototype.getTileBlock = function (x, y, width, height) {
+ TilemapLayer.prototype.getTempBlock = function (x, y, width, height, collisionOnly) {
+ if (typeof collisionOnly === "undefined") { collisionOnly = false; }
if(x < 0) {
x = 0;
}
@@ -11201,16 +11277,25 @@ var Phaser;
height = this.heightInTiles;
}
this._tempTileBlock = [];
- this._tempBlockResults = [];
for(var ty = y; ty < y + height; ty++) {
for(var tx = x; tx < x + width; tx++) {
- // We only want to consider the tile for checking if you can actually collide with it
- if(this.mapData[ty] && this.mapData[ty][tx] && this._parent.tiles[this.mapData[ty][tx]].allowCollisions != Phaser.Collision.NONE) {
- this._tempTileBlock.push({
- x: tx,
- y: ty,
- tile: this._parent.tiles[this.mapData[ty][tx]]
- });
+ if(collisionOnly) {
+ // We only want to consider the tile for checking if you can actually collide with it
+ if(this.mapData[ty] && this.mapData[ty][tx] && this._parent.tiles[this.mapData[ty][tx]].allowCollisions != Phaser.Collision.NONE) {
+ this._tempTileBlock.push({
+ x: tx,
+ y: ty,
+ tile: this._parent.tiles[this.mapData[ty][tx]]
+ });
+ }
+ } else {
+ if(this.mapData[ty] && this.mapData[ty][tx]) {
+ this._tempTileBlock.push({
+ x: tx,
+ y: ty,
+ tile: this._parent.tiles[this.mapData[ty][tx]]
+ });
+ }
}
}
}
@@ -11238,8 +11323,7 @@ var Phaser;
};
TilemapLayer.prototype.updateBounds = function () {
this.boundsInTiles.setTo(0, 0, this.widthInTiles, this.heightInTiles);
- //console.log('layer bounds', this.boundsInTiles);
- };
+ };
TilemapLayer.prototype.parseTileOffsets = function () {
this._tileOffsets = [];
var i = 0;
@@ -11612,13 +11696,15 @@ var Phaser;
return false;
}
};
+ Tilemap.prototype.putTile = function (x, y, index, layer) {
+ if (typeof layer === "undefined") { layer = 0; }
+ this.layers[layer].putTile(x, y, index);
+ };
return Tilemap;
})(Phaser.GameObject);
Phaser.Tilemap = Tilemap;
// Set current layer
// Set layer order?
- // Get block of tiles
- // Swap tiles around
// Delete tiles of certain type
// Erase tiles
})(Phaser || (Phaser = {}));
diff --git a/Tests/tilemap/fill tiles.js b/Tests/tilemap/fill tiles.js
new file mode 100644
index 00000000..f2c344ab
--- /dev/null
+++ b/Tests/tilemap/fill tiles.js
@@ -0,0 +1,54 @@
+///
+///
+///
+(function () {
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+ function init() {
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+ myGame.loader.load();
+ }
+ var map;
+ var car;
+ var marker;
+ var tile;
+ function create() {
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+ myGame.input.onDown.add(fillTiles);
+ }
+ function fillTiles() {
+ // Fills the given region of the map (2,2,10,20) with tile index 15
+ map.currentLayer.fillTile(15, 2, 2, 10, 20);
+ }
+ function update() {
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
+ car.angularVelocity = -200;
+ } else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
+ car.angularVelocity = 200;
+ }
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+ }
+ function render() {
+ tile = map.getTileFromInputXY();
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+ }
+})();
diff --git a/Tests/tilemap/fill tiles.ts b/Tests/tilemap/fill tiles.ts
new file mode 100644
index 00000000..e8dc41bb
--- /dev/null
+++ b/Tests/tilemap/fill tiles.ts
@@ -0,0 +1,86 @@
+///
+///
+///
+
+(function () {
+
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+
+ function init() {
+
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+
+ myGame.loader.load();
+
+ }
+
+ var map: Phaser.Tilemap;
+ var car: Phaser.Sprite;
+ var marker: Phaser.GeomSprite;
+ var tile: Phaser.Tile;
+
+ function create() {
+
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+
+ myGame.input.onDown.add(fillTiles);
+ }
+
+ function fillTiles() {
+
+ // Fills the given region of the map (2,2,10,20) with tile index 15
+ map.currentLayer.fillTile(15, 2, 2, 10, 20);
+
+ }
+
+ function update() {
+
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
+ {
+ car.angularVelocity = -200;
+ }
+ else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
+ {
+ car.angularVelocity = 200;
+ }
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
+ {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+
+ }
+
+ function render {
+
+ tile = map.getTileFromInputXY();
+
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+
+ }
+
+})();
diff --git a/Tests/tilemap/map draw.js b/Tests/tilemap/map draw.js
new file mode 100644
index 00000000..e788a8cc
--- /dev/null
+++ b/Tests/tilemap/map draw.js
@@ -0,0 +1,39 @@
+///
+///
+(function () {
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+ function init() {
+ myGame.loader.addTextFile('platform', 'assets/maps/mapdraw.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/platformer_tiles.png');
+ myGame.loader.addImageFile('carrot', 'assets/sprites/carrot.png');
+ myGame.loader.load();
+ }
+ var map;
+ var emitter;
+ var marker;
+ function create() {
+ map = myGame.createTilemap('tiles', 'platform', Phaser.Tilemap.FORMAT_TILED_JSON);
+ map.setCollisionRange(21, 53);
+ map.setCollisionRange(105, 109);
+ myGame.camera.backgroundColor = 'rgb(47,154,204)';
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(16, 16);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+ emitter = myGame.createEmitter(32, 80);
+ emitter.width = 700;
+ emitter.makeParticles('carrot', 100, 0, false, 1);
+ emitter.gravity = 150;
+ emitter.bounce = 0.8;
+ emitter.start(false, 20, 0.05);
+ }
+ function update() {
+ // Collide everything with the map
+ map.collide();
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 16);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 16);
+ if(myGame.input.mouse.isDown) {
+ map.putTile(marker.x, marker.y, 32);
+ }
+ }
+})();
diff --git a/Tests/tilemap/map draw.ts b/Tests/tilemap/map draw.ts
new file mode 100644
index 00000000..d0b87eb0
--- /dev/null
+++ b/Tests/tilemap/map draw.ts
@@ -0,0 +1,58 @@
+///
+///
+
+(function () {
+
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+
+ function init() {
+
+ myGame.loader.addTextFile('platform', 'assets/maps/mapdraw.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/platformer_tiles.png');
+ myGame.loader.addImageFile('carrot', 'assets/sprites/carrot.png');
+
+ myGame.loader.load();
+
+ }
+
+ var map: Phaser.Tilemap;
+ var emitter: Phaser.Emitter;
+ var marker: Phaser.GeomSprite;
+
+ function create() {
+
+ map = myGame.createTilemap('tiles', 'platform', Phaser.Tilemap.FORMAT_TILED_JSON);
+ map.setCollisionRange(21,53);
+ map.setCollisionRange(105,109);
+
+ myGame.camera.backgroundColor = 'rgb(47,154,204)';
+
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(16, 16);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+
+ emitter = myGame.createEmitter(32, 80);
+ emitter.width = 700;
+ emitter.makeParticles('carrot', 100, 0, false, 1);
+ emitter.gravity = 150;
+ emitter.bounce = 0.8;
+ emitter.start(false, 20, 0.05);
+ }
+
+ function update() {
+
+ // Collide everything with the map
+ map.collide();
+
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 16);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 16);
+
+ if (myGame.input.mouse.isDown)
+ {
+ map.putTile(marker.x, marker.y, 32);
+ }
+
+ }
+
+})();
diff --git a/Tests/tilemap/put tile.js b/Tests/tilemap/put tile.js
new file mode 100644
index 00000000..9dd8874d
--- /dev/null
+++ b/Tests/tilemap/put tile.js
@@ -0,0 +1,52 @@
+///
+///
+///
+(function () {
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+ function init() {
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+ myGame.loader.load();
+ }
+ var map;
+ var car;
+ var marker;
+ var tile;
+ function create() {
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+ }
+ function paintTile() {
+ map.putTile(marker.x, marker.y, 32);
+ }
+ function update() {
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
+ car.angularVelocity = -200;
+ } else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
+ car.angularVelocity = 200;
+ }
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+ }
+ function render() {
+ tile = map.getTileFromInputXY();
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+ }
+})();
diff --git a/Tests/tilemap/put tile.ts b/Tests/tilemap/put tile.ts
new file mode 100644
index 00000000..a351ecb5
--- /dev/null
+++ b/Tests/tilemap/put tile.ts
@@ -0,0 +1,83 @@
+///
+///
+///
+
+(function () {
+
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+
+ function init() {
+
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+
+ myGame.loader.load();
+
+ }
+
+ var map: Phaser.Tilemap;
+ var car: Phaser.Sprite;
+ var marker: Phaser.GeomSprite;
+ var tile: Phaser.Tile;
+
+ function create() {
+
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+ }
+
+ function paintTile() {
+
+ map.putTile(marker.x, marker.y, 32);
+
+ }
+
+ function update() {
+
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
+ {
+ car.angularVelocity = -200;
+ }
+ else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
+ {
+ car.angularVelocity = 200;
+ }
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
+ {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+
+ }
+
+ function render {
+
+ tile = map.getTileFromInputXY();
+
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+
+ }
+
+})();
diff --git a/Tests/tilemap/random tiles.js b/Tests/tilemap/random tiles.js
new file mode 100644
index 00000000..92516733
--- /dev/null
+++ b/Tests/tilemap/random tiles.js
@@ -0,0 +1,60 @@
+///
+///
+///
+(function () {
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+ function init() {
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+ myGame.loader.load();
+ }
+ var map;
+ var car;
+ var marker;
+ var tile;
+ function create() {
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+ myGame.input.onDown.add(randomTiles);
+ }
+ function randomTiles() {
+ map.currentLayer.randomiseTiles([
+ 30,
+ 31,
+ 32,
+ 38,
+ 39,
+ 40
+ ]);
+ }
+ function update() {
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
+ car.angularVelocity = -200;
+ } else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
+ car.angularVelocity = 200;
+ }
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+ }
+ function render() {
+ tile = map.getTileFromInputXY();
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+ }
+})();
diff --git a/Tests/tilemap/random tiles.ts b/Tests/tilemap/random tiles.ts
new file mode 100644
index 00000000..d92d5dc0
--- /dev/null
+++ b/Tests/tilemap/random tiles.ts
@@ -0,0 +1,85 @@
+///
+///
+///
+
+(function () {
+
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+
+ function init() {
+
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+
+ myGame.loader.load();
+
+ }
+
+ var map: Phaser.Tilemap;
+ var car: Phaser.Sprite;
+ var marker: Phaser.GeomSprite;
+ var tile: Phaser.Tile;
+
+ function create() {
+
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+
+ myGame.input.onDown.add(randomTiles);
+ }
+
+ function randomTiles() {
+
+ map.currentLayer.randomiseTiles([30, 31, 32, 38, 39, 40]);
+
+ }
+
+ function update() {
+
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
+ {
+ car.angularVelocity = -200;
+ }
+ else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
+ {
+ car.angularVelocity = 200;
+ }
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
+ {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+
+ }
+
+ function render {
+
+ tile = map.getTileFromInputXY();
+
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+
+ }
+
+})();
diff --git a/Tests/tilemap/replace tiles.js b/Tests/tilemap/replace tiles.js
new file mode 100644
index 00000000..813053fe
--- /dev/null
+++ b/Tests/tilemap/replace tiles.js
@@ -0,0 +1,53 @@
+///
+///
+///
+(function () {
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+ function init() {
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+ myGame.loader.load();
+ }
+ var map;
+ var car;
+ var marker;
+ var tile;
+ function create() {
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+ myGame.input.onDown.add(swapTiles);
+ }
+ function swapTiles() {
+ map.currentLayer.replaceTile(30, 31);
+ }
+ function update() {
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
+ car.angularVelocity = -200;
+ } else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
+ car.angularVelocity = 200;
+ }
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+ }
+ function render() {
+ tile = map.getTileFromInputXY();
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+ }
+})();
diff --git a/Tests/tilemap/replace tiles.ts b/Tests/tilemap/replace tiles.ts
new file mode 100644
index 00000000..8944f234
--- /dev/null
+++ b/Tests/tilemap/replace tiles.ts
@@ -0,0 +1,85 @@
+///
+///
+///
+
+(function () {
+
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+
+ function init() {
+
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+
+ myGame.loader.load();
+
+ }
+
+ var map: Phaser.Tilemap;
+ var car: Phaser.Sprite;
+ var marker: Phaser.GeomSprite;
+ var tile: Phaser.Tile;
+
+ function create() {
+
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+
+ myGame.input.onDown.add(swapTiles);
+ }
+
+ function swapTiles() {
+
+ map.currentLayer.replaceTile(30, 31);
+
+ }
+
+ function update() {
+
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
+ {
+ car.angularVelocity = -200;
+ }
+ else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
+ {
+ car.angularVelocity = 200;
+ }
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
+ {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+
+ }
+
+ function render {
+
+ tile = map.getTileFromInputXY();
+
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+
+ }
+
+})();
diff --git a/Tests/tilemap/sprite draw tiles.js b/Tests/tilemap/sprite draw tiles.js
new file mode 100644
index 00000000..06345b76
--- /dev/null
+++ b/Tests/tilemap/sprite draw tiles.js
@@ -0,0 +1,36 @@
+///
+///
+///
+(function () {
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+ function init() {
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+ myGame.loader.load();
+ }
+ var map;
+ var car;
+ function create() {
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+ // Fills the whole map to one tile
+ map.currentLayer.fillTile(30);
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+ myGame.camera.follow(car);
+ }
+ function update() {
+ map.putTile(car.x + 16, car.y + 16, 34);
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
+ car.angularVelocity = -200;
+ } else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
+ car.angularVelocity = 200;
+ }
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+ }
+})();
diff --git a/Tests/tilemap/sprite draw tiles.ts b/Tests/tilemap/sprite draw tiles.ts
new file mode 100644
index 00000000..5d6c1599
--- /dev/null
+++ b/Tests/tilemap/sprite draw tiles.ts
@@ -0,0 +1,59 @@
+///
+///
+///
+
+(function () {
+
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+
+ function init() {
+
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+
+ myGame.loader.load();
+
+ }
+
+ var map: Phaser.Tilemap;
+ var car: Phaser.Sprite;
+
+ function create() {
+
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+
+ // Fills the whole map to one tile
+ map.currentLayer.fillTile(30);
+
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+
+ myGame.camera.follow(car);
+ }
+
+ function update() {
+
+ map.putTile(car.x + 16, car.y + 16, 34);
+
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
+ {
+ car.angularVelocity = -200;
+ }
+ else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
+ {
+ car.angularVelocity = 200;
+ }
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
+ {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+
+ }
+
+})();
diff --git a/Tests/tilemap/swap tiles.js b/Tests/tilemap/swap tiles.js
new file mode 100644
index 00000000..95de4ec1
--- /dev/null
+++ b/Tests/tilemap/swap tiles.js
@@ -0,0 +1,53 @@
+///
+///
+///
+(function () {
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+ function init() {
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+ myGame.loader.load();
+ }
+ var map;
+ var car;
+ var marker;
+ var tile;
+ function create() {
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+ myGame.input.onDown.add(swapTiles);
+ }
+ function swapTiles() {
+ map.currentLayer.swapTile(30, 31);
+ }
+ function update() {
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
+ car.angularVelocity = -200;
+ } else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
+ car.angularVelocity = 200;
+ }
+ if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+ }
+ function render() {
+ tile = map.getTileFromInputXY();
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+ }
+})();
diff --git a/Tests/tilemap/swap tiles.ts b/Tests/tilemap/swap tiles.ts
new file mode 100644
index 00000000..9344ff93
--- /dev/null
+++ b/Tests/tilemap/swap tiles.ts
@@ -0,0 +1,85 @@
+///
+///
+///
+
+(function () {
+
+ var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
+
+ function init() {
+
+ myGame.loader.addTextFile('desert', 'assets/maps/desert.json');
+ myGame.loader.addImageFile('tiles', 'assets/tiles/tmw_desert_spacing.png');
+ myGame.loader.addImageFile('car', 'assets/sprites/car90.png');
+
+ myGame.loader.load();
+
+ }
+
+ var map: Phaser.Tilemap;
+ var car: Phaser.Sprite;
+ var marker: Phaser.GeomSprite;
+ var tile: Phaser.Tile;
+
+ function create() {
+
+ map = myGame.createTilemap('tiles', 'desert', Phaser.Tilemap.FORMAT_TILED_JSON);
+
+ car = myGame.createSprite(250, 200, 'car');
+ car.setBounds(0, 0, map.widthInPixels - 32, map.heightInPixels - 32);
+
+ marker = myGame.createGeomSprite(0, 0);
+ marker.createRectangle(32, 32);
+ marker.renderFill = false;
+ marker.lineColor = 'rgb(0,0,0)';
+
+ myGame.camera.follow(car);
+ myGame.onRenderCallback = render;
+
+ myGame.input.onDown.add(swapTiles);
+ }
+
+ function swapTiles() {
+
+ map.currentLayer.swapTile(30, 31);
+
+ }
+
+ function update() {
+
+ marker.x = myGame.math.snapToFloor(myGame.input.worldX, 32);
+ marker.y = myGame.math.snapToFloor(myGame.input.worldY, 32);
+
+ car.velocity.x = 0;
+ car.velocity.y = 0;
+ car.angularVelocity = 0;
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
+ {
+ car.angularVelocity = -200;
+ }
+ else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
+ {
+ car.angularVelocity = 200;
+ }
+
+ if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
+ {
+ car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
+ }
+
+ }
+
+ function render {
+
+ tile = map.getTileFromInputXY();
+
+ myGame.stage.context.font = '18px Arial';
+ myGame.stage.context.fillStyle = 'rgb(0,0,0)';
+ myGame.stage.context.fillText(tile.toString(), 32, 32);
+
+ myGame.input.renderDebugInfo(32, 64, 'rgb(0,0,0)');
+
+ }
+
+})();
diff --git a/build/phaser.d.ts b/build/phaser.d.ts
index ff270381..49d2d8b8 100644
--- a/build/phaser.d.ts
+++ b/build/phaser.d.ts
@@ -3000,16 +3000,14 @@ module Phaser {
/**
* Fetch a random entry from the given array.
* Will return null if random selection is missing, or array has no entries.
- * G.getRandom() is deterministic and safe for use with replays/recordings.
- * HOWEVER, U.getRandom() is NOT deterministic and unsafe for use with replays/recordings.
*
- * @param Objects An array of objects.
- * @param StartIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
- * @param Length Optional restriction on the number of values you want to randomly select from.
+ * @param objects An array of objects.
+ * @param startIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
+ * @param length Optional restriction on the number of values you want to randomly select from.
*
* @return The random object that was selected.
*/
- public getRandom(Objects, StartIndex?: number, Length?: number);
+ public getRandom(objects, startIndex?: number, length?: number);
/**
* Round down to the next whole number. E.g. floor(1.7) == 1, and floor(-2.7) == -2.
*
@@ -5363,9 +5361,15 @@ module Phaser {
public heightInPixels: number;
public tileMargin: number;
public tileSpacing: number;
+ public putTile(x: number, y: number, index: number): void;
+ public swapTile(tileA: number, tileB: number, x?: number, y?: number, width?: number, height?: number): void;
+ public fillTile(index: number, x?: number, y?: number, width?: number, height?: number): void;
+ public randomiseTiles(tiles: number[], x?: number, y?: number, width?: number, height?: number): void;
+ public replaceTile(tileA: number, tileB: number, x?: number, y?: number, width?: number, height?: number): void;
+ public getTileBlock(x: number, y: number, width: number, height: number): any[];
public getTileFromWorldXY(x: number, y: number): number;
public getTileOverlaps(object: GameObject);
- public getTileBlock(x: number, y: number, width: number, height: number): void;
+ private getTempBlock(x, y, width, height, collisionOnly?);
public getTileIndex(x: number, y: number): number;
public addColumn(column): void;
public updateBounds(): void;
@@ -5454,6 +5458,7 @@ module Phaser {
public getTileOverlaps(object: GameObject);
public collide(objectOrGroup?, callback?, context?): void;
public collideGameObject(object: GameObject): bool;
+ public putTile(x: number, y: number, index: number, layer?: number): void;
}
}
/**
diff --git a/build/phaser.js b/build/phaser.js
index 4f038bfb..a32484be 100644
--- a/build/phaser.js
+++ b/build/phaser.js
@@ -6012,25 +6012,23 @@ var Phaser;
GameMath.prototype.getRandom = /**
* Fetch a random entry from the given array.
* Will return null if random selection is missing, or array has no entries.
- * G.getRandom() is deterministic and safe for use with replays/recordings.
- * HOWEVER, U.getRandom() is NOT deterministic and unsafe for use with replays/recordings.
*
- * @param Objects An array of objects.
- * @param StartIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
- * @param Length Optional restriction on the number of values you want to randomly select from.
+ * @param objects An array of objects.
+ * @param startIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
+ * @param length Optional restriction on the number of values you want to randomly select from.
*
* @return The random object that was selected.
*/
- function (Objects, StartIndex, Length) {
- if (typeof StartIndex === "undefined") { StartIndex = 0; }
- if (typeof Length === "undefined") { Length = 0; }
- if(Objects != null) {
- var l = Length;
- if((l == 0) || (l > Objects.length - StartIndex)) {
- l = Objects.length - StartIndex;
+ function (objects, startIndex, length) {
+ if (typeof startIndex === "undefined") { startIndex = 0; }
+ if (typeof length === "undefined") { length = 0; }
+ if(objects != null) {
+ var l = length;
+ if((l == 0) || (l > objects.length - startIndex)) {
+ l = objects.length - startIndex;
}
if(l > 0) {
- return Objects[StartIndex + Math.floor(Math.random() * l)];
+ return objects[startIndex + Math.floor(Math.random() * l)];
}
}
return null;
@@ -11158,6 +11156,82 @@ var Phaser;
this._tempTileBlock = [];
this._texture = this._game.cache.getImage(key);
}
+ TilemapLayer.prototype.putTile = 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(y >= 0 && y < this.mapData.length) {
+ if(x >= 0 && x < this.mapData[y].length) {
+ this.mapData[y][x] = index;
+ }
+ }
+ };
+ TilemapLayer.prototype.swapTile = function (tileA, tileB, x, y, width, height) {
+ if (typeof x === "undefined") { x = 0; }
+ if (typeof y === "undefined") { y = 0; }
+ if (typeof width === "undefined") { width = this.widthInTiles; }
+ if (typeof height === "undefined") { height = this.heightInTiles; }
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ // First sweep marking tileA as needing a new index
+ if(this._tempTileBlock[r].tile.index == tileA) {
+ this._tempTileBlock[r].newIndex = true;
+ }
+ // In the same pass we can swap tileB to tileA
+ if(this._tempTileBlock[r].tile.index == tileB) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileA;
+ }
+ }
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ // And now swap our newIndex tiles for tileB
+ if(this._tempTileBlock[r].newIndex == true) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileB;
+ }
+ }
+ };
+ TilemapLayer.prototype.fillTile = function (index, x, y, width, height) {
+ if (typeof x === "undefined") { x = 0; }
+ if (typeof y === "undefined") { y = 0; }
+ if (typeof width === "undefined") { width = this.widthInTiles; }
+ if (typeof height === "undefined") { height = this.heightInTiles; }
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = index;
+ }
+ };
+ TilemapLayer.prototype.randomiseTiles = function (tiles, x, y, width, height) {
+ if (typeof x === "undefined") { x = 0; }
+ if (typeof y === "undefined") { y = 0; }
+ if (typeof width === "undefined") { width = this.widthInTiles; }
+ if (typeof height === "undefined") { height = this.heightInTiles; }
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = this._game.math.getRandom(tiles);
+ }
+ };
+ TilemapLayer.prototype.replaceTile = function (tileA, tileB, x, y, width, height) {
+ if (typeof x === "undefined") { x = 0; }
+ if (typeof y === "undefined") { y = 0; }
+ if (typeof width === "undefined") { width = this.widthInTiles; }
+ if (typeof height === "undefined") { height = this.heightInTiles; }
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ if(this._tempTileBlock[r].tile.index == tileA) {
+ this.mapData[this._tempTileBlock[r].y][this._tempTileBlock[r].x] = tileB;
+ }
+ }
+ };
+ TilemapLayer.prototype.getTileBlock = function (x, y, width, height) {
+ var output = [];
+ this.getTempBlock(x, y, width, height);
+ for(var r = 0; r < this._tempTileBlock.length; r++) {
+ output.push({
+ x: this._tempTileBlock[r].x,
+ y: this._tempTileBlock[r].y,
+ tile: this._tempTileBlock[r].tile
+ });
+ }
+ return output;
+ };
TilemapLayer.prototype.getTileFromWorldXY = function (x, y) {
x = this._game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
y = this._game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
@@ -11174,7 +11248,8 @@ var Phaser;
this._tempTileW = (this._game.math.snapToCeil(object.bounds.width, this.tileWidth) + this.tileWidth) / this.tileWidth;
this._tempTileH = (this._game.math.snapToCeil(object.bounds.height, this.tileHeight) + this.tileHeight) / this.tileHeight;
// Loop through the tiles we've got and check overlaps accordingly (the results are stored in this._tempTileBlock)
- this.getTileBlock(this._tempTileX, this._tempTileY, this._tempTileW, this._tempTileH);
+ this._tempBlockResults = [];
+ this.getTempBlock(this._tempTileX, this._tempTileY, this._tempTileW, this._tempTileH, true);
Phaser.Collision.TILE_OVERLAP = false;
for(var r = 0; r < this._tempTileBlock.length; r++) {
if(Phaser.Collision.separateTile(object, this._tempTileBlock[r].x * this.tileWidth, this._tempTileBlock[r].y * this.tileHeight, this.tileWidth, this.tileHeight, this._tempTileBlock[r].tile.mass, this._tempTileBlock[r].tile.collideLeft, this._tempTileBlock[r].tile.collideRight, this._tempTileBlock[r].tile.collideUp, this._tempTileBlock[r].tile.collideDown, this._tempTileBlock[r].tile.separateX, this._tempTileBlock[r].tile.separateY) == true) {
@@ -11187,7 +11262,8 @@ var Phaser;
}
return this._tempBlockResults;
};
- TilemapLayer.prototype.getTileBlock = function (x, y, width, height) {
+ TilemapLayer.prototype.getTempBlock = function (x, y, width, height, collisionOnly) {
+ if (typeof collisionOnly === "undefined") { collisionOnly = false; }
if(x < 0) {
x = 0;
}
@@ -11201,16 +11277,25 @@ var Phaser;
height = this.heightInTiles;
}
this._tempTileBlock = [];
- this._tempBlockResults = [];
for(var ty = y; ty < y + height; ty++) {
for(var tx = x; tx < x + width; tx++) {
- // We only want to consider the tile for checking if you can actually collide with it
- if(this.mapData[ty] && this.mapData[ty][tx] && this._parent.tiles[this.mapData[ty][tx]].allowCollisions != Phaser.Collision.NONE) {
- this._tempTileBlock.push({
- x: tx,
- y: ty,
- tile: this._parent.tiles[this.mapData[ty][tx]]
- });
+ if(collisionOnly) {
+ // We only want to consider the tile for checking if you can actually collide with it
+ if(this.mapData[ty] && this.mapData[ty][tx] && this._parent.tiles[this.mapData[ty][tx]].allowCollisions != Phaser.Collision.NONE) {
+ this._tempTileBlock.push({
+ x: tx,
+ y: ty,
+ tile: this._parent.tiles[this.mapData[ty][tx]]
+ });
+ }
+ } else {
+ if(this.mapData[ty] && this.mapData[ty][tx]) {
+ this._tempTileBlock.push({
+ x: tx,
+ y: ty,
+ tile: this._parent.tiles[this.mapData[ty][tx]]
+ });
+ }
}
}
}
@@ -11238,8 +11323,7 @@ var Phaser;
};
TilemapLayer.prototype.updateBounds = function () {
this.boundsInTiles.setTo(0, 0, this.widthInTiles, this.heightInTiles);
- //console.log('layer bounds', this.boundsInTiles);
- };
+ };
TilemapLayer.prototype.parseTileOffsets = function () {
this._tileOffsets = [];
var i = 0;
@@ -11612,13 +11696,15 @@ var Phaser;
return false;
}
};
+ Tilemap.prototype.putTile = function (x, y, index, layer) {
+ if (typeof layer === "undefined") { layer = 0; }
+ this.layers[layer].putTile(x, y, index);
+ };
return Tilemap;
})(Phaser.GameObject);
Phaser.Tilemap = Tilemap;
// Set current layer
// Set layer order?
- // Get block of tiles
- // Swap tiles around
// Delete tiles of certain type
// Erase tiles
})(Phaser || (Phaser = {}));