ManaGarden.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 = Random.Range(0f, 30f);
  85. public static float AwardTimer = Random.Range(20f, 60f);
  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. SlotList = new List<Slot>()
  180. {
  181. ManaReso.Get("SlotA1").AddScript<Slot>(),
  182. ManaReso.Get("SlotA2").AddScript<Slot>(),
  183. ManaReso.Get("SlotA3").AddScript<Slot>(),
  184. ManaReso.Get("SlotA4").AddScript<Slot>(),
  185. ManaReso.Get("SlotA5").AddScript<Slot>(),
  186. ManaReso.Get("SlotA6").AddScript<Slot>(),
  187. ManaReso.Get("SlotA7").AddScript<Slot>(),
  188. ManaReso.Get("SlotA8").AddScript<Slot>(),
  189. ManaReso.Get("SlotA9").AddScript<Slot>(),
  190. ManaReso.Get("SlotB1").AddScript<Slot>(),
  191. ManaReso.Get("SlotB2").AddScript<Slot>(),
  192. ManaReso.Get("SlotB3").AddScript<Slot>(),
  193. ManaReso.Get("SlotB4").AddScript<Slot>(),
  194. ManaReso.Get("SlotB5").AddScript<Slot>(),
  195. ManaReso.Get("SlotB6").AddScript<Slot>(),
  196. ManaReso.Get("SlotB7").AddScript<Slot>(),
  197. ManaReso.Get("SlotB8").AddScript<Slot>(),
  198. ManaReso.Get("SlotB9").AddScript<Slot>(),
  199. };
  200. #region 读花朵存档
  201. List<int> flowerList = Data.GetFlowerList();
  202. for (int i = 0; i < flowerList.Count; i++)
  203. {
  204. FlowerInfoDic[flowerList[i]].Unlock = true;
  205. }
  206. List<KV<int, string>> plantList = Data.GetPlantList();
  207. for (int i = 0; i < plantList.Count; i++)
  208. {
  209. PlantFlower(plantList[i].Key - 1, plantList[i].Value);
  210. }
  211. #endregion
  212. }
  213. public static void UnlockSlot()
  214. {
  215. for (int i = 0; i < SlotList.Count; i++)
  216. {
  217. if (SlotList[i].Valid == false)
  218. {
  219. ManaData.Slot++;
  220. SlotList[i].Valid = true;
  221. SlotList[i].Available = true;
  222. return;
  223. }
  224. }
  225. ManaDebug.Log("所有土地已解锁");
  226. }
  227. public static void SetFlowerCard(FlowerInfo flowerInfo)
  228. {
  229. SeleFlowerInfo = flowerInfo;
  230. ManaText.Add(ManaReso.Get<Text>("H_Lab"), new LanStr("FlowerName", flowerInfo._Name));
  231. Image image = ManaReso.Get<Image>("H_Icon2");
  232. image.sprite = flowerInfo.Sprite;
  233. Vector2 newSize = flowerInfo.Sprite.rect.size;
  234. newSize.x *= 0.65f;
  235. newSize.y *= 0.65f;
  236. image.rectTransform.sizeDelta = newSize;
  237. }
  238. public static void ShowPlantCard(Slot slot, FlowerInfo flowerInfo)
  239. {
  240. ManaReso.Get("H_FlowerCard").TweenForCG();
  241. ManaReso.SetActive("H_Grid", true);
  242. ManaReso.SetActive("H_Prev", true);
  243. ManaReso.SetActive("H_Next", true);
  244. ManaReso.SetActive("H_Place", true);
  245. ManaReso.SetActive("H_Icon1", false);
  246. ManaReso.SetActive("H_Retrieve", false);
  247. SeleSlot = slot;
  248. SetFlowerCard(flowerInfo);
  249. }
  250. public static void ShowRetrieveCard(FlowerInfo flowerInfo)
  251. {
  252. SeleSlot = flowerInfo.Slot;
  253. SetFlowerCard(flowerInfo);
  254. ManaReso.SetActive("H_Grid", false);
  255. ManaReso.SetActive("H_Prev", false);
  256. ManaReso.SetActive("H_Next", false);
  257. ManaReso.SetActive("H_Place", false);
  258. ManaReso.SetActive("H_Icon1", true);
  259. ManaReso.SetActive("H_Retrieve", true);
  260. ManaReso.Get("H_FlowerCard").TweenForCG();
  261. }
  262. public static void RetriveFlower()
  263. {
  264. SeleSlot.Retrieve();
  265. if (PlantList.Count == 0)
  266. {
  267. Award = false;
  268. }
  269. }
  270. public static void RetriveFlowerAll()
  271. {
  272. for (int i = 0; i < PlantList.Count; i++)
  273. {
  274. PlantList[i].Retrieve();
  275. i--;
  276. }
  277. Award = false;
  278. }
  279. public static void PlantFlower()
  280. {
  281. SeleSlot.Plant(SeleFlowerInfo, true);
  282. }
  283. public static void PlantFlower(FlowerInfo flowerInfo)
  284. {
  285. if (flowerInfo.Plant)
  286. {
  287. ShowRetrieveCard(flowerInfo);
  288. }
  289. else
  290. {
  291. Slot slot = null;
  292. for (int i = 0; i < SlotList.Count; i++)
  293. {
  294. if (SlotList[i].Available)
  295. {
  296. slot = SlotList[i];
  297. break;
  298. }
  299. }
  300. if (slot == null)
  301. {
  302. ManaDebug.Log("已经没有空地了");
  303. }
  304. else
  305. {
  306. slot.Plant(flowerInfo, true);
  307. }
  308. }
  309. }
  310. public static void PlantFlower(int id, string parName)
  311. {
  312. Slot slot = ManaReso.Get<Slot>(parName);
  313. FlowerInfo flowerInfo = FlowerInfoDic[id];
  314. slot.Plant(flowerInfo, false);
  315. }
  316. }