ManaGarden.cs 9.1 KB

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