projectEli/Assets/Inventory System/Scripts/Inventory/LootContainer.cs
2023-03-16 15:44:34 -07:00

138 lines
4.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SimpleInventorySystem
{
public class LootContainer : MonoBehaviour
{
// Start is called before the first frame update
public bool hasBeenLooted = false;
public bool itemsHaveBeenInstantiated = false;
public bool itemsHaveBeenHidden = false;
[HideInInspector] public List<ItemUI> itemUIs;
/// <summary>
/// Instantiates the item prefab first and calls necessary stuff.
/// </summary>
public void Loot()
{
if (!hasBeenLooted)
{
itemUIs = new List<ItemUI>();
Size size = GenerateContainerSize();
PopulateContainer(size);
hasBeenLooted = true;
}
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;
}
}
public ScriptableLootContainer data;
// Start is called before the first frame update
void Start()
{
if (data == null)
{
Debug.LogError("Loot Container should have a Scriptable Loot Container referenced.");
return;
}
}
public Size GenerateContainerSize()
{
int width = Random.Range(data.minimumContainerSize.Width, data.maximumContainerSize.Width + 1);
int height = Random.Range(data.minimumContainerSize.Height, data.maximumContainerSize.Height + 1);
return new Size(width, height);
}
public void PopulateContainer(Size size)
{
int cells = size.Width * size.Height;
int limit = 50;
while(limit > 0 && cells > 0)
{
GameObject item = Spawn();
ItemUI itemUI = item.GetComponentInChildren<ItemUI>();
PickUp pickUp = item.GetComponentInChildren<PickUp>();
item.SetActive(true);
item.transform.position = Vector3.zero;
item.transform.SetParent(InventorySystem.instance.DefaultItemSpawn, false);
if (item.GetComponent<Rigidbody>() != null)
item.GetComponent<Rigidbody>().useGravity = false;
if (itemUI != null)
{
itemUIs.Add(itemUI);
itemUI.gameObject.SetActive(true);
pickUp.gameObject.SetActive(true);
//AddOwnership(item, this);
//item.transform.SetParent(transform, true);
itemUI.InitializeImageSize();
itemUI.ResetTransform();
//gObj.transform.SetParent(item.transform, true);
//gObj.SetActive(false);
//item.pickUp = gObj.GetComponentInChildren<PickUp>();
}
cells -= itemUI.width * itemUI.height;
limit--;
}
Invoke(nameof(HideGameObjects), .1f);
itemsHaveBeenInstantiated = true;
}
private GameObject Spawn()
{
// RNG rarity
float rate = Random.Range(0f, 1f);
RarityType rarity = data.GetRandomRarity(rate);
// RNG item
GameObject prefab = data.GetRandomItem(rarity);
GameObject newItem = Instantiate(prefab);
Debug.Log($"Loot Container: {newItem.name}");
return newItem;
}
// Update is called once per frame
void Update()
{
if (data == null)
{
return;
}
}
/*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;
}*/
}
}