using System.Collections.Generic; using UFS3; using UFS3.Old; using UnityEngine; public class UI_InventoryCase : MonoBehaviourSingleton { [SerializeField] private Vector2Int caseCellsGridSize; [SerializeField] private UI_Item itemTemplate; [SerializeField] private UI_ItemCell itemCellTemplate; [SerializeField] private RectTransform itemBagGridParent; [SerializeField] private ScriptableEventItemData onItemPlaced; private RectTransform rectTransform; public List itemsInBag; public UI_ItemCell[,] itemCellsGridArray; public new static UI_InventoryCase Instance; private void OnEnable() { onItemPlaced.OnRaised += OnItemPlaced_OnRaised; } private void OnDisable() { onItemPlaced.OnRaised -= OnItemPlaced_OnRaised; } private void Start() { Instance = this; CreateGrid(); LoadItemsToGrid(); } private void OnItemPlaced_OnRaised(BaseItemData obj) { RemoveItem(obj); } public void ClearItemCells(Vector2Int fromPos, Vector2Int size) { for (int i = fromPos.y; i < fromPos.y + size.y; i++) { for (int j = fromPos.x; j < fromPos.x + size.x; j++) { itemCellsGridArray[j, i].IsEmpty = true; } } } public bool TryPutItem(Vector2Int fromPos, Vector2Int size) { int num = size.x * size.y; List list = new List(); for (int i = fromPos.y; i < fromPos.y + size.y; i++) { for (int j = fromPos.x; j < fromPos.x + size.x; j++) { if (j >= itemCellsGridArray.GetLength(0) || i >= itemCellsGridArray.GetLength(1)) { ResetValues(); continue; } UI_ItemCell uI_ItemCell = itemCellsGridArray[j, i]; if (!uI_ItemCell.IsEmpty) { ResetValues(); continue; } num--; list.Add(uI_ItemCell); if (num == 0) { list.ForEach(delegate(UI_ItemCell x) { x.IsEmpty = false; }); return true; } } } return false; void ResetValues() { _ = size.x; _ = size.y; new List(); } } private void CreateGrid() { rectTransform = GetComponent(); rectTransform.sizeDelta = new Vector2(caseCellsGridSize.x * 100, caseCellsGridSize.y * 100); itemCellsGridArray = new UI_ItemCell[caseCellsGridSize.x, caseCellsGridSize.y]; for (int i = 0; i < caseCellsGridSize.y; i++) { for (int j = 0; j < caseCellsGridSize.x; j++) { UI_ItemCell uI_ItemCell = Object.Instantiate(itemCellTemplate, itemCellTemplate.transform.parent); uI_ItemCell.Initialize(new Vector2Int(j, i)); GameObject gameObject = uI_ItemCell.gameObject; gameObject.name = gameObject.name + " " + j + " " + i; uI_ItemCell.gameObject.SetActive(value: true); itemCellsGridArray[j, i] = uI_ItemCell; } } } private void LoadItemsToGrid() { for (int i = 0; i < itemsInBag.Count; i++) { BaseItemData original = itemsInBag[i]; AddItem(Object.Instantiate(original)); } } private void KillItems() { UI_Item[] componentsInChildren = GetComponentsInChildren(); foreach (UI_Item uI_Item in componentsInChildren) { if (uI_Item != itemTemplate) { Object.Destroy(uI_Item.gameObject); } } UI_ItemCell[] componentsInChildren2 = GetComponentsInChildren(); foreach (UI_ItemCell uI_ItemCell in componentsInChildren2) { if (uI_ItemCell != itemCellTemplate) { Object.Destroy(uI_ItemCell.gameObject); } } } private void AddItem(BaseItemData item) { for (int i = 0; i < caseCellsGridSize.y; i++) { for (int j = 0; j < caseCellsGridSize.x; j++) { UI_ItemCell uI_ItemCell = itemCellsGridArray[j, i]; if (uI_ItemCell.IsEmpty && TryPutItem(new Vector2Int(j, i), new Vector2Int(item.Width, item.Height))) { UI_Item uI_Item = Object.Instantiate(itemTemplate, base.transform); uI_Item.Initialize(item, uI_ItemCell.GridPosition); uI_Item.gameObject.SetActive(value: true); ((RectTransform)uI_Item.transform).anchoredPosition += new Vector2((float)uI_ItemCell.GridPosition.x * 100f, (float)uI_ItemCell.GridPosition.y * -100f); Debug.Log("CREATED ITEM " + item.name + " for slot " + uI_ItemCell.name); return; } } } Debug.Log("NO PLACE FOR ITEM"); } public void RemoveItem(BaseItemData item) { ClearItemCells(item.GridPosition, item.Size); itemsInBag.Remove(item); } }