✨ Grid layer system
This commit is contained in:
@@ -1,31 +1,68 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
namespace AceFieldNewHorizon.Scripts.System;
|
||||
|
||||
public enum GridLayer
|
||||
{
|
||||
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<Vector2I, Node2D> _grid = new();
|
||||
private Dictionary<GridLayer, Dictionary<Vector2I, Node2D>> _layers = new();
|
||||
|
||||
public bool IsCellFree(Vector2I cell)
|
||||
public GridManager()
|
||||
{
|
||||
return !_grid.ContainsKey(cell);
|
||||
// Initialize all layers
|
||||
foreach (GridLayer layer in Enum.GetValues(typeof(GridLayer)))
|
||||
{
|
||||
_layers[layer] = new Dictionary<Vector2I, Node2D>();
|
||||
}
|
||||
}
|
||||
|
||||
public void OccupyCell(Vector2I cell, Node2D building)
|
||||
public bool IsCellFree(Vector2I cell, GridLayer layer = GridLayer.Building)
|
||||
{
|
||||
_grid[cell] = building;
|
||||
return !_layers[layer].ContainsKey(cell);
|
||||
}
|
||||
|
||||
public void FreeCell(Vector2I cell)
|
||||
public void OccupyCell(Vector2I cell, Node2D building, GridLayer layer = GridLayer.Building)
|
||||
{
|
||||
_grid.Remove(cell);
|
||||
_layers[layer][cell] = building;
|
||||
}
|
||||
|
||||
public Node2D? GetBuildingAtCell(Vector2I cell)
|
||||
public void FreeCell(Vector2I cell, GridLayer layer = GridLayer.Building)
|
||||
{
|
||||
return _grid.GetValueOrDefault(cell);
|
||||
if (_layers[layer].ContainsKey(cell))
|
||||
_layers[layer].Remove(cell);
|
||||
}
|
||||
|
||||
public Node2D? GetBuildingAtCell(Vector2I cell, GridLayer layer = GridLayer.Building)
|
||||
{
|
||||
return _layers[layer].GetValueOrDefault(cell);
|
||||
}
|
||||
|
||||
public bool IsAnyCellOccupied(Vector2I cell, params GridLayer[] layers)
|
||||
{
|
||||
if (layers.Length == 0)
|
||||
{
|
||||
// Check all layers if none specified
|
||||
foreach (var layer in _layers.Values)
|
||||
{
|
||||
if (layer.ContainsKey(cell)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var layer in layers)
|
||||
{
|
||||
if (_layers[layer].ContainsKey(cell)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user