ManaGarden.cs 9.5 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. private static int _MyFlower;
  77. private static int _MyFlowerSpec;
  78. private static int _MyFlowerRegu;
  79. private static int _TotalFlower;
  80. private static int _TotalFlowerSpec;
  81. private static int _TotalFlowerRegu;
  82. public static bool Award = true;
  83. public static bool AwardLock;
  84. public static float AnimTimer;
  85. public static float AwardTimer;
  86. public static Slot SeleSlot;
  87. public static FlowerInfo SeleFlowerInfo;
  88. public static List<Slot> SlotList = new List<Slot>();
  89. public static List<Slot> PlantList = new List<Slot>();
  90. public static List<ObjType> AnimList = new List<ObjType>();
  91. public static Dictionary<int, FlowerInfo> FlowerInfoDic = new Dictionary<int, FlowerInfo>();
  92. #endregion
  93. public void FixedUpdate()
  94. {
  95. if (ManaTutorial.TutorialA)
  96. {
  97. TutorialFixedUpdate();
  98. }
  99. else
  100. {
  101. RegularFixedUpdate();
  102. }
  103. }
  104. private void TutorialFixedUpdate()
  105. {
  106. }
  107. private void RegularFixedUpdate()
  108. {
  109. AnimThread();
  110. AwardThread();
  111. }
  112. public void AnimThread()
  113. {
  114. AnimTimer -= Time.fixedDeltaTime;
  115. if (AnimTimer < 0)
  116. {
  117. AnimTimer = Random.Range(0f, 30f);
  118. if (AnimList.Count > 0 && PlantList.Count > 0)
  119. {
  120. PlantList.Random().Flower.PlayAnim(AnimList.Random());
  121. }
  122. }
  123. }
  124. public void AwardThread()
  125. {
  126. if (Award && !AwardLock)
  127. {
  128. AwardTimer -= Time.fixedDeltaTime;
  129. if (AwardTimer <= 0)
  130. {
  131. List<Flower> spareList = new List<Flower>();
  132. for (int i = 0; i < PlantList.Count; i++)
  133. {
  134. if (PlantList[i].Flower.Award == false)
  135. {
  136. spareList.Add(PlantList[i].Flower);
  137. }
  138. }
  139. if (spareList.Count == 0)
  140. {
  141. return;
  142. }
  143. if (spareList.Count == 1)
  144. {
  145. Award = false;
  146. spareList[0].Award = true;
  147. AwardTimer = Random.Range(20, 60);
  148. }
  149. else
  150. {
  151. spareList.Random().Award = true;
  152. AwardTimer = Random.Range(20, 60);
  153. }
  154. }
  155. }
  156. }
  157. public override void Instantiate()
  158. {
  159. ManaReso.Get("Garden", Folder.Garden, true, transform, true).AddScript<Garden>();
  160. #region 生成FlowerItem
  161. List<XmlAttributeCollection> attributeList = Data.GetFlowerConfig();
  162. for (int i = 0; i < attributeList.Count; i++)
  163. {
  164. FlowerInfo flowerInfo = new FlowerInfo(attributeList[i]);
  165. if (flowerInfo.Special)
  166. {
  167. TotalFlowerSpec++;
  168. }
  169. else
  170. {
  171. TotalFlowerRegu++;
  172. }
  173. FlowerInfoDic.Add(flowerInfo.ID, flowerInfo);
  174. }
  175. #endregion
  176. }
  177. public override void RegistValueA()
  178. {
  179. AnimTimer = Random.Range(0f, 30f);
  180. AwardTimer = Random.Range(20f, 60f);
  181. SlotList = new List<Slot>()
  182. {
  183. ManaReso.Get("SlotA1").AddScript<Slot>(),
  184. ManaReso.Get("SlotA2").AddScript<Slot>(),
  185. ManaReso.Get("SlotA3").AddScript<Slot>(),
  186. ManaReso.Get("SlotA4").AddScript<Slot>(),
  187. ManaReso.Get("SlotA5").AddScript<Slot>(),
  188. ManaReso.Get("SlotA6").AddScript<Slot>(),
  189. ManaReso.Get("SlotA7").AddScript<Slot>(),
  190. ManaReso.Get("SlotA8").AddScript<Slot>(),
  191. ManaReso.Get("SlotA9").AddScript<Slot>(),
  192. ManaReso.Get("SlotB1").AddScript<Slot>(),
  193. ManaReso.Get("SlotB2").AddScript<Slot>(),
  194. ManaReso.Get("SlotB3").AddScript<Slot>(),
  195. ManaReso.Get("SlotB4").AddScript<Slot>(),
  196. ManaReso.Get("SlotB5").AddScript<Slot>(),
  197. ManaReso.Get("SlotB6").AddScript<Slot>(),
  198. ManaReso.Get("SlotB7").AddScript<Slot>(),
  199. ManaReso.Get("SlotB8").AddScript<Slot>(),
  200. ManaReso.Get("SlotB9").AddScript<Slot>(),
  201. };
  202. #region 读花朵存档
  203. List<int> flowerList = Data.GetFlowerList();
  204. for (int i = 0; i < flowerList.Count; i++)
  205. {
  206. FlowerInfoDic[flowerList[i]].Unlock = true;
  207. }
  208. List<KV<int, string>> plantList = Data.GetPlantList();
  209. for (int i = 0; i < plantList.Count; i++)
  210. {
  211. PlantFlower(plantList[i].Key - 1, plantList[i].Value);
  212. }
  213. #endregion
  214. }
  215. public static void UnlockSlot()
  216. {
  217. for (int i = 0; i < SlotList.Count; i++)
  218. {
  219. if (SlotList[i].Valid == false)
  220. {
  221. ManaData.Slot++;
  222. SlotList[i].Valid = true;
  223. SlotList[i].Available = true;
  224. return;
  225. }
  226. }
  227. ManaDebug.Log("所有土地已解锁");
  228. }
  229. public static void SetFlowerCard(FlowerInfo flowerInfo)
  230. {
  231. SeleFlowerInfo = flowerInfo;
  232. ManaText.Add(ManaReso.Get<Text>("H_Lab"), new LanStr("FlowerName", flowerInfo._Name));
  233. Image image = ManaReso.Get<Image>("H_Icon2");
  234. image.sprite = flowerInfo.Sprite;
  235. Vector2 newSize = flowerInfo.Sprite.rect.size;
  236. newSize.x *= 0.65f;
  237. newSize.y *= 0.65f;
  238. image.rectTransform.sizeDelta = newSize;
  239. }
  240. public static void ShowPlantCard(Slot slot, FlowerInfo flowerInfo)
  241. {
  242. ManaReso.Get("H_FlowerCard").TweenForCG();
  243. ManaReso.SetActive("H_Grid", true);
  244. ManaReso.SetActive("H_Prev", true);
  245. ManaReso.SetActive("H_Next", true);
  246. ManaReso.SetActive("H_Place", true);
  247. ManaReso.SetActive("H_Icon1", false);
  248. ManaReso.SetActive("H_Retrieve", false);
  249. SeleSlot = slot;
  250. SetFlowerCard(flowerInfo);
  251. }
  252. public static void ShowRetrieveCard(FlowerInfo flowerInfo)
  253. {
  254. SeleSlot = flowerInfo.Slot;
  255. SetFlowerCard(flowerInfo);
  256. ManaReso.SetActive("H_Grid", false);
  257. ManaReso.SetActive("H_Prev", false);
  258. ManaReso.SetActive("H_Next", false);
  259. ManaReso.SetActive("H_Place", false);
  260. ManaReso.SetActive("H_Icon1", true);
  261. ManaReso.SetActive("H_Retrieve", true);
  262. ManaReso.Get("H_FlowerCard").TweenForCG();
  263. }
  264. public static void RetriveFlower()
  265. {
  266. SeleSlot.Retrieve();
  267. if (PlantList.Count == 0)
  268. {
  269. Award = false;
  270. }
  271. }
  272. public static void RetriveFlowerAll()
  273. {
  274. for (int i = 0; i < PlantList.Count; i++)
  275. {
  276. PlantList[i].Retrieve();
  277. i--;
  278. }
  279. Award = false;
  280. }
  281. public static void PlantFlower()
  282. {
  283. SeleSlot.Plant(SeleFlowerInfo, true);
  284. }
  285. public static void PlantFlower(FlowerInfo flowerInfo)
  286. {
  287. if (flowerInfo.Plant)
  288. {
  289. ShowRetrieveCard(flowerInfo);
  290. }
  291. else
  292. {
  293. Slot slot = null;
  294. for (int i = 0; i < SlotList.Count; i++)
  295. {
  296. if (SlotList[i].Available)
  297. {
  298. slot = SlotList[i];
  299. break;
  300. }
  301. }
  302. if (slot == null)
  303. {
  304. ManaDebug.Log("已经没有空地了");
  305. }
  306. else
  307. {
  308. slot.Plant(flowerInfo, true);
  309. }
  310. }
  311. }
  312. public static void PlantFlower(int id, string parName)
  313. {
  314. Slot slot = ManaReso.Get<Slot>(parName);
  315. FlowerInfo flowerInfo = FlowerInfoDic[id];
  316. slot.Plant(flowerInfo, false);
  317. }
  318. }