ManaGarden.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using UnityEngine.EventSystems;
  5. using System;
  6. using System.Xml;
  7. using System.Linq;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. public class ManaGarden : MonoBehaviour
  11. {
  12. #region 变量
  13. public static int MyFlowerSpec;
  14. public static int MyFlowerRegu;
  15. public static int TotalFlowerSpec;
  16. public static int TotalFlowerRegu;
  17. public static int SeleId;
  18. public static Flower SeleFlower;
  19. public static Transform SeleFlowerTra;
  20. public static List<Flower> PlantList;
  21. public static List<Transform> FlowerTraList;
  22. public static Dictionary<int, FlowerInfo> FlowerDic;
  23. #endregion
  24. private void Awake()
  25. {
  26. Initializer.RegistValue += RegistValue;
  27. PlantList = new List<Flower>();
  28. FlowerDic = new Dictionary<int, FlowerInfo>();
  29. ManaReso.Get("Garden", Folder.Object, true, transform, true).AddComponent<Garden>();
  30. #region 生成FlowerItem
  31. List<XmlAttributeCollection> attributesList = Data.GetFlowerConfig();
  32. for (int i = 0; i < attributesList.Count; i++)
  33. {
  34. FlowerInfo flowerInfo = new FlowerInfo(attributesList[i]);
  35. if (flowerInfo.Special)
  36. {
  37. TotalFlowerSpec++;
  38. }
  39. else
  40. {
  41. TotalFlowerRegu++;
  42. }
  43. FlowerDic.Add(flowerInfo.Id, flowerInfo);
  44. }
  45. #endregion
  46. #region 得到土堆
  47. FlowerTraList = new List<Transform>()
  48. {
  49. ManaReso.Get("FlowerTraA1"),
  50. ManaReso.Get("FlowerTraA2"),
  51. ManaReso.Get("FlowerTraA3"),
  52. ManaReso.Get("FlowerTraA4"),
  53. ManaReso.Get("FlowerTraA5"),
  54. ManaReso.Get("FlowerTraA6"),
  55. ManaReso.Get("FlowerTraA7"),
  56. ManaReso.Get("FlowerTraA8"),
  57. ManaReso.Get("FlowerTraA9"),
  58. ManaReso.Get("FlowerTraB1"),
  59. ManaReso.Get("FlowerTraB2"),
  60. ManaReso.Get("FlowerTraB3"),
  61. ManaReso.Get("FlowerTraB4"),
  62. ManaReso.Get("FlowerTraB5"),
  63. ManaReso.Get("FlowerTraB6"),
  64. ManaReso.Get("FlowerTraB7"),
  65. ManaReso.Get("FlowerTraB8"),
  66. ManaReso.Get("FlowerTraB9"),
  67. };
  68. #endregion
  69. }
  70. private static void RegistValue()
  71. {
  72. SeleId = 1;
  73. }
  74. public static void SetFlowerCard(int id)
  75. {
  76. SeleId = id;
  77. FlowerInfo flowerInfo = FlowerDic[id];
  78. ManaReso.SetText("H_Lab", flowerInfo.Name);
  79. Image image = ManaReso.Get<Image>("H_Icon2");
  80. image.sprite = flowerInfo.Sprite;
  81. Vector2 newSize = flowerInfo.Sprite.rect.size;
  82. newSize.x *= 0.65f;
  83. newSize.y *= 0.65f;
  84. image.rectTransform.sizeDelta = newSize;
  85. }
  86. public static void UpdateCollectStatus()
  87. {
  88. int myTotalFlower = MyFlowerSpec + MyFlowerRegu;
  89. ManaReso.SetText("F_FlowerLab", myTotalFlower.ToString());
  90. ManaReso.SetText("G_CollectLab1", string.Format("{0}/{1}", myTotalFlower, TotalFlowerRegu + TotalFlowerSpec));
  91. }
  92. public static void RetriveFlower()
  93. {
  94. ManaReso.Save(SeleFlower);
  95. PlantList.Remove(SeleFlower);
  96. FlowerInfo flowerInfo = FlowerDic[SeleFlower.Id];
  97. flowerInfo.Plant = false;
  98. SeleFlower.PosTra.SetCollider(true);
  99. }
  100. public static void RetriveFlowerAll()
  101. {
  102. for (int i = 0; i < PlantList.Count; i++)
  103. {
  104. ManaReso.Save(PlantList[i]);
  105. PlantList[i].PosTra.SetCollider(true);
  106. PlantList.RemoveAt(i--);
  107. }
  108. foreach (var kv in FlowerDic)
  109. {
  110. kv.Value.Plant = false;
  111. }
  112. }
  113. public static void PlaceFlower()
  114. {
  115. FlowerInfo flowerInfo = FlowerDic[SeleId];
  116. if (flowerInfo.Plant)
  117. {
  118. ManaMessage.Show("已经种植过了", 1);
  119. }
  120. else
  121. {
  122. flowerInfo.Plant = true;
  123. Flower flower = ManaReso.GetFlower(SeleId, true, SeleFlowerTra);
  124. PlantList.Add(flower);
  125. }
  126. } //FlowerCard种植
  127. public static void PlaceFlower(int id)
  128. {
  129. Transform tra = null;
  130. for (int i = 0; i < FlowerTraList.Count; i++)
  131. {
  132. if (FlowerTraList[i].childCount == 0)
  133. {
  134. tra = FlowerTraList[i];
  135. break;
  136. }
  137. }
  138. if (tra == null)
  139. {
  140. ManaMessage.Show("已经没有空地了", 1);
  141. }
  142. else
  143. {
  144. FlowerInfo flowerInfo = FlowerDic[id];
  145. if (flowerInfo.Plant)
  146. {
  147. ManaMessage.Show("已经种植过了", 1);
  148. }
  149. else
  150. {
  151. flowerInfo.Plant = true;
  152. Flower flower = ManaReso.GetFlower(id, true, tra);
  153. flower.Id = id;
  154. flower.PosTra = tra;
  155. PlantList.Add(flower);
  156. tra.SetCollider(false); //空地Collider
  157. }
  158. }
  159. }
  160. public static void PlaceFlower(int id, Transform tra)
  161. {
  162. FlowerInfo flowerInfo = FlowerDic[id];
  163. flowerInfo.Plant = true;
  164. Flower flower = ManaReso.GetFlower(id, true, tra);
  165. PlantList.Add(flower);
  166. } //读存档时调用
  167. }