Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

47 tile autotiling (fixed from #2125) #2184

Merged
merged 8 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/images/tile/autotiles_full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 67 additions & 2 deletions flixel/tile/FlxBaseTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
*/
public var auto:FlxTilemapAutoTiling = OFF;

static var offsetAutoTile:Array<Int> =
[
0, 0, 0, 0, 2, 2, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
11, 11, 0, 0, 13, 13, 0, 14, 0, 0, 0, 0, 18, 18, 0, 19,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51, 51, 0, 0, 53, 53, 0, 54, 0, 0, 0, 0, 0, 0, 0, 0,
62, 62, 0, 0, 64, 64, 0, 65, 0, 0, 0, 0, 69, 69, 0, 70,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86, 86, 0, 0, 88, 88, 0, 89, 0, 0, 0, 0, 93, 93, 0, 94,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 159, 0, 0, 0, 162, 0, 163, 0, 0, 0, 0, 0, 0, 0, 0,
0, 172, 0, 0, 0, 175, 0, 176, 0, 0, 0, 0, 0, 181, 0, 182,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 199, 0, 0, 0, 202, 0, 203, 0, 0, 0, 0, 0, 208, 0, 209
];

public var widthInTiles(default, null):Int = 0;

public var heightInTiles(default, null):Int = 0;
Expand Down Expand Up @@ -420,7 +440,7 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
}

/**
* An internal function used by the binary auto-tilers.
* An internal function used by the binary auto-tilers. (16 tiles)
*
* @param Index The index of the tile you want to analyze.
*/
Expand All @@ -430,6 +450,12 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
{
return;
}

if (auto == FULL)
{
autoTileFull(Index);
return;
}

_data[Index] = 0;

Expand Down Expand Up @@ -482,6 +508,41 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
_data[Index] += 1;
}

/**
* An internal function used by the binary auto-tilers. (47 tiles)
*
* @param Index The index of the tile you want to analyze.
*/
function autoTileFull(Index:Int):Void
{
_data[Index] = 0;

var wallUp:Bool = Index - widthInTiles < 0;
var wallRight:Bool = Index % widthInTiles >= widthInTiles - 1;
var wallDown:Bool = Std.int(Index + widthInTiles) >= totalTiles;
var wallLeft:Bool = Index % widthInTiles <= 0;

var up = wallUp || _data[Index - widthInTiles] > 0;
var upRight = wallUp || wallRight || _data[Index - widthInTiles + 1] > 0;
var right = wallRight || _data[Index + 1] > 0;
var rightDown = wallRight || wallDown || _data[Index + widthInTiles + 1] > 0;
var down = wallDown || _data[Index + widthInTiles] > 0;
var downLeft = wallDown || wallLeft || _data[Index + widthInTiles - 1] > 0;
var left = wallLeft || _data[Index - 1] > 0;
var leftUp = wallLeft || wallUp || _data[Index - widthInTiles - 1] > 0;

if (up) _data[Index] += 1;
if (upRight && up && right) _data[Index] += 2;
if (right) _data[Index] += 4;
if (rightDown && right && down) _data[Index] += 8;
if (down) _data[Index] += 16;
if (downLeft && down && left) _data[Index] += 32;
if (left) _data[Index] += 64;
if (leftUp && left && up) _data[Index] += 128;

_data[Index] -= offsetAutoTile[_data[Index]] - 1;
}

/**
* Set custom tile mapping and/or randomization rules prior to loading. This MUST be called BEFORE loadMap().
* WARNING: Using this will cause your maps to take longer to load. Be careful using this in very large tilemaps.
Expand Down Expand Up @@ -1284,6 +1345,10 @@ enum FlxTilemapAutoTiling
* Better for levels with thick walls that look better with interior corner art.
*/
ALT;
/**
* Better for all, but need 47 tiles.
*/
FULL;
}

@:enum
Expand All @@ -1301,4 +1366,4 @@ abstract FlxTilemapDiagonalPolicy(Int)
* Diagonal movement costs one more than orthogonal movement
*/
var WIDE = 2;
}
}
3 changes: 3 additions & 0 deletions flixel/tile/FlxTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class GraphicAuto extends BitmapData {}
@:keep @:bitmap("assets/images/tile/autotiles_alt.png")
class GraphicAutoAlt extends BitmapData {}

@:keep @:bitmap("assets/images/tile/autotiles_full.png")
class GraphicAutoFull extends BitmapData {}

// TODO: try to solve "tile tearing problem" (1px gap between tile at certain conditions) on native targets

/**
Expand Down