94 lines
3.3 KiB
C#
94 lines
3.3 KiB
C#
//using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace SimpleInventorySystem
|
|
{
|
|
public class Inventory : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
public bool hasBeenLooted = false;
|
|
public bool itemsHaveBeenInstantiated = false;
|
|
public bool itemsHaveBeenHidden = false;
|
|
public List<GameObject> prefabs;
|
|
[HideInInspector] public List<ItemUI> itemUIs;
|
|
|
|
//[SerializeField] private Slot[] slots;
|
|
|
|
void Start()
|
|
{
|
|
itemUIs = new List<ItemUI>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(hasBeenLooted && itemsHaveBeenInstantiated && itemsHaveBeenHidden)
|
|
{
|
|
foreach(ItemUI item in itemUIs)
|
|
{
|
|
item.pickUp.ItemGameObject.SetActive(false);
|
|
}
|
|
itemsHaveBeenHidden = true;
|
|
}
|
|
}
|
|
|
|
/// <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]);
|
|
}
|
|
else
|
|
{
|
|
GameObject gObj = Instantiate(prefabs[i]);
|
|
ItemUI item = gObj.GetComponentInChildren<ItemUI>();
|
|
PickUp pickUp = gObj.GetComponentInChildren<PickUp>();
|
|
gObj.SetActive(true);
|
|
gObj.transform.position = Vector3.zero;
|
|
gObj.transform.SetParent(this.transform, false);
|
|
gObj.GetComponent<Rigidbody>().useGravity = false;
|
|
if(item != null)
|
|
{
|
|
item.gameObject.SetActive(true);
|
|
pickUp.gameObject.SetActive(true);
|
|
AddOwnership(item, this);
|
|
//item.transform.SetParent(transform, true);
|
|
item.InitializeImageSize();
|
|
item.ResetTransform();
|
|
//gObj.transform.SetParent(item.transform, true);
|
|
//gObj.SetActive(false);
|
|
//item.pickUp = gObj.GetComponentInChildren<PickUp>();
|
|
}
|
|
}
|
|
}
|
|
hasBeenLooted = true;
|
|
itemsHaveBeenInstantiated = true;
|
|
}
|
|
else
|
|
{
|
|
hasBeenLooted = true;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |