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

94 lines
3.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SimpleInventorySystem
{
[CreateAssetMenu(fileName = "New Scriptable Loot Container", menuName = "Inventory System/Loot Containers")]
public class ScriptableLootContainer : ScriptableObject
{
public GameObject[] spawnableItemPrefabs;
public SpawnRairtyToItemRate[] spawnRates;
public float spawnTime = 1800; // 30 minutes * 60 seconds
public float minimumSpawnDistance = 100;
public Size minimumContainerSize = new Size(3, 3);
public Size maximumContainerSize = new Size(5, 5);
[HideInInspector] public Dictionary<RarityType, List<GameObject>> ItemPool = new Dictionary<RarityType, List<GameObject>>();
[HideInInspector] public Dictionary<int, List<GameObject>> ItemPoolBySize = new Dictionary<int, List<GameObject>>();
public void Awake()
{
}
public void OnEnable()
{
//Debug.Log("Awake");
if (minimumContainerSize.Width > maximumContainerSize.Width)
{
Debug.LogError("Minimum Container Size's width is greater than Maximum Containzer Size's width. Attempting to resolve.");
minimumContainerSize.Width = 3;
maximumContainerSize.Width = 5;
}
if (minimumContainerSize.Height > maximumContainerSize.Height)
{
Debug.LogError("Maximum Container Size's height is greater than Maximum Containzer Size's height. Attempting to resolve.");
minimumContainerSize.Height = 3;
maximumContainerSize.Height = 5;
}
//Debug.Log(spawnableItemPrefabs.Length);
ItemPool = new Dictionary<RarityType, List<GameObject>>();
foreach (GameObject gameObject in spawnableItemPrefabs)
{
ItemUI item = gameObject.GetComponentInChildren<ItemUI>();
if (item != null)
{
if (!ItemPool.ContainsKey(item.rarity))
{
ItemPool.Add(item.rarity, new List<GameObject>());
}
ItemPool[item.rarity].Add(gameObject);
}
//Debug.Log(gameObject.name);
}
}
public RarityType GetRandomRarity(float rate)
{
float currentRate = 0;
RarityType fallBackRarity = spawnableItemPrefabs[0].GetComponentInChildren<ItemUI>().rarity;
foreach (SpawnRairtyToItemRate sR in spawnRates)
{
if (rate <= currentRate + sR.spawnRate)
{
return sR.rarity;
}
else
{
currentRate = sR.spawnRate;
}
}
return fallBackRarity;
}
public GameObject GetRandomItem(RarityType rarity)
{
if (!ItemPool.ContainsKey(rarity))
{
Debug.LogError("Rarity not found in Item Pool.");
return null;
}
int index = UnityEngine.Random.Range(0, ItemPool[rarity].Count);
return ItemPool[rarity][index];
}
}
[Serializable]
public class SpawnRairtyToItemRate
{
public RarityType rarity;
[Range(0, 1)] public float spawnRate;
}
}