Flower.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. Water,
  14. Fertilize,
  15. }
  16. public class FlowerInfo
  17. {
  18. #region 变量
  19. public bool Plant
  20. {
  21. get { return _Plant; }
  22. set
  23. {
  24. _Plant = value;
  25. if (_Plant)
  26. {
  27. Text.text = "已放置";
  28. UIPartical.Begin();
  29. }
  30. else
  31. {
  32. Text.text = "";
  33. }
  34. }
  35. }
  36. public bool Unlock
  37. {
  38. get { return _Unlock; }
  39. set
  40. {
  41. _Unlock = value;
  42. if (_Unlock)
  43. {
  44. Text.text = "";
  45. Image.material = null;
  46. Button.interactable = true;
  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. ManaData.SkillPlus += 0.1f;
  61. ManaDebug.Log(string.Format("获得<color=red>{0}</color> 收入<color=red>+10%</color>", Name));
  62. }
  63. }
  64. }
  65. public bool _Plant;
  66. public bool _Unlock;
  67. public int Id;
  68. public bool Special;
  69. public string Name;
  70. public string Description;
  71. public Slot Slot;
  72. public Text Text;
  73. public Sprite Sprite;
  74. public Image Image;
  75. public Button Button;
  76. public UIPartical UIPartical;
  77. #endregion
  78. public FlowerInfo(XmlAttributeCollection attributes)
  79. {
  80. Transform tra = ManaReso.Get("FlowerItemG", Folder.UI, false, ManaReso.Get("G_RegularGrid"), false);
  81. Dictionary<string, Transform> dic = new Dictionary<string, Transform>();
  82. Auxiliary.CompileDic(tra, dic);
  83. Text = dic["Lab"].GetComponent<Text>();
  84. Image = dic["Icon"].GetComponent<Image>();
  85. Button = dic["FlowerItemG"].GetComponent<Button>();
  86. UIPartical = dic["UIParticle System"].GetComponent<UIPartical>();
  87. Id = int.Parse(attributes[0].Value);
  88. Sprite = ManaReso.Load<Sprite>(attributes[3].Value, Folder.Garden);
  89. Name = Language.GetStr("FlowerName", "Flower" + Id);
  90. Description = attributes[2].Value;
  91. Image.sprite = Sprite;
  92. Vector2 newSize = Sprite.rect.size;
  93. newSize.x *= 0.2f;
  94. newSize.y *= 0.2f;
  95. Image.rectTransform.sizeDelta = newSize;
  96. Button.onClick.AddListener
  97. (
  98. () =>
  99. {
  100. ManaGarden.PlantFlower(this);
  101. }
  102. );
  103. }
  104. }
  105. public class Flower : ObjRoot, IPointerClickHandler
  106. {
  107. #region 变量
  108. #region MiniGame
  109. public int Phase
  110. {
  111. get { return _Phase; }
  112. set
  113. {
  114. _Phase = value;
  115. if (Phase == 1)
  116. {
  117. Phase = 0;
  118. OperateBk.SetActive(false);
  119. OperateIcon.SetActive(false);
  120. OperateOutline.SetActive(false);
  121. ManaReso.GetHudText("得分+15", ManaColor.HudText, 25, ChildDic["ScoreTra"], ManaReso.Get("D_Status"), true);
  122. ManaMiniGame.Score += 15;
  123. }
  124. }
  125. }
  126. private int _Phase;
  127. private OpType OpType;
  128. private SpriteRenderer FlowerIcon;
  129. private SpriteRenderer OperateBk;
  130. private SpriteRenderer OperateIcon;
  131. private SpriteRenderer OperateOutline;
  132. #endregion
  133. public bool Award
  134. {
  135. get { return _Award; }
  136. set
  137. {
  138. _Award = value;
  139. if (_Award)
  140. {
  141. ShowAward();
  142. }
  143. else
  144. {
  145. GetAward();
  146. }
  147. }
  148. }
  149. public FlowerInfo FlowerInfo
  150. {
  151. get { return _FlowerInfo; }
  152. set
  153. {
  154. _FlowerInfo = value;
  155. FlowerIcon.sprite = FlowerInfo.Sprite;
  156. }
  157. }
  158. public bool _Award;
  159. private FlowerInfo _FlowerInfo;
  160. public int Id;
  161. public Slot Slot;
  162. public Vector3 LocalPos;
  163. public Animator Animator;
  164. public Transform GoldBk;
  165. public Transform GoldIcon;
  166. private Dictionary<string, Transform> ChildDic;
  167. #endregion
  168. private void Awake()
  169. {
  170. ChildDic = new Dictionary<string, Transform>();
  171. Auxiliary.CompileDic(transform, ChildDic);
  172. GoldBk = ChildDic["GoldBk"];
  173. GoldIcon = ChildDic["GoldIcon"];
  174. Animator = ChildDic["FlashLight"].GetComponent<Animator>();
  175. FlowerIcon = ChildDic["FlowerIcon"].GetComponent<SpriteRenderer>();
  176. OperateBk = ChildDic["OperateBk"].GetComponent<SpriteRenderer>();
  177. OperateIcon = ChildDic["OperateIcon"].GetComponent<SpriteRenderer>();
  178. OperateOutline = ChildDic["OperateOutline"].GetComponent<SpriteRenderer>();
  179. Tween tween = FlowerIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  180. tween.OnForwardFinish += () =>
  181. {
  182. FlowerIcon.TweenBacSr();
  183. };
  184. tween = OperateIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  185. tween.OnForwardFinish += () =>
  186. {
  187. OperateIcon.TweenBacSr();
  188. };
  189. tween = OperateBk.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  190. tween.OnForwardFinish += () =>
  191. {
  192. OperateBk.TweenBacSr();
  193. };
  194. OperateBk.CreateTweenScale(OperateBk.transform.localScale + new Vector3(0.1f, 0.1f, 0.1f), 0.25f, false, true, Curve.EaseOutQuad);
  195. }
  196. #region MiniGame
  197. public void GameOver()
  198. {
  199. ChildDic["MiniGame"].SetActive(false);
  200. OperateBk.TweenBacScale();
  201. }
  202. public void GameBegin()
  203. {
  204. ChildDic["MiniGame"].SetActive(true);
  205. OperateIcon.SetActive(false);
  206. }
  207. public void SetFirstOp()
  208. {
  209. OperateBk.TweenForScale();
  210. OperateIcon.SetAlpha(1);
  211. OperateOutline.SetAlpha(1);
  212. OperateIcon.SetActive(true);
  213. OperateOutline.SetActive(true);
  214. }
  215. public void SetSecondOp()
  216. {
  217. OperateIcon.SetAlpha(1);
  218. OperateOutline.SetAlpha(1);
  219. OperateIcon.SetActive(true);
  220. OperateOutline.SetActive(false);
  221. }
  222. public void SetThirdOp()
  223. {
  224. OperateIcon.SetAlpha(0.5f);
  225. OperateOutline.SetAlpha(0.5f);
  226. OperateIcon.SetActive(true);
  227. OperateOutline.SetActive(false);
  228. }
  229. public void CreateOp(int sequence)
  230. {
  231. float random = Random.Range(0f, 1f);
  232. OperateBk.SetActive(true);
  233. if (random <= 0.3333333f)
  234. {
  235. OpType = OpType.Rip;
  236. OperateIcon.sprite = ManaReso.Load<Sprite>("Rip", Folder.UI);
  237. }
  238. else if (random <= 0.6666666f)
  239. {
  240. OpType = OpType.Water;
  241. OperateIcon.sprite = ManaReso.Load<Sprite>("Water", Folder.UI);
  242. }
  243. else
  244. {
  245. OpType = OpType.Fertilize;
  246. OperateIcon.sprite = ManaReso.Load<Sprite>("Fertilize", Folder.UI);
  247. }
  248. if (sequence == 0)
  249. {
  250. SetFirstOp();
  251. }
  252. else if (sequence == 1)
  253. {
  254. SetSecondOp();
  255. }
  256. else
  257. {
  258. SetThirdOp();
  259. }
  260. }
  261. public bool Operate(OpType opType)
  262. {
  263. if (opType != OpType) //错误的操作
  264. {
  265. OperateBk.TweenForSr();
  266. FlowerIcon.TweenForSr();
  267. OperateIcon.TweenForSr();
  268. MoveVec move = OperateIcon.CreateMoveVec();
  269. move.StartMove(OperateIcon.transform.position + new Vector3(0.2f, 0), 0.1f, Curve.Linear);
  270. move.OnFinish = () =>
  271. {
  272. move.StartMove(OperateIcon.transform.position + new Vector3(-0.4f, 0), 0.1f, Curve.Linear);
  273. move.OnFinish = () =>
  274. {
  275. move.StartMove(OperateIcon.transform.position + new Vector3(0.2f, 0), 0.1f, Curve.Linear);
  276. move.OnFinish = () =>
  277. {
  278. move.StartMove(OperateIcon.transform.position + new Vector3(0.1f, 0), 0.1f, Curve.Linear);
  279. move.OnFinish = () =>
  280. {
  281. move.StartMove(OperateIcon.transform.position + new Vector3(-0.2f, 0), 0.1f, Curve.Linear);
  282. move.OnFinish = () =>
  283. {
  284. move.StartMove(OperateIcon.transform.position + new Vector3(0.1f, 0), 0.1f, Curve.Linear);
  285. move.OnFinish = () =>
  286. {
  287. };
  288. };
  289. };
  290. };
  291. };
  292. };
  293. return false;
  294. }
  295. else //操作正确 植物升级
  296. {
  297. Phase++;
  298. PlayAnim();
  299. OperateBk.TweenBacScale();
  300. return true;
  301. }
  302. }
  303. #endregion
  304. public void PlayAnim()
  305. {
  306. Animator.SetTrigger("Play");
  307. }
  308. public void GetAward()
  309. {
  310. PlayAnim();
  311. ManaGarden.Award = true;
  312. int coin = Mathf.CeilToInt(ManaData.CoinPerson*Random.Range(0.05f, 0.08f)*ManaData.SkillPlus);
  313. ManaData.Coin += coin;
  314. ManaReso.GetHudText("+" + coin, ManaColor.HudText, 25, ChildDic["GoldTra"], ManaReso.Get("A_HudParent"), true);
  315. GoldBk.SetActive(false);
  316. }
  317. public void ShowAward()
  318. {
  319. GoldBk.CreateTweenSr(new Color(1, 1, 1, 0), new Color(1, 1, 1, 1), 1f, true, true, Curve.EaseOutQuad);
  320. GoldIcon.CreateTweenSr(new Color(1, 1, 1, 0), new Color(1, 1, 1, 1), 1f, true, true, Curve.EaseOutQuad);
  321. GoldBk.SetY(transform.position.y + 2.5f);
  322. GoldBk.TweenForSr();
  323. GoldIcon.TweenForSr();
  324. GoldBk.MoveVec
  325. (
  326. GoldBk.transform.position + new Vector3(0, 0.5f, 0),
  327. 1f,
  328. Curve.EaseOutQuad
  329. );
  330. }
  331. public void OnPointerClick(PointerEventData eventData)
  332. {
  333. if (eventData.rawPointerPress.transform == transform)
  334. {
  335. ManaReso.Get("G_Flower").TweenForCG();
  336. ManaGarden.ShowRetrieveCard(FlowerInfo);
  337. }
  338. else if (eventData.rawPointerPress.transform == GoldBk)
  339. {
  340. Award = false;
  341. }
  342. }
  343. }