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; public AllHouseContro allHouseContro; 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) { if (!IsBoos) { PlayerMovePos.instance.HouseId = 1; } else { BossContro.instance.HouseId = 1; } } else if (EndPos == TypeEndPos2) { if (!IsBoos) { PlayerMovePos.instance.HouseId = 2; } else { BossContro.instance.HouseId = 2; } } else if(EndPos == TypeEndPos3) { if (!IsBoos) { PlayerMovePos.instance.HouseId = 3; } else { BossContro.instance.HouseId = 3; } } else if (EndPos == TypeEndPos4) { if (!IsBoos) { PlayerMovePos.instance.HouseId = 4; } else { BossContro.instance.HouseId = 4; } } else if (EndPos == TypeEndPos5) { if (!IsBoos) { PlayerMovePos.instance.HouseId = 5; } else { BossContro.instance.HouseId = 5; } } else if (EndPos == TypeEndPos6) { if (!IsBoos) { PlayerMovePos.instance.HouseId = 6; } else { BossContro.instance.HouseId = 6; } } } public void SetParentToHouse() { 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 (!PlayerMovePos.instance.IsReturn) { if (!IsBoos) { objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => { JudgeEnd(); SetParentToHouse(); PlayerMovePos.instance.StartPos = EndPos; PlayerMovePos.instance.PlayAni.SetInteger("State", 0); MaskContro.instance.SetMask(false); }); } else { JudgeEnd(); BossContro.instance.BossAni.SetInteger("State", 0);//停止移动 allHouseContro.ControOneDoorOpen(BossContro.instance.HouseId,() => {//开门 BossContro.instance.BossAni.SetInteger("State", 1); objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => {//进房间 SetParentToHouse(); StartCoroutine(BoosAttack()); BossContro.instance.BoosStartPos = EndPos; //JudgeSuccOrFail(); }); }); } } else { PlayerMovePos.instance.IsReturn = false;//重置 MaskContro.instance.SetMask(false); } } IEnumerator BoosAttack() { BossContro.instance.BossAni.SetInteger("State", 2); yield return new WaitForSeconds(3f); BossContro.instance.BossAni.SetInteger("State", 0); BettingBtn.instance.GiveMoney(); yield return new WaitForSeconds(2f); JudgeSuccOrFail(); } public void JudgeSuccOrFail() { if (BossContro.instance.BoosStartPos == PlayerMovePos.instance.StartPos && PlayerMovePos.instance.HadChoise) { //BossContro.instance.BossAni.SetInteger("State", 2);//boos攻击 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());//重新计算时间 } public void Rotate() { Vector3 currentRotation = objectToMove.localEulerAngles; currentRotation.y += 180f; objectToMove.localEulerAngles = currentRotation; } public void OnMove(Vector3[] waypoints) { if (!IsBoos) { PlayerMovePos.instance.PlayAni.SetInteger("State", 1); MaskContro.instance.SetMask(true); } MoveOutParent(); Tween pathTween = objectToMove.DOPath(waypoints, duration, PathType.Linear) .SetEase(Ease.Linear) // 设置线性过渡效果 .SetLoops(isLooping ? -1 : 0) .OnWaypointChange((index) => { // 当物体到达某个路径点时回调 if (index < waypoints.Length - 1) { Vector3 currentWaypoint = waypoints[index]; Vector3 nextWaypoint = waypoints[index + 1]; if (IsBoos) { if (nextWaypoint.x < currentWaypoint.x && !BossContro.instance.IsLeft) { BossContro.instance.IsLeft = true; // 旋转物体使其面向左侧(绕Y轴旋转180度) Rotate(); } else if (nextWaypoint.x > currentWaypoint.x && BossContro.instance.IsLeft) { BossContro.instance.IsLeft = false; Rotate(); } } else { // 判断物体是否需要旋转 if (nextWaypoint.x < currentWaypoint.x && !PlayerMovePos.instance.IsLeft) { PlayerMovePos.instance.IsLeft = true; // 旋转物体使其面向左侧(绕Y轴旋转180度) Rotate(); } else if (nextWaypoint.x > currentWaypoint.x && PlayerMovePos.instance.IsLeft) { PlayerMovePos.instance.IsLeft = false; Rotate(); } } } }) .OnComplete(AfterMove); } }