using System.Collections; using UnityEngine; using UnityEngine.UI; using DG.Tweening; public class PlayerMove : MonoBehaviour { public Transform objectToMove; // 要移动的对象 public Transform[] pathPoints; // 路径上的点的数组 public float duration = 5f; // 移动的持续时间 public bool isLooping = false; // 是否循环路径 int startIndex = 0; int endIndex = 0; public float Speed; public Transform EndPos; public Transform TypeEndPos1; public Transform TypeEndPos2; public Transform TypeEndPos3; public Transform TypeEndPos4; public Transform TypeEndPos5; public Transform TypeEndPos6; public Transform JumpPos; public Transform Pos1Parent; public Transform Pos2Parent; public Transform Pos3Parent; public Transform Pos4Parent; public Transform Pos5Parent; public Transform Pos6Parent; public Transform OutParent; public Transform ReturnPos;//返回的位置 public bool IsBoos; private void Start() { Speed = 2f; } public void MoveOutParent() { // 记录原始的父物体 // 将 childObject 设为新的 parentObject 的子节点,并放到最后一个位置 objectToMove.SetParent(OutParent); objectToMove.SetAsLastSibling(); } public void MoveInParent(Transform Parent) { objectToMove.SetParent(Parent); objectToMove.SetAsFirstSibling(); } public void JudgeEnd() { if (EndPos == TypeEndPos1) { MoveInParent(Pos1Parent); } else if (EndPos == TypeEndPos2) { MoveInParent(Pos2Parent); } else if(EndPos == TypeEndPos3) { MoveInParent(Pos3Parent); } else if (EndPos == TypeEndPos4) { MoveInParent(Pos4Parent); } else if (EndPos == TypeEndPos5) { MoveInParent(Pos5Parent); } else if (EndPos == TypeEndPos6) { MoveInParent(Pos6Parent); } } public void ReturnStartPos()//回到起始点 { EndPos = ReturnPos; StartMove(); } public bool JudagePos1() { if (EndPos==TypeEndPos1) { return true ; } return false; } public void StartMove() { // 提取路径中的所有点的位置 for (int i = 0; i < pathPoints.Length; i++) { if (!IsBoos&&PlayerMovePos.instance.StartPos == pathPoints[i]) { startIndex = i; Debug.Log("Startindex"+i); } else if (IsBoos && BossContro.instance.BoosStartPos== pathPoints[i]) { startIndex = i; Debug.Log("Startindex" + i); } if (EndPos == pathPoints[i]) { endIndex = i; Debug.Log("Endindex" + i); } } if (endIndex>startIndex) { duration=(endIndex-startIndex)/Speed; Vector3[] waypoints = new Vector3[endIndex-startIndex+1]; for (int i=startIndex,j=0;j< pathPoints.Length&&i<=endIndex; i++,j++) { if (!JudagePos1() && pathPoints[i]==JumpPos) { waypoints[j] = pathPoints[i - 1].position; continue; } waypoints[j]=pathPoints[i].position; } OnMove(waypoints); } else if(endIndex < startIndex) { duration = (startIndex-endIndex) / Speed; Vector3[] waypoints = new Vector3[startIndex-endIndex + 1]; for (int i = startIndex, j = 0; j < pathPoints.Length && i >= endIndex; i--, j++) { if (!JudagePos1() && pathPoints[i] == JumpPos) { waypoints[j] = pathPoints[i+1].position; continue; } waypoints[j] = pathPoints[i].position; } OnMove(waypoints); } } public void AfterMove() { if (!IsBoos) { PlayerMovePos.instance.StartPos =EndPos; Debug.Log("修改StartPos值" + PlayerMovePos.instance.StartPos); } else { BossContro.instance.BoosStartPos=EndPos; Debug.Log("修改BossPos值" + BossContro.instance.BoosStartPos); if (BossContro.instance.BoosStartPos==PlayerMovePos.instance.StartPos&& PlayerMovePos.instance.HadChoise) { Debug.Log("失败");//失败 FailPanel.instance.ShowPanel(); } else if(BossContro.instance.BoosStartPos != PlayerMovePos.instance.StartPos && PlayerMovePos.instance.HadChoise) { Debug.Log("胜利");//胜利 SucceePanel.instance.ShowPanel(); } else { Debug.Log("没有失败和胜利");//胜利 } HegemonTime.instance.StartCoroutine(HegemonTime.instance.StartGame());//重新计算时间 } if (!PlayerMovePos.instance.IsReturn) { objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(()=>{ JudgeEnd(); if (!IsBoos) { MaskContro.instance.SetMask(false);} }); } else { PlayerMovePos.instance.IsReturn = false;//重置 } } public void OnMove(Vector3[] waypoints) { MoveOutParent(); // 使用 DoTween 的 DOPath 方法创建路径动画 Tween pathTween = objectToMove.DOPath(waypoints, duration, PathType.Linear) .SetEase(Ease.Linear) // 线性过渡效果 .SetLoops(isLooping ? -1 : 0) .OnComplete(AfterMove); } }