✨ Better placement and data modeling
This commit is contained in:
@@ -7,43 +7,60 @@ namespace AceFieldNewHorizon.Scripts.System;
|
||||
|
||||
public enum GridLayer
|
||||
{
|
||||
Ground, // Base layer (e.g., terrain, floors)
|
||||
Building, // Main building layer
|
||||
Ground, // Base layer (e.g., terrain, floors)
|
||||
Building, // Main building layer
|
||||
Decoration // Additional layer for decorations, effects, etc.
|
||||
}
|
||||
|
||||
public partial class GridManager : Node
|
||||
{
|
||||
private Dictionary<GridLayer, Dictionary<Vector2I, Node2D>> _layers = new();
|
||||
private Dictionary<GridLayer, Dictionary<Vector2I, (Node2D Building, Vector2I Size, float Rotation)>> _layers =
|
||||
new();
|
||||
|
||||
public GridManager()
|
||||
{
|
||||
// Initialize all layers
|
||||
foreach (GridLayer layer in Enum.GetValues(typeof(GridLayer)))
|
||||
{
|
||||
_layers[layer] = new Dictionary<Vector2I, Node2D>();
|
||||
_layers[layer] = new Dictionary<Vector2I, (Node2D, Vector2I, float)>();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsCellFree(Vector2I cell, GridLayer layer = GridLayer.Building)
|
||||
public bool IsAreaFree(Vector2I topLeft, Vector2I size, float rotation, GridLayer layer = GridLayer.Building)
|
||||
{
|
||||
return !_layers[layer].ContainsKey(cell);
|
||||
var occupiedCells = GridUtils.GetOccupiedCells(topLeft, size, rotation);
|
||||
foreach (var cell in occupiedCells)
|
||||
{
|
||||
if (_layers[layer].ContainsKey(cell))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void OccupyCell(Vector2I cell, Node2D building, GridLayer layer = GridLayer.Building)
|
||||
public void OccupyArea(Vector2I topLeft, Node2D building, Vector2I size, float rotation,
|
||||
GridLayer layer = GridLayer.Building)
|
||||
{
|
||||
_layers[layer][cell] = building;
|
||||
var occupiedCells = GridUtils.GetOccupiedCells(topLeft, size, rotation);
|
||||
foreach (var cell in occupiedCells)
|
||||
{
|
||||
_layers[layer][cell] = (building, size, rotation);
|
||||
}
|
||||
}
|
||||
|
||||
public void FreeCell(Vector2I cell, GridLayer layer = GridLayer.Building)
|
||||
public void FreeArea(Vector2I topLeft, Vector2I size, float rotation, GridLayer layer = GridLayer.Building)
|
||||
{
|
||||
if (_layers[layer].ContainsKey(cell))
|
||||
_layers[layer].Remove(cell);
|
||||
var occupiedCells = GridUtils.GetOccupiedCells(topLeft, size, rotation);
|
||||
foreach (var cell in occupiedCells)
|
||||
{
|
||||
if (_layers[layer].ContainsKey(cell))
|
||||
_layers[layer].Remove(cell);
|
||||
}
|
||||
}
|
||||
|
||||
public Node2D? GetBuildingAtCell(Vector2I cell, GridLayer layer = GridLayer.Building)
|
||||
{
|
||||
return _layers[layer].GetValueOrDefault(cell);
|
||||
return _layers[layer].TryGetValue(cell, out var data) ? data.Building : null;
|
||||
}
|
||||
|
||||
public bool IsAnyCellOccupied(Vector2I cell, params GridLayer[] layers)
|
||||
@@ -55,6 +72,7 @@ public partial class GridManager : Node
|
||||
{
|
||||
if (layer.ContainsKey(cell)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -62,6 +80,34 @@ public partial class GridManager : Node
|
||||
{
|
||||
if (_layers[layer].ContainsKey(cell)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IsAreaOccupied(Vector2I topLeft, Vector2I size, float rotation, params GridLayer[] layers)
|
||||
{
|
||||
var occupiedCells = GridUtils.GetOccupiedCells(topLeft, size, rotation);
|
||||
|
||||
if (layers.Length == 0)
|
||||
{
|
||||
// Check all layers if none specified
|
||||
foreach (var cell in occupiedCells)
|
||||
{
|
||||
foreach (var layer in _layers.Values)
|
||||
if (layer.ContainsKey(cell))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var cell in occupiedCells)
|
||||
{
|
||||
foreach (var layer in layers)
|
||||
if (_layers[layer].ContainsKey(cell))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -85,4 +131,26 @@ public static class GridUtils
|
||||
cell.Y * TileSize
|
||||
);
|
||||
}
|
||||
|
||||
public static Vector2 GetCenterOffset(Vector2I size, float rotation)
|
||||
{
|
||||
var rotatedSize = (Mathf.RoundToInt(rotation / 90f) % 2) == 0 ? size : new Vector2I(size.Y, size.X);
|
||||
return new Vector2(rotatedSize.X * TileSize / 2f, rotatedSize.Y * TileSize / 2f);
|
||||
}
|
||||
|
||||
public static List<Vector2I> GetOccupiedCells(Vector2I topLeft, Vector2I size, float rotation)
|
||||
{
|
||||
var occupiedCells = new List<Vector2I>();
|
||||
var rotatedSize = (Mathf.RoundToInt(rotation / 90f) % 2) == 0 ? size : new Vector2I(size.Y, size.X);
|
||||
|
||||
for (int x = 0; x < rotatedSize.X; x++)
|
||||
{
|
||||
for (int y = 0; y < rotatedSize.Y; y++)
|
||||
{
|
||||
occupiedCells.Add(new Vector2I(topLeft.X + x, topLeft.Y + y));
|
||||
}
|
||||
}
|
||||
|
||||
return occupiedCells;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user