-
Notifications
You must be signed in to change notification settings - Fork 435
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
FlxTilemap#loadMap(): support Array<Array<Int>> instead of Array<Int>, closes #1290, closes #1324 #1292
Merged
Merged
FlxTilemap#loadMap(): support Array<Array<Int>> instead of Array<Int>, closes #1290, closes #1324 #1292
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa4b166
FlxTilemap#loadMap(): support Array<Array<Int>> instead of Array<Int>…
Gama11 2b863e6
Merge branch 'dev' into tilemapLoadMap2Darray
Gama11 0751bf8
Split loadMap() into csv, array and 2d array versions
Gama11 50de18d
Merge branch 'dev' into tilemapLoadMap2Darray
Gama11 15b13b4
Fix merge conflicts
Gama11 f5bf528
No need for casting anymore
Gama11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import flixel.FlxObject; | |
import flixel.math.FlxPoint; | ||
import flixel.math.FlxRandom; | ||
import flixel.math.FlxRect; | ||
import flixel.tile.FlxBaseTilemap.FlxTilemapAutoTiling; | ||
import flixel.util.FlxArrayUtil; | ||
import flixel.system.FlxAssets; | ||
import flixel.group.FlxGroup; | ||
|
@@ -16,18 +15,13 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject | |
* Set this flag to use one of the 16-tile binary auto-tile algorithms (OFF, AUTO, or ALT). | ||
*/ | ||
public var auto:FlxTilemapAutoTiling = OFF; | ||
/** | ||
* Read-only variable, do NOT recommend changing after the map is loaded! | ||
*/ | ||
public var widthInTiles:Int = 0; | ||
/** | ||
* Read-only variable, do NOT recommend changing after the map is loaded! | ||
*/ | ||
public var heightInTiles:Int = 0; | ||
/** | ||
* Read-only variable, do NOT recommend changing after the map is loaded! | ||
*/ | ||
public var totalTiles:Int = 0; | ||
|
||
public var widthInTiles(default, null):Int = 0; | ||
|
||
public var heightInTiles(default, null):Int = 0; | ||
|
||
public var totalTiles(default, null):Int = 0; | ||
|
||
/** | ||
* Set this to create your own image index remapper, so you can create your own tile layouts. | ||
* Mostly useful in combination with the auto-tilers. | ||
|
@@ -156,8 +150,7 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject | |
/** | ||
* Load the tilemap with string data and a tile graphic. | ||
* | ||
* @param MapData A csv-formatted string indicating what order the tiles should go in (or the path to that file), | ||
* or an Array<Int>. In the latter case YOU MUST SET widthInTiles and heightInTyles manually BEFORE CALLING loadMap()! | ||
* @param MapData A csv-formatted string indicating what order the tiles should go in (or the path to that file) | ||
* @param TileGraphic All the tiles you want to use, arranged in a strip corresponding to the numbers in MapData. | ||
* @param TileWidth The width of your tiles (e.g. 8) - defaults to height of the tile graphic if unspecified. | ||
* @param TileHeight The height of your tiles (e.g. 8) - defaults to width if unspecified. | ||
|
@@ -172,9 +165,146 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject | |
* Can override and customize per-tile-type collision behavior using setTileProperties(). | ||
* @return A reference to this instance of FlxTilemap, for chaining as usual :) | ||
*/ | ||
public function loadMap(MapData:FlxTilemapAsset, TileGraphic:FlxTilemapGraphicAsset, TileWidth:Int = 0, TileHeight:Int = 0, | ||
public function loadMapFromCSV(MapData:String, TileGraphic:FlxTilemapGraphicAsset, TileWidth:Int = 0, TileHeight:Int = 0, | ||
?AutoTile:FlxTilemapAutoTiling, StartingIndex:Int = 0, DrawIndex:Int = 1, CollideIndex:Int = 1) | ||
{ | ||
// path to map data file? | ||
if (Assets.exists(MapData)) | ||
{ | ||
MapData = Assets.getText(MapData); | ||
} | ||
|
||
// Figure out the map dimensions based on the data string | ||
_data = new Array<Int>(); | ||
var columns:Array<String>; | ||
var rows:Array<String> = StringTools.trim(cast (MapData, String)).split("\n"); | ||
heightInTiles = rows.length; | ||
widthInTiles = 0; | ||
var row:Int = 0; | ||
var column:Int; | ||
|
||
while (row < heightInTiles) | ||
{ | ||
columns = rows[row++].split(","); | ||
|
||
if (columns.length < 1) | ||
{ | ||
heightInTiles = heightInTiles - 1; | ||
continue; | ||
} | ||
if (widthInTiles == 0) | ||
{ | ||
widthInTiles = columns.length; | ||
} | ||
column = 0; | ||
|
||
while (column < widthInTiles) | ||
{ | ||
//the current tile to be added: | ||
var curTile:Int = Std.parseInt(columns[column]); | ||
|
||
if (curTile < 0) | ||
{ | ||
// anything < 0 should be treated as 0 for compatibility with certain map formats (ogmo) | ||
curTile = 0; | ||
} | ||
|
||
//if neko, make sure the value was not null, and if it is null, | ||
//make sure it is the last in the row (used to ignore commas) | ||
#if neko | ||
if (curTile != null) | ||
{ | ||
_data.push(curTile); | ||
column++; | ||
} | ||
else if (column == columns.length - 1) | ||
{ | ||
//if value was a comma, decrease the width by one | ||
widthInTiles--; | ||
} | ||
else | ||
{ | ||
//if a non-int value was passed not at the end, warn the user | ||
throw "Value passed wan NaN"; | ||
} | ||
#else | ||
//if not neko, dont worry about the comma | ||
_data.push(curTile); | ||
column++; | ||
#end | ||
|
||
} | ||
} | ||
|
||
loadMapHelper(TileGraphic, TileWidth, TileHeight, AutoTile, StartingIndex, DrawIndex, CollideIndex); | ||
return this; | ||
} | ||
|
||
/** | ||
* Load the tilemap with string data and a tile graphic. | ||
* | ||
* @param MapData An array containing the tile indices | ||
* @param WidthInTiles The width of the tilemap in tiles | ||
* @param HeightInTiles The height of the tilemap in tiles | ||
* @param TileGraphic All the tiles you want to use, arranged in a strip corresponding to the numbers in MapData. | ||
* @param TileWidth The width of your tiles (e.g. 8) - defaults to height of the tile graphic if unspecified. | ||
* @param TileHeight The height of your tiles (e.g. 8) - defaults to width if unspecified. | ||
* @param AutoTile Whether to load the map using an automatic tile placement algorithm (requires 16 tiles!). | ||
* Setting this to either AUTO or ALT will override any values you put for StartingIndex, DrawIndex, or CollideIndex. | ||
* @param StartingIndex Used to sort of insert empty tiles in front of the provided graphic. | ||
* Default is 0, usually safest ot leave it at that. Ignored if AutoTile is set. | ||
* @param DrawIndex Initializes all tile objects equal to and after this index as visible. | ||
* Default value is 1. Ignored if AutoTile is set. | ||
* @param CollideIndex Initializes all tile objects equal to and after this index as allowCollisions = ANY. | ||
* Default value is 1. Ignored if AutoTile is set. | ||
* Can override and customize per-tile-type collision behavior using setTileProperties(). | ||
* @return A reference to this instance of FlxTilemap, for chaining as usual :) | ||
*/ | ||
public function loadMapFromArray(MapData:Array<Int>, WidthInTiles:Int, HeightInTiles:Int, TileGraphic:FlxTilemapGraphicAsset, | ||
TileWidth:Int = 0, TileHeight:Int = 0, ?AutoTile:FlxTilemapAutoTiling, StartingIndex:Int = 0, DrawIndex:Int = 1, CollideIndex:Int = 1) | ||
{ | ||
widthInTiles = WidthInTiles; | ||
heightInTiles = HeightInTiles; | ||
_data = cast MapData; // need to cast this to make sure it works in js, can't call copy() on a Dynamic | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, why casting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason for casting, simply for got to update those lines. Thanks, fixed! |
||
_data = _data.copy(); // make a copy to make sure we don't mess with the original array, which might be used for something! | ||
|
||
loadMapHelper(TileGraphic, TileWidth, TileHeight, AutoTile, StartingIndex, DrawIndex, CollideIndex); | ||
return this; | ||
} | ||
|
||
/** | ||
* Load the tilemap with string data and a tile graphic. | ||
* | ||
* @param MapData A 2D array containing the tile indices. The length of the inner arrays should be consistent. | ||
* @param TileGraphic All the tiles you want to use, arranged in a strip corresponding to the numbers in MapData. | ||
* @param TileWidth The width of your tiles (e.g. 8) - defaults to height of the tile graphic if unspecified. | ||
* @param TileHeight The height of your tiles (e.g. 8) - defaults to width if unspecified. | ||
* @param AutoTile Whether to load the map using an automatic tile placement algorithm (requires 16 tiles!). | ||
* Setting this to either AUTO or ALT will override any values you put for StartingIndex, DrawIndex, or CollideIndex. | ||
* @param StartingIndex Used to sort of insert empty tiles in front of the provided graphic. | ||
* Default is 0, usually safest ot leave it at that. Ignored if AutoTile is set. | ||
* @param DrawIndex Initializes all tile objects equal to and after this index as visible. | ||
* Default value is 1. Ignored if AutoTile is set. | ||
* @param CollideIndex Initializes all tile objects equal to and after this index as allowCollisions = ANY. | ||
* Default value is 1. Ignored if AutoTile is set. | ||
* Can override and customize per-tile-type collision behavior using setTileProperties(). | ||
* @return A reference to this instance of FlxTilemap, for chaining as usual :) | ||
*/ | ||
public function loadMapFrom2DArray(MapData:Array<Array<Int>>, TileGraphic:FlxTilemapGraphicAsset, TileWidth:Int = 0, TileHeight:Int = 0, | ||
?AutoTile:FlxTilemapAutoTiling, StartingIndex:Int = 0, DrawIndex:Int = 1, CollideIndex:Int = 1) | ||
{ | ||
widthInTiles = MapData[0].length; | ||
heightInTiles = MapData.length; | ||
_data = FlxArrayUtil.flatten2DArray(MapData); | ||
|
||
loadMapHelper(TileGraphic, TileWidth, TileHeight, AutoTile, StartingIndex, DrawIndex, CollideIndex); | ||
return this; | ||
} | ||
|
||
private function loadMapHelper(TileGraphic:FlxTilemapGraphicAsset, TileWidth:Int = 0, TileHeight:Int = 0, ?AutoTile:FlxTilemapAutoTiling, | ||
StartingIndex:Int = 0, DrawIndex:Int = 1, CollideIndex:Int = 1) | ||
{ | ||
totalTiles = _data.length; | ||
auto = (AutoTile == null) ? OFF : AutoTile; | ||
_startingIndex = (StartingIndex <= 0) ? 0 : StartingIndex; | ||
|
||
|
@@ -185,8 +315,6 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject | |
CollideIndex = 1; | ||
} | ||
|
||
loadMapData(MapData); | ||
|
||
_drawIndex = DrawIndex; | ||
_collideIndex = CollideIndex; | ||
|
||
|
@@ -195,105 +323,15 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject | |
randomizeIndices(); | ||
cacheGraphics(TileWidth, TileHeight, TileGraphic); | ||
postGraphicLoad(); | ||
|
||
return this; | ||
} | ||
|
||
private function postGraphicLoad() | ||
{ | ||
initTileObjects(); | ||
computeDimensions(); | ||
updateMap(); | ||
} | ||
|
||
private function loadMapData(MapData:FlxTilemapAsset) | ||
{ | ||
// Populate data if MapData is a CSV string | ||
if (Std.is(MapData, String)) | ||
{ | ||
// path to map data file? | ||
if (Assets.exists(MapData)) | ||
{ | ||
MapData = Assets.getText(MapData); | ||
} | ||
|
||
// Figure out the map dimensions based on the data string | ||
_data = new Array<Int>(); | ||
var columns:Array<String>; | ||
var rows:Array<String> = StringTools.trim(cast (MapData, String)).split("\n"); | ||
heightInTiles = rows.length; | ||
widthInTiles = 0; | ||
var row:Int = 0; | ||
var column:Int; | ||
|
||
while (row < heightInTiles) | ||
{ | ||
columns = rows[row++].split(","); | ||
|
||
if (columns.length < 1) | ||
{ | ||
heightInTiles = heightInTiles - 1; | ||
continue; | ||
} | ||
if (widthInTiles == 0) | ||
{ | ||
widthInTiles = columns.length; | ||
} | ||
column = 0; | ||
|
||
while (column < widthInTiles) | ||
{ | ||
//the current tile to be added: | ||
var curTile:Int = Std.parseInt(columns[column]); | ||
|
||
if (curTile < 0) | ||
{ | ||
// anything < 0 should be treated as 0 for compatibility with certain map formats (ogmo) | ||
curTile = 0; | ||
} | ||
|
||
//if neko, make sure the value was not null, and if it is null, | ||
//make sure it is the last in the row (used to ignore commas) | ||
#if neko | ||
if (curTile != null) | ||
{ | ||
_data.push(curTile); | ||
column++; | ||
} | ||
else if (column == columns.length - 1) | ||
{ | ||
//if value was a comma, decrease the width by one | ||
widthInTiles--; | ||
} | ||
else | ||
{ | ||
//if a non-int value was passed not at the end, warn the user | ||
throw "Value passed wan NaN"; | ||
} | ||
#else | ||
//if not neko, dont worry about the comma | ||
_data.push(curTile); | ||
column++; | ||
#end | ||
|
||
} | ||
} | ||
} | ||
// Data is already set up as an Array<Int> | ||
// DON'T FORGET TO SET 'widthInTiles' and 'heightInTiles' manually BEFORE CALLING loadMap() if you pass an Array<Int>! | ||
else if (Std.is(MapData, Array)) | ||
{ | ||
_data = cast MapData; // need to cast this to make sure it works in js, can't call copy() on a Dynamic | ||
_data = _data.copy(); // make a copy to make sure we don't mess with the original array, which might be used for something! | ||
} | ||
else | ||
{ | ||
throw "Unexpected MapData format '" + Type.typeof(MapData) + "' passed into loadMap. Map data must be CSV string or Array<Int>."; | ||
} | ||
|
||
totalTiles = _data.length; | ||
} | ||
|
||
private function applyAutoTile():Void | ||
{ | ||
// Pre-process the map data if it's auto-tiled | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why casting here?