Garden.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public enum Direction
  7. {
  8. Left,
  9. Null,
  10. Right,
  11. }
  12. public class Garden : Regist, IDragHandler, IPointerClickHandler, IEndDragHandler, IBeginDragHandler
  13. {
  14. #region 变量
  15. public static int Page
  16. {
  17. get
  18. {
  19. return ManaGarden.PageList.Count - 1;
  20. }
  21. }
  22. public static int CurPage;
  23. public static bool Flag1;
  24. public static bool Flag2;
  25. public static float Ratio2;
  26. public static float Ratio3;
  27. public static float Ratio4;
  28. public static float SlideSensi;
  29. public static float RatioPlayer;
  30. public static Garden Instance;
  31. public static Direction Direction;
  32. public static Transform Player;
  33. public static Transform Garden2;
  34. public static Transform Garden3;
  35. public static Transform Garden4;
  36. public static Transform GardenPage;
  37. public static List<Vector3> PagePos = new List<Vector3>();
  38. public static List<Vector3> PlayerPos = new List<Vector3>();
  39. public static List<Vector3> GardenPos2 = new List<Vector3>();
  40. public static List<Vector3> GardenPos3 = new List<Vector3>();
  41. public static List<Vector3> GardenPos4 = new List<Vector3>();
  42. #endregion
  43. public static void Reload()
  44. {
  45. ResetPage();
  46. CurPage = 0;
  47. PagePos = new List<Vector3>();
  48. }
  49. public override void RegistValueA()
  50. {
  51. Flag1 = true;
  52. Flag2 = true;
  53. Instance = this;
  54. Ratio2 = 0.25f;
  55. Ratio3 = 0.5f;
  56. Ratio4 = 0.75f;
  57. SlideSensi = 0.1f;
  58. RatioPlayer = 0.75f;
  59. #region Garden
  60. #region Item
  61. ManaReso.Get("Bird0").CreateTweenSr(0, 1, 2f, false, true, Curve.EaseOutQuad, false, true);
  62. ManaReso.Get("Tree1").CreateTweenSr(0, 1, 2f, false, true, Curve.EaseOutQuad);
  63. ManaReso.Get("Tree2").CreateTweenSr(0, 1, 2f, false, true, Curve.EaseOutQuad);
  64. ManaReso.Get("Rainbow").CreateTweenSr(0, 1, 2f, false, true, Curve.EaseOutQuad);
  65. #endregion
  66. #region Cloud1
  67. Transform tra = ManaReso.Get("Cloud1");
  68. TweenRoot tween = tra.CreateTweenVec2D(new Vector3(-12, tra.position.y, tra.position.z), 50f, false, false, true, Curve.Linear);
  69. tween.Repeat = true;
  70. tween.StartForward();
  71. #endregion
  72. #region Cloud2
  73. tra = ManaReso.Get("Cloud2");
  74. tween = tra.CreateTweenVec2D(new Vector3(-12, tra.position.y, tra.position.z), 75f, false, false, true, Curve.Linear);
  75. tween.Repeat = true;
  76. tween.StartForward();
  77. #endregion
  78. #region Cloud3
  79. tra = ManaReso.Get("Cloud3");
  80. tween = tra.CreateTweenVec2D(new Vector3(-12, tra.position.y, tra.position.z), 100, false, false, true, Curve.Linear);
  81. tween.Repeat = true;
  82. tween.StartForward();
  83. #endregion
  84. #region GardenMini
  85. tween = ManaReso.Get("GardenMini").CreateTweenVec2D
  86. (
  87. ManaReso.Get("MiniPos").position,
  88. 0.5f,
  89. false,
  90. false,
  91. true,
  92. Curve.EaseOutQuad
  93. );
  94. #endregion
  95. #region GardenPage
  96. Move2D move2D = GardenPage.CreateMove2D();
  97. move2D.OnForwardStart += () =>
  98. {
  99. Flag2 = false;
  100. };
  101. move2D.OnForwardFinish += () =>
  102. {
  103. Flag2 = true;
  104. };
  105. #endregion
  106. #endregion
  107. }
  108. public override void RegistReference()
  109. {
  110. Garden2 = ManaReso.Get("Garden2");
  111. Garden3 = ManaReso.Get("Garden3");
  112. Garden4 = ManaReso.Get("Garden4");
  113. GardenPage = ManaReso.Get("GardenPage");
  114. GardenPos2.Add(ManaReso.Get("Pos21").position);
  115. GardenPos2.Add(ManaReso.Get("Pos22").position);
  116. GardenPos3.Add(ManaReso.Get("Pos31").position);
  117. GardenPos3.Add(ManaReso.Get("Pos32").position);
  118. GardenPos4.Add(ManaReso.Get("Pos41").position);
  119. GardenPos4.Add(ManaReso.Get("Pos42").position);
  120. if (!ManaTutorial.TutorialA)
  121. {
  122. Player = ManaReso.Get("Player");
  123. PlayerPos.Add(ManaPlayer.Player.ChildDic["Pos1"].position);
  124. PlayerPos.Add(ManaPlayer.Player.ChildDic["Pos2"].position);
  125. }
  126. }
  127. public override void TutorialToRegular()
  128. {
  129. Player = ManaReso.Get("Player");
  130. PlayerPos.Add(ManaPlayer.Player.ChildDic["Pos1"].position);
  131. PlayerPos.Add(ManaPlayer.Player.ChildDic["Pos2"].position);
  132. }
  133. public void OnDrag(PointerEventData eventData)
  134. {
  135. if (Flag1 && Flag2)
  136. {
  137. if (Direction == Direction.Null)
  138. {
  139. if (eventData.delta.x > 0)
  140. {
  141. Direction = Direction.Right;
  142. }
  143. else
  144. {
  145. Direction = Direction.Left;
  146. }
  147. }
  148. if (Direction == Direction.Left)
  149. {
  150. if (eventData.delta.x > 0)
  151. {
  152. Return();
  153. }
  154. else
  155. {
  156. #region MyRegion
  157. float rawDeltaX = eventData.position.x - eventData.pressPosition.x;
  158. if (CurPage < Page - 1)
  159. {
  160. float tempX = Mathf.Clamp(eventData.delta.x / 10, -2, 2);
  161. if (Page == 1)
  162. {
  163. tempX *= SlideSensi;
  164. }
  165. else
  166. {
  167. tempX *= SlideSensi / (Page - 1);
  168. }
  169. Player.Translate(Player.right * tempX * RatioPlayer, Space.World);
  170. Garden2.Translate(Garden2.right * tempX * Ratio2, Space.World);
  171. Garden3.Translate(Garden3.right * tempX * Ratio3, Space.World);
  172. Garden4.Translate(Garden4.right * tempX * Ratio4, Space.World);
  173. GardenPage.Translate(GardenPage.right * tempX, Space.World);
  174. if (rawDeltaX < -300)
  175. {
  176. NextPage();
  177. }
  178. }
  179. else
  180. {
  181. float ratio = Mathf.Abs(GardenPage.position.x - PagePos.Last(1).x)/0.7f;
  182. float sensi = Mathf.Lerp(SlideSensi, 0, ratio);
  183. float tempX = Mathf.Clamp(eventData.delta.x / 10, -2, 2) * sensi;
  184. GardenPage.Translate(GardenPage.right * tempX, Space.World);
  185. }
  186. #endregion
  187. }
  188. }
  189. else if (Direction == Direction.Right)
  190. {
  191. if (eventData.delta.x < 0)
  192. {
  193. Return();
  194. }
  195. else
  196. {
  197. #region MyRegion
  198. float rawDeltaX = eventData.position.x - eventData.pressPosition.x;
  199. if (CurPage > 0)
  200. {
  201. float tempX = Mathf.Clamp(eventData.delta.x / 10, -2, 2);
  202. if (Page == 1)
  203. {
  204. tempX *= SlideSensi;
  205. }
  206. else
  207. {
  208. tempX *= SlideSensi / (Page - 1);
  209. }
  210. Player.Translate(Player.right * tempX * RatioPlayer, Space.World);
  211. Garden2.Translate(Garden2.right * tempX * Ratio2, Space.World);
  212. Garden3.Translate(Garden3.right * tempX * Ratio3, Space.World);
  213. Garden4.Translate(Garden4.right * tempX * Ratio4, Space.World);
  214. GardenPage.Translate(GardenPage.right * tempX, Space.World);
  215. if (rawDeltaX > 300)
  216. {
  217. PrevPage();
  218. }
  219. }
  220. else
  221. {
  222. float ratio = Mathf.Abs(GardenPage.position.x - PagePos[0].x) / 0.7f;
  223. float sensi = Mathf.Lerp(SlideSensi, 0, ratio);
  224. float tempX = Mathf.Clamp(eventData.delta.x / 10, -2, 2) * sensi;
  225. GardenPage.Translate(GardenPage.right * tempX, Space.World);
  226. }
  227. #endregion
  228. }
  229. }
  230. }
  231. }
  232. public void OnEndDrag(PointerEventData eventData)
  233. {
  234. if (Flag1 && Flag2)
  235. {
  236. Return();
  237. }
  238. Flag1 = true;
  239. Direction = Direction.Null;
  240. }
  241. public void OnBeginDrag(PointerEventData eventData)
  242. {
  243. Direction = Direction.Null;
  244. }
  245. public void OnPointerClick(PointerEventData eventData)
  246. {
  247. if (!eventData.hovered.Contains(eventData.rawPointerPress))
  248. {
  249. return;
  250. }
  251. if (eventData.rawPointerPress.transform.name.Contains("Slot"))
  252. {
  253. if (ManaVisit.InVisit)
  254. {
  255. return;
  256. }
  257. Slot slot = eventData.rawPointerPress.GetComponent<Slot>();
  258. if (slot.Valid == false)
  259. {
  260. Toast.Show(1.5f, Language.GetStr("Common", "UnlockSlot"));
  261. }
  262. else
  263. {
  264. ManaReso.Get("G_Flower").TweenForCG();
  265. }
  266. }
  267. }
  268. public static void Return()
  269. {
  270. Flag1 = false;
  271. float ratio;
  272. if (Page == 1)
  273. {
  274. ratio = 0;
  275. }
  276. else
  277. {
  278. ratio = CurPage / (Page - 1f);
  279. }
  280. Vector3 playerPos = Vector3.Lerp(PlayerPos[0], PlayerPos[1], ratio);
  281. Vector3 gardenPos2 = Vector3.Lerp(GardenPos2[0], GardenPos2[1], ratio);
  282. Vector3 gardenPos3 = Vector3.Lerp(GardenPos3[0], GardenPos3[1], ratio);
  283. Vector3 gardenPos4 = Vector3.Lerp(GardenPos4[0], GardenPos4[1], ratio);
  284. Player.Move2D(playerPos, 0.5f, false, Curve.EaseOutQuad);
  285. Garden2.Move2D(gardenPos2, 0.5f, false, Curve.EaseOutQuad);
  286. Garden3.Move2D(gardenPos3, 0.5f, false, Curve.EaseOutQuad);
  287. Garden4.Move2D(gardenPos4, 0.5f, false, Curve.EaseOutQuad);
  288. GardenPage.Move2D(PagePos[CurPage], 0.5f, false, Curve.EaseOutQuad);
  289. }
  290. public static void PrevPage()
  291. {
  292. CurPage--;
  293. Flag1 = false;
  294. float ratio = CurPage / (Page - 1f);
  295. Vector3 playerPos = Vector3.Lerp(PlayerPos[0], PlayerPos[1], ratio);
  296. Vector3 gardenPos2 = Vector3.Lerp(GardenPos2[0], GardenPos2[1], ratio);
  297. Vector3 gardenPos3 = Vector3.Lerp(GardenPos3[0], GardenPos3[1], ratio);
  298. Vector3 gardenPos4 = Vector3.Lerp(GardenPos4[0], GardenPos4[1], ratio);
  299. Player.Move2D(playerPos, 0.5f, false, Curve.EaseOutQuad);
  300. Garden2.Move2D(gardenPos2, 0.5f, false, Curve.EaseOutQuad);
  301. Garden3.Move2D(gardenPos3, 0.5f, false, Curve.EaseOutQuad);
  302. Garden4.Move2D(gardenPos4, 0.5f, false, Curve.EaseOutQuad);
  303. GardenPage.Move2D(PagePos[CurPage], 0.5f, false, Curve.EaseOutQuad);
  304. }
  305. public static void NextPage()
  306. {
  307. CurPage++;
  308. Flag1 = false;
  309. float ratio = CurPage / (Page - 1f);
  310. Vector3 playerPos = Vector3.Lerp(PlayerPos[0], PlayerPos[1], ratio);
  311. Vector3 gardenPos2 = Vector3.Lerp(GardenPos2[0], GardenPos2[1], ratio);
  312. Vector3 gardenPos3 = Vector3.Lerp(GardenPos3[0], GardenPos3[1], ratio);
  313. Vector3 gardenPos4 = Vector3.Lerp(GardenPos4[0], GardenPos4[1], ratio);
  314. Player.Move2D(playerPos, 0.5f, false, Curve.EaseOutQuad);
  315. Garden2.Move2D(gardenPos2, 0.5f, false, Curve.EaseOutQuad);
  316. Garden3.Move2D(gardenPos3, 0.5f, false, Curve.EaseOutQuad);
  317. Garden4.Move2D(gardenPos4, 0.5f, false, Curve.EaseOutQuad);
  318. GardenPage.Move2D(PagePos[CurPage], 0.5f, false, Curve.EaseOutQuad);
  319. }
  320. public static void ResetPage()
  321. {
  322. Garden2.position = GardenPos2[0];
  323. Garden3.position = GardenPos3[0];
  324. Garden4.position = GardenPos4[0];
  325. GardenPage.position = PagePos[0];
  326. }
  327. }