ManaGarden.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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(20, 60);
  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 < PlantList.Count; i++)
  141. {
  142. PlantList[i].Retrieve();
  143. }
  144. foreach (var kv in FlowerInfoDic)
  145. {
  146. kv.Value.Unlock = false;
  147. }
  148. SlotAmt = 0;
  149. PageAmt = 0;
  150. MyFlowerSpec = 0;
  151. MyFlowerRegu = 0;
  152. ElfList = new List<ObjType>();
  153. SlotList = new List<Slot>();
  154. PageList = new List<Transform>();
  155. PlantList = new List<Slot>();
  156. CreatePage();
  157. CreatePage();
  158. Instance.RegistValueA();
  159. }
  160. public override void Instantiate()
  161. {
  162. ManaReso.Get<ObjRoot>("Garden", Folder.Scene, true, transform, true, ObjType.Garden).AddScript<Garden>();
  163. CreatePage();
  164. CreatePage();
  165. #region 生成FlowerItem
  166. List<XmlAttributeCollection> attributeList = Data.GetFlowerConfig();
  167. for (int i = 0; i < attributeList.Count; i++)
  168. {
  169. FlowerInfo flowerInfo = new FlowerInfo(attributeList[i]);
  170. if (flowerInfo.Special)
  171. {
  172. TotalFlowerSpec++;
  173. }
  174. else
  175. {
  176. TotalFlowerRegu++;
  177. }
  178. FlowerInfoDic.Add(flowerInfo.ID_, flowerInfo);
  179. }
  180. #endregion
  181. }
  182. public override void RegistValueA()
  183. {
  184. Instance = this;
  185. ElfTimer = Random.Range(0f, 30f);
  186. AwardTimer = Random.Range(20f, 60f);
  187. #region 读花朵存档
  188. for (int i = 0; i < Data.GetPlayerInt("Slot"); i++)
  189. {
  190. UnlockSlot();
  191. }
  192. List<int> flowerList = Data.GetFlowerList();
  193. for (int i = 0; i < flowerList.Count; i++)
  194. {
  195. FlowerInfoDic[flowerList[i]].Unlock = true;
  196. }
  197. List<KV<int, int>> plantList = Data.GetPlantList();
  198. for (int i = 0; i < plantList.Count; i++)
  199. {
  200. PlantFlower(plantList[i].Key, plantList[i].Value);
  201. }
  202. #endregion
  203. }
  204. public static void UnlockSlot()
  205. {
  206. for (int i = 0; i < SlotList.Count; i++)
  207. {
  208. if (SlotList[i].Valid == false)
  209. {
  210. SlotAmt++;
  211. SlotList[i].Valid = true;
  212. SlotList[i].Available = true;
  213. if (Mathf.Clamp(SlotAmt - 9, 0, 999)%9 == 7)
  214. {
  215. CreatePage();
  216. }
  217. return;
  218. }
  219. }
  220. ManaDebug.Log("所有土地已解锁");
  221. }
  222. public static void CreatePage()
  223. {
  224. Transform tra = ManaReso.Get<ObjRoot>("Page", Folder.Scene, false, ManaReso.Get("GardenPage"), false, ObjType.Page);
  225. float offset = PageAmt*19.2f;
  226. tra.SetLX(offset);
  227. Vector3 pos = ManaReso.Get("GardenPage").position;
  228. pos.x = -offset;
  229. Garden.PagePos.Add(pos);
  230. for (int i = 0; i < 9; i++)
  231. {
  232. Slot slot = tra.GetChild(i).AddScript<Slot>();
  233. slot.Index = SlotList.Count;
  234. SlotList.Add(slot);
  235. }
  236. PageList.Add(tra);
  237. PageAmt++;
  238. Garden.TotPage++;
  239. }
  240. public static void ShowFlowerCard(FlowerInfo flowerInfo)
  241. {
  242. SeleInfo = flowerInfo;
  243. ManaReso.Get("H_FlowerCard").TweenForCG();
  244. ManaLan.Add(ManaReso.Get<Text>("H_Lab"), new LanStr("FlowerName", flowerInfo.ID));
  245. Image image = ManaReso.Get<Image>("H_Icon2");
  246. image.sprite = flowerInfo.Icon;
  247. image.Resize(0.65f, 0.65f);
  248. }
  249. public static void RetriveFlower()
  250. {
  251. SeleInfo.Slot.Retrieve();
  252. }
  253. public static void RetriveFlowerAll()
  254. {
  255. for (int i = 0; i < PlantList.Count; i++)
  256. {
  257. PlantList[i--].Retrieve();
  258. }
  259. }
  260. public static void PlantFlower(int id, int index)
  261. {
  262. Slot slot = SlotList[index];
  263. FlowerInfo flowerInfo = FlowerInfoDic[id];
  264. slot.Plant(flowerInfo, false);
  265. }
  266. public static void PlantFlower(FlowerInfo flowerInfo)
  267. {
  268. if (flowerInfo.Plant)
  269. {
  270. ShowFlowerCard(flowerInfo);
  271. }
  272. else
  273. {
  274. Slot slot = null;
  275. for (int i = 0; i < SlotList.Count; i++)
  276. {
  277. if (SlotList[i].Available)
  278. {
  279. slot = SlotList[i];
  280. break;
  281. }
  282. }
  283. if (slot == null)
  284. {
  285. ManaDebug.Log("已经没有空地了");
  286. }
  287. else
  288. {
  289. slot.Plant(flowerInfo, true);
  290. }
  291. }
  292. }
  293. }