| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Xml;
- using LitJson;
- public class EquipManager
- {
- private Dictionary<int, EquipData> dataDict;
- private Dictionary<int, EquipedData> equipedDict;
- private List<Equipment> inventoryList;
- public List<Equipment> hpList;
- public List<Equipment> dmgList;
- public List<Equipment> moveList;
- public EquipManager()
- {
- dataDict = new Dictionary<int, EquipData>();
- InitByXML("XML/Config/All/equip_config");
- equipedDict = new Dictionary<int, EquipedData> ();
- inventoryList = new List<Equipment> ();
- hpList = new List<Equipment> ();
- dmgList = new List<Equipment> ();
- moveList = new List<Equipment> ();
- InitEquiped ();
- }
- public void InitByXML(string path)
- {
- TextAsset textAsset = (TextAsset)Resources.Load(path);
- XmlDocument xml = new XmlDocument();
- xml.LoadXml(textAsset.text);
- XmlNode mapNode = xml.SelectSingleNode("data");
- XmlNodeList nodeList = mapNode.SelectNodes("item");
- for(int i=0; i<nodeList.Count; i++)
- {
- EquipData data = new EquipData(nodeList[i] as XmlElement);
- if(!dataDict.ContainsKey(data.id))
- dataDict.Add(data.id, data);
- else
- Debuger.LogError("Equipment " + data.id + " has duplicate");
- }
- }
- public void InitEquipInventory(JsonData jsonArr)
- {
- inventoryList.Clear ();
- hpList.Clear ();
- dmgList.Clear ();
- moveList.Clear ();
- for(int i=0; i<jsonArr.Count; i++)
- {
- JsonData data = jsonArr [i];
- int id = JsonUtil.ToInt (data["id"]);
- int level = JsonUtil.ToInt (data["level"]);
- int count = JsonUtil.ToInt (data["count"]);
- Equipment equipment = new Equipment (GetData (id));
- equipment.SetCount (count);
- equipment.SetLevel (level);
- // equipment.SetCount (Random.Range(0, 10));
- // equipment.SetLevel (Random.Range(1, 5));
- inventoryList.Add (equipment);
- if (equipment.HasHpProperty ())
- hpList.Add (equipment);
- if (equipment.HasDamageProperty ())
- dmgList.Add (equipment);
- if (equipment.HasMoveProperty ())
- moveList.Add (equipment);
- }
- SortRecommand ();
- }
- private void SortRecommand()
- {
- hpList.Sort (HpCompare);
- dmgList.Sort (DamageCompare);
- moveList.Sort (MoveCompare);
- }
- private static int HpCompare(Equipment a, Equipment b)
- {
- if (a.GetHpProperty () > b.GetHpProperty ())
- return -1;
- else if (a.GetHpProperty () < b.GetHpProperty ())
- return 1;
- return 0;
- }
- private static int DamageCompare(Equipment a, Equipment b)
- {
- if (a.GetDamageProperty () > b.GetDamageProperty ())
- return -1;
- else if (a.GetDamageProperty () < b.GetDamageProperty ())
- return 1;
- return 0;
- }
- private static int MoveCompare(Equipment a, Equipment b)
- {
- if (a.GetMoveProperty () > b.GetMoveProperty ())
- return -1;
- else if (a.GetMoveProperty () < b.GetMoveProperty ())
- return 1;
- return 0;
- }
- public EquipData GetData(int id)
- {
- EquipData data = null;
- dataDict.TryGetValue (id, out data);
- return data;
- }
- public List<Equipment> GetInventoryList()
- {
- return inventoryList;
- }
- private void InitEquiped()
- {
- List<CraftConfigData> list = CraftManager.GetInstance ().GetDataList ();
- for(int i=0; i<list.Count; i++)
- {
- CraftConfigData data = list [i];
- EquipedData equipedData = new EquipedData (data.id);
- equipedDict.Add (data.id, equipedData);
- }
- }
- public EquipedData GetEquipedData(int craftId)
- {
- if(equipedDict.ContainsKey(craftId))
- return equipedDict [craftId];
- return null;
- }
- private static EquipManager instance;
- public static EquipManager GetInstance()
- {
- if(instance == null)
- {
- instance = new EquipManager ();
- }
- return instance;
- }
- }
|