Flower.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 GoldBk = "GoldBk";
  20. public static string GoldIcon = "GoldIcon";
  21. public static string FlashLight = "FlashLight";
  22. public static string FlowerIcon = "FlowerIcon";
  23. public static string OperateBk = "OperateBk";
  24. public static string OperateIcon = "OperateIcon";
  25. public static string OperateOutline1 = "OperateOutline1";
  26. public static string NewFlowerEffect = "NewFlowerEffect";
  27. public static string FlowerShadow = "FlowerShadow";
  28. public static string ScorePosTra = "ScorePosTra";
  29. public static string MiniGame = "MiniGame";
  30. public static string Flash = "Flash";
  31. public static string ParticleSystem = "Particle System";
  32. public static string GoldPosTra = "GoldPosTra";
  33. public static string GoldBKPosLeft = "GoldBKPosLeft";
  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. AmountLab.SetActive(true);
  45. AmountLab.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 Plant
  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 (Special)
  68. {
  69. if (GardenManager.MyFlowerSpec == 0)
  70. {
  71. ResourceManager.Get(ObjectLabel.G_Regular).TweenForVec();
  72. ResourceManager.SetActive(ObjectLabel.G_Special, true);
  73. }
  74. GardenManager.MyFlowerSpec++;
  75. }
  76. else
  77. {
  78. GardenManager.MyFlowerRegu++;
  79. }
  80. Amount = 1;
  81. Manager.SkillPlus += SkillPlus;
  82. }
  83. }
  84. }
  85. private float SkillPlus = 0.1f;
  86. public static string IDPrefix = "Flower";
  87. public static string AmountTextPrefix = "x";
  88. public string FullID
  89. {
  90. get
  91. {
  92. return IDPrefix + ID_;
  93. }
  94. }
  95. public string Name
  96. {
  97. get
  98. {
  99. return Language.GetStr(LabelUtility.CombineLanguageLabel(LanguageLabel.FlowerName, FullID));
  100. }
  101. }
  102. public string Description
  103. {
  104. get { return null; }
  105. }
  106. public Sprite Icon
  107. {
  108. get { return ResourceManager.LoadSprite(Icon_, Folder.Atlas2); }
  109. }
  110. public int ID_;
  111. public bool Unlock_;
  112. public string Icon_;
  113. public bool Special;
  114. public int UnlockAmt;
  115. public float FlowerCoinBuff;
  116. public Current UnlockCur;
  117. public Text Text;
  118. public Text AmountLab;
  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(ObjectLabel.G_RegularGrid), false, ObjType.FlowerItem);
  128. FlowerItem.AddComponent<FlowerItem>();
  129. Auxiliary.CompileDic(FlowerItem, ChildDic);
  130. Text = ChildDic[FlowerItemLabel.Lab].GetComponent<Text>();
  131. Image = ChildDic[FlowerItemLabel.Icon].GetComponent<Image>();
  132. Button = ChildDic[FlowerItemLabel.FlowerItem].GetComponent<Button>();
  133. UIPartical = ChildDic[FlowerItemLabel.UIParticleSystem].GetComponent<UIPartical>();
  134. AmountLab = ChildDic[FlowerItemLabel.AmtLab].GetComponent<Text>();
  135. ID_ = int.Parse(attribute[0].Value);
  136. Icon_ = attribute[5].Value;
  137. UnlockCur = Auxiliary.CurrentParse(attribute[3].Value);
  138. UnlockAmt = Auxiliary.StringToInt(attribute[4].Value, 0);
  139. FlowerCoinBuff = 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 (Plant)
  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 OpType
  175. {
  176. get { return OpType_; }
  177. set
  178. {
  179. OpType_ = value;
  180. if (OpType_ == OpType.Rip)
  181. {
  182. OperateIcon.sprite = RipSprite;
  183. }
  184. else if (OpType_ == OpType.Water)
  185. {
  186. OperateIcon.sprite = WaterSprite;
  187. }
  188. else if (OpType_ == OpType.Fertilize)
  189. {
  190. OperateIcon.sprite = FertilizeSprite;
  191. }
  192. }
  193. }
  194. public OpType OpType_;
  195. public SpriteRenderer FlowerIcon;
  196. public SpriteRenderer OperateBk;
  197. public SpriteRenderer OperateIcon;
  198. public SpriteRenderer OperateOutline;
  199. #endregion
  200. public bool Award
  201. {
  202. get { return Award_; }
  203. set
  204. {
  205. Award_ = value;
  206. if (Award_)
  207. {
  208. ShowAward();
  209. }
  210. else
  211. {
  212. GetAward();
  213. }
  214. }
  215. }
  216. public Sprite RipSprite
  217. {
  218. get
  219. {
  220. return ResourceManager.LoadSprite(ResourceLabel.Rip, Folder.UI);
  221. }
  222. }
  223. public Sprite WaterSprite
  224. {
  225. get
  226. {
  227. return ResourceManager.LoadSprite(ResourceLabel.Water, Folder.UI);
  228. }
  229. }
  230. public Sprite FertilizeSprite
  231. {
  232. get
  233. {
  234. return ResourceManager.LoadSprite(ResourceLabel.Fertilize, Folder.UI);
  235. }
  236. }
  237. public FlowerInfo FlowerInfo
  238. {
  239. get { return FlowerInfo_; }
  240. set
  241. {
  242. FlowerInfo_ = value;
  243. ID = FlowerInfo.ID_;
  244. FlowerIcon.sprite = FlowerInfo.Icon;
  245. }
  246. }
  247. public SpriteRenderer ShadowSr;
  248. public bool Award_;
  249. private FlowerInfo FlowerInfo_;
  250. public int ID;
  251. public Slot Slot;
  252. public Animator FlashLightAC;
  253. public Transform GoldBk;
  254. public Transform GoldIcon;
  255. public MaterialUnit NewFlowerEffectMatUnit;
  256. protected ParticleSystem NewFlowerEffect;
  257. public List<Transform> ElfList = new List<Transform>();
  258. public Dictionary<string, Transform> ChildDic = new Dictionary<string, Transform>();
  259. public static string FlowerCoinFml;
  260. #endregion
  261. public override bool InitAtOnce()
  262. {
  263. if (base.InitAtOnce())
  264. {
  265. return true;
  266. }
  267. enabled = true;
  268. Auxiliary.CompileDic(transform, ChildDic);
  269. GoldBk = ChildDic[FlowerLabel.GoldBk];
  270. GoldIcon = ChildDic[FlowerLabel.GoldIcon];
  271. FlashLightAC = ChildDic[FlowerLabel.FlashLight].GetComponent<Animator>();
  272. FlowerIcon = ChildDic[FlowerLabel.FlowerIcon].GetComponent<SpriteRenderer>();
  273. OperateBk = ChildDic[FlowerLabel.OperateBk].GetComponent<SpriteRenderer>();
  274. OperateIcon = ChildDic[FlowerLabel.OperateIcon].GetComponent<SpriteRenderer>();
  275. OperateOutline = ChildDic[FlowerLabel.OperateOutline1].GetComponent<SpriteRenderer>();
  276. NewFlowerEffect = ChildDic[FlowerLabel.NewFlowerEffect].GetComponent<ParticleSystem>();
  277. ShadowSr = ChildDic[FlowerLabel.FlowerShadow].GetComponent<SpriteRenderer>();
  278. GoldBk.CreateTweenSr(0, 1, 1f, false, true, Curve.EaseOutQuad);
  279. GoldIcon.CreateTweenSr(0, 1, 1f, false, true, Curve.EaseOutQuad);
  280. #region 新花粒子特效
  281. Renderer renderer = NewFlowerEffect.GetComponent<Renderer>();
  282. Material material = new Material(renderer.material);
  283. renderer.material = material;
  284. NewFlowerEffectMatUnit = new MaterialUnit(material, NewFlowerEffect.transform, new List<string>() { "_Alpha" });
  285. NewFlowerEffectMatUnit.CreateTweenMatFloat(0, 1, 0.25f, false, true, Curve.EaseOutQuad);
  286. #endregion
  287. TweenRoot tween = OperateBk.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad, false, true);
  288. tween.OnForwardFinish += () =>
  289. {
  290. OperateBk.TweenBacSr();
  291. };
  292. tween = OperateBk.CreateTweenScale(OperateBk.transform.localScale + new Vector3(0.1f, 0.1f, 0.1f), 0.25f, false, true, Curve.EaseOutQuad);
  293. tween.OnBackwardFinish += () =>
  294. {
  295. if (OpType != OpType.Null)
  296. {
  297. OperateBk.SetActive(true);
  298. }
  299. };
  300. return false;
  301. }
  302. #region MiniGame
  303. public void GameOver()
  304. {
  305. OpType = OpType.Null;
  306. ChildDic[FlowerLabel.MiniGame].SetActive(false);
  307. OperateBk.TweenBacScale();
  308. }
  309. public void GameBegin()
  310. {
  311. ChildDic[FlowerLabel.MiniGame].SetActive(true);
  312. OperateBk.SetActive(false);
  313. }
  314. public void SetFirstOp()
  315. {
  316. OperateBk.TweenForScale();
  317. OperateIcon.SetAlpha(1);
  318. OperateOutline.SetAlpha(1);
  319. OperateIcon.SetActive(true);
  320. OperateOutline.SetActive(true);
  321. }
  322. public void SetThirdOp()
  323. {
  324. OperateIcon.SetAlpha(0.5f);
  325. OperateOutline.SetAlpha(0.5f);
  326. OperateIcon.SetActive(true);
  327. OperateOutline.SetActive(false);
  328. }
  329. public void SetSecondOp()
  330. {
  331. OperateIcon.SetAlpha(1);
  332. OperateOutline.SetAlpha(1);
  333. OperateIcon.SetActive(true);
  334. OperateOutline.SetActive(false);
  335. }
  336. public void CreateOp(int sequence)
  337. {
  338. float random = Random.Range(0f, 1f);
  339. OperateBk.SetActive(true);
  340. if (random <= 0.3333333f)
  341. {
  342. OpType = OpType.Rip;
  343. }
  344. else if (random <= 0.6666666f)
  345. {
  346. OpType = OpType.Water;
  347. }
  348. else
  349. {
  350. OpType = OpType.Fertilize;
  351. }
  352. if (sequence == 0)
  353. {
  354. SetFirstOp();
  355. }
  356. else if (sequence == 1)
  357. {
  358. SetSecondOp();
  359. }
  360. else
  361. {
  362. SetThirdOp();
  363. }
  364. }
  365. public void CreateOp(int sequence, OpType opType)
  366. {
  367. OpType = opType;
  368. OperateBk.SetActive(true);
  369. if (sequence == 0)
  370. {
  371. SetFirstOp();
  372. }
  373. else if (sequence == 1)
  374. {
  375. SetSecondOp();
  376. }
  377. else
  378. {
  379. SetThirdOp();
  380. }
  381. }
  382. private int PunchScore = 15;
  383. public bool Operate(OpType opType)
  384. {
  385. if (opType == OpType.Rip)
  386. {
  387. ResourceManager.Get(ObjectLabel.D_Rip2).TweenForScale();
  388. }
  389. else if (opType == OpType.Water)
  390. {
  391. ResourceManager.Get(ObjectLabel.D_Water2).TweenForScale();
  392. }
  393. else if (opType == OpType.Fertilize)
  394. {
  395. ResourceManager.Get(ObjectLabel.D_Fertilize2).TweenForScale();
  396. }
  397. if (opType != OpType)
  398. {
  399. TweenRoot tween = FlowerIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad, false, true);
  400. tween.OnForwardFinish += () =>
  401. {
  402. FlowerIcon.TweenBacSr();
  403. };
  404. OperateBk.TweenForSr();
  405. FlowerIcon.TweenForSr();
  406. OperateIcon.Shake(0.5f, 3, new Vector3(0.2f, 0, 0), Curve.EaseOutQuad);
  407. AudioManager.PlayClip(AudioLabel.Error);
  408. return false;
  409. }
  410. else
  411. {
  412. ResourceManager.GetHudText($"+{PunchScore}", Color.white, 90, ChildDic[FlowerLabel.ScorePosTra], ResourceManager.Get(ObjectLabel.D_HudParent), true);
  413. MiniGameManager.Score += PunchScore;
  414. PlayFlashLight();
  415. OpType = OpType.Null;
  416. OperateBk.TweenBacScale();
  417. OperateBk.SetActive(false);
  418. OperateIcon.SetActive(false);
  419. OperateOutline.SetActive(false);
  420. AudioManager.PlayClip(AudioLabel.ClickButton);
  421. return true;
  422. }
  423. }
  424. #endregion
  425. public void GetElf(ElfType elfType, float xMin = -0.75f, float xMax = 0.75f, float yMin = 0, float yMax = 0.75f)
  426. {
  427. ElfList.Add(ResourceManager.GetElf(this, new Vector4(xMin, xMax, yMin, yMax), elfType));
  428. }
  429. public void Retrieve()
  430. {
  431. ChildDic[FlowerLabel.Flash].SetActive(false);
  432. ChildDic[FlowerLabel.ParticleSystem].SetActive(false);
  433. FlowerInfo.ChildDic[FlowerItemLabel.UIFlash].SetActive(false);
  434. FlowerInfo.ChildDic[FlowerItemLabel.UIParticleSystem].SetActive(false);
  435. ShadowSr.SetAlpha(1);
  436. StopNewFlowerEffec();
  437. ResourceManager.Save(this);
  438. RetrieveElf();
  439. Award_ = false;
  440. ResetAward();
  441. }
  442. public void RetrieveElf()
  443. {
  444. for (int i = 0; i < ElfList.Count; i++)
  445. {
  446. ResourceManager.Save(ElfList[i]);
  447. }
  448. ElfList = new List<Transform>();
  449. }
  450. public void PlayFlashLight()
  451. {
  452. FlashLightAC.SetTrigger("Play");
  453. }
  454. public void PlayNewFlowerEffec()
  455. {
  456. NewFlowerEffectMatUnit.TweenForMatFloat();
  457. }
  458. public void StopNewFlowerEffec()
  459. {
  460. NewFlowerEffectMatUnit.TweenBacMatFloat();
  461. }
  462. public void SetShadow()
  463. {
  464. //ShadowSr.transform.SetLX((FlowerIcon.sprite.pivot.x - 0.5f)*FlowerIcon.sprite.rect.width/100);
  465. //ShadowSr.transform.SetLY((FlowerIcon.sprite.pivot.y - 0.5f)*FlowerIcon.sprite.rect.height/100);
  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.Level.ToString()));
  475. int awardMax = Mathf.CeilToInt((float)Auxiliary.FmlParse(VisitManager.AwardMaxFml, "l", Manager.Level.ToString()));
  476. coin = Mathf.CeilToInt(Mathf.Lerp(awardMin, awardMax, Random.Range(0f, 1f)));
  477. }
  478. else
  479. {
  480. coin = Mathf.CeilToInt((float)Auxiliary.FmlParse(FlowerCoinFml, "l", Mathf.Clamp(Manager.Level, 1, Mathf.Infinity).ToString()) * FlowerInfo.FlowerCoinBuff);
  481. Manager.FlowerCoin++;
  482. }
  483. Transform flyGold = ResourceManager.Get(ResourceLabel.FlyGold, Folder.Scene, false, null, GoldIcon.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(ObjectLabel.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(ObjectLabel.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.GoldPosTra], ResourceManager.Get(ObjectLabel.A_HudParent), true);
  512. ResetAward();
  513. }
  514. public void ResetAward()
  515. {
  516. GoldBk.SetActive(false);
  517. GoldBk.GetTweenSr().Pause();
  518. GoldIcon.GetTweenSr().Pause();
  519. GoldBk.GetTweenSr().InOrigin = true;
  520. GoldIcon.GetTweenSr().InOrigin = true;
  521. }
  522. public void ShowAward()
  523. {
  524. GoldBk.SetX(ChildDic[FlowerLabel.GoldBKPosLeft].position.x);
  525. GoldBk.GetComponent<SpriteRenderer>().flipX = true;
  526. GoldBk.SetY(transform.position.y + 2.5f);
  527. GoldBk.TweenReForSr();
  528. GoldIcon.TweenReForSr();
  529. GoldBk.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(ObjectLabel.G_Flower).TweenForCG();
  546. GardenManager.ShowRetrieveCard(FlowerInfo, Slot);
  547. }
  548. else if (eventData.rawPointerPress.transform == GoldBk)
  549. {
  550. Award = false;
  551. }
  552. }
  553. public void OnDisable()
  554. {
  555. FlashLightAC.transform.GetChild(0).SetActive(false);
  556. FlashLightAC.transform.GetChild(1).SetActive(false);
  557. }
  558. }