Skip to content

Commit

Permalink
Merge pull request #1659 from larsiusprime/tilemap_diagonal
Browse files Browse the repository at this point in the history
Tilemap DiagonalPolicy
  • Loading branch information
Gama11 committed Nov 16, 2015
2 parents 7d6017b + 883df2f commit 7f2fb0d
Showing 1 changed file with 77 additions and 55 deletions.
132 changes: 77 additions & 55 deletions flixel/tile/FlxBaseTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,10 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
* @param End The end point in world coordinates.
* @param Simplify Whether to run a basic simplification algorithm over the path data, removing extra points that are on the same line. Default value is true.
* @param RaySimplify Whether to run an extra raycasting simplification algorithm over the remaining path data. This can result in some close corners being cut, and should be used with care if at all (yet). Default value is false.
* @param WideDiagonal Whether to require an additional tile to make diagonal movement. Default value is true;
* @param DiagonalPolicy How to treat diagonal movement. (Default is WIDE, count +1 tile for diagonal movement)
* @return An Array of FlxPoints, containing all waypoints from the start to the end. If no path could be found, then a null reference is returned.
*/
public function findPath(Start:FlxPoint, End:FlxPoint, Simplify:Bool = true, RaySimplify:Bool = false, WideDiagonal:Bool = true):Array<FlxPoint>
public function findPath(Start:FlxPoint, End:FlxPoint, Simplify:Bool = true, RaySimplify:Bool = false, DiagonalPolicy:FlxTilemapDiagonalPolicy = WIDE):Array<FlxPoint>
{
// Figure out what tile we are starting and ending on.
var startIndex:Int = getTileIndexByCoords(Start);
Expand All @@ -714,7 +714,7 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
}

// Figure out how far each of the tiles is from the starting tile
var distances:Array<Int> = computePathDistance(startIndex, endIndex, WideDiagonal);
var distances:Array<Int> = computePathDistance(startIndex, endIndex, DiagonalPolicy);

if (distances == null)
{
Expand Down Expand Up @@ -765,11 +765,11 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
*
* @param StartIndex The starting tile's map index.
* @param EndIndex The ending tile's map index.
* @param WideDiagonal Whether to require an additional tile to make diagonal movement. Default value is true.
* @param DiagonalPolicy How to treat diagonal movement.
* @param StopOnEnd Whether to stop at the end or not (default true)
* @return A Flash Array of FlxPoint nodes. If the end tile could not be found, then a null Array is returned instead.
*/
public function computePathDistance(StartIndex:Int, EndIndex:Int, WideDiagonal:Bool, StopOnEnd:Bool = true):Array<Int>
public function computePathDistance(StartIndex:Int, EndIndex:Int, DiagonalPolicy:FlxTilemapDiagonalPolicy, StopOnEnd:Bool = true):Array<Int>
{
// Create a distance-based representation of the tilemap.
// All walls are flagged as -2, all open areas as -1.
Expand Down Expand Up @@ -872,64 +872,69 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
neighbors.push(index);
}
}
if (up && right)
{
index = currentIndex - widthInTiles + 1;

if (WideDiagonal && (distances[index] == -1) && (distances[currentIndex-widthInTiles] >= -1) && (distances[currentIndex+1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
else if (!WideDiagonal && (distances[index] == -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
if (right && down)
{
index = currentIndex + widthInTiles + 1;

if (WideDiagonal && (distances[index] == -1) && (distances[currentIndex+widthInTiles] >= -1) && (distances[currentIndex+1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
else if (!WideDiagonal && (distances[index] == -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
if (left && down)

if (DiagonalPolicy != NONE)
{
index = currentIndex + widthInTiles - 1;

if (WideDiagonal && (distances[index] == -1) && (distances[currentIndex+widthInTiles] >= -1) && (distances[currentIndex-1] >= -1))
var WideDiagonal = DiagonalPolicy == WIDE;
if (up && right)
{
distances[index] = distance;
neighbors.push(index);
index = currentIndex - widthInTiles + 1;

if (WideDiagonal && (distances[index] == -1) && (distances[currentIndex-widthInTiles] >= -1) && (distances[currentIndex+1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
else if (!WideDiagonal && (distances[index] == -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
else if (!WideDiagonal && (distances[index] == -1))
if (right && down)
{
distances[index] = distance;
neighbors.push(index);
index = currentIndex + widthInTiles + 1;

if (WideDiagonal && (distances[index] == -1) && (distances[currentIndex+widthInTiles] >= -1) && (distances[currentIndex+1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
else if (!WideDiagonal && (distances[index] == -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
}
if (up && left)
{
index = currentIndex - widthInTiles - 1;

if (WideDiagonal && (distances[index] == -1) && (distances[currentIndex-widthInTiles] >= -1) && (distances[currentIndex-1] >= -1))
if (left && down)
{
distances[index] = distance;
neighbors.push(index);
index = currentIndex + widthInTiles - 1;

if (WideDiagonal && (distances[index] == -1) && (distances[currentIndex+widthInTiles] >= -1) && (distances[currentIndex-1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
else if (!WideDiagonal && (distances[index] == -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
else if (!WideDiagonal && (distances[index] == -1))
if (up && left)
{
distances[index] = distance;
neighbors.push(index);
index = currentIndex - widthInTiles - 1;

if (WideDiagonal && (distances[index] == -1) && (distances[currentIndex-widthInTiles] >= -1) && (distances[currentIndex-1] >= -1))
{
distances[index] = distance;
neighbors.push(index);
}
else if (!WideDiagonal && (distances[index] == -1))
{
distances[index] = distance;
neighbors.push(index);
}
}
}
}
Expand Down Expand Up @@ -1248,3 +1253,20 @@ enum FlxTilemapAutoTiling
*/
ALT;
}

@:enum
abstract FlxTilemapDiagonalPolicy(Int)
{
/**
* No diagonal movement allowed when calculating the path
*/
var NONE = 0;
/**
* Diagonal movement costs the same as orthogonal movement
*/
var NORMAL = 1;
/**
* Diagonal movement costs one more than orthogonal movement
*/
var WIDE = 2;
}

0 comments on commit 7f2fb0d

Please sign in to comment.