Flower.cs 17 KB

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