ManaGarden.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 MiniLock = true;
  82. public static float MinStarTime;
  83. public static float MaxStarTime;
  84. public static float ElfTimer;
  85. public static float StarTimer;
  86. public static float AwardTimer;
  87. public static FlowerInfo SeleInfo;
  88. public static ManaGarden Instance;
  89. public static List<Slot> SlotList = new List<Slot>();
  90. public static List<Slot> PlantList = new List<Slot>();
  91. public static List<Star> StarList = new List<Star>();
  92. public static List<ObjType> ElfList = new List<ObjType>();
  93. public static List<Transform> PageList = new List<Transform>();
  94. public static Dictionary<int, FlowerInfo> FlowerInfoDic = new Dictionary<int, FlowerInfo>();
  95. #endregion
  96. public void FixedUpdate()
  97. {
  98. if (ManaTutorial.TutorialA)
  99. {
  100. return;
  101. }
  102. ElfThread();
  103. StarThread();
  104. AwardThread();
  105. }
  106. public void ElfThread()
  107. {
  108. ElfTimer -= Time.fixedDeltaTime;
  109. if (ElfTimer < 0)
  110. {
  111. ElfTimer = Random.Range(5f, 30f);
  112. if (ElfList.Count > 0 && PlantList.Count > 0)
  113. {
  114. PlantList.Random().Flower.GetElf(ElfList.Random());
  115. }
  116. }
  117. }
  118. public void StarThread()
  119. {
  120. if (MiniLock && !ManaCenter.MiniLock && !ManaVisit.InVisit)
  121. {
  122. StarTimer -= Time.fixedDeltaTime;
  123. if (StarTimer < 0)
  124. {
  125. StarTimer = Mathf.Lerp(MinStarTime, MaxStarTime, Random.Range(0f, 1f));
  126. StarList.Add(ManaReso.GetStar());
  127. }
  128. }
  129. }
  130. public void AwardThread()
  131. {
  132. if (MiniLock)
  133. {
  134. AwardTimer -= Time.fixedDeltaTime;
  135. if (AwardTimer <= 0)
  136. {
  137. AwardTimer = Random.Range(5f, 15);
  138. List<Flower> spareList = new List<Flower>();
  139. for (int i = 0; i < PlantList.Count; i++)
  140. {
  141. if (PlantList[i].Flower.Award == false)
  142. {
  143. spareList.Add(PlantList[i].Flower);
  144. }
  145. }
  146. if (spareList.Count > 0)
  147. {
  148. spareList.Random().Award = true;
  149. }
  150. }
  151. }
  152. }
  153. public override void Instantiate()
  154. {
  155. ManaReso.Get("Garden", Folder.Discard, true, transform, true, ObjType.Garden).AddScript<Garden>();
  156. CreatePage();
  157. CreatePage();
  158. #region 生成FlowerItem
  159. List<XmlAttributeCollection> attributeList = ManaData.GetFlowerConfig();
  160. for (int i = 0; i < attributeList.Count; i++)
  161. {
  162. FlowerInfo flowerInfo = new FlowerInfo(attributeList[i]);
  163. if (flowerInfo.Special)
  164. {
  165. TotalFlowerSpec++;
  166. }
  167. else
  168. {
  169. TotalFlowerRegu++;
  170. }
  171. FlowerInfoDic.Add(flowerInfo.ID_, flowerInfo);
  172. }
  173. #endregion
  174. }
  175. public override void RegistValueA()
  176. {
  177. Instance = this;
  178. ElfTimer = Random.Range(5f, 30f);
  179. AwardTimer = Random.Range(5f, 15f);
  180. UnlockSlot();
  181. #region 读花朵存档
  182. List<int> flowerList = ManaData.GetFlowerList();
  183. for (int i = 0; i < flowerList.Count; i++)
  184. {
  185. FlowerInfoDic[flowerList[i]].Unlock = true;
  186. }
  187. List<KV<int, int>> plantList = ManaData.GetPlantList();
  188. for (int i = 0; i < plantList.Count; i++)
  189. {
  190. PlantFlower(plantList[i].Key, plantList[i].Value);
  191. }
  192. #endregion
  193. }
  194. public static void LockSlot()
  195. {
  196. for (int i = 0; i < SlotList.Count; i++)
  197. {
  198. if (SlotList[i].Lock)
  199. {
  200. Slot--;
  201. SlotList[i].Lock = false;
  202. SlotList[i].Available = false;
  203. return;
  204. }
  205. }
  206. throw new Exception();
  207. }
  208. public static void UnlockSlot()
  209. {
  210. for (int i = 0; i < SlotList.Count; i++)
  211. {
  212. if (SlotList[i].Lock == false)
  213. {
  214. Slot++;
  215. SlotList[i].Lock = true;
  216. SlotList[i].Available = true;
  217. if (Slot%9 == 7)
  218. {
  219. if (Slot/9 + 2 >= Page)
  220. {
  221. CreatePage();
  222. }
  223. }
  224. return;
  225. }
  226. }
  227. }
  228. public static void CreatePage()
  229. {
  230. Transform tra = ManaReso.Get("Page", Folder.Scene, false, ManaReso.Get("GardenPage"), false, ObjType.Page);
  231. float offset = Page*19.2f;
  232. tra.SetLX(offset);
  233. Vector3 pos = ManaReso.Get("GardenPage").position;
  234. pos.x = -offset;
  235. Garden.PagePos.Add(pos);
  236. for (int i = 0; i < 9; i++)
  237. {
  238. Slot slot = tra.GetChild(i).GetComponent<Slot>();
  239. if (slot == null)
  240. {
  241. slot = tra.GetChild(i).AddScript<Slot>();
  242. }
  243. slot.Index = SlotList.Count;
  244. SlotList.Add(slot);
  245. }
  246. PageList.Add(tra);
  247. }
  248. public static void ShowRetrieveCard(FlowerInfo flowerInfo)
  249. {
  250. SeleInfo = flowerInfo;
  251. ManaReso.Get("H_FlowerCard").TweenForCG();
  252. ManaReso.SetText("H_Lab", flowerInfo.Name);
  253. Image image = ManaReso.Get<Image>("H_Icon2");
  254. image.material = null;
  255. image.sprite = flowerInfo.Icon;
  256. image.Resize(true, 0.75f, 0.75f);
  257. ManaReso.SetText("H_BtnLab", Language.GetStr("UI", "H_BtnLab1"));
  258. ManaReso.Get<Image>("H_Btn").material = null;
  259. ManaReso.SetButtonEvent
  260. (
  261. "H_Btn",
  262. () =>
  263. {
  264. ManaAudio.PlayClip(Clip.BtnClip);
  265. ManaReso.Get("H_FlowerCard").TweenBacCG();
  266. RetriveFlower();
  267. }
  268. );
  269. }
  270. public static void ShowUnlockCard(FlowerInfo flowerInfo)
  271. {
  272. SeleInfo = flowerInfo;
  273. ManaReso.Get("H_FlowerCard").TweenForCG();
  274. ManaReso.SetText("H_Lab", flowerInfo.Name);
  275. Image image = ManaReso.Get<Image>("H_Icon2");
  276. image.material = Shortcut.GrayMat;
  277. image.sprite = flowerInfo.Icon;
  278. image.Resize(true, 0.75f, 0.75f);
  279. if (flowerInfo.UnlockCur == Current.Free)
  280. {
  281. ManaReso.SetText("H_BtnLab", Language.GetStr("UI", "H_BtnLab2"));
  282. ManaReso.Get<Image>("H_Btn").material = Shortcut.GrayMat;
  283. ManaReso.SetButtonEvent
  284. (
  285. "H_Btn",
  286. () =>
  287. {
  288. Bubble.Show(null, Language.GetStr("UI", "H_BtnLab3"));
  289. }
  290. );
  291. }
  292. else
  293. {
  294. ManaReso.SetText("H_BtnLab", Auxiliary.ImageParse(flowerInfo.UnlockCur) + flowerInfo.UnlockAmt);
  295. ManaReso.Get<Image>("H_Btn").material = null;
  296. ManaReso.SetButtonEvent
  297. (
  298. "H_Btn",
  299. () =>
  300. {
  301. ManaCenter.Pay
  302. (
  303. "",
  304. flowerInfo.UnlockAmt,
  305. flowerInfo.UnlockCur,
  306. () =>
  307. {
  308. flowerInfo.Unlock = true;
  309. Material material = new Material(ManaReso.Get<Image>("H_Icon2").material);
  310. MaterialUnit materialUnit = new MaterialUnit
  311. (
  312. material,
  313. ManaReso.Get("H_Icon2"),
  314. new List<string>()
  315. {
  316. "_GrayLerp",
  317. }
  318. );
  319. TweenMatFloat tweenMatFloat = materialUnit.CreateTweenMatFloat(1, 0, 1, true, true, Curve.EaseOutQuad, false);
  320. tweenMatFloat.OnForwardStart = () =>
  321. {
  322. materialUnit.Transform.GetComponent<Image>().material = materialUnit.Material;
  323. };
  324. tweenMatFloat.OnForwardFinish = () =>
  325. {
  326. ManaReso.Get<Image>("H_Icon2").material = null;
  327. };
  328. materialUnit.TweenForMatFloat();
  329. ManaAudio.PlayClip(Clip.CurrentClip);
  330. ManaReso.Get("H_Icon1").FindChild("UIFlashLight/UIParticle System").GetComponent<UIPartical>().Begin();
  331. ManaReso.SetText("H_BtnLab", Language.GetStr("UI", "H_BtnLab4"));
  332. ManaReso.SetButtonEvent
  333. (
  334. "H_Btn",
  335. () =>
  336. {
  337. PlantFlower(flowerInfo);
  338. ManaReso.Get("H_FlowerCard").TweenBacCG();
  339. }
  340. );
  341. },
  342. true,
  343. false,
  344. () =>
  345. {
  346. ManaReso.Get("H_FlowerCard").TweenBacCG();
  347. TweenCG tweenCg = ManaReso.Get("G_Flower").TweenBacCG();
  348. tweenCg.AddEventOnetime
  349. (
  350. EventType.BackwardFinish,
  351. () =>
  352. {
  353. ManaReso.Get("F_Manage0").TweenForVec();
  354. }
  355. );
  356. }
  357. );
  358. }
  359. );
  360. }
  361. }
  362. public static void RetriveFlower()
  363. {
  364. SeleInfo.Slot.Retrieve();
  365. }
  366. public static void RetriveFlowerAll()
  367. {
  368. for (int i = 0; i < PlantList.Count; i++)
  369. {
  370. PlantList[i--].Retrieve();
  371. }
  372. }
  373. public static void PlantFlower(int id, int index)
  374. {
  375. Slot slot = SlotList[index];
  376. FlowerInfo flowerInfo = FlowerInfoDic[id];
  377. slot.Plant(flowerInfo, false);
  378. }
  379. public static void PlantFlower(FlowerInfo flowerInfo)
  380. {
  381. Slot slot = null;
  382. for (int i = 0; i < SlotList.Count; i++)
  383. {
  384. if (SlotList[i].Available)
  385. {
  386. slot = SlotList[i];
  387. break;
  388. }
  389. }
  390. if (slot == null)
  391. {
  392. Bubble.Show(null, Language.GetStr("Common", "NoValidSlot"));
  393. }
  394. else
  395. {
  396. slot.Plant(flowerInfo, true);
  397. ManaAudio.PlayClip(Clip.FlowerClip);
  398. }
  399. }
  400. }
  401. #region 已测试内容
  402. //一共有3种花卡 回收卡 不能解锁卡 可以解锁卡
  403. //各种卡之间转换时的按钮变化(事件和Text)
  404. #endregion