Flower.cs 9.9 KB

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