ManaGarden.cs 9.6 KB

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