2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
|
|
|
|
|
|
|
function preload() {
|
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// game.load.tilemap('map', 'assets/tilemaps/maps/platform.json', null, Phaser.Tilemap.TILED_JSON);
|
|
|
|
// game.load.image('platformer_tiles', 'assets/tilemaps/tiles/platformer_tiles.png');
|
|
|
|
|
|
|
|
game.load.tilemap('map', 'assets/tilemaps/maps/features_test.json', null, Phaser.Tilemap.TILED_JSON);
|
|
|
|
game.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
|
|
|
|
game.load.image('walls_1x2', 'assets/tilemaps/tiles/walls_1x2.png');
|
|
|
|
game.load.image('tiles2', 'assets/tilemaps/tiles/tiles2.png');
|
|
|
|
|
2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var map;
|
|
|
|
var layer;
|
2014-01-29 17:10:13 +00:00
|
|
|
var mapLayer;
|
|
|
|
|
|
|
|
var polys = [];
|
|
|
|
var tiles = [];
|
|
|
|
var idx = 0;
|
|
|
|
var group = [];
|
2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
function create() {
|
|
|
|
|
|
|
|
game.stage.backgroundColor = '#124184';
|
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// map = game.add.tilemap('map');
|
|
|
|
// map.addTilesetImage('platformer_tiles');
|
|
|
|
// map.setCollisionBetween(21, 53);
|
|
|
|
// layer = map.createLayer('Tile Layer 1');
|
|
|
|
// layer.debug = true;
|
|
|
|
|
2014-01-28 17:13:07 +00:00
|
|
|
map = game.add.tilemap('map');
|
2014-01-29 17:10:13 +00:00
|
|
|
map.addTilesetImage('ground_1x1');
|
|
|
|
map.addTilesetImage('walls_1x2');
|
|
|
|
map.addTilesetImage('tiles2');
|
|
|
|
map.setCollisionBetween(1, 12);
|
|
|
|
layer = map.createLayer('Tile Layer 1');
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
mapLayer = map.layers[0];
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
tiles = getInterestingTiles();
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
console.log('found',tiles.length,'interesting tiles');
|
2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// group = [];
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// var tile = getFirstUnscanned();
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// console.log('starting from', tile);
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// floodFill(tile);
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// console.log('group collected, size', group.length);
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// now scan the group
|
|
|
|
// scanEdges();
|
2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// works great :)
|
|
|
|
while (getScannedLeft() > 0) {
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// group = [];
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
var tile = getFirstUnscanned();
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
console.log('starting from', tile);
|
|
|
|
|
|
|
|
floodFill(tile);
|
|
|
|
|
|
|
|
console.log('group collected, size', group.length);
|
|
|
|
|
|
|
|
console.log('remaining: ', getScannedLeft());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFirstUnscanned() {
|
|
|
|
|
|
|
|
for (var i = 0; i < tiles.length; i++)
|
|
|
|
{
|
|
|
|
if (!tiles[i].scanned)
|
|
|
|
{
|
|
|
|
return tiles[i];
|
2014-01-28 17:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-29 17:10:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function getScannedLeft() {
|
|
|
|
|
|
|
|
var total = tiles.length;
|
|
|
|
|
|
|
|
for (var i = 0; i < tiles.length; i++)
|
|
|
|
{
|
|
|
|
if (tiles[i].scanned)
|
|
|
|
{
|
|
|
|
total--;
|
|
|
|
}
|
2014-01-28 17:13:07 +00:00
|
|
|
}
|
2014-01-29 17:10:13 +00:00
|
|
|
|
|
|
|
return total;
|
|
|
|
|
2014-01-28 17:13:07 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
var current;
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
function scanEdges() {
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
var points = [];
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
var start = group[0];
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// special case for when group.length = 1 should go here
|
2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
function scanTop(origin) {
|
2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
function floodFill(origin) {
|
|
|
|
|
|
|
|
if (origin.scanned === true)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
origin.scanned = true;
|
|
|
|
|
|
|
|
group.push(origin);
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
// console.log('ff origin', origin.x, origin.y);
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
var west = map.getTileLeft(0, origin.x, origin.y);
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
if (west && (west.faceTop || west.faceBottom || west.faceLeft || west.faceRight))
|
|
|
|
{
|
|
|
|
// console.log('west found', west);
|
|
|
|
floodFill(west);
|
|
|
|
}
|
|
|
|
|
|
|
|
var east = map.getTileRight(0, origin.x, origin.y);
|
|
|
|
|
|
|
|
if (east && (east.faceTop || east.faceBottom || east.faceLeft || east.faceRight))
|
|
|
|
{
|
|
|
|
// console.log('east found', east);
|
|
|
|
floodFill(east);
|
|
|
|
}
|
|
|
|
|
|
|
|
var north = map.getTileAbove(0, origin.x, origin.y);
|
|
|
|
|
|
|
|
if (north && (north.faceTop || north.faceBottom || north.faceLeft || north.faceRight))
|
|
|
|
{
|
|
|
|
// console.log('north found', north);
|
|
|
|
floodFill(north);
|
|
|
|
}
|
|
|
|
|
|
|
|
var south = map.getTileBelow(0, origin.x, origin.y);
|
|
|
|
|
|
|
|
if (south && (south.faceTop || south.faceBottom || south.faceLeft || south.faceRight))
|
|
|
|
{
|
|
|
|
// console.log('south found', south);
|
|
|
|
floodFill(south);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInterestingTiles() {
|
|
|
|
|
|
|
|
var tiles = [];
|
|
|
|
|
|
|
|
for (var y = 0, h = mapLayer.height; y < h; y++)
|
|
|
|
{
|
|
|
|
for (var x = 0, w = mapLayer.width; x < w; x++)
|
|
|
|
{
|
|
|
|
var tile = mapLayer.data[y][x];
|
|
|
|
|
|
|
|
if (tile && (tile.faceTop || tile.faceBottom || tile.faceLeft || tile.faceRight))
|
|
|
|
{
|
|
|
|
// reset the scanned status
|
|
|
|
tile.scanned = false;
|
|
|
|
tiles.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tiles;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTile() {
|
|
|
|
|
|
|
|
for (var y = 0, h = mapLayer.height; y < h; y++)
|
|
|
|
{
|
|
|
|
for (var x = 0, w = mapLayer.width; x < w; x++)
|
|
|
|
{
|
|
|
|
var tile = mapLayer.data[y][x];
|
|
|
|
|
|
|
|
if (tile && (tile.faceTop || tile.faceBottom || tile.faceLeft || tile.faceRight))
|
|
|
|
{
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
}
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
function render() {
|
|
|
|
|
|
|
|
if (group)
|
|
|
|
{
|
|
|
|
game.context.fillStyle = 'rgba(255,0,0,0.7)';
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2014-01-29 17:10:13 +00:00
|
|
|
for (var i = 0; i < group.length; i++)
|
|
|
|
{
|
|
|
|
game.context.fillRect(group[i].x * group[i].width, group[i].y * group[i].height, group[i].width, group[i].height);
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 17:13:07 +00:00
|
|
|
|
|
|
|
}
|