C# Class XnaFlixel.FlxTilemap

This is a traditional tilemap display and collision class. It takes a string of comma-separated numbers and then associates those values with tiles from the sheet you pass in. It also includes some handy static parsers that can convert arrays or PNG files into strings that can be successfully loaded.
Inheritance: FlxObject
Show file Open project: jsbeckr/XnaFlixel

Public Properties

Property Type Description
ImgAuto Microsoft.Xna.Framework.Graphics.Texture2D
ImgAutoAlt Microsoft.Xna.Framework.Graphics.Texture2D
auto int
collideIndex int
drawIndex int
heightInTiles int
refresh bool
startingIndex int
totalTiles int
widthInTiles int

Protected Properties

Property Type Description
_block FlxObject
_boundsVisible bool
_data int[]
_flashRect Microsoft.Xna.Framework.Rectangle
_flashRect2 Microsoft.Xna.Framework.Rectangle
_rects List
_screenCols int
_screenRows int
_tileBitmap Microsoft.Xna.Framework.Graphics.Texture2D
_tileHeight int
_tileWidth int

Public Methods

Method Description
FlxTilemap ( ) : System

The tilemap constructor just initializes some basic variables.

Overlaps ( FlxObject Core ) : bool

Checks for overlaps between the provided object and any tiles above the collision index. @param Core The FlxObject you want to check against.

OverlapsPoint ( float X, float Y ) : bool

Checks to see if a point in 2D space overlaps a solid tile. @param X The X coordinate of the point. @param Y The Y coordinate of the point. @param PerPixel Not available in FlxTilemap, ignored. @return Whether or not the point overlaps this object.

OverlapsPoint ( float X, float Y, bool PerPixel ) : bool
PreCollide ( FlxObject Object ) : void

FlxU.collide() (and thus FlxObject.collide()) call this function each time two objects are compared to see if they collide. It doesn't necessarily mean these objects WILL collide, however. @param Object The FlxObject you're about to run into.

RefreshHulls ( ) : void

Called by FlxObject.updateMotion() and some constructors to rebuild the basic collision data for this object.

Render ( SpriteBatch spriteBatch ) : void

Draws the tilemap.

arrayToCSV ( int Data, int Width ) : string

Converts a one-dimensional array of tile data to a comma-separated string. @param Data An array full of integer tile references. @param Width The number of tiles in each row. @return A comma-separated string containing the level data in a FlxTilemap-friendly format.

bitmapToCSV ( Microsoft.Xna.Framework.Graphics.Texture2D bitmapData ) : string

Converts a BitmapData object to a comma-separated string. Black pixels are flagged as 'solid' by default, non-black pixels are set as non-colliding. Black pixels must be PURE BLACK. @param bitmapData A Texture2D, preferably black and white. @param Invert Load white pixels as solid instead. @return A comma-separated string containing the level data in a FlxTilemap-friendly format.

bitmapToCSV ( Microsoft.Xna.Framework.Graphics.Texture2D bitmapData, bool Invert ) : string
follow ( ) : void

Call this function to lock the automatic camera to the map's edges. @param Border Adjusts the camera follow boundary by whatever number of tiles you specify here. Handy for blocking off deadends that are offscreen, etc. Use a negative number to add padding instead of hiding the edges.

follow ( int Border ) : void
getTile ( int X, int Y ) : int

Check the value of a particular tile. @param X The X coordinate of the tile (in tiles, not pixels). @param Y The Y coordinate of the tile (in tiles, not pixels). @return A uint containing the value of the tile at this spot in the array.

getTileByIndex ( int Index ) : int

Get the value of a tile in the tilemap by index. @param Index The slot in the data array (Y/// widthInTiles + X) where this tile is stored. @return A uint containing the value of the tile at this spot in the array.

loadMap ( string MapData, Microsoft.Xna.Framework.Graphics.Texture2D TileGraphic ) : FlxTilemap

Load the tilemap with string data and a tile graphic. @param MapData A string of comma and line-return delineated indices indicating what order the tiles should go in. @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. @return A pointer this instance of FlxTilemap, for chaining as usual :)

loadMap ( string MapData, Microsoft.Xna.Framework.Graphics.Texture2D TileGraphic, int TileWidth, int TileHeight ) : FlxTilemap
ray ( int StartX, int StartY, int EndX, int EndY, Vector2 Result, int Resolution ) : bool

Shoots a ray from the start point to the end point. If/when it passes through a tile, it stores and returns that point. @param StartX The X component of the ray's start. @param StartY The Y component of the ray's start. @param EndX The X component of the ray's end. @param EndY The Y component of the ray's end. @param Result A Point object containing the first wall impact. @param Resolution Defaults to 1, meaning check every tile or so. Higher means more checks! @return Whether or not there was a collision between the ray and a colliding tile.

setCallback ( int Tile, int Callback, int Range ) : void

Bind a function Callback(Core:FlxCore,X:uint,Y:uint,Tile:uint) to a range of tiles. @param Tile The tile to trigger the callback. @param Callback The function to trigger. Parameters should be (Core:FlxCore,X:uint,Y:uint,Tile:uint). @param Range If you want this callback to work for a bunch of different tiles, input the range here. Default value is 1.

setTile ( int X, int Y, int Tile ) : bool

Change the data and graphic of a tile in the tilemap. @param X The X coordinate of the tile (in tiles, not pixels). @param Y The Y coordinate of the tile (in tiles, not pixels). @param Tile The new integer data you wish to inject. @param UpdateGraphics Whether the graphical representation of this tile should change. @return Whether or not the tile was actually changed.

setTile ( int X, int Y, int Tile, bool UpdateGraphics ) : bool
setTileByIndex ( int Index, int Tile, bool UpdateGraphics ) : bool

Change the data and graphic of a tile in the tilemap. @param Index The slot in the data array (Y/// widthInTiles + X) where this tile is stored. @param Tile The new integer data you wish to inject. @param UpdateGraphics Whether the graphical representation of this tile should change. @return Whether or not the tile was actually changed.

Protected Methods

Method Description
autoTile ( int Index ) : void

An internal function used by the binary auto-tilers. @param Index The index of the tile you want to analyze.

generateBoundingTiles ( ) : void

Generates a bounding box version of the tiles, flixel should call this automatically when necessary.

updateTile ( int Index ) : void

Internal function used in setTileByIndex() and the constructor to update the map. @param Index The index of the tile you want to update.

Method Details

FlxTilemap() public method

The tilemap constructor just initializes some basic variables.
public FlxTilemap ( ) : System
return System

Overlaps() public method

Checks for overlaps between the provided object and any tiles above the collision index. @param Core The FlxObject you want to check against.
public Overlaps ( FlxObject Core ) : bool
Core FlxObject
return bool

OverlapsPoint() public method

Checks to see if a point in 2D space overlaps a solid tile. @param X The X coordinate of the point. @param Y The Y coordinate of the point. @param PerPixel Not available in FlxTilemap, ignored. @return Whether or not the point overlaps this object.
public OverlapsPoint ( float X, float Y ) : bool
X float
Y float
return bool

OverlapsPoint() public method

public OverlapsPoint ( float X, float Y, bool PerPixel ) : bool
X float
Y float
PerPixel bool
return bool

PreCollide() public method

FlxU.collide() (and thus FlxObject.collide()) call this function each time two objects are compared to see if they collide. It doesn't necessarily mean these objects WILL collide, however. @param Object The FlxObject you're about to run into.
public PreCollide ( FlxObject Object ) : void
Object FlxObject
return void

RefreshHulls() public method

Called by FlxObject.updateMotion() and some constructors to rebuild the basic collision data for this object.
public RefreshHulls ( ) : void
return void

Render() public method

Draws the tilemap.
public Render ( SpriteBatch spriteBatch ) : void
spriteBatch Microsoft.Xna.Framework.Graphics.SpriteBatch
return void

arrayToCSV() public static method

Converts a one-dimensional array of tile data to a comma-separated string. @param Data An array full of integer tile references. @param Width The number of tiles in each row. @return A comma-separated string containing the level data in a FlxTilemap-friendly format.
public static arrayToCSV ( int Data, int Width ) : string
Data int
Width int
return string

autoTile() protected method

An internal function used by the binary auto-tilers. @param Index The index of the tile you want to analyze.
protected autoTile ( int Index ) : void
Index int
return void

bitmapToCSV() public static method

Converts a BitmapData object to a comma-separated string. Black pixels are flagged as 'solid' by default, non-black pixels are set as non-colliding. Black pixels must be PURE BLACK. @param bitmapData A Texture2D, preferably black and white. @param Invert Load white pixels as solid instead. @return A comma-separated string containing the level data in a FlxTilemap-friendly format.
public static bitmapToCSV ( Microsoft.Xna.Framework.Graphics.Texture2D bitmapData ) : string
bitmapData Microsoft.Xna.Framework.Graphics.Texture2D
return string

bitmapToCSV() public static method

public static bitmapToCSV ( Microsoft.Xna.Framework.Graphics.Texture2D bitmapData, bool Invert ) : string
bitmapData Microsoft.Xna.Framework.Graphics.Texture2D
Invert bool
return string

follow() public method

Call this function to lock the automatic camera to the map's edges. @param Border Adjusts the camera follow boundary by whatever number of tiles you specify here. Handy for blocking off deadends that are offscreen, etc. Use a negative number to add padding instead of hiding the edges.
public follow ( ) : void
return void

follow() public method

public follow ( int Border ) : void
Border int
return void

generateBoundingTiles() protected method

Generates a bounding box version of the tiles, flixel should call this automatically when necessary.
protected generateBoundingTiles ( ) : void
return void

getTile() public method

Check the value of a particular tile. @param X The X coordinate of the tile (in tiles, not pixels). @param Y The Y coordinate of the tile (in tiles, not pixels). @return A uint containing the value of the tile at this spot in the array.
public getTile ( int X, int Y ) : int
X int
Y int
return int

getTileByIndex() public method

Get the value of a tile in the tilemap by index. @param Index The slot in the data array (Y/// widthInTiles + X) where this tile is stored. @return A uint containing the value of the tile at this spot in the array.
public getTileByIndex ( int Index ) : int
Index int
return int

loadMap() public method

Load the tilemap with string data and a tile graphic. @param MapData A string of comma and line-return delineated indices indicating what order the tiles should go in. @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. @return A pointer this instance of FlxTilemap, for chaining as usual :)
public loadMap ( string MapData, Microsoft.Xna.Framework.Graphics.Texture2D TileGraphic ) : FlxTilemap
MapData string
TileGraphic Microsoft.Xna.Framework.Graphics.Texture2D
return FlxTilemap

loadMap() public method

public loadMap ( string MapData, Microsoft.Xna.Framework.Graphics.Texture2D TileGraphic, int TileWidth, int TileHeight ) : FlxTilemap
MapData string
TileGraphic Microsoft.Xna.Framework.Graphics.Texture2D
TileWidth int
TileHeight int
return FlxTilemap

ray() public method

Shoots a ray from the start point to the end point. If/when it passes through a tile, it stores and returns that point. @param StartX The X component of the ray's start. @param StartY The Y component of the ray's start. @param EndX The X component of the ray's end. @param EndY The Y component of the ray's end. @param Result A Point object containing the first wall impact. @param Resolution Defaults to 1, meaning check every tile or so. Higher means more checks! @return Whether or not there was a collision between the ray and a colliding tile.
public ray ( int StartX, int StartY, int EndX, int EndY, Vector2 Result, int Resolution ) : bool
StartX int
StartY int
EndX int
EndY int
Result Vector2
Resolution int
return bool

setCallback() public method

Bind a function Callback(Core:FlxCore,X:uint,Y:uint,Tile:uint) to a range of tiles. @param Tile The tile to trigger the callback. @param Callback The function to trigger. Parameters should be (Core:FlxCore,X:uint,Y:uint,Tile:uint). @param Range If you want this callback to work for a bunch of different tiles, input the range here. Default value is 1.
public setCallback ( int Tile, int Callback, int Range ) : void
Tile int
Callback int
Range int
return void

setTile() public method

Change the data and graphic of a tile in the tilemap. @param X The X coordinate of the tile (in tiles, not pixels). @param Y The Y coordinate of the tile (in tiles, not pixels). @param Tile The new integer data you wish to inject. @param UpdateGraphics Whether the graphical representation of this tile should change. @return Whether or not the tile was actually changed.
public setTile ( int X, int Y, int Tile ) : bool
X int
Y int
Tile int
return bool

setTile() public method

public setTile ( int X, int Y, int Tile, bool UpdateGraphics ) : bool
X int
Y int
Tile int
UpdateGraphics bool
return bool

setTileByIndex() public method

Change the data and graphic of a tile in the tilemap. @param Index The slot in the data array (Y/// widthInTiles + X) where this tile is stored. @param Tile The new integer data you wish to inject. @param UpdateGraphics Whether the graphical representation of this tile should change. @return Whether or not the tile was actually changed.
public setTileByIndex ( int Index, int Tile, bool UpdateGraphics ) : bool
Index int
Tile int
UpdateGraphics bool
return bool

updateTile() protected method

Internal function used in setTileByIndex() and the constructor to update the map. @param Index The index of the tile you want to update.
protected updateTile ( int Index ) : void
Index int
return void

Property Details

ImgAuto public static property

public static Texture2D,Microsoft.Xna.Framework.Graphics ImgAuto
return Microsoft.Xna.Framework.Graphics.Texture2D

ImgAutoAlt public static property

public static Texture2D,Microsoft.Xna.Framework.Graphics ImgAutoAlt
return Microsoft.Xna.Framework.Graphics.Texture2D

_block protected property

protected FlxObject _block
return FlxObject

_boundsVisible protected property

protected bool _boundsVisible
return bool

_data protected property

protected int[] _data
return int[]

_flashRect protected property

Rendering helper.
protected Rectangle,Microsoft.Xna.Framework _flashRect
return Microsoft.Xna.Framework.Rectangle

_flashRect2 protected property

protected Rectangle,Microsoft.Xna.Framework _flashRect2
return Microsoft.Xna.Framework.Rectangle

_rects protected property

protected List _rects
return List

_screenCols protected property

protected int _screenCols
return int

_screenRows protected property

protected int _screenRows
return int

_tileBitmap protected property

protected Texture2D,Microsoft.Xna.Framework.Graphics _tileBitmap
return Microsoft.Xna.Framework.Graphics.Texture2D

_tileHeight protected property

protected int _tileHeight
return int

_tileWidth protected property

protected int _tileWidth
return int

auto public property

Set this flag to use one of the 16-tile binary auto-tile algorithms (OFF, AUTO, or ALT).
public int auto
return int

collideIndex public property

What tile index will you start colliding with (default: 1).
public int collideIndex
return int

drawIndex public property

What tile index will you start drawing with (default: 1) NOTE: should always be >= startingIndex. If you want to change it, do so before calling loadMap().
public int drawIndex
return int

heightInTiles public property

Read-only variable, do NOT recommend changing after the map is loaded!
public int heightInTiles
return int

refresh public property

Set this flag to true to force the tilemap buffer to refresh on the next render frame.
public bool refresh
return bool

startingIndex public property

The first index of your tile sheet (default: 0) If you want to change it, do so before calling loadMap().
public int startingIndex
return int

totalTiles public property

Read-only variable, do NOT recommend changing after the map is loaded!
public int totalTiles
return int

widthInTiles public property

Read-only variable, do NOT recommend changing after the map is loaded!
public int widthInTiles
return int