using UnityEngine; public class Inventory : MonoBehaviour { [SerializeField] private int width = 10; [SerializeField] private int height = 6; public int Width => width; public int Height => height; private InventoryItemPlacement[,] grid; [SerializeField] private InventoryItem testInventoryItem; private void Start() { PlaceItem(testInventoryItem, 0, 0); } private void Awake() { grid = new InventoryItemPlacement[width, height]; } public bool CanPlaceItem(InventoryItem item, int startX, int startY) { for (int x = 0; x < item.gridX; x++) { for (int y = 0; y < item.gridY; y++) { if (!item.IsOccupied(x, y)) continue; int inventoryX = startX + x; int inventoryY = startY + y; // Outside inventory? if (inventoryX < 0 || inventoryX >= width || inventoryY < 0 || inventoryY >= height) { return false; } // Something already here? if (grid[inventoryX, inventoryY] != null) { return false; } } } return true; } public bool PlaceItem(InventoryItem item, int startX, int startY) { if (!CanPlaceItem(item, startX, startY)) { return false; } InventoryItemPlacement placement = new InventoryItemPlacement(item, new Vector2Int(startX, startY)); for (int x = 0; x < item.gridX; x++) { for (int y = 0; y < item.gridY; y++) { if (!item.IsOccupied(x, y)) { continue; } grid[startX + x, startY + y] = placement; } } return true; } public void RemoveItem(InventoryItem item) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (grid[x, y] != null && grid[x, y].Item == item) { grid[x, y] = null; } } } } public InventoryItemPlacement GetPlacementAt(int x, int y) { if (x < 0 || x >= width || y < 0 || y >= height) { return null; } return grid[x, y]; } public InventoryItem GetItemAt(int x, int y) { InventoryItemPlacement placement = GetPlacementAt(x, y); if (placement == null) return null; return placement.Item; } }