ManaGarden.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 override void TutorialInstantiate()
  220. {
  221. Instantiate();
  222. }
  223. public override void TutorialRegistValue()
  224. {
  225. RegistValueA();
  226. }
  227. public static void UnlockSlot()
  228. {
  229. for (int i = 0; i < SlotList.Count; i++)
  230. {
  231. if (SlotList[i].Valid == false)
  232. {
  233. ManaData.Slot++;
  234. SlotList[i].Valid = true;
  235. SlotList[i].Available = true;
  236. return;
  237. }
  238. }
  239. ManaDebug.Log("所有土地已解锁");
  240. }
  241. public static void SetFlowerCard(FlowerInfo flowerInfo)
  242. {
  243. SeleFlowerInfo = flowerInfo;
  244. ManaText.Add(ManaReso.Get<Text>("H_Lab"), new LanStr("FlowerName", flowerInfo._Name));
  245. Image image = ManaReso.Get<Image>("H_Icon2");
  246. image.sprite = flowerInfo.Sprite;
  247. Vector2 newSize = flowerInfo.Sprite.rect.size;
  248. newSize.x *= 0.65f;
  249. newSize.y *= 0.65f;
  250. image.rectTransform.sizeDelta = newSize;
  251. }
  252. public static void ShowPlantCard(Slot slot, FlowerInfo flowerInfo)
  253. {
  254. ManaReso.Get("H_FlowerCard").TweenForCG();
  255. ManaReso.SetActive("H_Grid", true);
  256. ManaReso.SetActive("H_Prev", true);
  257. ManaReso.SetActive("H_Next", true);
  258. ManaReso.SetActive("H_Place", true);
  259. ManaReso.SetActive("H_Icon1", false);
  260. ManaReso.SetActive("H_Retrieve", false);
  261. SeleSlot = slot;
  262. SetFlowerCard(flowerInfo);
  263. }
  264. public static void ShowRetrieveCard(FlowerInfo flowerInfo)
  265. {
  266. SeleSlot = flowerInfo.Slot;
  267. SetFlowerCard(flowerInfo);
  268. ManaReso.SetActive("H_Grid", false);
  269. ManaReso.SetActive("H_Prev", false);
  270. ManaReso.SetActive("H_Next", false);
  271. ManaReso.SetActive("H_Place", false);
  272. ManaReso.SetActive("H_Icon1", true);
  273. ManaReso.SetActive("H_Retrieve", true);
  274. ManaReso.Get("H_FlowerCard").TweenForCG();
  275. }
  276. public static void RetriveFlower()
  277. {
  278. SeleSlot.Retrieve();
  279. if (PlantList.Count == 0)
  280. {
  281. Award = false;
  282. }
  283. }
  284. public static void RetriveFlowerAll()
  285. {
  286. for (int i = 0; i < PlantList.Count; i++)
  287. {
  288. PlantList[i].Retrieve();
  289. i--;
  290. }
  291. Award = false;
  292. }
  293. public static void PlantFlower()
  294. {
  295. SeleSlot.Plant(SeleFlowerInfo, true);
  296. }
  297. public static void PlantFlower(FlowerInfo flowerInfo)
  298. {
  299. if (flowerInfo.Plant)
  300. {
  301. ShowRetrieveCard(flowerInfo);
  302. }
  303. else
  304. {
  305. Slot slot = null;
  306. for (int i = 0; i < SlotList.Count; i++)
  307. {
  308. if (SlotList[i].Available)
  309. {
  310. slot = SlotList[i];
  311. break;
  312. }
  313. }
  314. if (slot == null)
  315. {
  316. ManaDebug.Log("已经没有空地了");
  317. }
  318. else
  319. {
  320. slot.Plant(flowerInfo, true);
  321. }
  322. }
  323. }
  324. public static void PlantFlower(int id, string parName)
  325. {
  326. Slot slot = ManaReso.Get<Slot>(parName);
  327. FlowerInfo flowerInfo = FlowerInfoList[id];
  328. slot.Plant(flowerInfo, false);
  329. }
  330. }