ManaGarden.cs 9.4 KB

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