using System; using System.Collections.Generic; using System.Linq; using Assets.HeroEditor4D.Common.Scripts.Collections; using Assets.HeroEditor4D.Common.Scripts.Data; using Assets.HeroEditor4D.InventorySystem.Scripts.Data; using Assets.HeroEditor4D.InventorySystem.Scripts.Enums; using UnityEngine; namespace Assets.HeroEditor4D.InventorySystem.Scripts { [CreateAssetMenu(fileName = "ItemCollection", menuName = "HeroEditor4D/ItemCollection")] public class ItemCollection : ScriptableObject { [Header("Main")] public List SpriteCollections = new(); public List IconCollections = new(); public List Items = new(); [Header("Extra")] public Sprite BackgroundBlue; public Sprite BackgroundBrown; public Sprite BackgroundGreen; public Sprite BackgroundGrey; public Sprite BackgroundPurple; public Sprite BackgroundRed; public static ItemCollection Active; private Dictionary _itemSprites; private Dictionary _itemIcons; public Func GetItemParamsOverride; public void OnEnable() { Items.ForEach(i => i.Properties.ForEach(p => p.ParseValue())); } public ItemParams GetItemParams(Item item) { if (GetItemParamsOverride != null) return GetItemParamsOverride(item); var itemParams = Items.SingleOrDefault(i => i.Id == item.Id); if (itemParams == null) { throw new Exception($"Item params not found: {item.Id}"); } return itemParams; } public ItemSprite GetItemSprite(Item item) { _itemSprites ??= CacheSprites(); var itemParams = GetItemParams(item); if (itemParams.SpriteId == null) return null; if (_itemSprites.ContainsKey(itemParams.SpriteId)) { return _itemSprites[itemParams.SpriteId]; } Debug.LogWarning($"Sprite not found: {itemParams.SpriteId}"); return null; } public ItemIcon GetItemIcon(Item item) { _itemIcons ??= CacheIcons(); var itemParams = GetItemParams(item); if (itemParams.IconId == null) return null; if (_itemIcons.ContainsKey(itemParams.IconId)) { return _itemIcons[itemParams.IconId]; } Debug.LogWarning($"Icon not found: {itemParams.IconId}"); return null; } /// /// Can be used to find sprites directly, with no existing items needed. /// public Sprite FindSprite(string spriteId) { _itemSprites ??= CacheSprites(); if (spriteId != null && _itemSprites.ContainsKey(spriteId)) { return _itemSprites[spriteId].Sprite; } Debug.LogWarning($"Sprite not found: {spriteId}"); return null; } /// /// Can be used to find icons directly, with no existing items needed. /// public Sprite FindIcon(string iconId) { _itemIcons ??= CacheIcons(); if (iconId != null && _itemIcons.ContainsKey(iconId)) { return _itemIcons[iconId].Sprite; } Debug.LogWarning($"Icon not found: {iconId}"); return null; } public Func GetBackgroundCustom; public Sprite GetBackground(Item item) { if (GetBackgroundCustom != null) return GetBackgroundCustom(item) ?? BackgroundBrown; switch (item.Params.Rarity) { case ItemRarity.Legacy: return BackgroundGrey; case ItemRarity.Basic: return BackgroundGrey; case ItemRarity.Common: return BackgroundBrown; case ItemRarity.Rare: return BackgroundRed; case ItemRarity.Epic: return BackgroundBlue; case ItemRarity.Legendary: return BackgroundPurple; default: throw new NotImplementedException(); } } public void Reset() { _itemSprites = null; _itemIcons = null; } private Dictionary CacheSprites() { var dict = new Dictionary(); foreach (var sprite in SpriteCollections.SelectMany(i => i.GetAllSprites())) { if (!dict.ContainsKey(sprite.Id)) { dict.Add(sprite.Id, sprite); } } return dict; } private Dictionary CacheIcons() { var dict = new Dictionary(); foreach (var icon in IconCollections.SelectMany(i => i.Icons)) { if (!dict.ContainsKey(icon.Id)) { dict.Add(icon.Id, icon); } } return dict; } } }