ManaGarden.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. using Random = UnityEngine.Random;
  11. public class ManaGarden : Regist
  12. {
  13. #region 变量
  14. public static int MyFlower
  15. {
  16. get { return _MyFlower; }
  17. set
  18. {
  19. _MyFlower = value;
  20. ManaReso.SetText("F_FlowerLab", string.Format("{0}", MyFlower));
  21. ManaReso.SetText("G_CollectLab1", string.Format("{0}/{1}", MyFlower, TotalFlower));
  22. }
  23. }
  24. public static int MyFlowerSpec
  25. {
  26. get { return _MyFlowerSpec; }
  27. set
  28. {
  29. _MyFlowerSpec = value;
  30. MyFlower = _MyFlowerSpec + _MyFlowerRegu;
  31. }
  32. }
  33. public static int MyFlowerRegu
  34. {
  35. get { return _MyFlowerRegu; }
  36. set
  37. {
  38. _MyFlowerRegu = value;
  39. MyFlower = _MyFlowerSpec + _MyFlowerRegu;
  40. }
  41. }
  42. public static int TotalFlower
  43. {
  44. get { return _TotalFlower; }
  45. set { _TotalFlower = value; }
  46. }
  47. public static int TotalFlowerSpec
  48. {
  49. get { return _TotalFlowerSpec; }
  50. set
  51. {
  52. _TotalFlowerSpec = value;
  53. TotalFlower = _TotalFlowerSpec + _TotalFlowerRegu;
  54. }
  55. }
  56. public static int TotalFlowerRegu
  57. {
  58. get { return _TotalFlowerRegu; }
  59. set
  60. {
  61. _TotalFlowerRegu = value;
  62. TotalFlower = _TotalFlowerSpec + _TotalFlowerRegu;
  63. }
  64. }
  65. public static List<ObjType> AnimList
  66. {
  67. get
  68. {
  69. if (_AnimList == null)
  70. {
  71. _AnimList = new List<ObjType>();
  72. }
  73. return _AnimList;
  74. }
  75. set { _AnimList = value; }
  76. }
  77. private static int _MyFlower;
  78. private static int _MyFlowerSpec;
  79. private static int _MyFlowerRegu;
  80. private static int _TotalFlower;
  81. private static int _TotalFlowerSpec;
  82. private static int _TotalFlowerRegu;
  83. private static List<ObjType> _AnimList;
  84. public static bool Award;
  85. public static bool AwardLock;
  86. public static float AnimTimer;
  87. public static float AwardTimer;
  88. public static Slot SeleSlot;
  89. public static FlowerInfo SeleFlowerInfo;
  90. public static List<Slot> SlotList;
  91. public static List<Slot> PlantList;
  92. public static List<FlowerInfo> FlowerInfoList;
  93. #endregion
  94. public void FixedUpdate()
  95. {
  96. AnimThread();
  97. AwardThread();
  98. }
  99. public void AnimThread()
  100. {
  101. AnimTimer -= Time.fixedDeltaTime;
  102. if (AnimTimer < 0)
  103. {
  104. AnimTimer = Random.Range(0f, 30f);
  105. if (AnimList.Count > 0 && PlantList.Count > 0)
  106. {
  107. PlantList.Random().Flower.PlayAnim(AnimList.Random());
  108. }
  109. }
  110. }
  111. public void AwardThread()
  112. {
  113. if (Award && !AwardLock)
  114. {
  115. AwardTimer -= Time.fixedDeltaTime;
  116. if (AwardTimer <= 0)
  117. {
  118. List<Flower> spareList = new List<Flower>();
  119. for (int i = 0; i < PlantList.Count; i++)
  120. {
  121. if (PlantList[i].Flower.Award == false)
  122. {
  123. spareList.Add(PlantList[i].Flower);
  124. }
  125. }
  126. if (spareList.Count == 0)
  127. {
  128. return;
  129. }
  130. if (spareList.Count == 1)
  131. {
  132. Award = false;
  133. spareList[0].Award = true;
  134. AwardTimer = Random.Range(20, 60);
  135. }
  136. else
  137. {
  138. spareList.Random().Award = true;
  139. AwardTimer = Random.Range(20, 60);
  140. }
  141. }
  142. }
  143. }
  144. public override void Instantiate()
  145. {
  146. ManaReso.Get("Garden", Folder.Garden, true, transform, true).AddScript<Garden>();
  147. #region 生成FlowerItem
  148. PlantList = new List<Slot>();
  149. FlowerInfoList = new List<FlowerInfo>();
  150. List<XmlAttributeCollection> attributesList = Data.GetFlowerConfig();
  151. for (int i = 0; i < attributesList.Count; i++)
  152. {
  153. FlowerInfo flowerInfo = new FlowerInfo(attributesList[i]);
  154. if (flowerInfo.Special)
  155. {
  156. TotalFlowerSpec++;
  157. }
  158. else
  159. {
  160. TotalFlowerRegu++;
  161. }
  162. FlowerInfoList.Add(flowerInfo);
  163. }
  164. #endregion
  165. }
  166. public override void RegistValueA()
  167. {
  168. Award = true;
  169. AwardLock = false;
  170. AnimTimer = Random.Range(0f, 30f);
  171. AwardTimer = Random.Range(20f, 60f);
  172. SlotList = new List<Slot>()
  173. {
  174. ManaReso.Get("SlotA1").AddComponent<Slot>(),
  175. ManaReso.Get("SlotA2").AddComponent<Slot>(),
  176. ManaReso.Get("SlotA3").AddComponent<Slot>(),
  177. ManaReso.Get("SlotA4").AddComponent<Slot>(),
  178. ManaReso.Get("SlotA5").AddComponent<Slot>(),
  179. ManaReso.Get("SlotA6").AddComponent<Slot>(),
  180. ManaReso.Get("SlotA7").AddComponent<Slot>(),
  181. ManaReso.Get("SlotA8").AddComponent<Slot>(),
  182. ManaReso.Get("SlotA9").AddComponent<Slot>(),
  183. ManaReso.Get("SlotB1").AddComponent<Slot>(),
  184. ManaReso.Get("SlotB2").AddComponent<Slot>(),
  185. ManaReso.Get("SlotB3").AddComponent<Slot>(),
  186. ManaReso.Get("SlotB4").AddComponent<Slot>(),
  187. ManaReso.Get("SlotB5").AddComponent<Slot>(),
  188. ManaReso.Get("SlotB6").AddComponent<Slot>(),
  189. ManaReso.Get("SlotB7").AddComponent<Slot>(),
  190. ManaReso.Get("SlotB8").AddComponent<Slot>(),
  191. ManaReso.Get("SlotB9").AddComponent<Slot>(),
  192. };
  193. }
  194. public static void UnlockSlot()
  195. {
  196. for (int i = 0; i < SlotList.Count; i++)
  197. {
  198. if (SlotList[i].Valid == false)
  199. {
  200. ManaData.Slot++;
  201. SlotList[i].Valid = true;
  202. SlotList[i].Available = true;
  203. return;
  204. }
  205. }
  206. ManaDebug.Log("所有土地已解锁");
  207. }
  208. public static void SetFlowerCard(FlowerInfo flowerInfo)
  209. {
  210. SeleFlowerInfo = flowerInfo;
  211. ManaText.Add(ManaReso.Get<Text>("H_Lab"), new LanStr("FlowerName", flowerInfo._Name));
  212. Image image = ManaReso.Get<Image>("H_Icon2");
  213. image.sprite = flowerInfo.Sprite;
  214. Vector2 newSize = flowerInfo.Sprite.rect.size;
  215. newSize.x *= 0.65f;
  216. newSize.y *= 0.65f;
  217. image.rectTransform.sizeDelta = newSize;
  218. }
  219. public static void ShowPlantCard(Slot slot, FlowerInfo flowerInfo)
  220. {
  221. ManaReso.Get("H_FlowerCard").TweenForCG();
  222. ManaReso.SetActive("H_Grid", true);
  223. ManaReso.SetActive("H_Prev", true);
  224. ManaReso.SetActive("H_Next", true);
  225. ManaReso.SetActive("H_Place", true);
  226. ManaReso.SetActive("H_Icon1", false);
  227. ManaReso.SetActive("H_Retrieve", false);
  228. SeleSlot = slot;
  229. SetFlowerCard(flowerInfo);
  230. }
  231. public static void ShowRetrieveCard(FlowerInfo flowerInfo)
  232. {
  233. SeleSlot = flowerInfo.Slot;
  234. SetFlowerCard(flowerInfo);
  235. ManaReso.SetActive("H_Grid", false);
  236. ManaReso.SetActive("H_Prev", false);
  237. ManaReso.SetActive("H_Next", false);
  238. ManaReso.SetActive("H_Place", false);
  239. ManaReso.SetActive("H_Icon1", true);
  240. ManaReso.SetActive("H_Retrieve", true);
  241. ManaReso.Get("H_FlowerCard").TweenForCG();
  242. }
  243. public static void RetriveFlower()
  244. {
  245. SeleSlot.Retrieve();
  246. if (PlantList.Count == 0)
  247. {
  248. Award = false;
  249. }
  250. }
  251. public static void RetriveFlowerAll()
  252. {
  253. for (int i = 0; i < PlantList.Count; i++)
  254. {
  255. PlantList[i].Retrieve();
  256. i--;
  257. }
  258. Award = false;
  259. }
  260. public static void PlantFlower()
  261. {
  262. SeleSlot.Plant(SeleFlowerInfo, true);
  263. }
  264. public static void PlantFlower(FlowerInfo flowerInfo)
  265. {
  266. if (flowerInfo.Plant)
  267. {
  268. ShowRetrieveCard(flowerInfo);
  269. }
  270. else
  271. {
  272. Slot slot = null;
  273. for (int i = 0; i < SlotList.Count; i++)
  274. {
  275. if (SlotList[i].Available)
  276. {
  277. slot = SlotList[i];
  278. break;
  279. }
  280. }
  281. if (slot == null)
  282. {
  283. ManaDebug.Log("已经没有空地了");
  284. }
  285. else
  286. {
  287. slot.Plant(flowerInfo, true);
  288. }
  289. }
  290. }
  291. public static void PlantFlower(int id, string parName)
  292. {
  293. Slot slot = ManaReso.Get<Slot>(parName);
  294. FlowerInfo flowerInfo = FlowerInfoList[id];
  295. slot.Plant(flowerInfo, false);
  296. }
  297. }