public static var TILE_WIDTH:Int = 64;
public static var TILE_HEIGHT:Int = 32;
public var _MapGroup:FlxSpriteGroup;
public var _Map1:Array<Array<Int>> =
[[0, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0]];
override public function create():Void
{
super.create();
_MapGroup = new FlxSpriteGroup();
add(_MapGroup);
buildMap(_Map1);
}
public function isoTo2D(pt:FlxPoint):FlxPoint{
var tempPt:FlxPoint = new FlxPoint(0, 0);
tempPt.x = ((2 * pt.y) + pt.x) / 2;
tempPt.y = ((2 * pt.y) - pt.x) / 2;
return tempPt;
}
public function twoDToIso(pt:FlxPoint):FlxPoint{
var tempPt:FlxPoint = new FlxPoint(0,0);
tempPt.x = pt.x - pt.y;
tempPt.y = (pt.x + pt.y) / 2;
return tempPt;
}
public function placeTile(type:Int = 0, point:FlxPoint, pos:Array<Int>):Void
{
var tile:MyTile = new MyTile(point.x, point.y);
tile.loadGraphic("assets/images/tiles.png", true, TILE_WIDTH, TILE_HEIGHT);
tile._Type = type;
tile._Pos = pos;
tile.animation.add("tile0", [0]);
tile.animation.add("tile1", [1]);
tile.animation.play("tile" + tile._Type, true);
_MapGroup.add(tile);
FlxMouseEventManager.add(tile, onTileDown);
}
private function onTileDown(tile:MyTile):Void
{
var mousePoint:FlxPoint = new FlxPoint(FlxG.mouse.x, FlxG.mouse.y);
trace("Iso Coords: " + getTileCoordinates(isoTo2D(mousePoint)));
}
public function buildMap(map:Array<Array<Int>>):Void
{
for (i in 0...map.length) {
for (j in 0...map[0].length) {
var X:Int = j * TILE_WIDTH;
var Y:Int = i * TILE_HEIGHT;
placeTile(map[i][j], twoDToIso(new FlxPoint(X, Y)), [i,j]);
}
}
}
public function getTileCoordinates(point:FlxPoint):FlxPoint{
var tempPt:FlxPoint = new FlxPoint(0, 0);
tempPt.x = Math.floor(point.x / TILE_WIDTH);
tempPt.y = Math.floor(point.y / TILE_HEIGHT);
return(tempPt);
}