Flower.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. //Animator.Play("FlashLight");
  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 Animator Animator;
  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. //Animator = dic["FlashLight"].GetComponent<Animator>();
  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. OperateIcon.SetActive(false);
  119. ManaReso.GetHudText("得分+15", Color.red, 18, ChildDic["ScoreTra"], ManaReso.Get("D_Status"), true);
  120. ManaMiniGame.Score += 15;
  121. }
  122. }
  123. }
  124. private int _Phase;
  125. private OpType OpType;
  126. private SpriteRenderer FlowerIcon;
  127. private SpriteRenderer OperateIcon;
  128. #endregion
  129. public bool Award
  130. {
  131. get { return _Award; }
  132. set
  133. {
  134. _Award = value;
  135. if (_Award)
  136. {
  137. ShowAward();
  138. }
  139. else
  140. {
  141. GetAward();
  142. }
  143. }
  144. }
  145. public FlowerInfo FlowerInfo
  146. {
  147. get { return _FlowerInfo; }
  148. set
  149. {
  150. _FlowerInfo = value;
  151. FlowerIcon.sprite = FlowerInfo.Sprite;
  152. }
  153. }
  154. public bool _Award;
  155. private FlowerInfo _FlowerInfo;
  156. public int Id;
  157. public Slot Slot;
  158. public Vector3 LocalPos;
  159. public Animator Animator;
  160. public Transform GoldBk;
  161. public Transform GoldIcon;
  162. private Dictionary<string, Transform> ChildDic;
  163. #endregion
  164. private void Awake()
  165. {
  166. ChildDic = new Dictionary<string, Transform>();
  167. Auxiliary.CompileDic(transform, ChildDic);
  168. GoldBk = ChildDic["GoldBk"];
  169. GoldIcon = ChildDic["GoldIcon"];
  170. Animator = ChildDic["FlashLight"].GetComponent<Animator>();
  171. FlowerIcon = ChildDic["FlowerIcon"].GetComponent<SpriteRenderer>();
  172. OperateIcon = ChildDic["OperateIcon"].GetComponent<SpriteRenderer>();
  173. Tween tween = FlowerIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  174. tween.OnForwardFinish += () =>
  175. {
  176. FlowerIcon.TweenBacSr();
  177. };
  178. tween = OperateIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  179. tween.OnForwardFinish += () =>
  180. {
  181. OperateIcon.TweenBacSr();
  182. };
  183. }
  184. #region MiniGame
  185. public void GameOver()
  186. {
  187. ChildDic["MiniGame"].SetActive(false);
  188. }
  189. public void GameBegin()
  190. {
  191. ChildDic["MiniGame"].SetActive(true);
  192. OperateIcon.SetActive(false);
  193. }
  194. public void SetFirstOp()
  195. {
  196. OperateIcon.SetAlpha(1);
  197. OperateIcon.material.shader = Shader.Find("DashGame/HighLight");
  198. OperateIcon.SetActive(true);
  199. }
  200. public void SetSecondOp()
  201. {
  202. OperateIcon.SetAlpha(1);
  203. OperateIcon.material.shader = Shader.Find("Sprites/Default");
  204. OperateIcon.SetActive(true);
  205. }
  206. public void SetThirdOp()
  207. {
  208. OperateIcon.SetAlpha(0.5f);
  209. OperateIcon.material.shader = Shader.Find("Sprites/Default");
  210. OperateIcon.SetActive(true);
  211. }
  212. public void CreateOp(int sequence)
  213. {
  214. float random = Random.Range(0f, 1f);
  215. if (random <= 0.3333333f)
  216. {
  217. OpType = OpType.Rip;
  218. OperateIcon.sprite = ManaReso.Load<Sprite>("Rip", Folder.UI);
  219. }
  220. else if (random <= 0.6666666f)
  221. {
  222. OpType = OpType.Water;
  223. OperateIcon.sprite = ManaReso.Load<Sprite>("Water", Folder.UI);
  224. }
  225. else
  226. {
  227. OpType = OpType.Fertilize;
  228. OperateIcon.sprite = ManaReso.Load<Sprite>("Fertilize", Folder.UI);
  229. }
  230. if (sequence == 0)
  231. {
  232. SetFirstOp();
  233. }
  234. else if (sequence == 1)
  235. {
  236. SetSecondOp();
  237. }
  238. else
  239. {
  240. SetThirdOp();
  241. }
  242. }
  243. public bool Operate(OpType opType)
  244. {
  245. if (opType != OpType) //错误的操作
  246. {
  247. FlowerIcon.TweenForSr();
  248. OperateIcon.TweenForSr();
  249. MoveVec move = OperateIcon.CreateMoveVec();
  250. move.StartMove(OperateIcon.transform.position + new Vector3(0.2f, 0), 0.1f, Curve.Linear);
  251. move.OnFinish = () =>
  252. {
  253. move.StartMove(OperateIcon.transform.position + new Vector3(-0.4f, 0), 0.1f, Curve.Linear);
  254. move.OnFinish = () =>
  255. {
  256. move.StartMove(OperateIcon.transform.position + new Vector3(0.2f, 0), 0.1f, Curve.Linear);
  257. move.OnFinish = () =>
  258. {
  259. move.StartMove(OperateIcon.transform.position + new Vector3(0.1f, 0), 0.1f, Curve.Linear);
  260. move.OnFinish = () =>
  261. {
  262. move.StartMove(OperateIcon.transform.position + new Vector3(-0.2f, 0), 0.1f, Curve.Linear);
  263. move.OnFinish = () =>
  264. {
  265. move.StartMove(OperateIcon.transform.position + new Vector3(0.1f, 0), 0.1f, Curve.Linear);
  266. move.OnFinish = () =>
  267. {
  268. };
  269. };
  270. };
  271. };
  272. };
  273. };
  274. return false;
  275. }
  276. else //操作正确 植物升级
  277. {
  278. Phase++;
  279. PlayAnim();
  280. OperateIcon.SetActive(false);
  281. return true;
  282. }
  283. }
  284. #endregion
  285. public void PlayAnim()
  286. {
  287. Animator.SetTrigger("Play");
  288. }
  289. public void GetAward()
  290. {
  291. PlayAnim();
  292. ManaGarden.Award = true;
  293. int coin = Mathf.CeilToInt(ManaData.CoinPerson*Random.Range(0.05f, 0.08f)*ManaData.SkillPlus);
  294. ManaData.Coin += coin;
  295. ManaReso.GetHudText("+" + coin, Color.red, 18, ChildDic["GoldTra"], ManaReso.Get("A_HudParent"), true);
  296. GoldBk.SetActive(false);
  297. }
  298. public void ShowAward()
  299. {
  300. GoldBk.CreateTweenSr(new Color(1, 1, 1, 0), new Color(1, 1, 1, 1), 1f, true, true, Curve.EaseOutQuad);
  301. GoldIcon.CreateTweenSr(new Color(1, 1, 1, 0), new Color(1, 1, 1, 1), 1f, true, true, Curve.EaseOutQuad);
  302. GoldBk.SetY(transform.position.y + 2.5f);
  303. GoldBk.TweenForSr();
  304. GoldIcon.TweenForSr();
  305. GoldBk.MoveVec
  306. (
  307. GoldBk.transform.position + new Vector3(0, 0.5f, 0),
  308. 1f,
  309. Curve.EaseOutQuad
  310. );
  311. }
  312. public void OnPointerClick(PointerEventData eventData)
  313. {
  314. if (eventData.rawPointerPress.transform == transform)
  315. {
  316. ManaReso.Get("G_Flower").TweenForCG();
  317. ManaGarden.ShowRetrieveCard(FlowerInfo);
  318. }
  319. else if (eventData.rawPointerPress.transform == GoldBk)
  320. {
  321. Award = false;
  322. }
  323. }
  324. }