_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Battle_Royale/OtherMove.cs

284 lines
7.4 KiB
C#
Raw Normal View History

2024-11-21 00:04:33 +08:00
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OtherMove : MonoBehaviour
{
public Transform objectToMove; // Ҫ<>ƶ<EFBFBD><C6B6>Ķ<EFBFBD><C4B6><EFBFBD>
public Transform[] pathPoints; // ·<><C2B7><EFBFBD>ϵĵ<CFB5><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public float duration = 5f; // <20>ƶ<EFBFBD><C6B6>ij<EFBFBD><C4B3><EFBFBD>ʱ<EFBFBD><CAB1>
public bool isLooping = false; // <20>Ƿ<EFBFBD>ѭ<EFBFBD><D1AD>·<EFBFBD><C2B7>
int startIndex = 0;
int endIndex = 0;
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
public float Speed;
public Transform EndPos;
2024-11-21 09:38:16 +08:00
public Transform posGroup;
2024-11-21 00:04:33 +08:00
2024-11-21 12:03:01 +08:00
public Transform JumpPos;
2024-11-21 00:04:33 +08:00
public Transform OutParent;
public Transform ReturnPos;//<2F><><EFBFBD>ص<EFBFBD>λ<EFBFBD><CEBB>
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
public AllHouseContro allHouseContro;
2024-11-21 09:38:16 +08:00
2024-11-21 12:03:01 +08:00
private void Awake()
2024-11-21 00:04:33 +08:00
{
Speed = 2f;
2024-11-21 09:38:16 +08:00
objectToMove = transform;
allHouseContro=GameObject.Find("Canvas/Bg").GetComponent<AllHouseContro>();
posGroup=GameObject.Find("Canvas/Bg/Posgroup").transform;
if (posGroup != null)
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>
pathPoints = posGroup.GetComponentsInChildren<Transform>();
2024-11-21 00:04:33 +08:00
2024-11-21 09:38:16 +08:00
// ȥ<><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻҪ<D6BB>ӽڵ㣩
pathPoints = System.Array.FindAll(pathPoints, t => t != posGroup);
2024-11-21 00:04:33 +08:00
2024-11-21 12:03:01 +08:00
Debug.Log("<22>ӽڵ<D3BD><DAB5>Ѵ<EFBFBD><D1B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"+ pathPoints.Length);
2024-11-21 09:38:16 +08:00
}
else
{
Debug.LogError("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E4B8B8><EFBFBD>壡");
}
OutParent= GameObject.Find("Canvas/Bg").transform ;
2024-11-21 12:03:01 +08:00
JumpPos = GameObject.Find("Canvas/Bg/Posgroup/pos (3)").transform;
2024-11-21 00:04:33 +08:00
}
public void MoveOutParent()
{
// <20><>¼ԭʼ<D4AD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
// <20><> childObject <20><>Ϊ<EFBFBD>µ<EFBFBD> parentObject <20><><EFBFBD>ӽڵ㣬<DAB5><E3A3AC><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>λ<EFBFBD><CEBB>
objectToMove.SetParent(OutParent);
objectToMove.SetAsLastSibling();
}
public void MoveInParent(Transform Parent)
{
objectToMove.SetParent(Parent);
objectToMove.SetAsFirstSibling();
}
public void SetParentToHouse()
{
2024-11-21 09:38:16 +08:00
foreach (HouseBtn house in allHouseContro.HouseBtnList)
{
if (EndPos==house.post.transform)
{
MoveInParent(house.transform);
}
}
2024-11-21 00:04:33 +08:00
}
public void ReturnStartPos()//<2F>ص<EFBFBD><D8B5><EFBFBD>ʼ<EFBFBD><CABC>
{
}
2024-11-21 12:03:01 +08:00
public bool JudagePos1()
{
if (EndPos == JumpPos)
{
return true;
}
return false;
}
2024-11-21 00:04:33 +08:00
public void StartMove()
{
2024-11-21 12:03:01 +08:00
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>ƶ<EFBFBD>");
2024-11-21 00:04:33 +08:00
// <20><>ȡ·<C8A1><C2B7><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>λ<EFBFBD><CEBB>
2024-11-21 12:03:01 +08:00
Debug.Log(<><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"+ pathPoints.Length);
2024-11-21 00:04:33 +08:00
for (int i = 0; i < pathPoints.Length; i++)
{
2024-11-21 12:03:01 +08:00
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>յ<EFBFBD>");
2024-11-21 00:04:33 +08:00
if (objectToMove.GetComponent<PlayerMovePos>().StartPos == pathPoints[i])
{
startIndex = i;
//Debug.Log("Startindex"+i);
}
if (EndPos == pathPoints[i])
{
endIndex = i;
//Debug.Log("Endindex" + i);
}
}
if (endIndex > startIndex)
{
2024-11-21 12:03:01 +08:00
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD>·<EFBFBD><C2B7>");
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
duration = (endIndex - startIndex) / Speed;
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
Vector3[] waypoints = new Vector3[endIndex - startIndex + 1];
for (int i = startIndex, j = 0; j < pathPoints.Length && i <= endIndex; i++, j++)
{
2024-11-21 12:03:01 +08:00
if (!JudagePos1() && pathPoints[i] == JumpPos)
{
waypoints[j] = pathPoints[i - 1].position;
continue;
}
2024-11-21 00:04:33 +08:00
waypoints[j] = pathPoints[i].position;
}
OnMove(waypoints);
}
else if (endIndex < startIndex)
{
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
duration = (startIndex - endIndex) / Speed;
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
//duration = (startIndex-endIndex) / Speed;
Vector3[] waypoints = new Vector3[startIndex - endIndex + 1];
for (int i = startIndex, j = 0; j < pathPoints.Length && i >= endIndex; i--, j++)
{
2024-11-21 12:03:01 +08:00
if (!JudagePos1() && pathPoints[i] == JumpPos)
{
waypoints[j] = pathPoints[i + 1].position;
continue;
}
2024-11-21 00:04:33 +08:00
waypoints[j] = pathPoints[i].position;
}
OnMove(waypoints);
}
}
public void AfterMove()
{
2024-11-21 12:03:01 +08:00
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>");
2024-11-21 00:04:33 +08:00
RectTransform rectTransform = objectToMove.GetComponent<RectTransform>();
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
//objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => {
rectTransform.DOAnchorPosY(rectTransform.anchoredPosition.y + 80f, 0.5f).SetEase(Ease.Linear)
2024-11-21 00:04:33 +08:00
.OnComplete(() => {
SetParentToHouse();
objectToMove.GetComponent<PlayerMovePos>().StartPos = EndPos;
objectToMove.GetComponent<PlayerMovePos>().PlayAni.SetInteger("State", 0);
// rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x + Random.Range(-100, 100), rectTransform.anchoredPosition.y);
float range = Random.Range(-100, 100);
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA>ת
if (range>0&& objectToMove.GetComponent<PlayerMovePos>().IsLeft)
{
objectToMove.GetComponent<PlayerMovePos>().IsLeft = false;
// <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0A3A8>Y<EFBFBD><59><EFBFBD><EFBFBD>ת180<38>ȣ<EFBFBD>
Rotate();
}
else if (range<0 && !objectToMove.GetComponent<PlayerMovePos>().IsLeft)
{
objectToMove.GetComponent<PlayerMovePos>().IsLeft = true;
Rotate();
}
rectTransform.DOAnchorPosX(rectTransform.anchoredPosition.x + range, 1f).SetEase(Ease.Linear);
2024-11-21 00:04:33 +08:00
});
//});
}
public void Rotate()
{
Vector3 currentRotation = objectToMove.localEulerAngles;
currentRotation.y += 180f;
objectToMove.localEulerAngles = currentRotation;
}
public void OnMove(Vector3[] waypoints)
{
2024-11-21 12:03:01 +08:00
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3>ʼ<EFBFBD>ƶ<EFBFBD>");
2024-11-21 00:04:33 +08:00
objectToMove.GetComponent<PlayerMovePos>().PlayAni.SetInteger("State", 1);
MoveOutParent();
Tween pathTween = objectToMove.DOPath(waypoints, duration, PathType.Linear)
.SetEase(Ease.Linear) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD><D4B9><EFBFBD>Ч<EFBFBD><D0A7>
.SetLoops(isLooping ? -1 : 0)
.OnWaypointChange((index) =>
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5B5BD>ij<EFBFBD><C4B3>·<EFBFBD><C2B7><EFBFBD><EFBFBD>ʱ<EFBFBD>ص<EFBFBD>
if (index < waypoints.Length - 1)
{
Vector3 currentWaypoint = waypoints[index];
Vector3 nextWaypoint = waypoints[index + 1];
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA>ת
if (nextWaypoint.x < currentWaypoint.x && !objectToMove.GetComponent<PlayerMovePos>().IsLeft)
{
objectToMove.GetComponent<PlayerMovePos>().IsLeft = true;
// <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0A3A8>Y<EFBFBD><59><EFBFBD><EFBFBD>ת180<38>ȣ<EFBFBD>
Rotate();
}
else if (nextWaypoint.x > currentWaypoint.x && objectToMove.GetComponent<PlayerMovePos>().IsLeft)
{
objectToMove.GetComponent<PlayerMovePos>().IsLeft = false;
Rotate();
}
}
2024-11-21 09:38:16 +08:00
2024-11-21 00:04:33 +08:00
})
.OnComplete(AfterMove);
}
}