More tilemap & layer fun: Copy, FIll, Randomize, Shuffle, GetTilesWithin

Note: methods that mutate the tiles are not installed on StaticTilemapLayer
This commit is contained in:
Michael Hadley 2017-11-15 20:06:07 -06:00
parent db5cab8494
commit 355d9accf3
8 changed files with 165 additions and 8 deletions

View file

@ -127,12 +127,34 @@ var Tilemap = new Class({
return layer;
},
copy: function (srcTileX, srcTileY, width, height, destTileX, destTileY, layer)
{
layer = this.getLayer(layer);
if (layer !== null)
{
TilemapComponents.Copy(srcTileX, srcTileY, width, height, destTileX, destTileY, layer);
}
return this;
},
fill: function (index, tileX, tileY, width, height, layer)
{
layer = this.getLayer(layer);
if (layer !== null)
{
TilemapComponents.Fill(index, tileX, tileY, width, height, layer);
}
return this;
},
forEachTile: function (callback, context, tileX, tileY, width, height, layer)
{
layer = this.getLayer(layer);
if (layer === null) { return; }
TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, layer);
if (layer !== null)
{
TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, layer);
}
return this;
},
getIndex: function (location, name)
@ -186,7 +208,6 @@ var Tilemap = new Class({
{
layer = this.getLayer(layer);
if (layer === null) { return null; }
return TilemapComponents.GetTileAt(tileX, tileY, layer, nonNull);
},
@ -194,7 +215,6 @@ var Tilemap = new Class({
{
layer = this.getLayer(layer);
if (layer === null) { return null; }
return TilemapComponents.GetTilesWithin(tileX, tileY, width, height, layer);
},
@ -207,7 +227,6 @@ var Tilemap = new Class({
{
layer = this.getLayer(layer);
if (layer === null) { return null; }
return TilemapComponents.HasTileAt(tileX, tileY, layer);
},
@ -223,6 +242,16 @@ var Tilemap = new Class({
}
},
randomize: function (tileX, tileY, width, height, indices, layer)
{
layer = this.getLayer(layer);
if (layer !== null)
{
TilemapComponents.Randomize(tileX, tileY, width, height, indices, layer);;
}
return this;
},
setLayer: function (layer)
{
var index = this.getLayerIndex(layer);
@ -231,6 +260,16 @@ var Tilemap = new Class({
this.currentLayerIndex = index;
}
return this;
},
shuffle: function (tileX, tileY, width, height, layer)
{
layer = this.getLayer(layer);
if (layer !== null)
{
TilemapComponents.Shuffle(tileX, tileY, width, height, layer);
}
return this;
}
});

View file

@ -0,0 +1,24 @@
var GetTilesWithin = require('./GetTilesWithin');
var Fill = function (srcTileX, srcTileY, width, height, destTileX, destTileY, layer)
{
if (srcTileX === undefined || srcTileX < 0) { srcTileX = 0; }
if (srcTileY === undefined || srcTileY < 0) { srcTileY = 0; }
var srcTiles = GetTilesWithin(srcTileX, srcTileY, width, height, layer);
var offsetX = destTileX - srcTileX;
var offsetY = destTileY - srcTileY;
for (var i = 0; i < srcTiles.length; i++)
{
var tileX = srcTiles[i].x + offsetX;
var tileY = srcTiles[i].y + offsetY;
if (tileX >= 0 && tileX < layer.width && tileY >= 0 && tileY < layer.height)
{
layer.data[tileY][tileX].index = srcTiles[i].index;
}
}
};
module.exports = Fill;

View file

@ -0,0 +1,12 @@
var GetTilesWithin = require('./GetTilesWithin');
var Fill = function (index, tileX, tileY, width, height, layer)
{
var tiles = GetTilesWithin(tileX, tileY, width, height, layer);
for (var i = 0; i < tiles.length; i++)
{
tiles[i].index = index;
}
};
module.exports = Fill;

View file

@ -0,0 +1,32 @@
// TODO: add options for filtering by empty, collides, interestingFace
var GetTilesWithin = function (tileX, tileY, width, height, layer)
{
if (tileX === undefined || tileX < 0) { tileX = 0; }
if (tileY === undefined || tileY < 0) { tileY = 0; }
if (width === undefined || tileX + width > layer.width)
{
width = Math.max(layer.width - tileX, 0);
}
if (height === undefined || tileY + height > layer.height)
{
height = Math.max(layer.height - tileY, 0);
}
var results = [];
for (var ty = tileY; ty < tileY + height; ty++)
{
for (var tx = tileX; tx < tileX + width; tx++)
{
var tile = layer.data[ty][tx];
if (tile !== null)
{
results.push(tile);
}
}
}
return results;
};
module.exports = GetTilesWithin;

View file

@ -0,0 +1,28 @@
var GetTilesWithin = require('./GetTilesWithin');
var GetRandomElement = require('../../../utils/array/GetRandomElement');
var Shuffle = function (tileX, tileY, width, height, indices, layer)
{
var i;
var tiles = GetTilesWithin(tileX, tileY, width, height, layer);
// If no indicies are given, then find all the unique indices within the specified region
if (indices === undefined)
{
indices = [];
for (i = 0; i < tiles.length; i++)
{
if (indices.indexOf(tiles[i].index) === -1)
{
indices.push(tiles[i].index);
}
}
}
for (i = 0; i < tiles.length; i++)
{
tiles[i].index = GetRandomElement(indices);
}
};
module.exports = Shuffle;

View file

@ -0,0 +1,17 @@
var GetTilesWithin = require('./GetTilesWithin');
var ShuffleArray = require('../../../utils/array/Shuffle');
var Shuffle = function (tileX, tileY, width, height, layer)
{
var tiles = GetTilesWithin(tileX, tileY, width, height, layer);
var indices = tiles.map(function (tile) { return tile.index; });
ShuffleArray(indices);
for (var i = 0; i < tiles.length; i++)
{
tiles[i].index = indices[i];
}
};
module.exports = Shuffle;

View file

@ -1,8 +1,12 @@
module.exports = {
Copy: require('./Copy'),
Fill: require('./Fill'),
ForEachTile: require('./ForEachTile'),
GetTileAt: require('./GetTileAt'),
GetTilesWithin: require('./GetTilesWithin'),
HasTileAt: require('./HasTileAt'),
GetTilesWithin: require('./GetTilesWithin')
Randomize: require('./Randomize'),
Shuffle: require('./Shuffle')
};

View file

@ -271,7 +271,8 @@ var StaticTilemapLayer = new Class({
forEachTile: function (callback, context, tileX, tileY, width, height)
{
return TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, this.layer);
TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, this.layer);
return this;
},
getTileAt: function (tileX, tileY, nonNull)