Flower.cs 15 KB

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