-
Notifications
You must be signed in to change notification settings - Fork 1
MapHelper
MapHelper provides utility methods for tile-based game development, including coordinate conversion, spatial queries, and pathfinding helpers.
| Feature | Description |
|---|---|
| Coordinate Conversion | Tile-to-world and world-to-tile conversion |
| Index Conversion | 1D to 2D index conversion and vice versa |
| Spatial Queries | Circle, ring, line, rectangle, and edge queries |
| Distance Calculations | Manhattan and Chebyshev distance |
| Adjacency | Check if units are adjacent with optional diagonals |
| Flood Fill | Find all connected walkable tiles |
Converts a tile-based grid location into world-space coordinates.
// Tile (5, 3) with 32px tiles becomes world position (160, 96)
Vect2 worldPos = MapHelper.MapToWorld(new Vect2(5, 3), 32);Converts a world-space position into map grid coordinates.
// World position (160, 96) with 32px tiles becomes tile (5, 3)
Vect2 tilePos = MapHelper.WorldToMap(new Vect2(160, 96), 32);Converts a 1-dimensional tile index into a 2D coordinate.
Vect2 tile = MapHelper.To2D(45, 10); // Returns (5, 4)Converts a 2D tile coordinate into a 1-dimensional index.
int index = MapHelper.To1D(new Vect2(5, 4), 10); // Returns 45Converts a world-space position into a 1D tile index.
int index = MapHelper.WorldToIndex(new Vect2(160, 96), 32, 10); // Returns 45Converts a 1D tile index into a world-space position (top-left corner of the tile).
Vect2 worldPos = MapHelper.IndexToWorld(45, 32, 10); // Returns (160, 96)Converts a world-space size into a list of tile coordinates covering the area.
var tiles = MapHelper.ToMap(
new Vect2(64, 64), // Size in pixels
new Vect2(0, 0), // Starting tile
32 // Tile size
);
// Returns tiles (0,0), (1,0), (0,1), (1,1)Returns all tile coordinates within a circular radius of a center point.
var tiles = MapHelper.ToCircle(new Vect2(5, 5), 3);
// Returns all tiles within 3 tiles of (5, 5)Returns all tile coordinates within a circular ring (donut shape).
var tiles = MapHelper.ToRing(new Vect2(5, 5), 2, 5);
// Returns tiles between 2 and 5 tiles from centerReturns all tile coordinates along a line between two tile positions using Bresenham's algorithm.
var tiles = MapHelper.ToLine(new Vect2(0, 0), new Vect2(5, 3));
// Returns all tiles along the line from (0,0) to (5,3)Returns all tiles on the border of a rectangular area.
var tiles = MapHelper.ToEdge(new Vect2(0, 0), 5, 4);
// Returns all tiles on the border of a 5x4 rectangleCalculates the Manhattan distance between two tile coordinates.
int distance = MapHelper.ManhattanDistance(
new Vect2(0, 0),
new Vect2(3, 4)
); // Returns 7 (3 + 4)Calculates the Chebyshev distance between two tile coordinates.
int distance = MapHelper.ChebyshevDistance(
new Vect2(0, 0),
new Vect2(3, 4)
); // Returns 4 (max of 3 and 4)Determines whether a tile coordinate is within the bounds of a map.
bool inBounds = MapHelper.IsInBounds(
new Vect2(5, 3),
10, // mapWidth
10 // mapHeight
); // Returns true
bool outOfBounds = MapHelper.IsInBounds(
new Vect2(10, 3),
10, // mapWidth
10 // mapHeight
); // Returns falseDetermines whether a unit is adjacent to another unit on a tile grid.
// Check orthogonal adjacency only
bool adjacent = MapHelper.IsUnitAround(
new Vect2(5, 5),
new Vect2(5, 6),
false // exclude corners
); // Returns true (down)
// Check orthogonal and diagonal adjacency
bool adjacent = MapHelper.IsUnitAround(
new Vect2(5, 5),
new Vect2(6, 6),
true // include corners
); // Returns true (diagonal)Performs a flood fill starting from a tile coordinate.
// Define walkable tiles
Func<Vect2, bool> isWalkable = (tile) =>
{
// Check if the tile is walkable
return !MapManager.HasCollided(tile);
};
// Find all connected walkable tiles from start
var walkableTiles = MapHelper.FloodFill(
new Vect2(0, 0),
isWalkable
);public void UpdatePlayerPosition(Vect2 worldPosition)
{
// Convert world position to tile
var tile = MapHelper.WorldToMap(worldPosition, Globals.TileSize);
// Check if the tile is walkable
if (!MapManager.HasCollided(tile))
{
player.Position = MapHelper.MapToWorld(tile, Globals.TileSize);
}
}public List<Enemy> GetNearbyEnemies(Vect2 playerTile, int radius)
{
var nearbyTiles = MapHelper.ToCircle(playerTile, radius);
var enemies = new List<Enemy>();
foreach (var tile in nearbyTiles)
{
enemies.AddRange(_enemyMap[tile]);
}
return enemies;
}public void DrawMapBorder()
{
var borderTiles = MapHelper.ToEdge(
Vect2.Zero,
(int)Level.GridSize.X,
(int)Level.GridSize.Y
);
foreach (var tile in borderTiles)
{
// Draw border tile
DrawTile(tile, Color.White);
}
}public bool CanSeePlayer(Vect2 enemyTile, Vect2 playerTile, int visionRange)
{
// Check if player is within vision range
if (ManhattanDistance(enemyTile, playerTile) > visionRange)
return false;
// Check line of sight
var lineTiles = MapHelper.ToLine(enemyTile, playerTile);
foreach (var tile in lineTiles)
{
if (MapManager.HasCollided(tile))
return false;
}
return true;
}| Method | Description |
|---|---|
MapToWorld |
Converts tile to world position |
WorldToMap |
Converts world position to tile |
To2D |
Converts 1D index to 2D tile |
To1D |
Converts 2D tile to 1D index |
WorldToIndex |
Converts world position to 1D index |
IndexToWorld |
Converts 1D index to world position |
ToMap |
Converts world area to tiles |
ToCircle |
Gets tiles within a circle |
ToRing |
Gets tiles within a ring |
ToLine |
Gets tiles along a line |
ToEdge |
Gets tiles on a rectangle border |
IsInBounds |
Checks if tile is in bounds |
ManhattanDistance |
Calculates Manhattan distance |
ChebyshevDistance |
Calculates Chebyshev distance |
IsUnitAround |
Checks adjacency |
FloodFill |
Finds connected walkable tiles |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions