Flower.cs 18 KB

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