projectEli/Assets/Inventory System/Scripts/Inventory/Inventory.cs

105 lines
3.7 KiB
C#
Raw Normal View History

2023-01-15 11:39:47 +00:00
//using System.Collections;
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;
//[SerializeField] private Slot[] slots;
void Start()
{
2023-01-15 11:39:47 +00:00
itemUIs = new List<ItemUI>();
}
// 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)
{
//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;
gObj.transform.SetParent(InventorySystem.instance.DefaultItemSpawn, false);
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
}
Invoke(nameof(HideGameObjects), .1f);
2023-01-15 11:39:47 +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;
}
}
void HideGameObjects()
{
if (hasBeenLooted && itemsHaveBeenInstantiated && !itemsHaveBeenHidden)
{
foreach (ItemUI item in itemUIs)
{
Debug.Log(item.gameObject.name);
Debug.Log(item.pickUp.gameObject.name);
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);
//item.Inventory = this;
2023-01-15 11:39:47 +00:00
}
}
}