SaveableItem.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization;
  4. using UnityEngine;
  5. namespace HQFPSWeapons
  6. {
  7. /// <summary>
  8. /// A SavableItem is an instance of an item, which can have it's own properties, vs ItemData which is just the data for an item, just the definition.
  9. /// </summary>
  10. [Serializable]
  11. public class SaveableItem : IDeserializationCallback
  12. {
  13. [NonSerialized]
  14. public Message<ItemProperty.Value> PropertyChanged = new Message<ItemProperty.Value>();
  15. [NonSerialized]
  16. public Message StackChanged = new Message();
  17. public ItemData Data { get { return ItemDatabase.Default.GetItemData(m_Name); } }
  18. public string Name { get { return m_Name; } }
  19. public int CurrentStackSize { get { return m_CurrentStackSize; } set { m_CurrentStackSize = value; StackChanged.Send(); } }
  20. public ItemPropertyList Properties { get { return m_Properties; } }
  21. private string m_Name;
  22. private int m_CurrentStackSize;
  23. private ItemPropertyList m_Properties;
  24. public static implicit operator bool(SaveableItem item)
  25. {
  26. return item != null;
  27. }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. public SaveableItem(ItemData data, int currentInStack = 1, ItemPropertyList customProperties = null)
  32. {
  33. m_Name = data.Name;
  34. CurrentStackSize = Mathf.Clamp(currentInStack, 1, data.StackSize);
  35. // if(data.IsContainer)
  36. // m_Container = data.GenerateContainer(null);
  37. if(customProperties != null)
  38. m_Properties = CloneProperties(customProperties);
  39. else
  40. m_Properties = CloneProperties(data.Properties);
  41. for(int i = 0;i < m_Properties.Count;i ++)
  42. m_Properties[i].Changed.AddListener(On_PropertyChanged);
  43. }
  44. public void OnDeserialization(object sender)
  45. {
  46. PropertyChanged = new Message<ItemProperty.Value>();
  47. StackChanged = new Message();
  48. var itemDatabase = ItemDatabase.Default;
  49. if(itemDatabase)
  50. {
  51. ItemData data;
  52. if(itemDatabase.TryGetItem(Name, out data))
  53. {
  54. for(int i = 0;i < m_Properties.Count;i ++)
  55. {
  56. m_Properties[i].Changed = new Message<ItemProperty.Value>();
  57. m_Properties[i].Changed.AddListener(On_PropertyChanged);
  58. }
  59. }
  60. else
  61. Debug.LogErrorFormat("[SavableItem] - This item couldn't be initialized and will not function properly. No item with the name {0} was found in the database!", Name);
  62. }
  63. else
  64. Debug.LogError("[SavableItem] - This item couldn't be initialized and will not function properly. The item database provided is null!");
  65. }
  66. public string GetDescription(int index)
  67. {
  68. string description = string.Empty;
  69. if(index > -1 && Data.Descriptions.Length > index)
  70. {
  71. try
  72. {
  73. description = string.Format(Data.Descriptions[index].Description, m_Properties.ToArray());
  74. }
  75. catch
  76. {
  77. Debug.LogError("[SavableItem] - You tried to access a property through the item description, but the property doesn't exist. The item name is: " + Name);
  78. }
  79. }
  80. return description;
  81. }
  82. public bool HasProperty(string name)
  83. {
  84. for(int i = 0;i < m_Properties.Count;i ++)
  85. if(m_Properties[i].Name == name)
  86. return true;
  87. return false;
  88. }
  89. /// <summary>
  90. /// Use this if you are sure the item has this property.
  91. /// </summary>
  92. public ItemProperty.Value GetProperty(string name)
  93. {
  94. ItemProperty.Value propertyValue = null;
  95. for(int i = 0;i < m_Properties.Count;i ++)
  96. if(m_Properties[i].Name == name)
  97. {
  98. propertyValue = m_Properties[i];
  99. break;
  100. }
  101. return propertyValue;
  102. }
  103. /// <summary>
  104. /// Use this if you are NOT sure the item has this property.
  105. /// </summary>
  106. public bool FindProperty(string name, out ItemProperty.Value propertyValue)
  107. {
  108. propertyValue = null;
  109. for(int i = 0;i < m_Properties.Count;i ++)
  110. if(m_Properties[i].Name == name)
  111. {
  112. propertyValue = m_Properties[i];
  113. return true;
  114. }
  115. return false;
  116. }
  117. private ItemPropertyList CloneProperties(ItemPropertyList properties)
  118. {
  119. List<ItemProperty.Value> list = new List<ItemProperty.Value>();
  120. for(int i = 0;i < properties.Count;i ++)
  121. list.Add(properties[i].GetClone());
  122. return new ItemPropertyList() { List = list };
  123. }
  124. private void On_PropertyChanged(ItemProperty.Value propertyValue)
  125. {
  126. PropertyChanged.Send(propertyValue);
  127. }
  128. }
  129. }