Flower.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 Slot Slot;
  206. public Animator ParticleAC;
  207. public Transform GoldBk;
  208. public Transform GoldIcon;
  209. public List<Transform> ElfList = new List<Transform>();
  210. private Dictionary<string, Transform> ChildDic = new Dictionary<string, Transform>();
  211. #endregion
  212. public override void RegistImmed()
  213. {
  214. if (RegistFlag)
  215. {
  216. return;
  217. }
  218. else
  219. {
  220. RegistFlag = true;
  221. }
  222. enabled = true;
  223. Auxiliary.CompileDic(transform, ChildDic);
  224. GoldBk = ChildDic["GoldBk"];
  225. GoldIcon = ChildDic["GoldIcon"];
  226. ParticleAC = ChildDic["FlashLight"].GetComponent<Animator>();
  227. FlowerIcon = ChildDic["FlowerIcon"].GetComponent<SpriteRenderer>();
  228. OperateBk = ChildDic["OperateBk"].GetComponent<SpriteRenderer>();
  229. OperateIcon = ChildDic["OperateIcon"].GetComponent<SpriteRenderer>();
  230. OperateOutline = ChildDic["OperateOutline"].GetComponent<SpriteRenderer>();
  231. Tween tween = FlowerIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  232. tween.OnForwardFinish += () =>
  233. {
  234. FlowerIcon.TweenBacSr();
  235. };
  236. tween = OperateIcon.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  237. tween.OnForwardFinish += () =>
  238. {
  239. OperateIcon.TweenBacSr();
  240. };
  241. tween = OperateBk.CreateTweenSr(new Color(1, 1, 1), new Color(1, 0.5f, 0.5f), 0.2f, true, true, Curve.EaseOutQuad);
  242. tween.OnForwardFinish += () =>
  243. {
  244. OperateBk.TweenBacSr();
  245. };
  246. tween = OperateBk.CreateTweenScale(OperateBk.transform.localScale + new Vector3(0.1f, 0.1f, 0.1f), 0.25f, false, true, Curve.EaseOutQuad);
  247. tween.OnBackwardFinish += () =>
  248. {
  249. if (OpType != OpType.Null)
  250. {
  251. OperateBk.SetActive(true);
  252. }
  253. };
  254. }
  255. #region MiniGame
  256. public void GameOver()
  257. {
  258. OpType = OpType.Null;
  259. ChildDic["MiniGame"].SetActive(false);
  260. OperateBk.TweenBacScale();
  261. }
  262. public void GameBegin()
  263. {
  264. ChildDic["MiniGame"].SetActive(true);
  265. OperateIcon.SetActive(false);
  266. }
  267. public void FirstOp()
  268. {
  269. OperateBk.TweenForScale();
  270. OperateIcon.SetAlpha(1);
  271. OperateOutline.SetAlpha(1);
  272. OperateIcon.SetActive(true);
  273. OperateOutline.SetActive(true);
  274. }
  275. public void SecondOp()
  276. {
  277. OperateIcon.SetAlpha(1);
  278. OperateOutline.SetAlpha(1);
  279. OperateIcon.SetActive(true);
  280. OperateOutline.SetActive(false);
  281. }
  282. public void ThirdOp()
  283. {
  284. OperateIcon.SetAlpha(0.5f);
  285. OperateOutline.SetAlpha(0.5f);
  286. OperateIcon.SetActive(true);
  287. OperateOutline.SetActive(false);
  288. }
  289. public void CreateOp(int sequence)
  290. {
  291. float random = Random.Range(0f, 1f);
  292. OperateBk.SetActive(true);
  293. if (random <= 0.3333333f)
  294. {
  295. OpType = OpType.Rip;
  296. }
  297. else if (random <= 0.6666666f)
  298. {
  299. OpType = OpType.Water;
  300. }
  301. else
  302. {
  303. OpType = OpType.Fertilize;
  304. }
  305. if (sequence == 0)
  306. {
  307. FirstOp();
  308. }
  309. else if (sequence == 1)
  310. {
  311. SecondOp();
  312. }
  313. else
  314. {
  315. ThirdOp();
  316. }
  317. }
  318. public void CreateOp(int sequence, OpType opType)
  319. {
  320. OpType = opType;
  321. OperateBk.SetActive(true);
  322. if (sequence == 0)
  323. {
  324. FirstOp();
  325. }
  326. else if (sequence == 1)
  327. {
  328. SecondOp();
  329. }
  330. else
  331. {
  332. ThirdOp();
  333. }
  334. }
  335. public bool Operate(OpType opType)
  336. {
  337. if (opType != OpType) //操作错误
  338. {
  339. OperateBk.TweenForSr();
  340. FlowerIcon.TweenForSr();
  341. OperateIcon.TweenForSr();
  342. OperateIcon.Shake(0.5f, 3, new Vector3(0.2f, 0, 0), Curve.EaseOutQuad);
  343. return false;
  344. }
  345. else //操作正确
  346. {
  347. ManaReso.GetHudText("得分+15", Color.white, 25, ChildDic["ScorePosTra"], ManaReso.Get("D_Status"), true);
  348. ManaMiniGame.Score += 15;
  349. PlayParticle();
  350. OpType = OpType.Null;
  351. OperateBk.TweenBacScale();
  352. OperateBk.SetActive(false);
  353. OperateIcon.SetActive(false);
  354. OperateOutline.SetActive(false);
  355. return true;
  356. }
  357. }
  358. #endregion
  359. public void PlayAnim(ObjType obj, float xMin = -0.75f, float xMax = 0.75f, float yMin = 0, float yMax = 0.75f)
  360. {
  361. ElfList.Add(ManaReso.GetElf(this, new Vector4(xMin, xMax, yMin, yMax), obj));
  362. }
  363. public void PlayParticle()
  364. {
  365. ParticleAC.SetTrigger("Play");
  366. }
  367. public void GetAward()
  368. {
  369. PlayParticle();
  370. ManaGarden.AwardValid = true;
  371. int coin = Mathf.CeilToInt(ManaData.CoinPerson*Random.Range(0.05f, 0.08f)*ManaData.SkillPlus);
  372. ManaData.Coin += coin;
  373. ManaData.FlowerCoin++;
  374. ManaReso.GetHudText("+" + coin, Color.white, 25, ChildDic["GoldPosTra"], ManaReso.Get("A_HudParent"), true);
  375. GoldBk.SetActive(false);
  376. }
  377. public void ShowAward()
  378. {
  379. GoldBk.CreateTweenSr(new Color(1, 1, 1, 0), new Color(1, 1, 1, 1), 1f, true, true, Curve.EaseOutQuad);
  380. GoldIcon.CreateTweenSr(new Color(1, 1, 1, 0), new Color(1, 1, 1, 1), 1f, true, true, Curve.EaseOutQuad);
  381. GoldBk.SetY(transform.position.y + 2.5f);
  382. GoldBk.TweenForSr();
  383. GoldIcon.TweenForSr();
  384. GoldBk.MoveOffset2D
  385. (
  386. new Vector3(0, 0.5f, 0),
  387. 1f,
  388. true,
  389. Curve.EaseOutQuad
  390. );
  391. }
  392. public void OnPointerClick(PointerEventData eventData)
  393. {
  394. if (eventData.rawPointerPress.transform == transform)
  395. {
  396. ManaReso.Get("G_Flower").TweenForCG();
  397. ManaGarden.ShowFlowerCard(FlowerInfo);
  398. }
  399. else if (eventData.rawPointerPress.transform == GoldBk)
  400. {
  401. Award = false;
  402. }
  403. }
  404. }