Property.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Runtime.Serialization;
  3. using System.Text.RegularExpressions;
  4. using Assets.HeroEditor4D.Common.Scripts.Common;
  5. using Assets.HeroEditor4D.InventorySystem.Scripts.Enums;
  6. using UnityEngine;
  7. namespace Assets.HeroEditor4D.InventorySystem.Scripts.Data
  8. {
  9. /// <summary>
  10. /// Represents key-value pair for storing item params.
  11. /// Supported value formats:
  12. /// "[VALUE]"
  13. /// "[VALUE_MIN]-[VALUE_MAX]"
  14. /// "[VALUE]/[ELEMENT]"
  15. /// "[VALUE]/[ELEMENT]/[DURATION]"
  16. /// </summary>
  17. [Serializable]
  18. public class Property
  19. {
  20. public PropertyId Id;
  21. public string Value;
  22. [HideInInspector] [NonSerialized] public int ValueInt;
  23. [HideInInspector] [NonSerialized] public int Min;
  24. [HideInInspector] [NonSerialized] public int Max;
  25. [HideInInspector] [NonSerialized] public int Duration;
  26. [HideInInspector] [NonSerialized] public ElementId Element;
  27. [HideInInspector] [NonSerialized] public bool Percentage;
  28. public Property()
  29. {
  30. }
  31. public Property(PropertyId id, object value)
  32. {
  33. Id = id;
  34. Value = value.ToString();
  35. ParseValue();
  36. }
  37. public void ParseValue()
  38. {
  39. var parts = Value.Split('/');
  40. if (Id == PropertyId.Damage || Id == PropertyId.Resistance)
  41. {
  42. switch (parts.Length)
  43. {
  44. case 2:
  45. Element = parts[1].ToEnum<ElementId>();
  46. break;
  47. case 3:
  48. Element = parts[1].ToEnum<ElementId>();
  49. Duration = int.Parse(parts[2]);
  50. break;
  51. default:
  52. Element = ElementId.Physic;
  53. break;
  54. }
  55. }
  56. if (Regex.IsMatch(parts[0], @"^\d+-\d+$"))
  57. {
  58. parts = parts[0].Split('-');
  59. Min = int.Parse(parts[0]);
  60. Max = int.Parse(parts[1]);
  61. }
  62. else if (parts[0].EndsWith("%"))
  63. {
  64. ValueInt = int.Parse(parts[0].Replace("%", null));
  65. Percentage = true;
  66. }
  67. else
  68. {
  69. if (int.TryParse(parts[0], out var valueInt))
  70. {
  71. ValueInt = valueInt;
  72. }
  73. }
  74. }
  75. public void ReplaceValue(string value)
  76. {
  77. Value = value;
  78. ParseValue();
  79. }
  80. public void ReplaceValue(float value)
  81. {
  82. ReplaceValue(Mathf.RoundToInt(value));
  83. }
  84. public void ReplaceValue(int value)
  85. {
  86. Value = value.ToString();
  87. ParseValue();
  88. }
  89. public void Add(float value)
  90. {
  91. Add(Mathf.RoundToInt(value));
  92. }
  93. public void Add(int value)
  94. {
  95. if (Min > 0)
  96. {
  97. Min += value;
  98. Max += value;
  99. Value = $"{Min}-{Max}" + (Element == ElementId.Physic ? null : "/" + Element);
  100. }
  101. else
  102. {
  103. ValueInt += value;
  104. Value = ValueInt + (Element == ElementId.Physic ? null : "/" + Element);
  105. }
  106. }
  107. public void AddInPercentage(float value)
  108. {
  109. if (Min > 0)
  110. {
  111. Min = Mathf.RoundToInt(Min * (1 + value / 100f));
  112. Max = Mathf.RoundToInt(Max * (1 + value / 100f));
  113. Value = $"{Min}-{Max}" + (Element == ElementId.Physic ? null : "/" + Element);
  114. }
  115. else
  116. {
  117. ValueInt = Mathf.RoundToInt(ValueInt * (1 + value / 100f));
  118. Value = ValueInt + (Element == ElementId.Physic ? null : "/" + Element);
  119. }
  120. }
  121. public static Property Parse(string value)
  122. {
  123. var parts = value.Split('=');
  124. var property = new Property
  125. {
  126. Id = parts[0].ToEnum<PropertyId>(),
  127. Value = parts[1]
  128. };
  129. property.ParseValue();
  130. return property;
  131. }
  132. [OnDeserialized]
  133. internal void OnDeserializedMethod(StreamingContext context)
  134. {
  135. ParseValue();
  136. }
  137. public Property Copy()
  138. {
  139. return new Property(Id, Value);
  140. }
  141. }
  142. }