Table of Contents

Class TileGrid

Namespace
PopcorningGuineaPig.Engine.Gameplay
Assembly
PopcorningGuineaPig.Engine.dll

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

width int

Number of tiles horizontally

height int

Number of tiles vertically

tileSize Vector2

Size of each tile (width, height)

gridOrigin Vector3

World position of the grid center

Properties

GridOrigin

public Vector3 GridOrigin { get; }

Property Value

Vector3

Height

public int Height { get; }

Property Value

int

TileSize

public Vector2 TileSize { get; }

Property Value

Vector2

Width

public int Width { get; }

Property Value

int

Methods

ClearOccupant(int, int)

Clears the occupant from the tile at the specified coordinates.

public object? ClearOccupant(int x, int y)

Parameters

x int

Grid X coordinate

y int

Grid Y coordinate

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

startX int

Start grid X coordinate

startY int

Start grid Y coordinate

endX int

End grid X coordinate

endY int

End grid Y coordinate

Returns

List<GameTile>

List of tiles representing the path, or empty list if no path found

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

x int

Grid X coordinate

y int

Grid Y coordinate

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

x int

Grid X coordinate

y int

Grid Y coordinate

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

x int

Grid X coordinate

y int

Grid Y coordinate

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

worldPosition Vector3

3D 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

showOccupied bool

Whether to highlight occupied tiles (ignored - kept for compatibility)

showBounds bool

Whether 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

fromX int

Source grid X coordinate

fromY int

Source grid Y coordinate

toX int

Destination grid X coordinate

toY int

Destination 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

x int

Grid X coordinate

y int

Grid Y coordinate

occupant object

Object to place on the tile

Returns

bool

True if placement was successful, false if tile is invalid