using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections; using System.Collections.Generic; public enum Direction { Left, Null, Right, } public class Garden : Regist, IDragHandler, IPointerClickHandler, IEndDragHandler, IBeginDragHandler { #region 变量 public static int Page { get { return GardenManager.PageList.Count - 1; } } public static int CurPage; public static bool Flag1 = true; public static bool Flag2 = true; public static float SlideSensi = 0.1f; public static float BK2MoveRatio = 0.25f; public static float BK3MoveRatio = 0.5f; public static float BK4MoveRatio = 0.75f; public static float PlayerMoveRatio = 0.75f; public static Garden Instance; public static Direction Direction; public static Transform Player; public static Transform GardenBK2; public static Transform GardenBK3; public static Transform GardenBK4; public static Transform GardenPage; public static List PagePos = new List(); public static List PlayerPos = new List(); public static List GardenBK2Pos = new List(); public static List GardenBK3Pos = new List(); public static List GardenBK4Pos = new List(); #endregion public override bool InitAtOnce() { if (base.InitAtOnce()) { return true; } Instance = this; #region Item ResourceManager.Get("Bird0").CreateTweenSr(0, 1, 2f, false, true, Curve.EaseOutQuad, false, true); ResourceManager.Get("Tree1").CreateTweenSr(0, 1, 2f, false, true, Curve.EaseOutQuad); ResourceManager.Get("Tree2").CreateTweenSr(0, 1, 2f, false, true, Curve.EaseOutQuad); ResourceManager.Get("Rainbow").CreateTweenSr(0, 1, 2f, false, true, Curve.EaseOutQuad); #endregion #region Cloud1 Transform tra = ResourceManager.Get("Cloud1"); TweenRoot tween = tra.CreateTweenVec2D(new Vector3(-14, tra.localPosition.y, tra.localPosition.z), 85f, true, false, true, Curve.Linear); tween.Repeat = true; tween.StartForward(); #endregion #region Cloud2 tra = ResourceManager.Get("Cloud2"); tween = tra.CreateTweenVec2D(new Vector3(-14, tra.localPosition.y, tra.localPosition.z), 127.5f, true, false, true, Curve.Linear); tween.Repeat = true; tween.StartForward(); #endregion #region Cloud3 tra = ResourceManager.Get("Cloud3"); tween = tra.CreateTweenVec2D(new Vector3(-14, tra.localPosition.y, tra.localPosition.z), 170f, true, false, true, Curve.Linear); tween.Repeat = true; tween.StartForward(); #endregion #region Garden transform.CreateTweenSr(0, 1, 0.25f, false, true, Curve.EaseOutQuad, false, true); #endregion #region GardenMini ResourceManager.Get("GardenMini").CreateTweenVec2D ( ResourceManager.Get("MiniPos").position, 0.5f, false, false, true, Curve.EaseOutQuad ); #endregion #region GardenPage GardenPage = ResourceManager.Get("GardenPage"); Move2D move2D = GardenPage.CreateMove2D(); move2D.OnForwardStart += () => { Flag2 = false; }; move2D.OnForwardFinish += () => { Flag2 = true; }; #endregion return false; } public override void RegistReference() { GardenBK2 = ResourceManager.Get("GardenBK2"); GardenBK3 = ResourceManager.Get("GardenBK3"); GardenBK4 = ResourceManager.Get("GardenBK4"); GardenBK2Pos.Add(ResourceManager.Get("BK2RightPos").position); GardenBK2Pos.Add(ResourceManager.Get("BK2LeftPos").position); GardenBK3Pos.Add(ResourceManager.Get("BK3RightPos").position); GardenBK3Pos.Add(ResourceManager.Get("BK3LeftPos").position); GardenBK4Pos.Add(ResourceManager.Get("BK4RightPos").position); GardenBK4Pos.Add(ResourceManager.Get("BK4LeftPos").position); if (!TutorialManager.NewplayerTutorial) { Player = ResourceManager.Get("Player"); PlayerPos.Add(PlayerManager.Player.ChildDic["RightPos"].position); PlayerPos.Add(PlayerManager.Player.ChildDic["LeftPos"].position); } } public override void FirstInit() { ResetPage(); } public override void TutorialToRegular() { Player = ResourceManager.Get("Player"); PlayerPos.Add(PlayerManager.Player.ChildDic["RightPos"].position); PlayerPos.Add(PlayerManager.Player.ChildDic["LeftPos"].position); } public void OnDrag(PointerEventData eventData) { if (Flag1 && Flag2) { if (Direction == Direction.Null) { if (eventData.delta.x > 0) { Direction = Direction.Right; } else { Direction = Direction.Left; } } if (Direction == Direction.Left) { if (eventData.delta.x > 0) { Return(); } else { #region MyRegion float rawDeltaX = eventData.position.x - eventData.pressPosition.x; if (CurPage < Page - 1) { float tempX = Mathf.Clamp(eventData.delta.x / 10, -2, 2); if (Page == 1) { tempX *= SlideSensi; } else { tempX *= SlideSensi / (Page - 1); } Player.Translate(Player.right * tempX * PlayerMoveRatio, Space.World); GardenBK2.Translate(GardenBK2.right * tempX * BK2MoveRatio, Space.World); GardenBK3.Translate(GardenBK3.right * tempX * BK3MoveRatio, Space.World); GardenBK4.Translate(GardenBK4.right * tempX * BK4MoveRatio, Space.World); GardenPage.Translate(GardenPage.right * tempX, Space.World); if (rawDeltaX < -300) { NextPage(); } } else { float ratio = Mathf.Abs(GardenPage.position.x - PagePos.Back(1).x)/0.7f; float sensi = Mathf.Lerp(SlideSensi, 0, ratio); float tempX = Mathf.Clamp(eventData.delta.x / 10, -2, 2) * sensi; GardenPage.Translate(GardenPage.right * tempX, Space.World); } #endregion } } else if (Direction == Direction.Right) { if (eventData.delta.x < 0) { Return(); } else { #region MyRegion float rawDeltaX = eventData.position.x - eventData.pressPosition.x; if (CurPage > 0) { float tempX = Mathf.Clamp(eventData.delta.x / 10, -2, 2); if (Page == 1) { tempX *= SlideSensi; } else { tempX *= SlideSensi / (Page - 1); } Player.Translate(Player.right * tempX * PlayerMoveRatio, Space.World); GardenBK2.Translate(GardenBK2.right * tempX * BK2MoveRatio, Space.World); GardenBK3.Translate(GardenBK3.right * tempX * BK3MoveRatio, Space.World); GardenBK4.Translate(GardenBK4.right * tempX * BK4MoveRatio, Space.World); GardenPage.Translate(GardenPage.right * tempX, Space.World); if (rawDeltaX > 300) { PrevPage(); } } else { float ratio = Mathf.Abs(GardenPage.position.x - PagePos[0].x) / 0.7f; float sensi = Mathf.Lerp(SlideSensi, 0, ratio); float tempX = Mathf.Clamp(eventData.delta.x / 10, -2, 2) * sensi; GardenPage.Translate(GardenPage.right * tempX, Space.World); } #endregion } } } } public void OnEndDrag(PointerEventData eventData) { if (Flag1 && Flag2) { Return(); } Flag1 = true; Direction = Direction.Null; } public void OnBeginDrag(PointerEventData eventData) { Direction = Direction.Null; } public void OnPointerClick(PointerEventData eventData) { if (!eventData.hovered.Contains(eventData.rawPointerPress)) { return; } if (eventData.rawPointerPress.transform.name.Contains("Slot")) { if (VisitManager.InVisit) { return; } Slot slot = eventData.rawPointerPress.GetComponentInParent(); if (slot.Lock == false) { Toast.Show(1.5f, Language.GetStr("Common", "UnlockSlot")); } else { ResourceManager.Get(PrefabLabel.G_Flower).TweenForCG(); } } } public static void Return() { Flag1 = false; float ratio; if (Page == 1) { ratio = 0; } else { ratio = CurPage / (Page - 1f); } Vector3 playerPos = Vector3.Lerp(PlayerPos[0], PlayerPos[1], ratio); Vector3 gardenPos2 = Vector3.Lerp(GardenBK2Pos[0], GardenBK2Pos[1], ratio); Vector3 gardenPos3 = Vector3.Lerp(GardenBK3Pos[0], GardenBK3Pos[1], ratio); Vector3 gardenPos4 = Vector3.Lerp(GardenBK4Pos[0], GardenBK4Pos[1], ratio); Player.Move2D(playerPos, 0.5f, false, Curve.EaseOutQuad); GardenBK2.Move2D(gardenPos2, 0.5f, false, Curve.EaseOutQuad); GardenBK3.Move2D(gardenPos3, 0.5f, false, Curve.EaseOutQuad); GardenBK4.Move2D(gardenPos4, 0.5f, false, Curve.EaseOutQuad); GardenPage.Move2D(PagePos[CurPage], 0.5f, false, Curve.EaseOutQuad); } public static void PrevPage() { CurPage--; Flag1 = false; float ratio = CurPage / (Page - 1f); Vector3 playerPos = Vector3.Lerp(PlayerPos[0], PlayerPos[1], ratio); Vector3 gardenPos2 = Vector3.Lerp(GardenBK2Pos[0], GardenBK2Pos[1], ratio); Vector3 gardenPos3 = Vector3.Lerp(GardenBK3Pos[0], GardenBK3Pos[1], ratio); Vector3 gardenPos4 = Vector3.Lerp(GardenBK4Pos[0], GardenBK4Pos[1], ratio); Player.Move2D(playerPos, 0.5f, false, Curve.EaseOutQuad); GardenBK2.Move2D(gardenPos2, 0.5f, false, Curve.EaseOutQuad); GardenBK3.Move2D(gardenPos3, 0.5f, false, Curve.EaseOutQuad); GardenBK4.Move2D(gardenPos4, 0.5f, false, Curve.EaseOutQuad); GardenPage.Move2D(PagePos[CurPage], 0.5f, false, Curve.EaseOutQuad); } public static void NextPage() { CurPage++; Flag1 = false; float ratio = CurPage / (Page - 1f); Vector3 playerPos = Vector3.Lerp(PlayerPos[0], PlayerPos[1], ratio); Vector3 gardenPos2 = Vector3.Lerp(GardenBK2Pos[0], GardenBK2Pos[1], ratio); Vector3 gardenPos3 = Vector3.Lerp(GardenBK3Pos[0], GardenBK3Pos[1], ratio); Vector3 gardenPos4 = Vector3.Lerp(GardenBK4Pos[0], GardenBK4Pos[1], ratio); Player.Move2D(playerPos, 0.5f, false, Curve.EaseOutQuad); GardenBK2.Move2D(gardenPos2, 0.5f, false, Curve.EaseOutQuad); GardenBK3.Move2D(gardenPos3, 0.5f, false, Curve.EaseOutQuad); GardenBK4.Move2D(gardenPos4, 0.5f, false, Curve.EaseOutQuad); GardenPage.Move2D(PagePos[CurPage], 0.5f, false, Curve.EaseOutQuad); } public static void ResetPage() { CurPage = 0; GardenBK2.position = GardenBK2Pos[0]; GardenBK3.position = GardenBK3Pos[0]; GardenBK4.position = GardenBK4Pos[0]; GardenPage.position = PagePos[0]; } }