Flower.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Serialization;
  4. using UnityEngine.EventSystems;
  5. using System;
  6. using System.Xml;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using Random = UnityEngine.Random;
  10. public enum OpType
  11. {
  12. Rip,
  13. Null,
  14. Water,
  15. Fertilize,
  16. }
  17. public class FlowerLabel
  18. {
  19. public static string AwardGoldBk = "AwardGoldBk";
  20. public static string AwardGoldIcon = "AwardGoldIcon";
  21. public static string AwardGoldPos = "AwardGoldPos";
  22. public static string FlashLight = "FlashLight";
  23. public static string FlowerIcon = "FlowerIcon";
  24. public static string OperatePanelBk = "OperatePanelBk";
  25. public static string OperateIcon = "OperateIcon";
  26. public static string OperateOutlineParent = "OperateOutlineParent";
  27. public static string NewFlowerEffect = "NewFlowerEffect";
  28. public static string FlowerShadow = "FlowerShadow";
  29. public static string ScorePos = "ScorePos";
  30. public static string MiniGamePanel = "MiniGamePanel";
  31. public static string Flash = "Flash";
  32. public static string ParticleSystem = "ParticleSystem";
  33. public static string AwardGoldBkLeftPos = "AwardGoldBkLeftPos";
  34. }
  35. public class FlowerInfo
  36. {
  37. #region Config
  38. public int Amount
  39. {
  40. get { return amount; }
  41. set
  42. {
  43. amount = value;
  44. AmountText.SetActive(true);
  45. AmountText.text = AmountTextPrefix + amount;
  46. }
  47. }
  48. public int amount;
  49. public int RemainAmount
  50. {
  51. get { return Amount - PlantAmt; }
  52. }
  53. public int PlantAmt;
  54. public bool IsPlanted
  55. {
  56. get { return PlantAmt > 0; }
  57. }
  58. public bool Unlock
  59. {
  60. get { return unlock; }
  61. set
  62. {
  63. unlock = value;
  64. if (unlock)
  65. {
  66. Image.material = null;
  67. if (IsSpecial)
  68. {
  69. if (GardenManager.TotalUnlockSpecialFlower == 0)
  70. {
  71. ResourceManager.Get(CanvasLabel.G_Regular).TweenForVec();
  72. ResourceManager.SetActive(CanvasLabel.G_Special, true);
  73. }
  74. GardenManager.TotalUnlockSpecialFlower++;
  75. }
  76. else
  77. {
  78. GardenManager.TotalUnlockNormalFlower++;
  79. }
  80. Amount = 1;
  81. Manager.FlowerPlus += SkillPlus;
  82. }
  83. }
  84. }
  85. public bool unlock;
  86. public bool IsSpecial;
  87. public float SkillPlus = 0.1f;
  88. public static string IDPrefix = "Flower";
  89. public static string AmountTextPrefix = "x";
  90. public string FullID
  91. {
  92. get
  93. {
  94. return IDPrefix + ID;
  95. }
  96. }
  97. public int ID;
  98. public string Name
  99. {
  100. get
  101. {
  102. return Language.GetStr(LanguageLabel.CombineLanguageLabel(LanguageLabel.FlowerName, FullID));
  103. }
  104. }
  105. public string Description
  106. {
  107. get { return null; }
  108. }
  109. public Sprite Icon
  110. {
  111. get { return ResourceManager.LoadSprite(icon, Folder.Atlas2); }
  112. }
  113. public string icon;
  114. public int UnlockCost;
  115. public float AwardCoinBuff;
  116. public Current UnlockCur;
  117. public float itemSize;
  118. public float flowerCardSize;
  119. public float minigameSize;
  120. public Text StatusText;
  121. public Text AmountText;
  122. public Image Image;
  123. public Button Button;
  124. public UIPartical UIPartical;
  125. public Transform FlowerItem;
  126. public Dictionary<string, Transform> ChildDic = new Dictionary<string, Transform>();
  127. #endregion
  128. public FlowerInfo(XmlAttributeCollection attribute)
  129. {
  130. FlowerItem = ResourceManager.Get(ResourceLabel.FlowerItem, Folder.UI, false, ResourceManager.Get(CanvasLabel.G_RegularGrid), false, ObjType.FlowerItem);
  131. FlowerItem.AddComponent<FlowerItem>();
  132. Auxiliary.CompileDic(FlowerItem, ChildDic);
  133. StatusText = ChildDic[FlowerItemLabel.Status].GetComponent<Text>();
  134. Image = ChildDic[FlowerItemLabel.Icon].GetComponent<Image>();
  135. Button = ChildDic[FlowerItemLabel.FlowerItem].GetComponent<Button>();
  136. UIPartical = ChildDic[FlowerItemLabel.UIParticleSystem].GetComponent<UIPartical>();
  137. AmountText = ChildDic[FlowerItemLabel.Amount].GetComponent<Text>();
  138. ID = int.Parse(attribute[0].Value);
  139. icon = attribute[5].Value;
  140. UnlockCur = Auxiliary.CurrentParse(attribute[3].Value);
  141. UnlockCost = Auxiliary.StringToInt(attribute[4].Value, 0);
  142. AwardCoinBuff = Auxiliary.StringToFloat(attribute[7].Value, 1);
  143. itemSize = Auxiliary.StringToFloat(attribute[8].Value, 1);
  144. flowerCardSize = Auxiliary.StringToFloat(attribute[9].Value, 1);
  145. minigameSize = Auxiliary.StringToFloat(attribute[10].Value, 1);
  146. Image.sprite = Icon;
  147. Image.Resize(true, itemSize, itemSize);
  148. Button.onClick.AddListener
  149. (
  150. () =>
  151. {
  152. if (IsPlanted)
  153. {
  154. AudioManager.PlayClip(AudioLabel.ClickButton);
  155. GardenManager.ShowRetrieveCard(this);
  156. }
  157. else if (Unlock)
  158. {
  159. GardenManager.ShowPlantCard(this);
  160. }
  161. else
  162. {
  163. GardenManager.ShowUnlockCard(this);
  164. }
  165. }
  166. );
  167. }
  168. public void Add()
  169. {
  170. if (Unlock)
  171. Amount++;
  172. else
  173. Unlock = true;
  174. }
  175. }
  176. public class Flower : Regist, IPointerClickHandler
  177. {
  178. #region Config
  179. #region MiniGame
  180. public OpType PunchGameOperateType
  181. {
  182. get { return punchGameOperateType; }
  183. set
  184. {
  185. punchGameOperateType = value;
  186. if (punchGameOperateType == OpType.Rip)
  187. {
  188. OperateIcon.sprite = RipSprite;
  189. }
  190. else if (punchGameOperateType == OpType.Water)
  191. {
  192. OperateIcon.sprite = WaterSprite;
  193. }
  194. else if (punchGameOperateType == OpType.Fertilize)
  195. {
  196. OperateIcon.sprite = FertilizeSprite;
  197. }
  198. }
  199. }
  200. public OpType punchGameOperateType;
  201. public SpriteRenderer FlowerIcon;
  202. public SpriteRenderer OperatePanelBk;
  203. public SpriteRenderer OperateIcon;
  204. public SpriteRenderer OperateOutlineParent;
  205. #endregion
  206. public bool HaveAward
  207. {
  208. get { return haveAward; }
  209. set
  210. {
  211. haveAward = value;
  212. if (haveAward)
  213. {
  214. ShowAward();
  215. }
  216. else
  217. {
  218. GetAward();
  219. }
  220. }
  221. }
  222. public bool haveAward;
  223. public Sprite RipSprite
  224. {
  225. get
  226. {
  227. return ResourceManager.LoadSprite(ResourceLabel.Rip, Folder.UI);
  228. }
  229. }
  230. public Sprite WaterSprite
  231. {
  232. get
  233. {
  234. return ResourceManager.LoadSprite(ResourceLabel.Water, Folder.UI);
  235. }
  236. }
  237. public Sprite FertilizeSprite
  238. {
  239. get
  240. {
  241. return ResourceManager.LoadSprite(ResourceLabel.Fertilize, Folder.UI);
  242. }
  243. }
  244. public FlowerInfo FlowerInfo
  245. {
  246. get { return flowerInfo; }
  247. set
  248. {
  249. flowerInfo = value;
  250. ID = FlowerInfo.ID;
  251. FlowerIcon.sprite = FlowerInfo.Icon;
  252. }
  253. }
  254. private FlowerInfo flowerInfo;
  255. public SpriteRenderer ShadowSR;
  256. public int ID;
  257. public Slot Slot;
  258. public Animator FlashLightAC;
  259. public Transform AwardGoldBk;
  260. public Transform AwardGoldIcon;
  261. public MaterialUnit NewFlowerEffectMatUnit;
  262. public ParticleSystem NewFlowerEffect;
  263. public List<Transform> ElfList = new List<Transform>();
  264. public Dictionary<string, Transform> ChildDic = new Dictionary<string, Transform>();
  265. public static string AwardCoinFml;
  266. #endregion
  267. #region MiniGame
  268. public void GameOver()
  269. {
  270. PunchGameOperateType = OpType.Null;
  271. ChildDic[FlowerLabel.MiniGamePanel].SetActive(false);
  272. OperatePanelBk.TweenBacScale();
  273. }
  274. public void GameBegin()
  275. {
  276. ChildDic[FlowerLabel.MiniGamePanel].SetActive(true);
  277. OperatePanelBk.SetActive(false);
  278. }
  279. public void SetFirstOp()
  280. {
  281. OperatePanelBk.TweenForScale();
  282. OperateIcon.SetAlpha(1);
  283. OperateOutlineParent.SetAlpha(1);
  284. OperateIcon.SetActive(true);
  285. OperateOutlineParent.SetActive(true);
  286. }
  287. public void SetThirdOp()
  288. {
  289. OperateIcon.SetAlpha(0.5f);
  290. OperateOutlineParent.SetAlpha(0.5f);
  291. OperateIcon.SetActive(true);
  292. OperateOutlineParent.SetActive(false);
  293. }
  294. public void SetSecondOp()
  295. {
  296. OperateIcon.SetAlpha(1);
  297. OperateOutlineParent.SetAlpha(1);
  298. OperateIcon.SetActive(true);
  299. OperateOutlineParent.SetActive(false);
  300. }
  301. public void CreateOp(int sequence)
  302. {
  303. float random = Random.Range(0f, 1f);
  304. OperatePanelBk.SetActive(true);
  305. if (random <= 0.3333333f)
  306. {
  307. PunchGameOperateType = OpType.Rip;
  308. }
  309. else if (random <= 0.6666666f)
  310. {
  311. PunchGameOperateType = OpType.Water;
  312. }
  313. else
  314. {
  315. PunchGameOperateType = OpType.Fertilize;
  316. }
  317. if (sequence == 0)
  318. {
  319. SetFirstOp();
  320. }
  321. else if (sequence == 1)
  322. {
  323. SetSecondOp();
  324. }
  325. else
  326. {
  327. SetThirdOp();
  328. }
  329. }
  330. public void CreateOp(int sequence, OpType opType)
  331. {
  332. PunchGameOperateType = opType;
  333. OperatePanelBk.SetActive(true);
  334. if (sequence == 0)
  335. {
  336. SetFirstOp();
  337. }
  338. else if (sequence == 1)
  339. {
  340. SetSecondOp();
  341. }
  342. else
  343. {
  344. SetThirdOp();
  345. }
  346. }
  347. public bool Operate(OpType opType)
  348. {
  349. if (opType == OpType.Rip)
  350. {
  351. ResourceManager.Get(CanvasLabel.D_Rip2).TweenForScale();
  352. }
  353. else if (opType == OpType.Water)
  354. {
  355. ResourceManager.Get(CanvasLabel.D_Water2).TweenForScale();
  356. }
  357. else if (opType == OpType.Fertilize)
  358. {
  359. ResourceManager.Get(CanvasLabel.D_Fertilize2).TweenForScale();
  360. }
  361. if (opType != PunchGameOperateType)
  362. {
  363. TweenRoot tween = FlowerIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad, false, true);
  364. tween.OnForwardFinish += () =>
  365. {
  366. FlowerIcon.TweenBacSr();
  367. };
  368. OperatePanelBk.TweenForSr();
  369. FlowerIcon.TweenForSr();
  370. OperateIcon.Shake(0.5f, 3, new Vector3(0.2f, 0, 0), Curve.EaseOutQuad);
  371. AudioManager.PlayClip(AudioLabel.Error);
  372. return false;
  373. }
  374. else
  375. {
  376. ResourceManager.GetHudText($"+{MiniGameManager.PunchCorrectScore}", Color.white, 90, ChildDic[FlowerLabel.ScorePos], ResourceManager.Get(CanvasLabel.D_HudParent), true);
  377. MiniGameManager.Score += MiniGameManager.PunchCorrectScore;
  378. PlayFlashLight();
  379. PunchGameOperateType = OpType.Null;
  380. OperatePanelBk.TweenBacScale();
  381. OperatePanelBk.SetActive(false);
  382. OperateIcon.SetActive(false);
  383. OperateOutlineParent.SetActive(false);
  384. AudioManager.PlayClip(AudioLabel.ClickButton);
  385. return true;
  386. }
  387. }
  388. #endregion
  389. public override bool InitAtOnce()
  390. {
  391. if (base.InitAtOnce())
  392. {
  393. return true;
  394. }
  395. enabled = true;
  396. Auxiliary.CompileDic(transform, ChildDic);
  397. AwardGoldBk = ChildDic[FlowerLabel.AwardGoldBk];
  398. AwardGoldIcon = ChildDic[FlowerLabel.AwardGoldIcon];
  399. FlashLightAC = ChildDic[FlowerLabel.FlashLight].GetComponent<Animator>();
  400. FlowerIcon = ChildDic[FlowerLabel.FlowerIcon].GetComponent<SpriteRenderer>();
  401. OperatePanelBk = ChildDic[FlowerLabel.OperatePanelBk].GetComponent<SpriteRenderer>();
  402. OperateIcon = ChildDic[FlowerLabel.OperateIcon].GetComponent<SpriteRenderer>();
  403. OperateOutlineParent = ChildDic[FlowerLabel.OperateOutlineParent].GetComponent<SpriteRenderer>();
  404. NewFlowerEffect = ChildDic[FlowerLabel.NewFlowerEffect].GetComponent<ParticleSystem>();
  405. ShadowSR = ChildDic[FlowerLabel.FlowerShadow].GetComponent<SpriteRenderer>();
  406. AwardGoldBk.CreateTweenSr(0, 1, 1f, false, true, Curve.EaseOutQuad);
  407. AwardGoldIcon.CreateTweenSr(0, 1, 1f, false, true, Curve.EaseOutQuad);
  408. #region 新花粒子特效
  409. Renderer renderer = NewFlowerEffect.GetComponent<Renderer>();
  410. Material material = new Material(renderer.material);
  411. renderer.material = material;
  412. NewFlowerEffectMatUnit = new MaterialUnit(material, NewFlowerEffect.transform, new List<string>() { "_Alpha" });
  413. NewFlowerEffectMatUnit.CreateTweenMatFloat(0, 1, 0.25f, false, true, Curve.EaseOutQuad);
  414. #endregion
  415. TweenRoot tween = OperatePanelBk.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad, false, true);
  416. tween.OnForwardFinish += () =>
  417. {
  418. OperatePanelBk.TweenBacSr();
  419. };
  420. tween = OperatePanelBk.CreateTweenScale(OperatePanelBk.transform.localScale + new Vector3(0.1f, 0.1f, 0.1f), 0.25f, false, true, Curve.EaseOutQuad);
  421. tween.OnBackwardFinish += () =>
  422. {
  423. if (PunchGameOperateType != OpType.Null)
  424. {
  425. OperatePanelBk.SetActive(true);
  426. }
  427. };
  428. return false;
  429. }
  430. public void GetElf(ElfType elfType, float xMin = -0.75f, float xMax = 0.75f, float yMin = 0, float yMax = 0.75f)
  431. {
  432. ElfList.Add(ResourceManager.GetElf(this, new Vector4(xMin, xMax, yMin, yMax), elfType));
  433. }
  434. public void RetrieveFlower()
  435. {
  436. ChildDic[FlowerLabel.Flash].SetActive(false);
  437. ChildDic[FlowerLabel.ParticleSystem].SetActive(false);
  438. FlowerInfo.ChildDic[FlowerItemLabel.UIFlash].SetActive(false);
  439. FlowerInfo.ChildDic[FlowerItemLabel.UIParticleSystem].SetActive(false);
  440. ShadowSR.SetAlpha(1);
  441. StopNewFlowerEffec();
  442. ResourceManager.Save(this);
  443. RetrieveElf();
  444. haveAward = false;
  445. ResetAward();
  446. }
  447. public void RetrieveElf()
  448. {
  449. for (int i = 0; i < ElfList.Count; i++)
  450. {
  451. ResourceManager.Save(ElfList[i]);
  452. }
  453. ElfList = new List<Transform>();
  454. }
  455. public void PlayFlashLight()
  456. {
  457. FlashLightAC.SetTrigger("Play");
  458. }
  459. public void PlayNewFlowerEffec()
  460. {
  461. NewFlowerEffectMatUnit.TweenForMatFloat();
  462. }
  463. public void StopNewFlowerEffec()
  464. {
  465. NewFlowerEffectMatUnit.TweenBacMatFloat();
  466. }
  467. public void GetAward()
  468. {
  469. AudioManager.PlayClip(AudioLabel.GetCurrent);
  470. PlayFlashLight();
  471. int coin;
  472. if (VisitManager.InVisit)
  473. {
  474. int awardMin = Mathf.CeilToInt((float)Auxiliary.FmlParse(VisitManager.AwardMinFml, "l", Manager.GardenLevel.ToString()));
  475. int awardMax = Mathf.CeilToInt((float)Auxiliary.FmlParse(VisitManager.AwardMaxFml, "l", Manager.GardenLevel.ToString()));
  476. coin = Mathf.CeilToInt(Mathf.Lerp(awardMin, awardMax, Random.Range(0f, 1f)));
  477. }
  478. else
  479. {
  480. coin = Mathf.CeilToInt((float)Auxiliary.FmlParse(AwardCoinFml, "l", Mathf.Clamp(Manager.GardenLevel, 1, Mathf.Infinity).ToString()) * FlowerInfo.AwardCoinBuff);
  481. Manager.TotalFlowerAwardCoin++;
  482. }
  483. Transform flyGold = ResourceManager.Get(ResourceLabel.FlyGold, Folder.Scene, false, null, AwardGoldIcon.position, ObjType.FlyGold);
  484. TweenRoot tweenRoot = flyGold.GetTweenSr();
  485. if (tweenRoot == null)
  486. {
  487. tweenRoot = flyGold.CreateTweenSr(1, 0, 0.25f, true, false, Curve.EaseOutQuad);
  488. tweenRoot.AddEventOnetime(EventType.ForwardFinish, () => { ResourceManager.Save(flyGold); });
  489. }
  490. else
  491. {
  492. tweenRoot.InOrigin = true;
  493. }
  494. Move2D move2D = new Move2D(flyGold);
  495. move2D.Restrain2D = Restrain2D.Y;
  496. Vector3 destination = Camera.main.ScreenToWorldPoint(ResourceManager.Get(CanvasLabel.C_Coin).position);
  497. move2D.StartMove(destination, 0.5f, false, Curve.EaseOutQuad);
  498. move2D = new Move2D(flyGold);
  499. move2D.Restrain2D = Restrain2D.X;
  500. destination = Camera.main.ScreenToWorldPoint(ResourceManager.Get(CanvasLabel.C_Coin).position);
  501. move2D.StartMove(destination, 0.5f, false, Curve.Linear);
  502. DelayCall.Call
  503. (
  504. 0.4f,
  505. () =>
  506. {
  507. tweenRoot.StartForward();
  508. Manager.AddCoin(coin, StaticsManager.ItemID.获得金币, StaticsManager.ConsumeModule.ClickFlower);
  509. }
  510. );
  511. ResourceManager.GetHudText($"{TransferLabel.CoinSprite}+{coin}", Color.white, 90, ChildDic[FlowerLabel.AwardGoldPos], ResourceManager.Get(CanvasLabel.A_HudParent), true);
  512. ResetAward();
  513. }
  514. public void ResetAward()
  515. {
  516. AwardGoldBk.SetActive(false);
  517. AwardGoldBk.GetTweenSr().Pause();
  518. AwardGoldIcon.GetTweenSr().Pause();
  519. AwardGoldBk.GetTweenSr().InOrigin = true;
  520. AwardGoldIcon.GetTweenSr().InOrigin = true;
  521. }
  522. public void ShowAward()
  523. {
  524. AwardGoldBk.SetX(ChildDic[FlowerLabel.AwardGoldBkLeftPos].position.x);
  525. AwardGoldBk.GetComponent<SpriteRenderer>().flipX = true;
  526. AwardGoldBk.SetY(transform.position.y + 2.5f);
  527. AwardGoldBk.TweenReForSr();
  528. AwardGoldIcon.TweenReForSr();
  529. AwardGoldBk.MoveOffset2D
  530. (
  531. new Vector3(0, 0.5f, 0),
  532. 1f,
  533. true,
  534. Curve.EaseOutQuad
  535. );
  536. }
  537. public void OnPointerClick(PointerEventData eventData)
  538. {
  539. if (eventData.rawPointerPress.transform == transform)
  540. {
  541. if (VisitManager.InVisit)
  542. {
  543. return;
  544. }
  545. ResourceManager.Get(CanvasLabel.G_Flower).TweenForCG();
  546. GardenManager.ShowRetrieveCard(FlowerInfo, Slot);
  547. }
  548. else if (eventData.rawPointerPress.transform == AwardGoldBk)
  549. {
  550. HaveAward = false;
  551. }
  552. }
  553. public void OnDisable()
  554. {
  555. FlashLightAC.transform.GetChild(0).SetActive(false);
  556. FlashLightAC.transform.GetChild(1).SetActive(false);
  557. }
  558. }