503 lines
15 KiB
C#
503 lines
15 KiB
C#
using DG.Tweening;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
public class OtherMove : 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 posGroup;
|
||
|
||
public Transform JumpPos;
|
||
|
||
|
||
public Transform OutParent;
|
||
|
||
public Transform ReturnPos;//返回的位置
|
||
|
||
|
||
|
||
public AllHouseContro allHouseContro;
|
||
|
||
private Vector3 originalScale;
|
||
|
||
public bool IsRangeMove;//是否开始随机移动
|
||
public float moveInterval; // 隔多久移动一次,单位是秒
|
||
|
||
private float timer = 0f; // 计时器,用于控制时间间隔
|
||
private void Awake()
|
||
{
|
||
originalScale=transform.localScale;
|
||
Speed = 2f;
|
||
objectToMove = transform;
|
||
allHouseContro=GameObject.Find("Canvas/Bg").GetComponent<AllHouseContro>();
|
||
posGroup=GameObject.Find("Canvas/Bg/Posgroup").transform;
|
||
if (posGroup != null)
|
||
{
|
||
// 获取父物体的所有子节点
|
||
pathPoints = posGroup.GetComponentsInChildren<Transform>();
|
||
|
||
// 去掉父物体自身(只要子节点)
|
||
pathPoints = System.Array.FindAll(pathPoints, t => t != posGroup);
|
||
|
||
//Debug.Log("子节点已存入数组"+ pathPoints.Length);
|
||
}
|
||
else
|
||
{
|
||
//Debug.LogError("请分配父物体!");
|
||
}
|
||
OutParent= GameObject.Find("Canvas/Bg").transform ;
|
||
JumpPos = GameObject.Find("Canvas/Bg/Posgroup/pos (3)").transform;
|
||
moveInterval = Random.Range(3,10);
|
||
}
|
||
|
||
|
||
private void Update()
|
||
{
|
||
if (IsRangeMove)
|
||
{
|
||
// 累加计时器
|
||
timer += Time.deltaTime;
|
||
|
||
// 如果计时器超过指定的间隔时间
|
||
if (timer >= moveInterval)
|
||
{
|
||
RandMove(); // 调用随机移动的方法
|
||
timer = 0f; // 重置计时器
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
public void MoveOutParent()
|
||
{
|
||
// 记录原始的父物体
|
||
|
||
|
||
// 将 childObject 设为新的 parentObject 的子节点,并放到最后一个位置
|
||
objectToMove.SetParent(OutParent);
|
||
objectToMove.SetAsLastSibling();
|
||
|
||
|
||
}
|
||
public void MoveInParent(Transform Parent)
|
||
{
|
||
objectToMove.SetParent(Parent);
|
||
objectToMove.SetAsFirstSibling();
|
||
}
|
||
|
||
public void RandMove() // 随机移动
|
||
{
|
||
RectTransform rectTransform = objectToMove.GetComponent<RectTransform>();
|
||
|
||
// 获取父物体的边界
|
||
RectTransform parentRect = rectTransform.parent.GetComponent<RectTransform>();
|
||
float parentWidth = parentRect.rect.width;
|
||
|
||
// 获取物体的宽度(也就是它的宽度的一半)
|
||
float objectWidth = rectTransform.rect.width;
|
||
float halfObjectWidth = objectWidth / 2f;
|
||
|
||
// 当前物体的位置
|
||
float currentX = rectTransform.anchoredPosition.x;
|
||
|
||
// 随机生成一个相对父物体范围的偏移量,确保不超出范围
|
||
float moveRange = Random.Range(-100f, 100f);
|
||
if (moveRange > 0 && objectToMove.GetComponent<PlayerMovePos>().IsLeft)
|
||
{
|
||
objectToMove.GetComponent<PlayerMovePos>().IsLeft = false;
|
||
// 旋转物体使其面向左侧(绕Y轴旋转180度)
|
||
Rotate();
|
||
}
|
||
else if (moveRange < 0 && !objectToMove.GetComponent<PlayerMovePos>().IsLeft)
|
||
{
|
||
objectToMove.GetComponent<PlayerMovePos>().IsLeft = true;
|
||
Rotate();
|
||
}
|
||
// 计算目标位置,确保目标位置在父物体的宽度范围内
|
||
float targetX = currentX + moveRange;
|
||
|
||
// 限制目标位置,减去自身宽度的一半,确保物体不超出父物体的边界
|
||
targetX = Mathf.Clamp(targetX, -parentWidth / 2 + halfObjectWidth+20, parentWidth / 2 - halfObjectWidth-20);
|
||
|
||
// 使用 DoTween 实现平滑移动
|
||
rectTransform.DOAnchorPosX(targetX, 1f).SetEase(Ease.Linear);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
public void SetParentToHouse()
|
||
{
|
||
foreach (HouseBtn house in allHouseContro.HouseBtnList)
|
||
{
|
||
if (EndPos==house.post.transform)
|
||
{
|
||
MoveInParent(house.transform);
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
public void ReturnStartPos()//回到起始点
|
||
{
|
||
|
||
}
|
||
|
||
public bool JudagePos1()
|
||
{
|
||
if (EndPos == JumpPos)
|
||
{
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
public void StartMove()
|
||
{
|
||
|
||
//Debug.Log("启动其他蜗牛移动");
|
||
// 提取路径中的所有点的位置
|
||
//Debug.Log("路径长度"+ pathPoints.Length);
|
||
for (int i = 0; i < pathPoints.Length; i++)
|
||
{
|
||
//Debug.Log("其他蜗判断终点");
|
||
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)
|
||
{
|
||
//Debug.Log("其他蜗牛分配路径");
|
||
|
||
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&&GetComponent<PlayerMovePos>().StartPos != JumpPos)
|
||
{
|
||
waypoints[j] = pathPoints[i - 1].position;
|
||
continue;
|
||
|
||
}
|
||
waypoints[j] = pathPoints[i].position;
|
||
|
||
}
|
||
OnMove(waypoints);
|
||
}
|
||
else if (endIndex < startIndex)
|
||
{
|
||
|
||
duration = (startIndex - endIndex) / Speed;
|
||
|
||
//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&&GetComponent<PlayerMovePos>().StartPos!=JumpPos)
|
||
{
|
||
waypoints[j] = pathPoints[i + 1].position;
|
||
continue;
|
||
|
||
}
|
||
|
||
waypoints[j] = pathPoints[i].position;
|
||
|
||
}
|
||
OnMove(waypoints);
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
public void AfterMove()
|
||
{
|
||
//Debug.Log("其他蜗牛到达终点");
|
||
RectTransform rectTransform = objectToMove.GetComponent<RectTransform>();
|
||
|
||
|
||
//objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => {
|
||
Zoomout();
|
||
rectTransform.DOAnchorPosY(rectTransform.anchoredPosition.y + 80f, 0.5f).SetEase(Ease.Linear)
|
||
.OnComplete(() => {
|
||
|
||
|
||
|
||
SetParentToHouse();
|
||
|
||
|
||
objectToMove.GetComponent<PlayerMovePos>().StartPos = EndPos;
|
||
|
||
|
||
|
||
|
||
|
||
// rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x + Random.Range(-100, 100), rectTransform.anchoredPosition.y);
|
||
float range = Random.Range(-100, 100);
|
||
// 判断物体是否需要旋转
|
||
if (range>0&& objectToMove.GetComponent<PlayerMovePos>().IsLeft)
|
||
{
|
||
objectToMove.GetComponent<PlayerMovePos>().IsLeft = false;
|
||
// 旋转物体使其面向左侧(绕Y轴旋转180度)
|
||
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).OnComplete(() => { objectToMove.GetComponent<PlayerMovePos>().PlayAni.SetInteger("State", 0); IsRangeMove = true; });
|
||
|
||
});
|
||
|
||
|
||
//});
|
||
}
|
||
|
||
public void AfterReMove()
|
||
{
|
||
//Debug.Log("其他蜗牛返回起点");
|
||
RectTransform rectTransform = objectToMove.GetComponent<RectTransform>();
|
||
|
||
|
||
//objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => {
|
||
rectTransform.DOAnchorPosY(rectTransform.anchoredPosition.y+Random.Range(-200,-20), 0.5f).SetEase(Ease.Linear)
|
||
.OnComplete(() => {
|
||
|
||
objectToMove.GetComponent<PlayerMovePos>().StartPos = EndPos;
|
||
// rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x + Random.Range(-100, 100), rectTransform.anchoredPosition.y);
|
||
float range = Random.Range(-350, 350);
|
||
// 判断物体是否需要旋转
|
||
if (range > 0 && objectToMove.GetComponent<PlayerMovePos>().IsLeft)
|
||
{
|
||
objectToMove.GetComponent<PlayerMovePos>().IsLeft = false;
|
||
// 旋转物体使其面向左侧(绕Y轴旋转180度)
|
||
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).OnComplete(() => { objectToMove.GetComponent<PlayerMovePos>().PlayAni.SetInteger("State", 0); });
|
||
|
||
});
|
||
|
||
|
||
//});
|
||
}
|
||
|
||
|
||
|
||
public void ReTurnStartPosMove()
|
||
{
|
||
IsRangeMove = false;
|
||
//Debug.Log("启动其他蜗牛移动");
|
||
// 提取路径中的所有点的位置
|
||
//Debug.Log("路径长度" + pathPoints.Length);
|
||
for (int i = 0; i < pathPoints.Length; i++)
|
||
{
|
||
//Debug.Log("其他蜗判断终点");
|
||
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)
|
||
{
|
||
//Debug.Log("其他蜗牛分配路径");
|
||
|
||
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 && GetComponent<PlayerMovePos>().StartPos != JumpPos)
|
||
{
|
||
waypoints[j] = pathPoints[i - 1].position;
|
||
continue;
|
||
|
||
}
|
||
waypoints[j] = pathPoints[i].position;
|
||
|
||
}
|
||
OnReMove(waypoints);
|
||
}
|
||
else if (endIndex < startIndex)
|
||
{
|
||
|
||
duration = (startIndex - endIndex) / Speed;
|
||
|
||
//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 && GetComponent<PlayerMovePos>().StartPos != JumpPos)
|
||
{
|
||
waypoints[j] = pathPoints[i + 1].position;
|
||
continue;
|
||
|
||
}
|
||
|
||
waypoints[j] = pathPoints[i].position;
|
||
|
||
}
|
||
OnReMove(waypoints);
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
public void Zoomout()//缩小
|
||
{
|
||
Image image = GetComponent<Image>();
|
||
image.rectTransform.DOScale(0.9f, 0.5f).SetEase(Ease.Linear);
|
||
}
|
||
public void ReplySize()//恢复大小
|
||
{
|
||
Image image = GetComponent<Image>();
|
||
image.rectTransform.DOScale(originalScale, 0.5f).SetEase(Ease.Linear);
|
||
}
|
||
|
||
|
||
|
||
|
||
public void Rotate()
|
||
{
|
||
|
||
Vector3 currentRotation = objectToMove.localEulerAngles;
|
||
currentRotation.y += 180f;
|
||
objectToMove.localEulerAngles = currentRotation;
|
||
|
||
}
|
||
|
||
public void OnMove(Vector3[] waypoints)
|
||
{
|
||
|
||
objectToMove.GetComponent<PlayerMovePos>().PlayAni.SetInteger("State", 1);
|
||
|
||
|
||
|
||
|
||
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 (nextWaypoint.x < currentWaypoint.x && !objectToMove.GetComponent<PlayerMovePos>().IsLeft)
|
||
{
|
||
objectToMove.GetComponent<PlayerMovePos>().IsLeft = true;
|
||
// 旋转物体使其面向左侧(绕Y轴旋转180度)
|
||
Rotate();
|
||
}
|
||
else if (nextWaypoint.x > currentWaypoint.x && objectToMove.GetComponent<PlayerMovePos>().IsLeft)
|
||
{
|
||
objectToMove.GetComponent<PlayerMovePos>().IsLeft = false;
|
||
Rotate();
|
||
}
|
||
}
|
||
|
||
|
||
})
|
||
.OnComplete(AfterMove);
|
||
}
|
||
|
||
|
||
public void OnReMove(Vector3[] waypoints)
|
||
{
|
||
|
||
objectToMove.GetComponent<PlayerMovePos>().PlayAni.SetInteger("State", 1);
|
||
|
||
|
||
|
||
|
||
MoveOutParent();
|
||
ReplySize();//恢复大小
|
||
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 (nextWaypoint.x < currentWaypoint.x && !objectToMove.GetComponent<PlayerMovePos>().IsLeft)
|
||
{
|
||
objectToMove.GetComponent<PlayerMovePos>().IsLeft = true;
|
||
// 旋转物体使其面向左侧(绕Y轴旋转180度)
|
||
Rotate();
|
||
}
|
||
else if (nextWaypoint.x > currentWaypoint.x && objectToMove.GetComponent<PlayerMovePos>().IsLeft)
|
||
{
|
||
objectToMove.GetComponent<PlayerMovePos>().IsLeft = false;
|
||
Rotate();
|
||
}
|
||
}
|
||
|
||
|
||
})
|
||
.OnComplete(AfterReMove);
|
||
}
|
||
}
|