CraftManager.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. public class CraftManager
  6. {
  7. private Dictionary<int, CraftConfigData> dataDict;
  8. private List<CraftConfigData> dataList;
  9. public CraftManager()
  10. {
  11. dataDict = new Dictionary<int, CraftConfigData>();
  12. dataList = new List<CraftConfigData>();
  13. InitByXML("XML/Config/All/craft_config");
  14. }
  15. public void InitByXML(string path)
  16. {
  17. TextAsset textAsset = (TextAsset)Resources.Load(path);
  18. XmlDocument xml = new XmlDocument();
  19. xml.LoadXml(textAsset.text);
  20. XmlNode mapNode = xml.SelectSingleNode("data");
  21. XmlNodeList nodeList = mapNode.SelectNodes("item");
  22. for(int i=0; i<nodeList.Count; i++)
  23. {
  24. CraftConfigData data = new CraftConfigData(nodeList[i]);
  25. if(!dataDict.ContainsKey(data.id))
  26. {
  27. dataDict.Add(data.id, data);
  28. if(data.id <= 1000)
  29. dataList.Add(data);
  30. }
  31. else
  32. Debuger.LogError("buff " + data.id + " has duplicate");
  33. }
  34. }
  35. private static CraftManager instance;
  36. public static CraftManager GetInstance()
  37. {
  38. if(instance == null)
  39. {
  40. instance = new CraftManager();
  41. }
  42. return instance;
  43. }
  44. public CraftConfigData GetData(int id)
  45. {
  46. if(dataDict.ContainsKey(id))
  47. return dataDict[id];
  48. Debuger.LogWarning("can not find craft["+id+"]");
  49. return null;
  50. }
  51. public List<CraftConfigData> GetDataList()
  52. {
  53. return dataList;
  54. }
  55. public int GetRandomCraftId(Player player)
  56. {
  57. int index = Random.Range(0, dataList.Count);
  58. return dataList[index].id;
  59. }
  60. }