Flower.cs 19 KB

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