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 string room_id; public float Speed; public Transform EndPos; public Transform TypeEndPos0; 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; public Transform SelfTextTrans; public Transform SelfImage; public bool IsOtherWoniu; 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) { objectToMove.GetComponent().HouseId = 1; } else { GetComponent().HouseId = 1; } } else if (EndPos == TypeEndPos2) { if (!IsBoos) { objectToMove.GetComponent().HouseId = 2; } else { GetComponent().HouseId = 2; } } else if(EndPos == TypeEndPos3) { if (!IsBoos) { objectToMove.GetComponent().HouseId = 3; } else { GetComponent().HouseId = 3; } } else if (EndPos == TypeEndPos4) { if (!IsBoos) { objectToMove.GetComponent().HouseId = 4; } else { GetComponent().HouseId = 4; } } else if (EndPos == TypeEndPos5) { if (!IsBoos) { objectToMove.GetComponent().HouseId = 5; } else { GetComponent().HouseId = 5; } } else if (EndPos == TypeEndPos6) { if (!IsBoos) { objectToMove.GetComponent().HouseId = 6; } else { GetComponent().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&& objectToMove.GetComponent().StartPos == pathPoints[i]) { startIndex = i; Debug.Log("Startindex"+i); } else if (IsBoos && GetComponent().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||!objectToMove.GetComponent().IsReturn) { if (!IsBoos) { objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => { JudgeEnd(); SetParentToHouse(); objectToMove.GetComponent().StartPos = EndPos; objectToMove.GetComponent().PlayAni.SetInteger("State", 0); if (!IsOtherWoniu) { MaskContro.instance.SetMask(false); } }); } else { Debug.Log("cscscscscscscscscscscs"); JudgeEnd(); GetComponent().BossAni.SetInteger("State", 0);//停止移动 Debug.Log("nnnnnnnn"); allHouseContro.ControOneDoorOpen(GetComponent().HouseId,() => {//开门 GetComponent().BossAni.SetInteger("State", 1); objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => {//进房间 SetParentToHouse(); StartCoroutine(BoosAttack()); GetComponent().BoosStartPos = EndPos; //JudgeSuccOrFail(); }); }); } } else { objectToMove.GetComponent().IsReturn = false;//重置 MaskContro.instance.SetMask(false); } } IEnumerator BoosAttack() { GetComponent().BossAni.SetInteger("State", 2); yield return new WaitForSeconds(3f); GetComponent().BossAni.SetInteger("State", 0); BettingBtn.instance.GiveMoney(room_id); yield return new WaitForSeconds(2f); allHouseContro.BoosReturn(); //JudgeSuccOrFail(); } public void JudgeSuccOrFail() { Debug.Log("=000000000000000000000000000000000000000000000000"); if (GetComponent().BoosStartPos == transform.GetComponent().StartPos && transform.GetComponent().HadChoise) { //BossContro.instance.BossAni.SetInteger("State", 2);//boos攻击 Debug.Log("失败");//失败 FailPanel.instance.ShowPanel(); } else if (GetComponent().BoosStartPos != transform.GetComponent().StartPos && transform.GetComponent().HadChoise) { Debug.Log("胜利");//胜利 SucceePanel.instance.ShowPanel(); } else { Debug.Log("没有失败和胜利");//胜利 foreach (GameObject item in allHouseContro.bossobj) { item.GetComponent().ReturnParent(); } } //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) { objectToMove.GetComponent().PlayAni.SetInteger("State", 1); if (!IsOtherWoniu) { 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 && !GetComponent().IsLeft) { GetComponent().IsLeft = true; // 旋转物体使其面向左侧(绕Y轴旋转180度) Rotate(); } else if (nextWaypoint.x > currentWaypoint.x && GetComponent().IsLeft) { GetComponent().IsLeft = false; Rotate(); } } else { // 判断物体是否需要旋转 if (nextWaypoint.x < currentWaypoint.x && !objectToMove.GetComponent().IsLeft) { objectToMove.GetComponent().IsLeft = true; // 旋转物体使其面向左侧(绕Y轴旋转180度) Rotate(); } else if (nextWaypoint.x > currentWaypoint.x && objectToMove.GetComponent().IsLeft) { objectToMove.GetComponent().IsLeft = false; Rotate(); } } } }) .OnComplete(AfterMove); } }