Flower.cs 16 KB

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