Garden.cs 13 KB

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