GardenManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 GardenManager : Regist
  35. {
  36. #region Config
  37. public static int Slot
  38. {
  39. get { return Slot_; }
  40. set
  41. {
  42. Slot_ = value;
  43. ResourceManager.SetText(ObjectLabel.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. AchieveManager.UpdateStatus(AchieveType.FlowerAmt, MyFlower_);
  57. ResourceManager.SetText(ObjectLabel.F_FlowerLab, string.Format("{0}", MyFlower));
  58. ResourceManager.SetText(ObjectLabel.G_CollectLab1, string.Format("{0}/{1}", MyFlower, TotalFlower));
  59. if (MyFlower_ >= TutorialManager.MinFlowerForFindGame && Manager.Level >= TutorialManager.FindMinigameTutorialLevel)
  60. {
  61. if (TutorialManager.findMinigameTutorial)
  62. {
  63. TutorialManager.PlayFindGame();
  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. public static int DefaultSlot = 1;
  106. private static int MyFlower_;
  107. private static int MyFlowerSpec_;
  108. private static int MyFlowerRegu_;
  109. private static int TotalFlowerSpec_;
  110. private static int TotalFlowerRegu_;
  111. public static int FirstUnlockFlowerID
  112. {
  113. get
  114. {
  115. foreach (var kv in FlowerInfoDic)
  116. {
  117. if (!kv.Value.Unlock)
  118. {
  119. return kv.Key;
  120. }
  121. }
  122. return -1;
  123. }
  124. }
  125. public static List<int> UnlockedFlowerIDList
  126. {
  127. get
  128. {
  129. List<int> unlockedFlowerIDList = new List<int>();
  130. foreach (var kv in FlowerInfoDic)
  131. {
  132. if (kv.Value.Unlock)
  133. {
  134. unlockedFlowerIDList.Add(kv.Key);
  135. }
  136. }
  137. return unlockedFlowerIDList;
  138. }
  139. }
  140. public static int TotalFlower;
  141. public static bool MiniLock = true;
  142. public static float ElfTimer;
  143. public static float StarTimer;
  144. public static float AwardTimer;
  145. public static Button RetrieveButton;
  146. public static Text RetrieveButtonLab;
  147. public static Button PurchaseButton;
  148. public static Text PurchaseButtonLab;
  149. public static FlowerInfo SeleInfo;
  150. public static GardenManager Instance;
  151. public static List<Slot> SlotList = new List<Slot>();
  152. public static List<Slot> PlantList = new List<Slot>();
  153. public static List<Star> StarList = new List<Star>();
  154. public static List<ElfType> ElfList = new List<ElfType>();
  155. public static List<Transform> PageList = new List<Transform>();
  156. public static Dictionary<int, FlowerInfo> FlowerInfoDic = new Dictionary<int, FlowerInfo>();
  157. public static TweenMatFloat FlowerCardMatTween;
  158. #endregion
  159. public void Update()
  160. {
  161. if (TutorialManager.NewplayerTutorial || Player.InDressRoom || SFSManager.GardenSmartFox.PlazaRoomController.JoinedPlazaRoom)
  162. {
  163. return;
  164. }
  165. ElfThread();
  166. StarThread();
  167. AwardThread();
  168. }
  169. public static float MinElfTime = 5;
  170. public static float MaxElfTime = 30;
  171. public void ElfThread()
  172. {
  173. if (!MiniLock)
  174. {
  175. return;
  176. }
  177. ElfTimer -= Time.deltaTime;
  178. if (ElfTimer < 0)
  179. {
  180. ElfTimer = Random.Range(MinElfTime, MaxElfTime);
  181. if (ElfList.Count > 0 && PlantList.Count > 0)
  182. {
  183. PlantList.Random()[0].Flower.GetElf(ElfList.Random()[0]);
  184. }
  185. }
  186. }
  187. public static float MinStarTime;
  188. public static float MaxStarTime;
  189. public void StarThread()
  190. {
  191. if (MiniLock && !Manager.MiniLock && !VisitManager.InVisit)
  192. {
  193. StarTimer -= Time.deltaTime;
  194. if (StarTimer < 0)
  195. {
  196. StarTimer = Mathf.Lerp(MinStarTime, MaxStarTime, Random.Range(0f, 1f));
  197. StarList.Add(ResourceManager.GetStar());
  198. }
  199. }
  200. }
  201. public static float MinAwardTime = 5;
  202. public static float MaxAwardTime = 15;
  203. public void AwardThread()
  204. {
  205. if (MiniLock)
  206. {
  207. AwardTimer -= Time.deltaTime;
  208. if (AwardTimer <= 0)
  209. {
  210. AwardTimer = Random.Range(MinAwardTime, MaxAwardTime);
  211. List<Flower> spareList = new List<Flower>();
  212. for (int i = 0; i < PlantList.Count; i++)
  213. {
  214. if (PlantList[i].Flower.Award == false)
  215. {
  216. spareList.Add(PlantList[i].Flower);
  217. }
  218. }
  219. if (spareList.Count > 0)
  220. {
  221. spareList.Random()[0].Award = true;
  222. }
  223. }
  224. }
  225. }
  226. public override void InstantiatePrefabs()
  227. {
  228. ResourceManager.Get(ResourceLabel.Garden, Folder.Scene, true, transform, true, ObjType.Garden).AddScript<Garden>();
  229. for (int i = 0; i < DefaultPage; i++)
  230. {
  231. CreatePage();
  232. }
  233. #region 生成FlowerItem
  234. List<XmlAttributeCollection> attributeList = ConfigManager.GetFlowerConfig();
  235. for (int i = 0; i < attributeList.Count; i++)
  236. {
  237. FlowerInfo flowerInfo = new FlowerInfo(attributeList[i]);
  238. if (flowerInfo.Special)
  239. {
  240. TotalFlowerSpec++;
  241. }
  242. else
  243. {
  244. TotalFlowerRegu++;
  245. }
  246. FlowerInfoDic.Add(flowerInfo.ID_, flowerInfo);
  247. }
  248. #endregion
  249. }
  250. public override void FirstInit()
  251. {
  252. Instance = this;
  253. ElfTimer = Random.Range(MinElfTime, MaxElfTime);
  254. AwardTimer = Random.Range(MinAwardTime, MaxAwardTime);
  255. UnlockSlot();
  256. for (int i = 0; i < ConfigManager.GetPlayerInt(PlayerConfigLabel.ExtraSlot); i++)
  257. {
  258. UnlockSlot();
  259. }
  260. #region 读花朵存档
  261. List<int> flowerIDList = ConfigManager.GetFlowerList();
  262. for (int i = 0; i < flowerIDList.Count; i++)
  263. {
  264. FlowerInfoDic[flowerIDList[i]].Unlock = true;
  265. }
  266. List<int> flowerAmountList = ConfigManager.GetIntList(' ', PlayerConfigLabel.FlowerAmtList, null);
  267. for (int i = 0; i < flowerAmountList.Count; i++)
  268. {
  269. FlowerInfoDic[flowerIDList[i]].Amount = flowerAmountList[i];
  270. }
  271. List<KV<int, int>> plantList = ConfigManager.GetPlantList();
  272. for (int i = 0; i < plantList.Count; i++)
  273. {
  274. PlantFlower(plantList[i].Key, plantList[i].Value);
  275. }
  276. #endregion
  277. }
  278. public override void RegistReference()
  279. {
  280. RetrieveButton = ResourceManager.Get<Button>(ObjectLabel.H_Btn);
  281. RetrieveButtonLab = ResourceManager.Get<Text>(ObjectLabel.H_BtnLab);
  282. PurchaseButton = ResourceManager.Get<Button>(ObjectLabel.H_Btn1);
  283. PurchaseButtonLab = ResourceManager.Get<Text>(ObjectLabel.H_BtnLab1);
  284. }
  285. public static int DefaultPage = 2;
  286. public static int CriticalSlotAmt = 7;
  287. public static int TotalSlotAmtInOnePage = 9;
  288. public static void UnlockSlot()
  289. {
  290. for (int i = 0; i < SlotList.Count; i++)
  291. {
  292. if (SlotList[i].Lock == false)
  293. {
  294. Slot++;
  295. SlotList[i].Lock = true;
  296. SlotList[i].Available = true;
  297. if (Slot% TotalSlotAmtInOnePage == CriticalSlotAmt)
  298. {
  299. if (Slot/ TotalSlotAmtInOnePage + DefaultPage >= Page)
  300. {
  301. CreatePage();
  302. }
  303. }
  304. return;
  305. }
  306. }
  307. }
  308. private static float PageXOffset = 18.9f;
  309. public static void CreatePage()
  310. {
  311. Transform tra = ResourceManager.Get(ResourceLabel.Page, Folder.Scene, false, ResourceManager.Get("GardenPage"), false, ObjType.Page);
  312. float offset = Page*PageXOffset;
  313. tra.SetLX(offset);
  314. Vector3 pos = ResourceManager.Get("GardenPage").position;
  315. pos.x = -offset;
  316. Garden.PagePos.Add(pos);
  317. for (int i = 0; i < TotalSlotAmtInOnePage; i++)
  318. {
  319. Slot slot = tra.GetChild(i).GetComponent<Slot>();
  320. if (slot == null)
  321. {
  322. slot = tra.GetChild(i).AddScript<Slot>();
  323. }
  324. slot.Index = SlotList.Count;
  325. SlotList.Add(slot);
  326. }
  327. PageList.Add(tra);
  328. }
  329. public static void ShowPlantCard(FlowerInfo flowerInfo, Slot seleSlot = null)
  330. {
  331. ShowFlowerCard(flowerInfo, null, seleSlot);
  332. RetrieveButton.SetActive(false);
  333. PurchaseButton.SetActive(true);
  334. }
  335. public static void ShowRetrieveCard(FlowerInfo flowerInfo, Slot seleSlot = null)
  336. {
  337. ShowFlowerCard(flowerInfo, null, seleSlot);
  338. RetrieveButton.SetActive(true);
  339. PurchaseButton.SetActive(true);
  340. }
  341. public static void ShowUnlockCard(FlowerInfo flowerInfo, Slot seleSlot = null)
  342. {
  343. ShowFlowerCard(flowerInfo, Lib.GrayMat, seleSlot);
  344. RetrieveButton.SetActive(false);
  345. PurchaseButton.SetActive(true);
  346. }
  347. private static void ShowFlowerCard(FlowerInfo flowerInfo, Material material, Slot seleSlot)
  348. {
  349. SetRetrieveBtn(RetrieveButton, RetrieveButtonLab, seleSlot);
  350. SetPurchaseBtn(flowerInfo, PurchaseButton, PurchaseButtonLab);
  351. SeleInfo = flowerInfo;
  352. ResourceManager.Get(ObjectLabel.H_FlowerCard).TweenForCG();
  353. ResourceManager.SetText(ObjectLabel.H_Lab, flowerInfo.Name);
  354. SetPlantStatus(flowerInfo);
  355. Image image = ResourceManager.Get<Image>(ObjectLabel.H_Icon2);
  356. image.material = material;
  357. image.sprite = flowerInfo.Icon;
  358. image.Resize(true, 0.6125f, 0.6125f);
  359. }
  360. private static void SetRetrieveBtn(Button button, Text buttonLab, Slot seleSlot = null)
  361. {
  362. RetrieveButton = button;
  363. buttonLab.text = Language.GetStr(LanguageLabel.UI__H_BtnLab1);
  364. button.image.material = null;
  365. button.onClick.RemoveAllListeners();
  366. button.onClick.AddListener
  367. (
  368. () =>
  369. {
  370. AudioManager.PlayClip(Clip.BtnClip);
  371. RetriveFlower(seleSlot);
  372. }
  373. );
  374. }
  375. private static void SetPurchaseBtn(FlowerInfo flowerInfo, Button button, Text buttonLab)
  376. {
  377. if (flowerInfo.UnlockCur == Current.Free)
  378. {
  379. buttonLab.text = Language.GetStr(LanguageLabel.UI__H_BtnLab2);
  380. button.image.material = Lib.GrayMat;
  381. button.onClick.RemoveAllListeners();
  382. button.onClick.AddListener
  383. (
  384. () =>
  385. {
  386. Bubble.Show(null, Language.GetStr(LanguageLabel.UI__H_BtnLab3));
  387. }
  388. );
  389. }
  390. else
  391. {
  392. buttonLab.text = Auxiliary.ImageParse(flowerInfo.UnlockCur) + flowerInfo.UnlockAmt;
  393. button.image.material = null;
  394. button.onClick.RemoveAllListeners();
  395. button.onClick.AddListener
  396. (
  397. () =>
  398. {
  399. Manager.Pay
  400. (
  401. "",
  402. flowerInfo.UnlockAmt,
  403. flowerInfo.UnlockCur,
  404. () =>
  405. {
  406. flowerInfo.Add();
  407. SetPlantStatus(flowerInfo);
  408. HttpManager.Save();
  409. PlayFlowerCardMatTween();
  410. AudioManager.PlayClip(Clip.CurrentClip);
  411. ResourceManager.Get(ObjectLabel.H_Icon1).FindChild($"{ObjectLabel.H_UIFlashLight}/{ObjectLabel.H_UIParticleSystem}").GetComponent<UIPartical>().Begin();
  412. },
  413. StaticsManager.ItemID.解锁花朵,
  414. StaticsManager.ConsumeModule.Shop,
  415. true,
  416. false,
  417. () =>
  418. {
  419. ResourceManager.Get(ObjectLabel.H_FlowerCard).TweenBacCG();
  420. TweenCG tweenCg = ResourceManager.Get(ObjectLabel.G_Flower).TweenBacCG();
  421. tweenCg.AddEventOnetime
  422. (
  423. EventType.BackwardFinish,
  424. () =>
  425. {
  426. ResourceManager.Get(ObjectLabel.F_Manage0).TweenForVec();
  427. }
  428. );
  429. }
  430. );
  431. }
  432. );
  433. }
  434. }
  435. private static void SetPlantStatus(FlowerInfo flowerInfo)
  436. {
  437. if (flowerInfo.RemainAmount == 0)
  438. {
  439. ResourceManager.Get<Button>(ObjectLabel.H_Icon2).interactable = false;
  440. ResourceManager.SetActive(ObjectLabel.H_AmtLab, false);
  441. ResourceManager.SetActive(ObjectLabel.H_Desc, false);
  442. ResourceManager.SetActive(ObjectLabel.H_Rotate, false);
  443. }
  444. else
  445. {
  446. ResourceManager.SetActive(ObjectLabel.H_Desc, true);
  447. ResourceManager.Get<Button>(ObjectLabel.H_Icon2).interactable = true;
  448. ResourceManager.SetActive(ObjectLabel.H_AmtLab, true);
  449. ResourceManager.SetText(ObjectLabel.H_AmtLab, "x" + flowerInfo.RemainAmount);
  450. ResourceManager.SetActive(ObjectLabel.H_Rotate, true);
  451. }
  452. }
  453. private static void SetRetrieveStatus(FlowerInfo flowerInfo, Button button)
  454. {
  455. if (flowerInfo.PlantAmt == 0)
  456. button.SetActive(false);
  457. else
  458. button.SetActive(true);
  459. }
  460. private static void PlayFlowerCardMatTween()
  461. {
  462. if (ResourceManager.Get<Image>(ObjectLabel.H_Icon2).material != Lib.GrayMat)
  463. {
  464. return;
  465. }
  466. Material material = new Material(ResourceManager.Get<Image>(ObjectLabel.H_Icon2).material);
  467. MaterialUnit materialUnit = new MaterialUnit
  468. (
  469. material,
  470. ResourceManager.Get(ObjectLabel.H_Icon2),
  471. new List<string>()
  472. {
  473. "_GrayLerp",
  474. }
  475. );
  476. FlowerCardMatTween = materialUnit.CreateTweenMatFloat(1, 0, 1, true, true, Curve.EaseOutQuad, false);
  477. FlowerCardMatTween.OnForwardStart = () =>
  478. {
  479. materialUnit.Transform.GetComponent<Image>().material = materialUnit.Material;
  480. };
  481. FlowerCardMatTween.OnForwardFinish = () =>
  482. {
  483. ResourceManager.Get<Image>(ObjectLabel.H_Icon2).material = null;
  484. };
  485. materialUnit.TweenForMatFloat();
  486. }
  487. public static void RetriveFlower(Slot seleSlot = null)
  488. {
  489. if (seleSlot != null && seleSlot.Flower == null)
  490. seleSlot = null;
  491. if (seleSlot == null)
  492. {
  493. foreach (var slot in PlantList)
  494. {
  495. if (slot.Flower.FlowerInfo == SeleInfo)
  496. {
  497. seleSlot = slot;
  498. break;
  499. }
  500. }
  501. }
  502. seleSlot.Retrieve();
  503. SetPlantStatus(seleSlot.FlowerInfo);
  504. SetRetrieveStatus(seleSlot.FlowerInfo, RetrieveButton);
  505. }
  506. public static void RetriveFlowerAll()
  507. {
  508. for (int i = 0; i < PlantList.Count; i++)
  509. {
  510. PlantList[i--].Retrieve();
  511. }
  512. }
  513. public static void PlantFlower(int id, int index)
  514. {
  515. Slot slot = SlotList[index];
  516. FlowerInfo flowerInfo = FlowerInfoDic[id];
  517. slot.Plant(flowerInfo, false);
  518. }
  519. public static void PlantFlower(FlowerInfo flowerInfo)
  520. {
  521. Slot slot = null;
  522. for (int i = 0; i < SlotList.Count; i++)
  523. {
  524. if (SlotList[i].Available)
  525. {
  526. slot = SlotList[i];
  527. break;
  528. }
  529. }
  530. if (slot == null)
  531. {
  532. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__NoValidSlot));
  533. }
  534. else
  535. {
  536. slot.Plant(flowerInfo, true);
  537. SetPlantStatus(flowerInfo);
  538. SetRetrieveStatus(flowerInfo, RetrieveButton);
  539. AudioManager.PlayClip(Clip.FlowerClip);
  540. }
  541. }
  542. public static void RetrieveAllElf()
  543. {
  544. for (int i = 0; i < PlantList.Count; i++)
  545. {
  546. PlantList[i].Flower.RetrieveElf();
  547. }
  548. }
  549. public static void RetrieveAllStar()
  550. {
  551. for (int i = 0; i < StarList.Count; i++)
  552. {
  553. ResourceManager.Save(StarList[i]);
  554. }
  555. }
  556. }