ManaGarden.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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.FlowerAmt, 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 TotalFlowerSpec
  54. {
  55. get { return TotalFlowerSpec_; }
  56. set
  57. {
  58. TotalFlowerSpec_ = value;
  59. TotalFlower = TotalFlowerSpec_ + TotalFlowerRegu_;
  60. }
  61. }
  62. public static int TotalFlowerRegu
  63. {
  64. get { return TotalFlowerRegu_; }
  65. set
  66. {
  67. TotalFlowerRegu_ = value;
  68. TotalFlower = TotalFlowerSpec_ + TotalFlowerRegu_;
  69. }
  70. }
  71. private static int MyFlower_;
  72. private static int MyFlowerSpec_;
  73. private static int MyFlowerRegu_;
  74. private static int TotalFlowerSpec_;
  75. private static int TotalFlowerRegu_;
  76. public static int SlotAmt;
  77. public static int PageAmt;
  78. public static int TotalFlower;
  79. public static bool AwardValid = true;
  80. public static float ElfTimer;
  81. public static float AwardTimer;
  82. public static FlowerInfo SeleInfo;
  83. public static ManaGarden Instance;
  84. public static List<Slot> SlotList = new List<Slot>();
  85. public static List<Slot> PlantList = new List<Slot>();
  86. public static List<ObjType> ElfList = new List<ObjType>();
  87. public static List<Transform> PageList = new List<Transform>();
  88. public static Dictionary<int, FlowerInfo> FlowerInfoDic = new Dictionary<int, FlowerInfo>();
  89. #endregion
  90. public void FixedUpdate()
  91. {
  92. if (ManaTutorial.TutorialA)
  93. {
  94. return;
  95. }
  96. ElfThread();
  97. AwardThread();
  98. }
  99. public void ElfThread()
  100. {
  101. ElfTimer -= Time.fixedDeltaTime;
  102. if (ElfTimer < 0)
  103. {
  104. ElfTimer = Random.Range(0f, 30f);
  105. if (ElfList.Count > 0 && PlantList.Count > 0)
  106. {
  107. PlantList.Random().Flower.PlayAnim(ElfList.Random());
  108. }
  109. }
  110. }
  111. public void AwardThread()
  112. {
  113. if (AwardValid)
  114. {
  115. AwardTimer -= Time.fixedDeltaTime;
  116. if (AwardTimer <= 0)
  117. {
  118. AwardTimer = Random.Range(5, 15);
  119. List<Flower> spareList = new List<Flower>();
  120. for (int i = 0; i < PlantList.Count; i++)
  121. {
  122. if (PlantList[i].Flower.Award == false)
  123. {
  124. spareList.Add(PlantList[i].Flower);
  125. }
  126. }
  127. if (spareList.Count > 0)
  128. {
  129. spareList.Random().Award = true;
  130. }
  131. }
  132. }
  133. }
  134. public static void Reload()
  135. {
  136. for (int i = 0; i < PageList.Count; i++)
  137. {
  138. ManaReso.Save(PageList[i]);
  139. }
  140. for (int i = 0; i < SlotList.Count; i++)
  141. {
  142. SlotList[i].Valid = false;
  143. SlotList[i].Available = false;
  144. Destroy(SlotList[i]);
  145. }
  146. for (int i = 0; i < PlantList.Count; i++)
  147. {
  148. PlantList[i].Retrieve();
  149. }
  150. foreach (var kv in FlowerInfoDic)
  151. {
  152. kv.Value.Unlock = false;
  153. }
  154. SlotAmt = 0;
  155. PageAmt = 0;
  156. MyFlowerSpec = 0;
  157. MyFlowerRegu = 0;
  158. ElfList = new List<ObjType>();
  159. SlotList = new List<Slot>();
  160. PageList = new List<Transform>();
  161. PlantList = new List<Slot>();
  162. CreatePage();
  163. CreatePage();
  164. Garden.TotPage = 1;
  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. Garden.TotPage=1;
  173. #region 生成FlowerItem
  174. List<XmlAttributeCollection> attributeList = Data.GetFlowerConfig();
  175. for (int i = 0; i < attributeList.Count; i++)
  176. {
  177. FlowerInfo flowerInfo = new FlowerInfo(attributeList[i]);
  178. if (flowerInfo.Special)
  179. {
  180. TotalFlowerSpec++;
  181. }
  182. else
  183. {
  184. TotalFlowerRegu++;
  185. }
  186. FlowerInfoDic.Add(flowerInfo.ID_, flowerInfo);
  187. }
  188. #endregion
  189. }
  190. public override void RegistValueA()
  191. {
  192. Instance = this;
  193. ElfTimer = Random.Range(0f, 30f);
  194. AwardTimer = Random.Range(5, 15);
  195. #region 读花朵存档
  196. for (int i = 0; i < Data.GetPlayerInt("Slot"); i++)
  197. {
  198. UnlockSlot();
  199. }
  200. List<int> flowerList = Data.GetFlowerList();
  201. for (int i = 0; i < flowerList.Count; i++)
  202. {
  203. FlowerInfoDic[flowerList[i]].Unlock = true;
  204. }
  205. List<KV<int, int>> plantList = Data.GetPlantList();
  206. for (int i = 0; i < plantList.Count; i++)
  207. {
  208. PlantFlower(plantList[i].Key, plantList[i].Value);
  209. }
  210. #endregion
  211. }
  212. public static void UnlockSlot()
  213. {
  214. for (int i = 0; i < SlotList.Count; i++)
  215. {
  216. if (SlotList[i].Valid == false)
  217. {
  218. SlotAmt++;
  219. SlotList[i].Valid = true;
  220. SlotList[i].Available = true;
  221. if (SlotAmt%9 == 7)
  222. {
  223. CreatePage();
  224. }
  225. return;
  226. }
  227. }
  228. ManaDebug.Log("所有土地已解锁");
  229. }
  230. public static void CreatePage()
  231. {
  232. Transform tra = ManaReso.Get("Page", Folder.Scene, false, ManaReso.Get("GardenPage"), false, ObjType.Page);
  233. float offset = PageAmt*19.2f;
  234. tra.SetLX(offset);
  235. Vector3 pos = ManaReso.Get("GardenPage").position;
  236. pos.x = -offset;
  237. Garden.PagePos.Add(pos);
  238. for (int i = 0; i < 9; i++)
  239. {
  240. Slot slot = tra.GetChild(i).AddScript<Slot>();
  241. slot.Index = SlotList.Count;
  242. SlotList.Add(slot);
  243. }
  244. PageList.Add(tra);
  245. PageAmt++;
  246. Garden.TotPage++;
  247. }
  248. public static void ShowFlowerCard(FlowerInfo flowerInfo)
  249. {
  250. SeleInfo = flowerInfo;
  251. ManaReso.Get("H_FlowerCard").TweenForCG();
  252. ManaLan.Add(ManaReso.Get<Text>("H_Lab"), new LanStr("FlowerName", flowerInfo.ID));
  253. Image image = ManaReso.Get<Image>("H_Icon2");
  254. image.sprite = flowerInfo.Icon;
  255. image.Resize(0.65f, 0.65f);
  256. }
  257. public static void RetriveFlower()
  258. {
  259. SeleInfo.Slot.Retrieve();
  260. }
  261. public static void RetriveFlowerAll()
  262. {
  263. for (int i = 0; i < PlantList.Count; i++)
  264. {
  265. PlantList[i--].Retrieve();
  266. }
  267. }
  268. public static void PlantFlower(int id, int index)
  269. {
  270. Slot slot = SlotList[index];
  271. FlowerInfo flowerInfo = FlowerInfoDic[id];
  272. slot.Plant(flowerInfo, false);
  273. }
  274. public static void PlantFlower(FlowerInfo flowerInfo)
  275. {
  276. if (flowerInfo.Plant)
  277. {
  278. ManaAudio.PlayClip(Clip.BtnClip);
  279. ShowFlowerCard(flowerInfo);
  280. }
  281. else
  282. {
  283. Slot slot = null;
  284. for (int i = 0; i < SlotList.Count; i++)
  285. {
  286. if (SlotList[i].Available)
  287. {
  288. slot = SlotList[i];
  289. break;
  290. }
  291. }
  292. if (slot == null)
  293. {
  294. Bubble.Show(null, Language.GetStr("Common", "NoValidSlot"));
  295. }
  296. else
  297. {
  298. slot.Plant(flowerInfo, true);
  299. ManaAudio.PlayClip(Clip.FlowerClip);
  300. }
  301. }
  302. }
  303. }