Class TileGrid
Manages a 2D grid of GameTiles for grid-based games. Provides efficient access, spatial queries, and occupancy management.
public class TileGrid
- Inheritance
-
TileGrid
- Inherited Members
Constructors
TileGrid(int, int, Vector2, Vector3)
Creates a new tile grid with specified dimensions and tile size.
public TileGrid(int width, int height, Vector2 tileSize, Vector3 gridOrigin = default)
Parameters
widthintNumber of tiles horizontally
heightintNumber of tiles vertically
tileSizeVector2Size of each tile (width, height)
gridOriginVector3World position of the grid center
Properties
GridOrigin
public Vector3 GridOrigin { get; }
Property Value
Height
public int Height { get; }
Property Value
TileSize
public Vector2 TileSize { get; }
Property Value
Width
public int Width { get; }
Property Value
Methods
ClearOccupant(int, int)
Clears the occupant from the tile at the specified coordinates.
public object? ClearOccupant(int x, int y)
Parameters
Returns
- object
The removed occupant, if any
FindPath(int, int, int, int)
Finds a path between two tiles using simple A* pathfinding. Useful for AI movement in 2D games.
public List<GameTile> FindPath(int startX, int startY, int endX, int endY)
Parameters
startXintStart grid X coordinate
startYintStart grid Y coordinate
endXintEnd grid X coordinate
endYintEnd grid Y coordinate
Returns
GetAllTiles()
Gets all tiles in the grid.
public IEnumerable<GameTile> GetAllTiles()
Returns
- IEnumerable<GameTile>
Collection of all tiles
GetEmptyTiles()
Gets all tiles that are empty (no occupants).
public IEnumerable<GameTile> GetEmptyTiles()
Returns
- IEnumerable<GameTile>
Collection of empty tiles
GetNeighbors(int, int)
Gets all neighboring tiles of the specified tile (4-directional).
public IEnumerable<GameTile> GetNeighbors(int x, int y)
Parameters
Returns
- IEnumerable<GameTile>
Collection of neighboring tiles
GetNeighbors8(int, int)
Gets all neighboring tiles of the specified tile (8-directional).
public IEnumerable<GameTile> GetNeighbors8(int x, int y)
Parameters
Returns
- IEnumerable<GameTile>
Collection of neighboring tiles including diagonals
GetOccupiedTiles()
Gets all tiles that currently have occupants.
public IEnumerable<GameTile> GetOccupiedTiles()
Returns
- IEnumerable<GameTile>
Collection of occupied tiles
GetTile(int, int)
Gets the tile at the specified grid coordinates.
public GameTile? GetTile(int x, int y)
Parameters
Returns
- GameTile
GameTile at the specified coordinates, or null if out of bounds
GetTileAtWorldPosition(Vector3)
Gets the tile that contains the specified world position.
public GameTile? GetTileAtWorldPosition(Vector3 worldPosition)
Parameters
worldPositionVector33D world position
Returns
- GameTile
GameTile containing the position, or null if outside grid
RenderDebug(bool, bool)
Renders debug visualization of the tile grid.
public void RenderDebug(bool showOccupied = true, bool showBounds = true)
Parameters
showOccupiedboolWhether to highlight occupied tiles (ignored - kept for compatibility)
showBoundsboolWhether to show tile boundaries
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
TryMoveOccupant(int, int, int, int)
Attempts to move an occupant from one tile to another.
public bool TryMoveOccupant(int fromX, int fromY, int toX, int toY)
Parameters
fromXintSource grid X coordinate
fromYintSource grid Y coordinate
toXintDestination grid X coordinate
toYintDestination grid Y coordinate
Returns
- bool
True if move was successful, false if invalid coordinates
TryPlaceOccupant(int, int, object)
Attempts to place an occupant on the tile at the specified coordinates.
public bool TryPlaceOccupant(int x, int y, object occupant)
Parameters
Returns
- bool
True if placement was successful, false if tile is invalid