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> ItemPool = new Dictionary>(); [HideInInspector] public Dictionary> ItemPoolBySize = new Dictionary>(); 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>(); foreach (GameObject gameObject in spawnableItemPrefabs) { ItemUI item = gameObject.GetComponentInChildren(); if (item != null) { if (!ItemPool.ContainsKey(item.rarity)) { ItemPool.Add(item.rarity, new List()); } ItemPool[item.rarity].Add(gameObject); } //Debug.Log(gameObject.name); } } public RarityType GetRandomRarity(float rate) { float currentRate = 0; RarityType fallBackRarity = spawnableItemPrefabs[0].GetComponentInChildren().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; } }