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 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(5f, 30f);
  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(5f, 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 < PlantList.Count; i++)
  144. {
  145. PlantList[i].Retrieve();
  146. }
  147. for (int i = 0; i < SlotList.Count; i++)
  148. {
  149. SlotList[i].Valid = false;
  150. SlotList[i].Available = false;
  151. DestroyImmediate(SlotList[i]);
  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 = ManaData.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(5f, 30f);
  194. AwardTimer = Random.Range(5f, 15f);
  195. UnlockSlot();
  196. #region 读花朵存档
  197. List<int> flowerList = ManaData.GetFlowerList();
  198. for (int i = 0; i < flowerList.Count; i++)
  199. {
  200. FlowerInfoDic[flowerList[i]].Unlock = true;
  201. }
  202. List<KV<int, int>> plantList = ManaData.GetPlantList();
  203. for (int i = 0; i < plantList.Count; i++)
  204. {
  205. PlantFlower(plantList[i].Key, plantList[i].Value);
  206. }
  207. #endregion
  208. }
  209. public static void LockSlot()
  210. {
  211. for (int i = 0; i < SlotList.Count; i++)
  212. {
  213. if (SlotList[i].Valid)
  214. {
  215. SlotAmt--;
  216. SlotList[i].Valid = false;
  217. SlotList[i].Available = false;
  218. return;
  219. }
  220. }
  221. throw new Exception();
  222. }
  223. public static void UnlockSlot()
  224. {
  225. for (int i = 0; i < SlotList.Count; i++)
  226. {
  227. if (SlotList[i].Valid == false)
  228. {
  229. SlotAmt++;
  230. SlotList[i].Valid = true;
  231. SlotList[i].Available = true;
  232. if (SlotAmt%9 == 7)
  233. {
  234. if (SlotAmt/9 + 2 >= Page)
  235. {
  236. CreatePage();
  237. }
  238. }
  239. return;
  240. }
  241. }
  242. ManaDebug.Log("所有土地已解锁");
  243. }
  244. public static void CreatePage()
  245. {
  246. Transform tra = ManaReso.Get("Page", Folder.Scene, false, ManaReso.Get("GardenPage"), false, ObjType.Page);
  247. float offset = Page*19.2f;
  248. tra.SetLX(offset);
  249. Vector3 pos = ManaReso.Get("GardenPage").position;
  250. pos.x = -offset;
  251. Garden.PagePos.Add(pos);
  252. for (int i = 0; i < 9; i++)
  253. {
  254. Slot slot = tra.GetChild(i).GetComponent<Slot>();
  255. if (slot == null)
  256. {
  257. slot = tra.GetChild(i).AddScript<Slot>();
  258. }
  259. slot.Index = SlotList.Count;
  260. SlotList.Add(slot);
  261. }
  262. PageList.Add(tra);
  263. }
  264. public static void ShowFlowerCard(FlowerInfo flowerInfo)
  265. {
  266. SeleInfo = flowerInfo;
  267. ManaReso.Get("H_FlowerCard").TweenForCG();
  268. ManaLan.Add(ManaReso.Get<Text>("H_Lab"), new LanStr("FlowerName", flowerInfo.ID));
  269. Image image = ManaReso.Get<Image>("H_Icon2");
  270. image.sprite = flowerInfo.Icon;
  271. image.Resize(0.65f, 0.65f);
  272. }
  273. public static void RetriveFlower()
  274. {
  275. SeleInfo.Slot.Retrieve();
  276. }
  277. public static void RetriveFlowerAll()
  278. {
  279. for (int i = 0; i < PlantList.Count; i++)
  280. {
  281. PlantList[i--].Retrieve();
  282. }
  283. }
  284. public static void PlantFlower(int id, int index)
  285. {
  286. Slot slot = SlotList[index];
  287. FlowerInfo flowerInfo = FlowerInfoDic[id];
  288. slot.Plant(flowerInfo, false);
  289. }
  290. public static void PlantFlower(FlowerInfo flowerInfo)
  291. {
  292. if (flowerInfo.Plant)
  293. {
  294. ManaAudio.PlayClip(Clip.BtnClip);
  295. ShowFlowerCard(flowerInfo);
  296. }
  297. else
  298. {
  299. Slot slot = null;
  300. for (int i = 0; i < SlotList.Count; i++)
  301. {
  302. if (SlotList[i].Available)
  303. {
  304. slot = SlotList[i];
  305. break;
  306. }
  307. }
  308. if (slot == null)
  309. {
  310. Bubble.Show(null, Language.GetStr("Common", "NoValidSlot"));
  311. }
  312. else
  313. {
  314. slot.Plant(flowerInfo, true);
  315. ManaAudio.PlayClip(Clip.FlowerClip);
  316. }
  317. }
  318. }
  319. }