Flower.cs 9.7 KB

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