123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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<SpriteCollection> SpriteCollections = new();
- public List<IconCollection> IconCollections = new();
- public List<ItemParams> 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<string, ItemSprite> _itemSprites;
- private Dictionary<string, ItemIcon> _itemIcons;
- public Func<Item, ItemParams> 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;
- }
- /// <summary>
- /// Can be used to find sprites directly, with no existing items needed.
- /// </summary>
- 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;
- }
- /// <summary>
- /// Can be used to find icons directly, with no existing items needed.
- /// </summary>
- 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<Item, Sprite> 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<string, ItemSprite> CacheSprites()
- {
- var dict = new Dictionary<string, ItemSprite>();
- foreach (var sprite in SpriteCollections.SelectMany(i => i.GetAllSprites()))
- {
- if (!dict.ContainsKey(sprite.Id))
- {
- dict.Add(sprite.Id, sprite);
- }
- }
- return dict;
- }
- private Dictionary<string, ItemIcon> CacheIcons()
- {
- var dict = new Dictionary<string, ItemIcon>();
- foreach (var icon in IconCollections.SelectMany(i => i.Icons))
- {
- if (!dict.ContainsKey(icon.Id))
- {
- dict.Add(icon.Id, icon);
- }
- }
- return dict;
- }
- }
- }
|