ManaGarden.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 Slot
  15. {
  16. get { return Slot_; }
  17. set
  18. {
  19. Slot_ = value;
  20. ManaReso.SetText("G_CollectLab2", string.Format("{0}/{1}", Slot_, Page * 9));
  21. }
  22. }
  23. public static int Page
  24. {
  25. get { return PageList.Count; }
  26. }
  27. public static int MyFlower
  28. {
  29. get { return MyFlower_; }
  30. set
  31. {
  32. MyFlower_ = value;
  33. ManaAchieve.UpdateStatus(AchieveType.FlowerAmt, MyFlower_);
  34. ManaReso.SetText("F_FlowerLab", string.Format("{0}", MyFlower));
  35. ManaReso.SetText("G_CollectLab1", string.Format("{0}/{1}", MyFlower, TotalFlower));
  36. }
  37. }
  38. public static int MyFlowerSpec
  39. {
  40. get { return MyFlowerSpec_; }
  41. set
  42. {
  43. MyFlowerSpec_ = value;
  44. MyFlower = MyFlowerSpec_ + MyFlowerRegu_;
  45. }
  46. }
  47. public static int MyFlowerRegu
  48. {
  49. get { return MyFlowerRegu_; }
  50. set
  51. {
  52. MyFlowerRegu_ = value;
  53. MyFlower = MyFlowerSpec_ + MyFlowerRegu_;
  54. }
  55. }
  56. public static int TotalFlowerSpec
  57. {
  58. get { return TotalFlowerSpec_; }
  59. set
  60. {
  61. TotalFlowerSpec_ = value;
  62. TotalFlower = TotalFlowerSpec_ + TotalFlowerRegu_;
  63. }
  64. }
  65. public static int TotalFlowerRegu
  66. {
  67. get { return TotalFlowerRegu_; }
  68. set
  69. {
  70. TotalFlowerRegu_ = value;
  71. TotalFlower = TotalFlowerSpec_ + TotalFlowerRegu_;
  72. }
  73. }
  74. public static int Slot_;
  75. private static int MyFlower_;
  76. private static int MyFlowerSpec_;
  77. private static int MyFlowerRegu_;
  78. private static int TotalFlowerSpec_;
  79. private static int TotalFlowerRegu_;
  80. public static int TotalFlower;
  81. public static bool AwardLock = true;
  82. public static float StarTime;
  83. public static float ElfTimer;
  84. public static float StarTimer;
  85. public static float AwardTimer;
  86. public static FlowerInfo SeleInfo;
  87. public static ManaGarden Instance;
  88. public static List<Slot> SlotList = new List<Slot>();
  89. public static List<Slot> PlantList = new List<Slot>();
  90. public static List<Star> StarList = new List<Star>();
  91. public static List<ObjType> ElfList = new List<ObjType>();
  92. public static List<Transform> PageList = new List<Transform>();
  93. public static Dictionary<int, FlowerInfo> FlowerInfoDic = new Dictionary<int, FlowerInfo>();
  94. #endregion
  95. public void FixedUpdate()
  96. {
  97. if (ManaTutorial.TutorialA)
  98. {
  99. return;
  100. }
  101. ElfThread();
  102. StarThread();
  103. AwardThread();
  104. }
  105. public void ElfThread()
  106. {
  107. ElfTimer -= Time.fixedDeltaTime;
  108. if (ElfTimer < 0)
  109. {
  110. ElfTimer = Random.Range(5f, 30f);
  111. if (ElfList.Count > 0 && PlantList.Count > 0)
  112. {
  113. PlantList.Random().Flower.GetElf(ElfList.Random());
  114. }
  115. }
  116. }
  117. public void StarThread()
  118. {
  119. if (!ManaCenter.MiniLock && !ManaVisit.InVisit)
  120. {
  121. StarTimer += Time.fixedDeltaTime;
  122. if (StarTimer >= StarTime)
  123. {
  124. StarTimer = 0;
  125. StarList.Add(ManaReso.GetStar());
  126. }
  127. }
  128. }
  129. public void AwardThread()
  130. {
  131. if (AwardLock)
  132. {
  133. AwardTimer -= Time.fixedDeltaTime;
  134. if (AwardTimer <= 0)
  135. {
  136. AwardTimer = Random.Range(5f, 15);
  137. List<Flower> spareList = new List<Flower>();
  138. for (int i = 0; i < PlantList.Count; i++)
  139. {
  140. if (PlantList[i].Flower.Award == false)
  141. {
  142. spareList.Add(PlantList[i].Flower);
  143. }
  144. }
  145. if (spareList.Count > 0)
  146. {
  147. spareList.Random().Award = true;
  148. }
  149. }
  150. }
  151. }
  152. public static void Reload()
  153. {
  154. for (int i = 0; i < PageList.Count; i++)
  155. {
  156. ManaReso.Save(PageList[i]);
  157. }
  158. for (int i = 0; i < PlantList.Count; i++)
  159. {
  160. PlantList[i].Retrieve();
  161. }
  162. for (int i = 0; i < SlotList.Count; i++)
  163. {
  164. SlotList[i].Lock = false;
  165. SlotList[i].Available = false;
  166. DestroyImmediate(SlotList[i]);
  167. }
  168. foreach (var kv in FlowerInfoDic)
  169. {
  170. kv.Value.Unlock = false;
  171. }
  172. Slot = 0;
  173. MyFlowerSpec = 0;
  174. MyFlowerRegu = 0;
  175. ElfList = new List<ObjType>();
  176. SlotList = new List<Slot>();
  177. PageList = new List<Transform>();
  178. PlantList = new List<Slot>();
  179. CreatePage();
  180. CreatePage();
  181. Instance.RegistValueA();
  182. }
  183. public override void Instantiate()
  184. {
  185. ManaReso.Get("Garden", Folder.Discard, true, transform, true, ObjType.Garden).AddScript<Garden>();
  186. CreatePage();
  187. CreatePage();
  188. #region 生成FlowerItem
  189. List<XmlAttributeCollection> attributeList = ManaData.GetFlowerConfig();
  190. for (int i = 0; i < attributeList.Count; i++)
  191. {
  192. FlowerInfo flowerInfo = new FlowerInfo(attributeList[i]);
  193. if (flowerInfo.Special)
  194. {
  195. TotalFlowerSpec++;
  196. }
  197. else
  198. {
  199. TotalFlowerRegu++;
  200. }
  201. FlowerInfoDic.Add(flowerInfo.ID_, flowerInfo);
  202. }
  203. #endregion
  204. }
  205. public override void RegistValueA()
  206. {
  207. Instance = this;
  208. ElfTimer = Random.Range(5f, 30f);
  209. AwardTimer = Random.Range(5f, 15f);
  210. UnlockSlot();
  211. #region 读花朵存档
  212. List<int> flowerList = ManaData.GetFlowerList();
  213. for (int i = 0; i < flowerList.Count; i++)
  214. {
  215. FlowerInfoDic[flowerList[i]].Unlock = true;
  216. }
  217. List<KV<int, int>> plantList = ManaData.GetPlantList();
  218. for (int i = 0; i < plantList.Count; i++)
  219. {
  220. PlantFlower(plantList[i].Key, plantList[i].Value);
  221. }
  222. #endregion
  223. }
  224. public static void LockSlot()
  225. {
  226. for (int i = 0; i < SlotList.Count; i++)
  227. {
  228. if (SlotList[i].Lock)
  229. {
  230. Slot--;
  231. SlotList[i].Lock = false;
  232. SlotList[i].Available = false;
  233. return;
  234. }
  235. }
  236. throw new Exception();
  237. }
  238. public static void UnlockSlot()
  239. {
  240. for (int i = 0; i < SlotList.Count; i++)
  241. {
  242. if (SlotList[i].Lock == false)
  243. {
  244. Slot++;
  245. SlotList[i].Lock = true;
  246. SlotList[i].Available = true;
  247. if (Slot%9 == 7)
  248. {
  249. if (Slot/9 + 2 >= Page)
  250. {
  251. CreatePage();
  252. }
  253. }
  254. return;
  255. }
  256. }
  257. }
  258. public static void CreatePage()
  259. {
  260. Transform tra = ManaReso.Get("Page", Folder.Scene, false, ManaReso.Get("GardenPage"), false, ObjType.Page);
  261. float offset = Page*19.2f;
  262. tra.SetLX(offset);
  263. Vector3 pos = ManaReso.Get("GardenPage").position;
  264. pos.x = -offset;
  265. Garden.PagePos.Add(pos);
  266. for (int i = 0; i < 9; i++)
  267. {
  268. Slot slot = tra.GetChild(i).GetComponent<Slot>();
  269. if (slot == null)
  270. {
  271. slot = tra.GetChild(i).AddScript<Slot>();
  272. }
  273. slot.Index = SlotList.Count;
  274. SlotList.Add(slot);
  275. }
  276. PageList.Add(tra);
  277. }
  278. public static void ShowFlowerCard(FlowerInfo flowerInfo)
  279. {
  280. SeleInfo = flowerInfo;
  281. ManaReso.Get("H_FlowerCard").TweenForCG();
  282. ManaReso.SetText("H_Lab", flowerInfo.Name);
  283. Image image = ManaReso.Get<Image>("H_Icon2");
  284. image.sprite = flowerInfo.Icon;
  285. image.Resize(0.65f, 0.65f);
  286. }
  287. public static void RetriveFlower()
  288. {
  289. SeleInfo.Slot.Retrieve();
  290. }
  291. public static void RetriveFlowerAll()
  292. {
  293. for (int i = 0; i < PlantList.Count; i++)
  294. {
  295. PlantList[i--].Retrieve();
  296. }
  297. }
  298. public static void PlantFlower(int id, int index)
  299. {
  300. Slot slot = SlotList[index];
  301. FlowerInfo flowerInfo = FlowerInfoDic[id];
  302. slot.Plant(flowerInfo, false);
  303. }
  304. public static void PlantFlower(FlowerInfo flowerInfo)
  305. {
  306. if (flowerInfo.Plant)
  307. {
  308. ManaAudio.PlayClip(Clip.BtnClip);
  309. ShowFlowerCard(flowerInfo);
  310. }
  311. else
  312. {
  313. Slot slot = null;
  314. for (int i = 0; i < SlotList.Count; i++)
  315. {
  316. if (SlotList[i].Available)
  317. {
  318. slot = SlotList[i];
  319. break;
  320. }
  321. }
  322. if (slot == null)
  323. {
  324. Bubble.Show(null, Language.GetStr("Common", "NoValidSlot"));
  325. }
  326. else
  327. {
  328. slot.Plant(flowerInfo, true);
  329. ManaAudio.PlayClip(Clip.FlowerClip);
  330. }
  331. }
  332. }
  333. }