ItemParams.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Assets.HeroEditor4D.Common.Scripts.Common;
  5. using Assets.HeroEditor4D.InventorySystem.Scripts.Enums;
  6. using Newtonsoft.Json;
  7. namespace Assets.HeroEditor4D.InventorySystem.Scripts.Data
  8. {
  9. /// <summary>
  10. /// Represents generic item params (common for all items).
  11. /// </summary>
  12. [Serializable]
  13. public class ItemParams
  14. {
  15. public string Id;
  16. public int Level;
  17. public ItemRarity Rarity;
  18. public ItemType Type;
  19. public ItemClass Class;
  20. public List<ItemTag> Tags = new List<ItemTag>();
  21. public List<Property> Properties = new List<Property>();
  22. public int Price;
  23. public int Weight;
  24. public ItemMaterial Material;
  25. public string IconId;
  26. public string SpriteId;
  27. public string Meta;
  28. /// <summary>
  29. /// Use this property to provide localization values for items.
  30. /// </summary>
  31. [JsonIgnore, NonSerialized] public List<LocalizedValue> Localization = new List<LocalizedValue>();
  32. public char Grade => (char) (65 + Level);
  33. public Property FindProperty(PropertyId id)
  34. {
  35. var target = Properties.SingleOrDefault(i => i.Id == id && i.Element == ElementId.Physic);
  36. return target;
  37. }
  38. public Property FindProperty(PropertyId id, ElementId element)
  39. {
  40. var target = Properties.SingleOrDefault(i => i.Id == id && i.Element == element);
  41. return target;
  42. }
  43. public string GetLocalizedName(string language)
  44. {
  45. var localized = Localization.SingleOrDefault(i => i.Language == language) ?? Localization.SingleOrDefault(i => i.Language == "English");
  46. return localized == null ? Id : localized.Value;
  47. }
  48. public List<string> MetaToList()
  49. {
  50. return Meta.IsEmpty() ? new List<string>() : JsonConvert.DeserializeObject<List<string>>(Meta);
  51. }
  52. public ItemParams Copy()
  53. {
  54. return new ItemParams
  55. {
  56. Id = Id,
  57. Level = Level,
  58. Rarity = Rarity,
  59. Type = Type,
  60. Class = Class,
  61. Tags = Tags.ToList(),
  62. Properties = Properties.Select(i => i.Copy()).ToList(),
  63. Price = Price,
  64. Weight = Weight,
  65. Material = Material,
  66. IconId = IconId,
  67. SpriteId = SpriteId,
  68. Meta = Meta
  69. };
  70. }
  71. }
  72. }