Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. public static Dictionary<string, BuildingType> LoadBuildingTypesWithString() {
  2. Dictionary<string, BuildingType> buildingTypes = new Dictionary<string, BuildingType>();
  3.  
  4. TextAsset text = Resources.Load<TextAsset>("Data/Buildings");
  5.  
  6. XmlTextReader reader_parent = new XmlTextReader(new StringReader(text.text));
  7.  
  8. if (reader_parent.ReadToDescendant("Buildings")) {
  9. if (reader_parent.ReadToDescendant("Building")) {
  10. do {
  11. string name = "";
  12. Sprite sprite;
  13. List<BuildCost> buildCosts = new List<BuildCost>();
  14. string rawLuaCode = "";
  15.  
  16. XmlReader reader = reader_parent.ReadSubtree();
  17.  
  18. while (reader.Read()) {
  19. switch (reader.Name) {
  20. case "Name":
  21. reader.Read();
  22. name = reader.ReadContentAsString();
  23. break;
  24. case "BuildCosts":
  25. string item = "";
  26. int amount = 0;
  27.  
  28. XmlReader buildCost_reader = reader.ReadSubtree();
  29. while (buildCost_reader.Read()) {
  30. switch (buildCost_reader.Name) {
  31. case "Item":
  32. reader.Read();
  33. item = buildCost_reader.ReadContentAsString();
  34. break;
  35. case "Amount":
  36. reader.Read();
  37. amount = buildCost_reader.ReadContentAsInt();
  38. break;
  39. }
  40. }
  41.  
  42. buildCosts.Add(new BuildCost(WorldController.instance.itemTypesString[item], amount));
  43. break;
  44. case "LUA":
  45. reader.Read();
  46. rawLuaCode = reader.ReadContentAsString();
  47. break;
  48. }
  49. }
  50.  
  51. sprite = (Sprite)Resources.Load("BuildingTypes/Sprites/" + name, typeof(Sprite));
  52.  
  53. buildingTypes.Add(name, new BuildingType(name, sprite, buildCosts, rawLuaCode));
  54. } while (reader_parent.ReadToNextSibling("Building"));
  55. } else {
  56. Debug.LogError("The Buildings definition file doesn't have any 'Building' elements");
  57. }
  58. } else {
  59. Debug.LogError("Did not find a 'Buildings' element in the definition file");
  60. }
  61.  
  62. /*foreach (BuildingType t in Resources.LoadAll("BuildingTypes", typeof(BuildingType))) {
  63. buildingTypes.Add(t.name, t);
  64. }*/
  65.  
  66. return buildingTypes;
  67. }
  68.  
  69. public static List<BuildingType> LoadBuildingTypes() {
  70. List<BuildingType> buildingTypes = new List<BuildingType>();
  71.  
  72. TextAsset text = Resources.Load<TextAsset>("Data/Buildings");
  73.  
  74. XmlTextReader reader_parent = new XmlTextReader(new StringReader(text.text));
  75.  
  76. if (reader_parent.ReadToDescendant("Buildings")) {
  77. if (reader_parent.ReadToDescendant("Building")) {
  78. do {
  79. string name = "";
  80. Sprite sprite;
  81. List<BuildCost> buildCosts = new List<BuildCost>();
  82. string rawLuaCode = "";
  83.  
  84. XmlReader reader = reader_parent.ReadSubtree();
  85.  
  86. while (reader.Read()) {
  87. switch (reader.Name) {
  88. case "Name":
  89. reader.Read();
  90. name = reader.ReadContentAsString();
  91. break;
  92. case "BuildCosts":
  93. string item = "";
  94. int amount = 0;
  95.  
  96. XmlReader buildCost_reader = reader.ReadSubtree();
  97. while (buildCost_reader.Read()) {
  98. switch (buildCost_reader.Name) {
  99. case "Item":
  100. reader.Read();
  101. item = buildCost_reader.ReadContentAsString();
  102. break;
  103. case "Amount":
  104. reader.Read();
  105. amount = buildCost_reader.ReadContentAsInt();
  106. break;
  107. }
  108. }
  109.  
  110. buildCosts.Add(new BuildCost(WorldController.instance.itemTypesString[item], amount));
  111. break;
  112. case "LUA":
  113. reader.Read();
  114. rawLuaCode = reader.ReadContentAsString();
  115. break;
  116. }
  117. }
  118.  
  119. sprite = (Sprite)Resources.Load("BuildingTypes/Sprites/" + name, typeof(Sprite));
  120.  
  121. buildingTypes.Add(new BuildingType(name, sprite, buildCosts, rawLuaCode));
  122. } while (reader_parent.ReadToNextSibling("Building"));
  123. } else {
  124. Debug.LogError("The Buildings definition file doesn't have any 'Building' elements");
  125. }
  126. } else {
  127. Debug.LogError("Did not find a 'Buildings' element in the definition file");
  128. }
  129.  
  130. /*foreach (BuildingType t in Resources.LoadAll("BuildingTypes", typeof(BuildingType))) {
  131. buildingTypes.Add(t);
  132. }*/
  133.  
  134. return buildingTypes;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement