ManaGarden.cs 9.5 KB

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