ManaGarden.cs 8.7 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 Page
  77. {
  78. get { return PageList.Count; }
  79. }
  80. public static int SlotAmt;
  81. public static int TotalFlower;
  82. public static bool AwardValid = true;
  83. public static float ElfTimer;
  84. public static float AwardTimer;
  85. public static FlowerInfo SeleInfo;
  86. public static ManaGarden Instance;
  87. public static List<Slot> SlotList = new List<Slot>();
  88. public static List<Slot> PlantList = new List<Slot>();
  89. public static List<ObjType> ElfList = new List<ObjType>();
  90. public static List<Transform> PageList = new List<Transform>();
  91. public static Dictionary<int, FlowerInfo> FlowerInfoDic = new Dictionary<int, FlowerInfo>();
  92. #endregion
  93. public void FixedUpdate()
  94. {
  95. if (ManaTutorial.TutorialA)
  96. {
  97. return;
  98. }
  99. ElfThread();
  100. AwardThread();
  101. }
  102. public void ElfThread()
  103. {
  104. ElfTimer -= Time.fixedDeltaTime;
  105. if (ElfTimer < 0)
  106. {
  107. ElfTimer = Random.Range(0f, 3f);
  108. if (ElfList.Count > 0 && PlantList.Count > 0)
  109. {
  110. PlantList.Random().Flower.GetElf(ElfList.Random());
  111. }
  112. }
  113. }
  114. public void AwardThread()
  115. {
  116. if (AwardValid)
  117. {
  118. AwardTimer -= Time.fixedDeltaTime;
  119. if (AwardTimer <= 0)
  120. {
  121. AwardTimer = Random.Range(5, 15);
  122. List<Flower> spareList = new List<Flower>();
  123. for (int i = 0; i < PlantList.Count; i++)
  124. {
  125. if (PlantList[i].Flower.Award == false)
  126. {
  127. spareList.Add(PlantList[i].Flower);
  128. }
  129. }
  130. if (spareList.Count > 0)
  131. {
  132. spareList.Random().Award = true;
  133. }
  134. }
  135. }
  136. }
  137. public static void Reload()
  138. {
  139. for (int i = 0; i < PageList.Count; i++)
  140. {
  141. ManaReso.Save(PageList[i]);
  142. }
  143. for (int i = 0; i < SlotList.Count; i++)
  144. {
  145. SlotList[i].Valid = false;
  146. SlotList[i].Available = false;
  147. Destroy(SlotList[i]);
  148. }
  149. for (int i = 0; i < PlantList.Count; i++)
  150. {
  151. PlantList[i].Retrieve();
  152. }
  153. foreach (var kv in FlowerInfoDic)
  154. {
  155. kv.Value.Unlock = false;
  156. }
  157. SlotAmt = 0;
  158. MyFlowerSpec = 0;
  159. MyFlowerRegu = 0;
  160. ElfList = new List<ObjType>();
  161. SlotList = new List<Slot>();
  162. PageList = new List<Transform>();
  163. PlantList = new List<Slot>();
  164. CreatePage();
  165. CreatePage();
  166. Instance.RegistValueA();
  167. }
  168. public override void Instantiate()
  169. {
  170. ManaReso.Get("Garden", Folder.Scene, true, transform, true, ObjType.Garden).AddScript<Garden>();
  171. CreatePage();
  172. CreatePage();
  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, 3f);
  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 = Page*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).GetComponent<Slot>();
  241. if (slot == null)
  242. {
  243. slot = tra.GetChild(i).AddScript<Slot>();
  244. }
  245. slot.Index = SlotList.Count;
  246. SlotList.Add(slot);
  247. }
  248. PageList.Add(tra);
  249. }
  250. public static void ShowFlowerCard(FlowerInfo flowerInfo)
  251. {
  252. SeleInfo = flowerInfo;
  253. ManaReso.Get("H_FlowerCard").TweenForCG();
  254. ManaLan.Add(ManaReso.Get<Text>("H_Lab"), new LanStr("FlowerName", flowerInfo.ID));
  255. Image image = ManaReso.Get<Image>("H_Icon2");
  256. image.sprite = flowerInfo.Icon;
  257. image.Resize(0.65f, 0.65f);
  258. }
  259. public static void RetriveFlower()
  260. {
  261. SeleInfo.Slot.Retrieve();
  262. }
  263. public static void RetriveFlowerAll()
  264. {
  265. for (int i = 0; i < PlantList.Count; i++)
  266. {
  267. PlantList[i--].Retrieve();
  268. }
  269. }
  270. public static void PlantFlower(int id, int index)
  271. {
  272. Slot slot = SlotList[index];
  273. FlowerInfo flowerInfo = FlowerInfoDic[id];
  274. slot.Plant(flowerInfo, false);
  275. }
  276. public static void PlantFlower(FlowerInfo flowerInfo)
  277. {
  278. if (flowerInfo.Plant)
  279. {
  280. ManaAudio.PlayClip(Clip.BtnClip);
  281. ShowFlowerCard(flowerInfo);
  282. }
  283. else
  284. {
  285. Slot slot = null;
  286. for (int i = 0; i < SlotList.Count; i++)
  287. {
  288. if (SlotList[i].Available)
  289. {
  290. slot = SlotList[i];
  291. break;
  292. }
  293. }
  294. if (slot == null)
  295. {
  296. Bubble.Show(null, Language.GetStr("Common", "NoValidSlot"));
  297. }
  298. else
  299. {
  300. slot.Plant(flowerInfo, true);
  301. ManaAudio.PlayClip(Clip.FlowerClip);
  302. }
  303. }
  304. }
  305. }