2022-10-19 13:51:46 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
using static SimpleInventorySystem.InventorySystem;
|
|
|
|
|
|
|
|
namespace SimpleInventorySystem
|
|
|
|
{
|
|
|
|
[RequireComponent(typeof (GridLayoutGroup))]
|
|
|
|
public class GridUI : MonoBehaviour
|
|
|
|
{
|
|
|
|
private LayoutElement layoutElement;
|
|
|
|
private GridLayoutGroup gridLayout;
|
|
|
|
public GameObject CellPrefab;
|
|
|
|
public bool generateCells; // will autopopulate grid
|
|
|
|
public SlotUI[] slots;
|
|
|
|
[SerializeField] public Size gridSize;
|
|
|
|
|
|
|
|
internal void ChangeGridLayoutSize(int rows, int columns)
|
|
|
|
{
|
|
|
|
gridSize.Width = rows;
|
|
|
|
gridSize.Height = columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Cell[,] cells;
|
|
|
|
public GameObject testItem;
|
|
|
|
public GameObject[] testItems;
|
|
|
|
private Size cellSize;
|
|
|
|
public Size CellSize { get { return cellSize; } }
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
gridLayout = GetComponent<GridLayoutGroup>();
|
|
|
|
|
|
|
|
SetUpCellsArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetUpCellsArray()
|
|
|
|
{
|
|
|
|
if (gridSize.Width == 0 && gridLayout.constraint == GridLayoutGroup.Constraint.FixedColumnCount)
|
|
|
|
{
|
|
|
|
gridSize.Width = gridLayout.constraintCount;
|
|
|
|
}
|
|
|
|
else if (gridSize.Height == 0 && gridLayout.constraint == GridLayoutGroup.Constraint.FixedRowCount)
|
|
|
|
{
|
|
|
|
gridSize.Height = gridLayout.constraintCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gridSize.Width == 0 || gridSize.Height == 0)
|
|
|
|
{
|
|
|
|
Debug.LogError("GridUI needs a defined width and height, or width or height needs to be constrained through the GridLayoutGroup component.");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Debug.Log("GridUI: GridSize: " + gridSize.Width + ", " + gridSize.Height);
|
|
|
|
|
|
|
|
cells = new Cell[gridSize.Height, gridSize.Width];
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateCells()
|
|
|
|
{
|
|
|
|
SetUpCellsArray();
|
|
|
|
|
|
|
|
if (CellPrefab != null)
|
|
|
|
{
|
|
|
|
/*foreach(Transform child in transform)
|
|
|
|
{
|
|
|
|
// TODO: make sure to unequip items currently in the grid
|
|
|
|
Destroy(child);
|
|
|
|
}*/
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
Debug.Log(gridSize.Width + ", " + gridSize.Height);
|
|
|
|
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < gridSize.Height; y++)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < gridSize.Width; x++)
|
|
|
|
{
|
|
|
|
// TODO: load inventory from data here upon intialization
|
|
|
|
GameObject cell = GameObject.Instantiate(CellPrefab, this.transform);
|
|
|
|
cell.transform.localScale = new Vector3(1f, 1f, 1f);
|
2022-10-29 19:56:00 +00:00
|
|
|
SlotUI slot = cell.GetComponentInChildren<SlotUI>();
|
|
|
|
// WARNING: All cell prefabs must now have a slot
|
|
|
|
//if (cell.GetComponentInChildren<SlotUI>() != null)
|
|
|
|
//{
|
|
|
|
slot.grid = this;
|
|
|
|
slot.AllowedItemTags = new List<ItemTags>();
|
|
|
|
slot.AllowedItemTags.Add(ItemTags.Any);
|
|
|
|
cells[y, x] = new Cell(x, y, cell.GetComponentInChildren<SlotUI>(), false);
|
|
|
|
cells[y, x].GetSlot().SetCell(cells[y, x]);
|
|
|
|
Debug.Log("Foo: " + (slot == null));
|
|
|
|
Debug.Log("Bar: " + (cells[y, x].GetSlot().cell == null));
|
|
|
|
//}
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cells != null && cells[0, 0] != null && testItem != null)
|
|
|
|
{
|
|
|
|
Debug.Log("Add item");
|
|
|
|
Cell cell = cells[0, 0];
|
|
|
|
GameObject item = Instantiate(testItem);
|
|
|
|
cell.slot.DropOntoSlot(item);
|
|
|
|
//item.transform.SetParent(.gameObject.transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (testItems != null)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int x = 0, y = 0;
|
2022-11-06 02:28:29 +00:00
|
|
|
int lootCount = 1;
|
2022-10-19 13:51:46 +00:00
|
|
|
foreach (GameObject testItemUI in testItems)
|
|
|
|
{
|
|
|
|
bool dropped = false;
|
|
|
|
GameObject item = null;
|
|
|
|
try
|
|
|
|
{
|
2022-11-06 02:28:29 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
x = i % gridSize.Width;
|
|
|
|
y = i / gridSize.Width;
|
2022-10-19 13:51:46 +00:00
|
|
|
|
2022-11-06 02:28:29 +00:00
|
|
|
item = Instantiate(testItemUI);
|
|
|
|
item.name = item.name + " " + lootCount;
|
|
|
|
dropped = cells[y, x].slot.DropOntoSlot(item);
|
2022-10-19 13:51:46 +00:00
|
|
|
|
2022-11-06 02:28:29 +00:00
|
|
|
if (!dropped)
|
|
|
|
{
|
|
|
|
Destroy(item);
|
2022-10-19 13:51:46 +00:00
|
|
|
|
2022-11-06 02:28:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lootCount++;
|
2022-11-15 22:58:54 +00:00
|
|
|
item.transform.localScale = new Vector3(1, 1, 1);
|
2022-11-06 02:28:29 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
} while (!dropped);
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
2022-11-06 02:28:29 +00:00
|
|
|
catch (IndexOutOfRangeException err)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
Debug.LogWarning("GridUI: " + x + ", " + y);
|
|
|
|
}
|
|
|
|
if (item == null)
|
|
|
|
{
|
|
|
|
Debug.LogWarning("Could not add all test items");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
if (!generateCells)
|
|
|
|
{
|
|
|
|
//if (slots != null)
|
|
|
|
for (int i = 0; i < gridLayout.transform.childCount; i++)
|
2022-10-29 19:56:00 +00:00
|
|
|
{
|
|
|
|
int x = i % gridSize.Width;
|
2022-10-19 13:51:46 +00:00
|
|
|
int y = (i - x) / gridSize.Width;
|
|
|
|
|
|
|
|
if (cells[y, x] != null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform child = gridLayout.transform.GetChild(i);
|
|
|
|
SlotUI slot = child.GetComponentInChildren<SlotUI>();
|
|
|
|
if (slot != null)
|
|
|
|
{
|
|
|
|
cells[y, x] = new Cell(x, y, slot, true);
|
2022-10-29 19:56:00 +00:00
|
|
|
slot.SetCell(cells[y, x]);
|
|
|
|
slot.grid = this;
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
if (slot.width > 1 || slot.height > 1)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < slot.height; j++)
|
|
|
|
{
|
|
|
|
for (int k = 1; k < slot.width; k++)
|
|
|
|
{
|
|
|
|
cells[y + j, x + k] = new Cell(x + k, y + j, slot, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-29 19:56:00 +00:00
|
|
|
|
|
|
|
Debug.Log("START: " + (slot.cell == null));
|
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cells[y, x] = null;// new Cell(null, false, false); // Setting null so that we can reserve memory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GenerateCells();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void HideSlotsOfItem(ItemUI itemDrop)
|
|
|
|
{
|
|
|
|
if(itemDrop != null)
|
|
|
|
{
|
|
|
|
itemDrop.HideSlots();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanMoveInCells(ItemUI item, SlotUI slot, List<Cell> occupiedCells = null)
|
|
|
|
{
|
2022-11-06 02:28:29 +00:00
|
|
|
if (slot == null || slot.cell == null || (slot.cell.inUse && (slot.GetItemUI() != null && !slot.GetItemUI().Stackable)))
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": A" + (slot.cell == null));
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-06 02:28:29 +00:00
|
|
|
else if (slot.IsSingleCellSlot() && item.IsSingleCellItem() && (!slot.cell.inUse || (slot.StackItemsOnSlot && slot.GetItemUI().name == item.name)))
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": B");
|
|
|
|
//if (!slot.cell.inUse)
|
|
|
|
return true;
|
|
|
|
//else
|
|
|
|
// return false;
|
|
|
|
}
|
|
|
|
else if (cells != null)
|
|
|
|
{
|
|
|
|
List<Cell> oCells = new List<Cell>();
|
|
|
|
if(occupiedCells != null)
|
|
|
|
{
|
|
|
|
oCells = occupiedCells;
|
|
|
|
}
|
|
|
|
Size size = item.SizeAfterOrientation();
|
|
|
|
// for (int h = 0; h < slot.height; h++){
|
|
|
|
// for (int w = 0; w < slot.width; w++)
|
|
|
|
for (int h = 0; h < size.Height; h++)
|
|
|
|
{
|
|
|
|
for (int w = 0; w < size.Width; w++)
|
|
|
|
{
|
|
|
|
if(slot.cell.Y + h >= gridSize.Height || slot.cell.X + w >= gridSize.Width)
|
|
|
|
{
|
|
|
|
Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": D");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Cell cell = cells[slot.cell.Y + h, slot.cell.X + w];
|
|
|
|
if (cell != null && cell.inUse)
|
|
|
|
{
|
|
|
|
Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": E");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
oCells.Add(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Debug.Log(occupiedCells.Count);
|
|
|
|
Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": F");
|
|
|
|
|
|
|
|
Debug.Log(oCells.Count);
|
2022-11-15 22:58:54 +00:00
|
|
|
|
|
|
|
return true;
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
2022-11-15 22:58:54 +00:00
|
|
|
Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": G");
|
|
|
|
|
2022-10-19 13:51:46 +00:00
|
|
|
|
2022-11-15 22:58:54 +00:00
|
|
|
return false;
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Cell
|
|
|
|
{
|
|
|
|
protected int x, y;
|
|
|
|
public int X { get { return x; } }
|
|
|
|
public int Y { get { return y; } }
|
|
|
|
public SlotUI slot;
|
|
|
|
public bool ownsSlot;
|
|
|
|
public bool inUse;
|
|
|
|
|
|
|
|
public Cell(int x, int y, SlotUI slot, bool ownsSlot)
|
|
|
|
{
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.slot = slot;
|
2022-10-29 19:56:00 +00:00
|
|
|
slot.SetCell(this);
|
2022-10-19 13:51:46 +00:00
|
|
|
inUse = false;
|
|
|
|
this.ownsSlot = ownsSlot;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool HasSlot()
|
|
|
|
{
|
|
|
|
return slot != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SlotUI GetSlot()
|
|
|
|
{
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|