Class TileGrid
Manages a 2D grid of GameTiles for grid-based games. Provides efficient access, spatial queries, and occupancy management.
Inherited Members
Namespace: PopcorningGuineaPig.Engine.Gameplay
Assembly: PopcorningGuineaPig.Engine.dll
Syntax
public class TileGrid
Constructors
| Edit this page View SourceTileGrid(int, int, Vector2, Vector3)
Creates a new tile grid with specified dimensions and tile size.
Declaration
public TileGrid(int width, int height, Vector2 tileSize, Vector3 gridOrigin = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | width | Number of tiles horizontally |
| int | height | Number of tiles vertically |
| Vector2 | tileSize | Size of each tile (width, height) |
| Vector3 | gridOrigin | World position of the grid center |
Properties
| Edit this page View SourceGridOrigin
Declaration
public Vector3 GridOrigin { get; }
Property Value
| Type | Description |
|---|---|
| Vector3 |
Height
Declaration
public int Height { get; }
Property Value
| Type | Description |
|---|---|
| int |
TileSize
Declaration
public Vector2 TileSize { get; }
Property Value
| Type | Description |
|---|---|
| Vector2 |
Width
Declaration
public int Width { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
| Edit this page View SourceClearOccupant(int, int)
Clears the occupant from the tile at the specified coordinates.
Declaration
public object? ClearOccupant(int x, int y)
Parameters
| Type | Name | Description |
|---|---|---|
| int | x | Grid X coordinate |
| int | y | Grid Y coordinate |
Returns
| Type | Description |
|---|---|
| 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.
Declaration
public List<GameTile> FindPath(int startX, int startY, int endX, int endY)
Parameters
| Type | Name | Description |
|---|---|---|
| int | startX | Start grid X coordinate |
| int | startY | Start grid Y coordinate |
| int | endX | End grid X coordinate |
| int | endY | End grid Y coordinate |
Returns
| Type | Description |
|---|---|
| List<GameTile> | List of tiles representing the path, or empty list if no path found |
GetAllTiles()
Gets all tiles in the grid.
Declaration
public IEnumerable<GameTile> GetAllTiles()
Returns
| Type | Description |
|---|---|
| IEnumerable<GameTile> | Collection of all tiles |
GetEmptyTiles()
Gets all tiles that are empty (no occupants).
Declaration
public IEnumerable<GameTile> GetEmptyTiles()
Returns
| Type | Description |
|---|---|
| IEnumerable<GameTile> | Collection of empty tiles |
GetNeighbors(int, int)
Gets all neighboring tiles of the specified tile (4-directional).
Declaration
public IEnumerable<GameTile> GetNeighbors(int x, int y)
Parameters
| Type | Name | Description |
|---|---|---|
| int | x | Grid X coordinate |
| int | y | Grid Y coordinate |
Returns
| Type | Description |
|---|---|
| IEnumerable<GameTile> | Collection of neighboring tiles |
GetNeighbors8(int, int)
Gets all neighboring tiles of the specified tile (8-directional).
Declaration
public IEnumerable<GameTile> GetNeighbors8(int x, int y)
Parameters
| Type | Name | Description |
|---|---|---|
| int | x | Grid X coordinate |
| int | y | Grid Y coordinate |
Returns
| Type | Description |
|---|---|
| IEnumerable<GameTile> | Collection of neighboring tiles including diagonals |
GetOccupiedTiles()
Gets all tiles that currently have occupants.
Declaration
public IEnumerable<GameTile> GetOccupiedTiles()
Returns
| Type | Description |
|---|---|
| IEnumerable<GameTile> | Collection of occupied tiles |
GetTile(int, int)
Gets the tile at the specified grid coordinates.
Declaration
public GameTile? GetTile(int x, int y)
Parameters
| Type | Name | Description |
|---|---|---|
| int | x | Grid X coordinate |
| int | y | Grid Y coordinate |
Returns
| Type | Description |
|---|---|
| GameTile | GameTile at the specified coordinates, or null if out of bounds |
GetTileAtWorldPosition(Vector3)
Gets the tile that contains the specified world position.
Declaration
public GameTile? GetTileAtWorldPosition(Vector3 worldPosition)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | worldPosition | 3D world position |
Returns
| Type | Description |
|---|---|
| GameTile | GameTile containing the position, or null if outside grid |
RenderDebug(bool, bool)
Renders debug visualization of the tile grid.
Declaration
public void RenderDebug(bool showOccupied = true, bool showBounds = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | showOccupied | Whether to highlight occupied tiles (ignored - kept for compatibility) |
| bool | showBounds | Whether to show tile boundaries |
ToString()
Returns a string that represents the current object.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string that represents the current object. |
Overrides
| Edit this page View SourceTryMoveOccupant(int, int, int, int)
Attempts to move an occupant from one tile to another.
Declaration
public bool TryMoveOccupant(int fromX, int fromY, int toX, int toY)
Parameters
| Type | Name | Description |
|---|---|---|
| int | fromX | Source grid X coordinate |
| int | fromY | Source grid Y coordinate |
| int | toX | Destination grid X coordinate |
| int | toY | Destination grid Y coordinate |
Returns
| Type | Description |
|---|---|
| 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.
Declaration
public bool TryPlaceOccupant(int x, int y, object occupant)
Parameters
| Type | Name | Description |
|---|---|---|
| int | x | Grid X coordinate |
| int | y | Grid Y coordinate |
| object | occupant | Object to place on the tile |
Returns
| Type | Description |
|---|---|
| bool | True if placement was successful, false if tile is invalid |