2023-01-15 11:39:47 +00:00
|
|
|
//using System.Collections;
|
2022-10-19 13:51:46 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace SimpleInventorySystem
|
|
|
|
{
|
|
|
|
public class Inventory : MonoBehaviour
|
|
|
|
{
|
|
|
|
// Start is called before the first frame update
|
2023-01-15 11:39:47 +00:00
|
|
|
public bool hasBeenLooted = false;
|
2023-01-30 03:54:27 +00:00
|
|
|
public bool itemsHaveBeenInstantiated = false;
|
|
|
|
public bool itemsHaveBeenHidden = false;
|
2023-01-15 11:39:47 +00:00
|
|
|
public List<GameObject> prefabs;
|
|
|
|
[HideInInspector] public List<ItemUI> itemUIs;
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
//[SerializeField] private Slot[] slots;
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2023-01-15 11:39:47 +00:00
|
|
|
itemUIs = new List<ItemUI>();
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
}
|
2023-01-15 11:39:47 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Instantiates the item prefab first and calls necessary stuff.
|
|
|
|
/// </summary>
|
|
|
|
public void Loot()
|
|
|
|
{
|
|
|
|
if (!hasBeenLooted && prefabs != null)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < prefabs.Count; i++)
|
|
|
|
{
|
|
|
|
if (prefabs[i] == null)
|
|
|
|
{
|
2023-02-04 01:42:50 +00:00
|
|
|
//prefabs.Remove(prefabs[i]);
|
|
|
|
continue;
|
2023-01-15 11:39:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GameObject gObj = Instantiate(prefabs[i]);
|
|
|
|
ItemUI item = gObj.GetComponentInChildren<ItemUI>();
|
2023-01-30 02:35:04 +00:00
|
|
|
PickUp pickUp = gObj.GetComponentInChildren<PickUp>();
|
|
|
|
gObj.SetActive(true);
|
|
|
|
gObj.transform.position = Vector3.zero;
|
2023-01-31 02:12:08 +00:00
|
|
|
gObj.transform.SetParent(InventorySystem.instance.DefaultItemSpawn, false);
|
2023-02-04 01:42:50 +00:00
|
|
|
if(gObj.GetComponent<Rigidbody>() != null)
|
|
|
|
gObj.GetComponent<Rigidbody>().useGravity = false;
|
2023-01-15 11:39:47 +00:00
|
|
|
if(item != null)
|
|
|
|
{
|
|
|
|
item.gameObject.SetActive(true);
|
2023-01-30 02:35:04 +00:00
|
|
|
pickUp.gameObject.SetActive(true);
|
2023-01-15 11:39:47 +00:00
|
|
|
AddOwnership(item, this);
|
|
|
|
//item.transform.SetParent(transform, true);
|
|
|
|
item.InitializeImageSize();
|
|
|
|
item.ResetTransform();
|
|
|
|
//gObj.transform.SetParent(item.transform, true);
|
2023-01-30 02:35:04 +00:00
|
|
|
//gObj.SetActive(false);
|
|
|
|
//item.pickUp = gObj.GetComponentInChildren<PickUp>();
|
2023-01-15 11:39:47 +00:00
|
|
|
}
|
2023-01-31 02:12:08 +00:00
|
|
|
Invoke(nameof(HideGameObjects), .1f);
|
2023-01-15 11:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-04 01:42:50 +00:00
|
|
|
Debug.Log(itemUIs.Count);
|
2023-01-15 11:39:47 +00:00
|
|
|
hasBeenLooted = true;
|
2023-01-30 03:54:27 +00:00
|
|
|
itemsHaveBeenInstantiated = true;
|
2023-01-15 11:39:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hasBeenLooted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 02:12:08 +00:00
|
|
|
void HideGameObjects()
|
|
|
|
{
|
|
|
|
if (hasBeenLooted && itemsHaveBeenInstantiated && !itemsHaveBeenHidden)
|
|
|
|
{
|
|
|
|
foreach (ItemUI item in itemUIs)
|
|
|
|
{
|
2023-03-13 10:25:15 +00:00
|
|
|
//Debug.Log(item.gameObject.name);
|
|
|
|
//Debug.Log(item.pickUp.gameObject.name);
|
2023-01-31 02:12:08 +00:00
|
|
|
item.pickUp.ItemGameObject.SetActive(false);
|
|
|
|
}
|
|
|
|
itemsHaveBeenHidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 11:39:47 +00:00
|
|
|
public void RemoveOwnership(ItemUI item, Inventory newOwnerInventory)
|
|
|
|
{
|
|
|
|
item.Inventory = newOwnerInventory;
|
|
|
|
//items.Remove(item.pickUp.ItemGameObject);
|
|
|
|
itemUIs.Remove(item);
|
|
|
|
newOwnerInventory.AddOwnership(item, this);
|
|
|
|
}
|
|
|
|
public void AddOwnership(ItemUI item, Inventory oldOwnerInventory)
|
|
|
|
{
|
|
|
|
//items.Add(item.pickUp.ItemGameObject);
|
|
|
|
itemUIs.Add(item);
|
2023-02-04 01:42:50 +00:00
|
|
|
//item.Inventory = this;
|
2023-01-15 11:39:47 +00:00
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
}
|