ManaGarden.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 enum ElfType
  12. {
  13. Bee_Red,
  14. Bee_Blue,
  15. Bee_White,
  16. Bee_Purple,
  17. Bee_Yellow,
  18. Beetle_Red,
  19. Beetle_Blue,
  20. Beetle_White,
  21. Beetle_Purple,
  22. Beetle_Yellow,
  23. Butterfly_Red,
  24. Butterfly_Blue,
  25. Butterfly_White,
  26. Butterfly_Purple,
  27. Butterfly_Yellow,
  28. Dragonfly_Red,
  29. Dragonfly_Blue,
  30. Dragonfly_White,
  31. Dragonfly_Purple,
  32. Dragonfly_Yellow,
  33. }
  34. public class ManaGarden : Regist
  35. {
  36. #region 变量
  37. public static int Slot
  38. {
  39. get { return Slot_; }
  40. set
  41. {
  42. Slot_ = value;
  43. ManaReso.SetText("G_CollectLab2", string.Format("{0}/{1}", Slot_, Page * 9));
  44. }
  45. }
  46. public static int Page
  47. {
  48. get { return PageList.Count; }
  49. }
  50. public static int MyFlower
  51. {
  52. get { return MyFlower_; }
  53. set
  54. {
  55. MyFlower_ = value;
  56. ManaAchieve.UpdateStatus(AchieveType.FlowerAmt, MyFlower_);
  57. ManaReso.SetText("F_FlowerLab", string.Format("{0}", MyFlower));
  58. ManaReso.SetText("G_CollectLab1", string.Format("{0}/{1}", MyFlower, TotalFlower));
  59. if (MyFlower_ >= 2 && ManaCenter.Level >= 22)
  60. {
  61. if (ManaTutorial.TutorialE_)
  62. {
  63. ManaTutorial.EnterE1();
  64. }
  65. }
  66. }
  67. }
  68. public static int MyFlowerSpec
  69. {
  70. get { return MyFlowerSpec_; }
  71. set
  72. {
  73. MyFlowerSpec_ = value;
  74. MyFlower = MyFlowerSpec_ + MyFlowerRegu_;
  75. }
  76. }
  77. public static int MyFlowerRegu
  78. {
  79. get { return MyFlowerRegu_; }
  80. set
  81. {
  82. MyFlowerRegu_ = value;
  83. MyFlower = MyFlowerSpec_ + MyFlowerRegu_;
  84. }
  85. }
  86. public static int TotalFlowerSpec
  87. {
  88. get { return TotalFlowerSpec_; }
  89. set
  90. {
  91. TotalFlowerSpec_ = value;
  92. TotalFlower = TotalFlowerSpec_ + TotalFlowerRegu_;
  93. }
  94. }
  95. public static int TotalFlowerRegu
  96. {
  97. get { return TotalFlowerRegu_; }
  98. set
  99. {
  100. TotalFlowerRegu_ = value;
  101. TotalFlower = TotalFlowerSpec_ + TotalFlowerRegu_;
  102. }
  103. }
  104. public static int Slot_;
  105. private static int MyFlower_;
  106. private static int MyFlowerSpec_;
  107. private static int MyFlowerRegu_;
  108. private static int TotalFlowerSpec_;
  109. private static int TotalFlowerRegu_;
  110. public static int FirstUnlockFlowerID
  111. {
  112. get
  113. {
  114. foreach (var kv in FlowerInfoDic)
  115. {
  116. if (!kv.Value.Unlock)
  117. {
  118. return kv.Key;
  119. }
  120. }
  121. return -1;
  122. }
  123. }
  124. public static List<int> UnlockedFlowerIDList
  125. {
  126. get
  127. {
  128. List<int> unlockedFlowerIDList = new List<int>();
  129. foreach (var kv in FlowerInfoDic)
  130. {
  131. if (kv.Value.Unlock)
  132. {
  133. unlockedFlowerIDList.Add(kv.Key);
  134. }
  135. }
  136. return unlockedFlowerIDList;
  137. }
  138. }
  139. public static int TotalFlower;
  140. public static bool MiniLock = true;
  141. public static float MinStarTime;
  142. public static float MaxStarTime;
  143. public static float ElfTimer;
  144. public static float StarTimer;
  145. public static float AwardTimer;
  146. public static Button RetrieveButton;
  147. public static Text RetrieveButtonLab;
  148. public static Button PurchaseButton;
  149. public static Text PurchaseButtonLab;
  150. public static FlowerInfo SeleInfo;
  151. public static ManaGarden Instance;
  152. public static List<Slot> SlotList = new List<Slot>();
  153. public static List<Slot> PlantList = new List<Slot>();
  154. public static List<Star> StarList = new List<Star>();
  155. public static List<ElfType> ElfList = new List<ElfType>();
  156. public static List<Transform> PageList = new List<Transform>();
  157. public static Dictionary<int, FlowerInfo> FlowerInfoDic = new Dictionary<int, FlowerInfo>();
  158. public static TweenMatFloat FlowerCardMatTween;
  159. #endregion
  160. public void Update()
  161. {
  162. if (ManaTutorial.TutorialA || Player.InDressRoom || GardenSmartFoxManager.GardenSmartFox.SFSPlazaRoomManager.InPlazaRoom)
  163. {
  164. return;
  165. }
  166. ElfThread();
  167. StarThread();
  168. AwardThread();
  169. }
  170. public void ElfThread()
  171. {
  172. if (!MiniLock)
  173. {
  174. return;
  175. }
  176. ElfTimer -= Time.deltaTime;
  177. if (ElfTimer < 0)
  178. {
  179. ElfTimer = Random.Range(5f, 30f);
  180. if (ElfList.Count > 0 && PlantList.Count > 0)
  181. {
  182. PlantList.Random()[0].Flower.GetElf(ElfList.Random()[0]);
  183. }
  184. }
  185. }
  186. public void StarThread()
  187. {
  188. if (MiniLock && !ManaCenter.MiniLock && !ManaVisit.InVisit)
  189. {
  190. StarTimer -= Time.deltaTime;
  191. if (StarTimer < 0)
  192. {
  193. StarTimer = Mathf.Lerp(MinStarTime, MaxStarTime, Random.Range(0f, 1f));
  194. StarList.Add(ManaReso.GetStar());
  195. }
  196. }
  197. }
  198. public void AwardThread()
  199. {
  200. if (MiniLock)
  201. {
  202. AwardTimer -= Time.deltaTime;
  203. if (AwardTimer <= 0)
  204. {
  205. AwardTimer = Random.Range(5f, 15);
  206. List<Flower> spareList = new List<Flower>();
  207. for (int i = 0; i < PlantList.Count; i++)
  208. {
  209. if (PlantList[i].Flower.Award == false)
  210. {
  211. spareList.Add(PlantList[i].Flower);
  212. }
  213. }
  214. if (spareList.Count > 0)
  215. {
  216. spareList.Random()[0].Award = true;
  217. }
  218. }
  219. }
  220. }
  221. public override void Instantiate()
  222. {
  223. ManaReso.Get("Garden", Folder.Discard, true, transform, true, ObjType.Garden).AddScript<Garden>();
  224. CreatePage();
  225. CreatePage();
  226. #region 生成FlowerItem
  227. List<XmlAttributeCollection> attributeList = ManaData.GetFlowerConfig();
  228. for (int i = 0; i < attributeList.Count; i++)
  229. {
  230. FlowerInfo flowerInfo = new FlowerInfo(attributeList[i]);
  231. if (flowerInfo.Special)
  232. {
  233. TotalFlowerSpec++;
  234. }
  235. else
  236. {
  237. TotalFlowerRegu++;
  238. }
  239. FlowerInfoDic.Add(flowerInfo.ID_, flowerInfo);
  240. }
  241. #endregion
  242. }
  243. public override void RegistValueA()
  244. {
  245. Instance = this;
  246. ElfTimer = Random.Range(5f, 30f);
  247. AwardTimer = Random.Range(5f, 15f);
  248. UnlockSlot();
  249. #region 读花朵存档
  250. List<int> flowerIDList = ManaData.GetFlowerList();
  251. for (int i = 0; i < flowerIDList.Count; i++)
  252. {
  253. FlowerInfoDic[flowerIDList[i]].Unlock = true;
  254. }
  255. List<int> flowerAmountList = ManaData.GetIntList(' ', "FlowerAmtList", null);
  256. for (int i = 0; i < flowerAmountList.Count; i++)
  257. {
  258. FlowerInfoDic[flowerIDList[i]].Amount = flowerAmountList[i];
  259. }
  260. List<KV<int, int>> plantList = ManaData.GetPlantList();
  261. for (int i = 0; i < plantList.Count; i++)
  262. {
  263. PlantFlower(plantList[i].Key, plantList[i].Value);
  264. }
  265. #endregion
  266. }
  267. public override void RegistReference()
  268. {
  269. RetrieveButton = ManaReso.Get<Button>("H_Btn");
  270. RetrieveButtonLab = ManaReso.Get<Text>("H_BtnLab");
  271. PurchaseButton = ManaReso.Get<Button>("H_Btn1");
  272. PurchaseButtonLab = ManaReso.Get<Text>("H_BtnLab1");
  273. }
  274. public static void UnlockSlot()
  275. {
  276. for (int i = 0; i < SlotList.Count; i++)
  277. {
  278. if (SlotList[i].Lock == false)
  279. {
  280. Slot++;
  281. SlotList[i].Lock = true;
  282. SlotList[i].Available = true;
  283. if (Slot%9 == 7)
  284. {
  285. if (Slot/9 + 2 >= Page)
  286. {
  287. CreatePage();
  288. }
  289. }
  290. return;
  291. }
  292. }
  293. }
  294. public static void CreatePage()
  295. {
  296. Transform tra = ManaReso.Get("Page", Folder.Scene, false, ManaReso.Get("GardenPage"), false, ObjType.Page);
  297. float offset = Page*18.9f;
  298. tra.SetLX(offset);
  299. Vector3 pos = ManaReso.Get("GardenPage").position;
  300. pos.x = -offset;
  301. Garden.PagePos.Add(pos);
  302. for (int i = 0; i < 9; i++)
  303. {
  304. Slot slot = tra.GetChild(i).GetComponent<Slot>();
  305. if (slot == null)
  306. {
  307. slot = tra.GetChild(i).AddScript<Slot>();
  308. }
  309. slot.Index = SlotList.Count;
  310. SlotList.Add(slot);
  311. }
  312. PageList.Add(tra);
  313. }
  314. public static void ShowPlantCard(FlowerInfo flowerInfo, Slot seleSlot = null)
  315. {
  316. ShowFlowerCard(flowerInfo, null, seleSlot);
  317. RetrieveButton.SetActive(false);
  318. PurchaseButton.SetActive(true);
  319. }
  320. public static void ShowRetrieveCard(FlowerInfo flowerInfo, Slot seleSlot = null)
  321. {
  322. ShowFlowerCard(flowerInfo, null, seleSlot);
  323. RetrieveButton.SetActive(true);
  324. PurchaseButton.SetActive(true);
  325. }
  326. public static void ShowUnlockCard(FlowerInfo flowerInfo, Slot seleSlot = null)
  327. {
  328. ShowFlowerCard(flowerInfo, Lib.GrayMat, seleSlot);
  329. RetrieveButton.SetActive(false);
  330. PurchaseButton.SetActive(true);
  331. }
  332. private static void ShowFlowerCard(FlowerInfo flowerInfo, Material material, Slot seleSlot)
  333. {
  334. SetRetrieveBtn(RetrieveButton, RetrieveButtonLab, seleSlot);
  335. SetPurchaseBtn(flowerInfo, PurchaseButton, PurchaseButtonLab);
  336. SeleInfo = flowerInfo;
  337. ManaReso.Get("H_FlowerCard").TweenForCG();
  338. ManaReso.SetText("H_Lab", flowerInfo.Name);
  339. SetPlantStatus(flowerInfo);
  340. Image image = ManaReso.Get<Image>("H_Icon2");
  341. image.material = material;
  342. image.sprite = flowerInfo.Icon;
  343. image.Resize(true, 0.6125f, 0.6125f);
  344. }
  345. private static void SetRetrieveBtn(Button button, Text buttonLab, Slot seleSlot = null)
  346. {
  347. RetrieveButton = button;
  348. buttonLab.text = Language.GetStr("UI", "H_BtnLab1");
  349. button.image.material = null;
  350. button.onClick.RemoveAllListeners();
  351. button.onClick.AddListener
  352. (
  353. () =>
  354. {
  355. ManaAudio.PlayClip(Clip.BtnClip);
  356. RetriveFlower(seleSlot);
  357. }
  358. );
  359. }
  360. private static void SetPurchaseBtn(FlowerInfo flowerInfo, Button button, Text buttonLab)
  361. {
  362. if (flowerInfo.UnlockCur == Current.Free)
  363. {
  364. buttonLab.text = Language.GetStr("UI", "H_BtnLab2");
  365. button.image.material = Lib.GrayMat;
  366. button.onClick.RemoveAllListeners();
  367. button.onClick.AddListener
  368. (
  369. () =>
  370. {
  371. Bubble.Show(null, Language.GetStr("UI", "H_BtnLab3"));
  372. }
  373. );
  374. }
  375. else
  376. {
  377. buttonLab.text = Auxiliary.ImageParse(flowerInfo.UnlockCur) + flowerInfo.UnlockAmt;
  378. button.image.material = null;
  379. button.onClick.RemoveAllListeners();
  380. button.onClick.AddListener
  381. (
  382. () =>
  383. {
  384. ManaCenter.Pay
  385. (
  386. "",
  387. flowerInfo.UnlockAmt,
  388. flowerInfo.UnlockCur,
  389. () =>
  390. {
  391. flowerInfo.Add();
  392. SetPlantStatus(flowerInfo);
  393. ManaServer.Save();
  394. PlayFlowerCardMatTween();
  395. ManaAudio.PlayClip(Clip.CurrentClip);
  396. ManaReso.Get("H_Icon1").FindChild("UIFlashLight/UIParticle System").GetComponent<UIPartical>().Begin();
  397. },
  398. StaticsManager.ItemID.解锁花朵,
  399. StaticsManager.ConsumeModule.Shop,
  400. true,
  401. false,
  402. () =>
  403. {
  404. ManaReso.Get("H_FlowerCard").TweenBacCG();
  405. TweenCG tweenCg = ManaReso.Get("G_Flower").TweenBacCG();
  406. tweenCg.AddEventOnetime
  407. (
  408. EventType.BackwardFinish,
  409. () =>
  410. {
  411. ManaReso.Get("F_Manage0").TweenForVec();
  412. }
  413. );
  414. }
  415. );
  416. }
  417. );
  418. }
  419. }
  420. private static void SetPlantStatus(FlowerInfo flowerInfo)
  421. {
  422. if (flowerInfo.RemainAmount == 0)
  423. {
  424. ManaReso.Get<Button>("H_Icon2").interactable = false;
  425. ManaReso.SetActive("H_AmtLab", false);
  426. ManaReso.SetActive("H_Desc", false);
  427. }
  428. else
  429. {
  430. ManaReso.SetActive("H_Desc", true);
  431. ManaReso.Get<Button>("H_Icon2").interactable = true;
  432. ManaReso.SetActive("H_AmtLab", true);
  433. ManaReso.SetText("H_AmtLab", "X" + flowerInfo.RemainAmount);
  434. }
  435. }
  436. private static void SetRetrieveStatus(FlowerInfo flowerInfo, Button button)
  437. {
  438. if (flowerInfo.PlantAmt == 0)
  439. button.SetActive(false);
  440. else
  441. button.SetActive(true);
  442. }
  443. private static void PlayFlowerCardMatTween()
  444. {
  445. if (ManaReso.Get<Image>("H_Icon2").material != Lib.GrayMat)
  446. {
  447. return;
  448. }
  449. Material material = new Material(ManaReso.Get<Image>("H_Icon2").material);
  450. MaterialUnit materialUnit = new MaterialUnit
  451. (
  452. material,
  453. ManaReso.Get("H_Icon2"),
  454. new List<string>()
  455. {
  456. "_GrayLerp",
  457. }
  458. );
  459. FlowerCardMatTween = materialUnit.CreateTweenMatFloat(1, 0, 1, true, true, Curve.EaseOutQuad, false);
  460. FlowerCardMatTween.OnForwardStart = () =>
  461. {
  462. materialUnit.Transform.GetComponent<Image>().material = materialUnit.Material;
  463. };
  464. FlowerCardMatTween.OnForwardFinish = () =>
  465. {
  466. ManaReso.Get<Image>("H_Icon2").material = null;
  467. };
  468. materialUnit.TweenForMatFloat();
  469. }
  470. public static void RetriveFlower(Slot seleSlot = null)
  471. {
  472. if (seleSlot != null && seleSlot.Flower == null)
  473. seleSlot = null;
  474. if (seleSlot == null)
  475. {
  476. foreach (var slot in PlantList)
  477. {
  478. if (slot.Flower.FlowerInfo == SeleInfo)
  479. {
  480. seleSlot = slot;
  481. break;
  482. }
  483. }
  484. }
  485. seleSlot.Retrieve();
  486. SetPlantStatus(seleSlot.FlowerInfo);
  487. SetRetrieveStatus(seleSlot.FlowerInfo, RetrieveButton);
  488. }
  489. public static void RetriveFlowerAll()
  490. {
  491. for (int i = 0; i < PlantList.Count; i++)
  492. {
  493. PlantList[i--].Retrieve();
  494. }
  495. }
  496. public static void PlantFlower(int id, int index)
  497. {
  498. Slot slot = SlotList[index];
  499. FlowerInfo flowerInfo = FlowerInfoDic[id];
  500. slot.Plant(flowerInfo, false);
  501. }
  502. public static void PlantFlower(FlowerInfo flowerInfo)
  503. {
  504. Slot slot = null;
  505. for (int i = 0; i < SlotList.Count; i++)
  506. {
  507. if (SlotList[i].Available)
  508. {
  509. slot = SlotList[i];
  510. break;
  511. }
  512. }
  513. if (slot == null)
  514. {
  515. Bubble.Show(null, Language.GetStr("Common", "NoValidSlot"));
  516. }
  517. else
  518. {
  519. slot.Plant(flowerInfo, true);
  520. SetPlantStatus(flowerInfo);
  521. SetRetrieveStatus(flowerInfo, RetrieveButton);
  522. ManaAudio.PlayClip(Clip.FlowerClip);
  523. }
  524. }
  525. public static void RetrieveAllElf()
  526. {
  527. for (int i = 0; i < PlantList.Count; i++)
  528. {
  529. PlantList[i].Flower.RetrieveElf();
  530. }
  531. }
  532. public static void RetrieveAllStar()
  533. {
  534. for (int i = 0; i < StarList.Count; i++)
  535. {
  536. ManaReso.Save(StarList[i]);
  537. }
  538. }
  539. }