EquipManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. using LitJson;
  6. public class EquipManager
  7. {
  8. private Dictionary<int, EquipData> dataDict;
  9. private Dictionary<int, EquipedData> equipedDict;
  10. private List<Equipment> inventoryList;
  11. public List<Equipment> hpList;
  12. public List<Equipment> dmgList;
  13. public List<Equipment> moveList;
  14. public EquipManager()
  15. {
  16. dataDict = new Dictionary<int, EquipData>();
  17. InitByXML("XML/Config/All/equip_config");
  18. equipedDict = new Dictionary<int, EquipedData> ();
  19. inventoryList = new List<Equipment> ();
  20. hpList = new List<Equipment> ();
  21. dmgList = new List<Equipment> ();
  22. moveList = new List<Equipment> ();
  23. InitEquiped ();
  24. }
  25. public void InitByXML(string path)
  26. {
  27. TextAsset textAsset = (TextAsset)Resources.Load(path);
  28. XmlDocument xml = new XmlDocument();
  29. xml.LoadXml(textAsset.text);
  30. XmlNode mapNode = xml.SelectSingleNode("data");
  31. XmlNodeList nodeList = mapNode.SelectNodes("item");
  32. for(int i=0; i<nodeList.Count; i++)
  33. {
  34. EquipData data = new EquipData(nodeList[i] as XmlElement);
  35. if(!dataDict.ContainsKey(data.id))
  36. dataDict.Add(data.id, data);
  37. else
  38. Debuger.LogError("Equipment " + data.id + " has duplicate");
  39. }
  40. }
  41. public void InitEquipInventory(JsonData jsonArr)
  42. {
  43. inventoryList.Clear ();
  44. hpList.Clear ();
  45. dmgList.Clear ();
  46. moveList.Clear ();
  47. for(int i=0; i<jsonArr.Count; i++)
  48. {
  49. JsonData data = jsonArr [i];
  50. int id = JsonUtil.ToInt (data["id"]);
  51. int level = JsonUtil.ToInt (data["level"]);
  52. int count = JsonUtil.ToInt (data["count"]);
  53. Equipment equipment = new Equipment (GetData (id));
  54. equipment.SetCount (count);
  55. equipment.SetLevel (level);
  56. // equipment.SetCount (Random.Range(0, 10));
  57. // equipment.SetLevel (Random.Range(1, 5));
  58. inventoryList.Add (equipment);
  59. if (equipment.HasHpProperty ())
  60. hpList.Add (equipment);
  61. if (equipment.HasDamageProperty ())
  62. dmgList.Add (equipment);
  63. if (equipment.HasMoveProperty ())
  64. moveList.Add (equipment);
  65. }
  66. SortRecommand ();
  67. }
  68. private void SortRecommand()
  69. {
  70. hpList.Sort (HpCompare);
  71. dmgList.Sort (DamageCompare);
  72. moveList.Sort (MoveCompare);
  73. }
  74. private static int HpCompare(Equipment a, Equipment b)
  75. {
  76. if (a.GetHpProperty () > b.GetHpProperty ())
  77. return -1;
  78. else if (a.GetHpProperty () < b.GetHpProperty ())
  79. return 1;
  80. return 0;
  81. }
  82. private static int DamageCompare(Equipment a, Equipment b)
  83. {
  84. if (a.GetDamageProperty () > b.GetDamageProperty ())
  85. return -1;
  86. else if (a.GetDamageProperty () < b.GetDamageProperty ())
  87. return 1;
  88. return 0;
  89. }
  90. private static int MoveCompare(Equipment a, Equipment b)
  91. {
  92. if (a.GetMoveProperty () > b.GetMoveProperty ())
  93. return -1;
  94. else if (a.GetMoveProperty () < b.GetMoveProperty ())
  95. return 1;
  96. return 0;
  97. }
  98. public EquipData GetData(int id)
  99. {
  100. EquipData data = null;
  101. dataDict.TryGetValue (id, out data);
  102. return data;
  103. }
  104. public List<Equipment> GetInventoryList()
  105. {
  106. return inventoryList;
  107. }
  108. private void InitEquiped()
  109. {
  110. List<CraftConfigData> list = CraftManager.GetInstance ().GetDataList ();
  111. for(int i=0; i<list.Count; i++)
  112. {
  113. CraftConfigData data = list [i];
  114. EquipedData equipedData = new EquipedData (data.id);
  115. equipedDict.Add (data.id, equipedData);
  116. }
  117. }
  118. public EquipedData GetEquipedData(int craftId)
  119. {
  120. if(equipedDict.ContainsKey(craftId))
  121. return equipedDict [craftId];
  122. return null;
  123. }
  124. private static EquipManager instance;
  125. public static EquipManager GetInstance()
  126. {
  127. if(instance == null)
  128. {
  129. instance = new EquipManager ();
  130. }
  131. return instance;
  132. }
  133. }