using System.Collections.Generic; using System.Linq; using Godot; using GodotGridInventory.Code.Grid.Enums; using GodotGridInventory.Code.UI; namespace GodotGridInventory.Code.Grid; #nullable enable public class InventoryGrid { private readonly InventoryController _inventoryController; public readonly Vector2 GridSize; public List GridCells = new(); public InventoryGrid(InventoryController inventoryController, Vector2 size) { _inventoryController = inventoryController; GridSize = size; InitializeGrid(); } public InventoryGrid(InventoryController inventoryController, List gridCells) { _inventoryController = inventoryController; GridCells = gridCells; } private void InitializeGrid() { for(var x = 0; x < GridSize.X; x++) { for(var y = 0; y < GridSize.Y; y++) { var cellPosition = new Vector2(x, y); var cell = new InventoryCell(cellPosition, this); GridCells.Add(cell); } } } #region Private Methods /// /// Sets the item of a cell at a given position. /// /// The position of the cell to update. /// The item to set the cell to. private void SetCellItem(Vector2 position, Item item) { var cell = GridCells.FirstOrDefault(cell => cell.Position == position); if(cell != null) { cell.Item = item; } } /// /// Clears the item of a cell at a given position. /// /// The position of the cell to update. private void ClearCellItem(Vector2 position) { var cell = GridCells.FirstOrDefault(cell => cell.Position == position); if(cell != null) { cell.Item = null; } } /// /// Sets the state of a cell at a given position in inventory grid space. /// /// The position of the cell to set. /// The state to set the cell private void SetCellState(Vector2 position, EnumInventoryGridCellState state) { var cell = GridCells.FirstOrDefault(cell => cell.Position == position); if(cell != null) { cell.State = state; } } /// /// Sets the state of a selection of cells at a given position and size in inventory grid space. /// /// The position of the cells to set from the top left.. /// The size of the area to set starting at the top left. /// The state to set the cells private void SetCellAreaState(Vector2 position, Vector2 size, EnumInventoryGridCellState state) { var cells = GridCells.Where(cell => cell.Position.X >= position.X && cell.Position.X < position.X + size.X && cell.Position.Y >= position.Y && cell.Position.Y < position.Y + size.Y).ToList(); foreach (var cell in cells) { cell.State = state; } } /// /// Sets the item of a selection of cells at a given position and size in inventory grid space. /// /// The position of the cells to set from the top left.. /// The size of the area to set starting at the top left. /// The item to set the cells private void SetCellAreaItem(Vector2 position, Vector2 size, Item item, InventoryGridItem itemGraphic) { var cells = GridCells.Where(cell => cell.Position.X >= position.X && cell.Position.X < position.X + size.X && cell.Position.Y >= position.Y && cell.Position.Y < position.Y + size.Y).ToList(); foreach (var cell in cells) { cell.Item = item; cell.ItemGraphic = itemGraphic; } } /// /// Gets the first available cell position that can fit an item of a given size. /// /// The size to check for. /// The first coordinates available for something of the given size, or null if no space available. private Vector2? GetAvailableCellPosition(Vector2 size) { for (int x = 0; x <= GridSize.X - size.X; x++) { for (int y = 0; y <= GridSize.Y - size.Y; y++) { // Check if the area is available if (IsSpaceAvailable(new Vector2(x, y), size)) { return new Vector2(x,y); } } } return null; } /// /// This checks all of the cells in a space to see if all of them are available. /// /// The top left position of the item in the inventories grid space. /// The size of the item in inventory grid space. /// A List of cells that are available for the item to be placed in. private bool IsSpaceAvailable(Vector2 position, Vector2 size) { var cells = GridCells.Where( cell => cell.Position.X >= position.X && cell.Position.X < position.X + size.X && cell.Position.Y >= position.Y && cell.Position.Y < position.Y + size.Y); return cells.All(cell => cell.State == EnumInventoryGridCellState.Available); } #endregion #region Public Methods /// /// Adds an item to the inventory grid. /// /// The ID of the item to add. /// The position to add the item at in inventory grid space. If null then will insert at first available place. /// public Vector2? AddItem(string itemId, InventoryGridItem itemGraphic, Vector2? position = null) { var item = ItemDatabase.Instance.GetItemConfiguration(itemId); if(item == null) { GD.PrintErr("Attempted to add item with id " + itemId + ", which does not exist."); return null; } if(position.HasValue==false) { position = GetAvailableCellPosition(item.Size); } if (position.HasValue==false) { GD.PrintErr("Attempted to add item with id " + itemId + ", but there is no space available."); return null; } SetCellAreaState((Vector2)position, item.Size, EnumInventoryGridCellState.Unavailable); SetCellAreaItem((Vector2)position, item.Size, item, itemGraphic); return position; } /// /// Gets the cell at the given location in inventory grid space. /// /// The position you want to get the cell of. /// The inventory cell at the given location in inventory grid space. public InventoryCell? GetCell(Vector2 position) { return GridCells.FirstOrDefault(cell => cell.Position == position); } #endregion }