Flower.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. Button.interactable = true;
  48. if (Special)
  49. {
  50. if (ManaGarden.MyFlowerSpec == 0)
  51. {
  52. ManaReso.Get("G_Regular").TweenForVec();
  53. ManaReso.SetActive("G_Special", true);
  54. }
  55. ManaGarden.MyFlowerSpec++;
  56. }
  57. else
  58. {
  59. ManaGarden.MyFlowerRegu++;
  60. }
  61. ManaData.SkillPlus += 0.1f;
  62. ManaDebug.Log(string.Format("获得<color=red>{0}</color> 收入<color=red>+10%</color>", Name));
  63. }
  64. }
  65. }
  66. public string Name
  67. {
  68. get
  69. {
  70. return Language.GetStr("FlowerName", ID);
  71. }
  72. }
  73. public string ID
  74. {
  75. get
  76. {
  77. return "Flower" + ID_;
  78. }
  79. }
  80. public string Description
  81. {
  82. get { return Language.GetStr("FlowerDesc", ID); }
  83. }
  84. public Sprite Icon
  85. {
  86. get { return ManaReso.Load<Sprite>(Icon_, Folder.Scene); }
  87. }
  88. public int ID_;
  89. public bool _Plant;
  90. public bool _Unlock;
  91. public string Icon_;
  92. public bool Special;
  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. #endregion
  100. public FlowerInfo(XmlAttributeCollection attribute)
  101. {
  102. FlowerItem = ManaReso.Get<ObjRoot>("FlowerItem", Folder.UI, false, ManaReso.Get("G_RegularGrid"), false, ObjType.FlowerItem);
  103. Dictionary<string, Transform> dic = new Dictionary<string, Transform>();
  104. Auxiliary.CompileDic(FlowerItem, dic);
  105. Text = dic["Lab"].GetComponent<Text>();
  106. Image = dic["Icon"].GetComponent<Image>();
  107. Button = dic["FlowerItem"].GetComponent<Button>();
  108. UIPartical = dic["UIParticle System"].GetComponent<UIPartical>();
  109. ID_ = int.Parse(attribute[0].Value);
  110. Icon_ = attribute[3].Value;
  111. Image.sprite = Icon;
  112. Vector2 newSize = Icon.rect.size;
  113. newSize.x *= 0.2f;
  114. newSize.y *= 0.2f;
  115. Image.rectTransform.sizeDelta = newSize;
  116. Button.onClick.AddListener
  117. (
  118. () =>
  119. {
  120. ManaGarden.PlantFlower(this);
  121. }
  122. );
  123. }
  124. }
  125. public class Flower : ObjRoot, IPointerClickHandler
  126. {
  127. #region 变量
  128. #region MiniGame
  129. public OpType OpType
  130. {
  131. get { return OpType_; }
  132. set
  133. {
  134. OpType_ = value;
  135. if (OpType_ == OpType.Rip)
  136. {
  137. OperateIcon.sprite = RipSprite;
  138. }
  139. else if (OpType_ == OpType.Water)
  140. {
  141. OperateIcon.sprite = WaterSprite;
  142. }
  143. else if (OpType_ == OpType.Fertilize)
  144. {
  145. OperateIcon.sprite = FertilizeSprite;
  146. }
  147. }
  148. }
  149. public OpType OpType_;
  150. public SpriteRenderer FlowerIcon;
  151. public SpriteRenderer OperateBk;
  152. public SpriteRenderer OperateIcon;
  153. public SpriteRenderer OperateOutline;
  154. #endregion
  155. public bool Award
  156. {
  157. get { return Award_; }
  158. set
  159. {
  160. Award_ = value;
  161. if (Award_)
  162. {
  163. ShowAward();
  164. }
  165. else
  166. {
  167. GetAward();
  168. }
  169. }
  170. }
  171. public Sprite RipSprite
  172. {
  173. get
  174. {
  175. return ManaReso.Load<Sprite>("Rip", Folder.UI);
  176. }
  177. }
  178. public Sprite WaterSprite
  179. {
  180. get
  181. {
  182. return ManaReso.Load<Sprite>("Water", Folder.UI);
  183. }
  184. }
  185. public Sprite FertilizeSprite
  186. {
  187. get
  188. {
  189. return ManaReso.Load<Sprite>("Fertilize", Folder.UI);
  190. }
  191. }
  192. public FlowerInfo FlowerInfo
  193. {
  194. get { return FlowerInfo_; }
  195. set
  196. {
  197. FlowerInfo_ = value;
  198. ID = FlowerInfo.ID_;
  199. FlowerIcon.sprite = FlowerInfo.Icon;
  200. }
  201. }
  202. public bool Award_;
  203. private FlowerInfo FlowerInfo_;
  204. public int ID;
  205. public static string CoinFml;
  206. public Slot Slot;
  207. public Animator ParticleAC;
  208. public Transform GoldBk;
  209. public Transform GoldIcon;
  210. public List<Transform> ElfList = new List<Transform>();
  211. private Dictionary<string, Transform> ChildDic = new Dictionary<string, Transform>();
  212. #endregion
  213. public override void RegistImmed()
  214. {
  215. if (RegistFlag)
  216. {
  217. return;
  218. }
  219. else
  220. {
  221. RegistFlag = true;
  222. }
  223. enabled = true;
  224. Auxiliary.CompileDic(transform, ChildDic);
  225. GoldBk = ChildDic["GoldBk"];
  226. GoldIcon = ChildDic["GoldIcon"];
  227. ParticleAC = ChildDic["FlashLight"].GetComponent<Animator>();
  228. FlowerIcon = ChildDic["FlowerIcon"].GetComponent<SpriteRenderer>();
  229. OperateBk = ChildDic["OperateBk"].GetComponent<SpriteRenderer>();
  230. OperateIcon = ChildDic["OperateIcon"].GetComponent<SpriteRenderer>();
  231. OperateOutline = ChildDic["OperateOutline"].GetComponent<SpriteRenderer>();
  232. Tween tween = FlowerIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  233. tween.OnForwardFinish += () =>
  234. {
  235. FlowerIcon.TweenBacSr();
  236. };
  237. tween = OperateIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  238. tween.OnForwardFinish += () =>
  239. {
  240. OperateIcon.TweenBacSr();
  241. };
  242. tween = OperateBk.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  243. tween.OnForwardFinish += () =>
  244. {
  245. OperateBk.TweenBacSr();
  246. };
  247. tween = OperateBk.CreateTweenScale(OperateBk.transform.localScale + new Vector3(0.1f, 0.1f, 0.1f), 0.25f, false, true, Curve.EaseOutQuad);
  248. tween.OnBackwardFinish += () =>
  249. {
  250. if (OpType != OpType.Null)
  251. {
  252. OperateBk.SetActive(true);
  253. }
  254. };
  255. }
  256. #region MiniGame
  257. public void GameOver()
  258. {
  259. OpType = OpType.Null;
  260. ChildDic["MiniGame"].SetActive(false);
  261. OperateBk.TweenBacScale();
  262. }
  263. public void GameBegin()
  264. {
  265. ChildDic["MiniGame"].SetActive(true);
  266. OperateIcon.SetActive(false);
  267. }
  268. public void FirstOp()
  269. {
  270. OperateBk.TweenForScale();
  271. OperateIcon.SetAlpha(1);
  272. OperateOutline.SetAlpha(1);
  273. OperateIcon.SetActive(true);
  274. OperateOutline.SetActive(true);
  275. }
  276. public void SecondOp()
  277. {
  278. OperateIcon.SetAlpha(1);
  279. OperateOutline.SetAlpha(1);
  280. OperateIcon.SetActive(true);
  281. OperateOutline.SetActive(false);
  282. }
  283. public void ThirdOp()
  284. {
  285. OperateIcon.SetAlpha(0.5f);
  286. OperateOutline.SetAlpha(0.5f);
  287. OperateIcon.SetActive(true);
  288. OperateOutline.SetActive(false);
  289. }
  290. public void CreateOp(int sequence)
  291. {
  292. float random = Random.Range(0f, 1f);
  293. OperateBk.SetActive(true);
  294. if (random <= 0.3333333f)
  295. {
  296. OpType = OpType.Rip;
  297. }
  298. else if (random <= 0.6666666f)
  299. {
  300. OpType = OpType.Water;
  301. }
  302. else
  303. {
  304. OpType = OpType.Fertilize;
  305. }
  306. if (sequence == 0)
  307. {
  308. FirstOp();
  309. }
  310. else if (sequence == 1)
  311. {
  312. SecondOp();
  313. }
  314. else
  315. {
  316. ThirdOp();
  317. }
  318. }
  319. public void CreateOp(int sequence, OpType opType)
  320. {
  321. OpType = opType;
  322. OperateBk.SetActive(true);
  323. if (sequence == 0)
  324. {
  325. FirstOp();
  326. }
  327. else if (sequence == 1)
  328. {
  329. SecondOp();
  330. }
  331. else
  332. {
  333. ThirdOp();
  334. }
  335. }
  336. public bool Operate(OpType opType)
  337. {
  338. if (opType != OpType) //操作错误
  339. {
  340. OperateBk.TweenForSr();
  341. FlowerIcon.TweenForSr();
  342. OperateIcon.TweenForSr();
  343. OperateIcon.Shake(0.5f, 3, new Vector3(0.2f, 0, 0), Curve.EaseOutQuad);
  344. return false;
  345. }
  346. else //操作正确
  347. {
  348. ManaReso.GetHudText("+15", Color.white, 25, ChildDic["ScorePosTra"], ManaReso.Get("D_Status"), true);
  349. ManaMiniGame.Score += 15;
  350. PlayParticle();
  351. OpType = OpType.Null;
  352. OperateBk.TweenBacScale();
  353. OperateBk.SetActive(false);
  354. OperateIcon.SetActive(false);
  355. OperateOutline.SetActive(false);
  356. return true;
  357. }
  358. }
  359. #endregion
  360. public void PlayAnim(ObjType obj, float xMin = -0.75f, float xMax = 0.75f, float yMin = 0, float yMax = 0.75f)
  361. {
  362. ElfList.Add(ManaReso.GetElf(this, new Vector4(xMin, xMax, yMin, yMax), obj));
  363. }
  364. public void PlayParticle()
  365. {
  366. ParticleAC.SetTrigger("Play");
  367. }
  368. public void GetAward()
  369. {
  370. ManaAudio.PlayClip(Clip.BubbleClip);
  371. PlayParticle();
  372. ManaGarden.AwardValid = true;
  373. int coin = Mathf.CeilToInt((float)Auxiliary.FmlParse(CoinFml, "l", Mathf.Clamp(ManaData.Level, 1, Mathf.Infinity).ToString()));
  374. ManaData.Coin += coin;
  375. ManaData.FlowerCoin++;
  376. ManaReso.GetHudText("+" + coin, Color.white, 25, ChildDic["GoldPosTra"], ManaReso.Get("A_HudParent"), true);
  377. GoldBk.SetActive(false);
  378. }
  379. public void ShowAward()
  380. {
  381. GoldBk.CreateTweenSr(new Color(1, 1, 1, 0), new Color(1, 1, 1, 1), 1f, true, true, Curve.EaseOutQuad);
  382. GoldIcon.CreateTweenSr(new Color(1, 1, 1, 0), new Color(1, 1, 1, 1), 1f, true, true, Curve.EaseOutQuad);
  383. GoldBk.SetY(transform.position.y + 2.5f);
  384. GoldBk.TweenForSr();
  385. GoldIcon.TweenForSr();
  386. GoldBk.MoveOffset2D
  387. (
  388. new Vector3(0, 0.5f, 0),
  389. 1f,
  390. true,
  391. Curve.EaseOutQuad
  392. );
  393. }
  394. public void OnPointerClick(PointerEventData eventData)
  395. {
  396. if (eventData.rawPointerPress.transform == transform)
  397. {
  398. ManaReso.Get("G_Flower").TweenForCG();
  399. ManaGarden.ShowFlowerCard(FlowerInfo);
  400. }
  401. else if (eventData.rawPointerPress.transform == GoldBk)
  402. {
  403. Award = false;
  404. }
  405. }
  406. }