Flower.cs 15 KB

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