Lots of new tile map commands and tests.

This commit is contained in:
Richard Davey 2013-05-02 05:01:34 +01:00
parent 2a5b6ef12a
commit 54a5e6477c
24 changed files with 1339 additions and 83 deletions

View file

@ -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.
* <code>G.getRandom()</code> is deterministic and safe for use with replays/recordings.
* HOWEVER, <code>U.getRandom()</code> 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)];
}
}

View file

@ -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

View file

@ -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 {

View file

@ -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.

View file

@ -159,12 +159,40 @@
<Content Include="tilemap\collision.js">
<DependentUpon>collision.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="tilemap\fill tiles.ts" />
<Content Include="tilemap\fill tiles.js">
<DependentUpon>fill tiles.ts</DependentUpon>
</Content>
<Content Include="tilemap\get tile.js">
<DependentUpon>get tile.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="tilemap\put tile.ts" />
<TypeScriptCompile Include="tilemap\map draw.ts" />
<Content Include="tilemap\map draw.js">
<DependentUpon>map draw.ts</DependentUpon>
</Content>
<Content Include="tilemap\put tile.js">
<DependentUpon>put tile.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="tilemap\replace tiles.ts" />
<TypeScriptCompile Include="tilemap\random tiles.ts" />
<Content Include="tilemap\random tiles.js">
<DependentUpon>random tiles.ts</DependentUpon>
</Content>
<Content Include="tilemap\replace tiles.js">
<DependentUpon>replace tiles.ts</DependentUpon>
</Content>
<Content Include="tilemap\small map.js">
<DependentUpon>small map.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="tilemap\swap tiles.ts" />
<TypeScriptCompile Include="tilemap\sprite draw tiles.ts" />
<Content Include="tilemap\sprite draw tiles.js">
<DependentUpon>sprite draw tiles.ts</DependentUpon>
</Content>
<Content Include="tilemap\swap tiles.js">
<DependentUpon>swap tiles.ts</DependentUpon>
</Content>
<Content Include="tweens\bounce.js">
<DependentUpon>bounce.ts</DependentUpon>
</Content>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="50" height="40" tilewidth="16" tileheight="16">
<tileset firstgid="1" name="platformer_tiles" tilewidth="16" tileheight="16">
<image source="../../../../kiwi-lite/Test Suite/assets/tiles/platformer_tiles.png" width="304" height="96"/>
</tileset>
<layer name="Tile Layer 1" width="50" height="40">
<data encoding="base64" compression="zlib">
eJztltsNwyAMRekQmaadod1/m4aPSJYV8BPHIK501Y8a4xPM4yilHIv4NdijdXFgzcqxynpk5JCMl3BQc2k5PHJwOTzq0OTnxkb3lXW/tMZTHHj80xytHJCjF+fBYa0fC+Zr3R84PnIPaOqI2h939Xl9hxLIIZGm/7JySMdk5NCwYA6NtOM4ebnfA55Xlh6g5rSsmYYD/mpqv5uTG2vp0zsOmJNioWTpV4l6feWxdyMYqvB97nHWwFycGI85ORyj7gDNfmj9x3lfaSTpO8u6jeToraFn30J5cmjPTA+N6qtoZX1fZeUYbev76lImDlzTk/LgmFGbI5cqw+f0e3JXht/p7+RegWF7e5v2H5fwcsk=
</data>
</layer>
</map>

File diff suppressed because one or more lines are too long

View file

@ -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.
* <code>G.getRandom()</code> is deterministic and safe for use with replays/recordings.
* HOWEVER, <code>U.getRandom()</code> 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 = {}));

View file

@ -0,0 +1,54 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

View file

@ -0,0 +1,86 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

39
Tests/tilemap/map draw.js Normal file
View file

@ -0,0 +1,39 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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);
}
}
})();

58
Tests/tilemap/map draw.ts Normal file
View file

@ -0,0 +1,58 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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);
}
}
})();

52
Tests/tilemap/put tile.js Normal file
View file

@ -0,0 +1,52 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

83
Tests/tilemap/put tile.ts Normal file
View file

@ -0,0 +1,83 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

View file

@ -0,0 +1,60 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

View file

@ -0,0 +1,85 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

View file

@ -0,0 +1,53 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

View file

@ -0,0 +1,85 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

View file

@ -0,0 +1,36 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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));
}
}
})();

View file

@ -0,0 +1,59 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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));
}
}
})();

View file

@ -0,0 +1,53 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

View file

@ -0,0 +1,85 @@
/// <reference path="../../Phaser/gameobjects/Tilemap.ts" />
/// <reference path="../../Phaser/system/Tile.ts" />
/// <reference path="../../Phaser/Game.ts" />
(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)');
}
})();

19
build/phaser.d.ts vendored
View file

@ -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.
* <code>G.getRandom()</code> is deterministic and safe for use with replays/recordings.
* HOWEVER, <code>U.getRandom()</code> 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;
}
}
/**

View file

@ -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.
* <code>G.getRandom()</code> is deterministic and safe for use with replays/recordings.
* HOWEVER, <code>U.getRandom()</code> 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 = {}));