Files
TaiWan/Assets/Roaming/Extenders/AutoGuiderEx/Scripts/AutoGuiderEx.cs
2025-10-31 15:20:38 +08:00

191 lines
6.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
public class AutoGuiderEx : MonoBehaviour
{
public Transform tranGuidePositions;
public string GuiderAudioPath = "Guide/";
int index = -1;
Coroutine corAnim1;
Coroutine corAnim2;
bool currentExMoveCompleted = true;
bool currentExRotateCompleted = true;
List<GuidePoint> lstGuidePoint;
void Start()
{
#region
lstGuidePoint = new List<GuidePoint>();
for (int i = 0; i < tranGuidePositions.childCount; i++)
{
lstGuidePoint.Add(tranGuidePositions.GetChild(i).GetComponent<GuideData>().GPoint);
}
// Debug.Log(JsonTools.ListToJson(lstGuidePoint));
#endregion
//StartCoroutine(DataLoader<GuidePoint>.LoadDataList("GuidePoints.json",
// delegate() { lstGuidePoint = DataLoader<GuidePoint>.DataList; }));
}
GuidePoint currentPoint;
public void Navigate()
{
if (currentExMoveCompleted && currentExRotateCompleted)
{
BaseController.IsAutoNavigating = true;
currentExMoveCompleted = false;
currentExRotateCompleted = false;
index++;
if (index == lstGuidePoint.Count)
{
BaseController.IsAutoNavigating = false;
BaseController.CanControl = true;
index = -1;
return;
}
currentPoint = lstGuidePoint[index];
//Debug.Log(index + "¿ªÊ¼");
if (!string.IsNullOrEmpty(currentPoint.MoveAudioName))
{
AudioManager.Instance.PlayAudio(GuiderAudioPath + currentPoint.MoveAudioName);
}
if (corAnim1 != null)
StopCoroutine(corAnim1);
if (corAnim2 != null)
StopCoroutine(corAnim2);
Vector3 toPosition = new Vector3(
currentPoint.Position.x,
CommonData.MainCamera.transform.position.y,
currentPoint.Position.z);
float distance = Vector3.Distance(CommonData.MainCamera.transform.position, toPosition);
//Debug.Log("¾àÀ룺"+distance);
corAnim1 = StartCoroutine(
CommonData.MainCamera.transform.ExMove(
toPosition,
distance / currentPoint.MoveTime,
delegate ()
{
currentExMoveCompleted = true;
//Debug.Log(index + "λÖÃÒÆ¶¯½áÊø" + Time.time);
NavigateCompleted();
},
null
)
);
float angle = Vector3.Distance(CommonData.MainCamera.transform.localRotation.eulerAngles, currentPoint.Rotation);
//Debug.Log("½Ç¶È£º"+angle);
Vector3 vAngle = currentPoint.Rotation;
if (currentPoint.Rotation.y < 0)
{
vAngle = new Vector3(vAngle.x, vAngle.y + 360, vAngle.z);
}
corAnim2 = StartCoroutine(
CommonData.MainCamera.transform.ExRotate(
vAngle, //currentPoint.Rotation,
Mathf.Abs( angle )/ currentPoint.MoveTime,
delegate ()
{
currentExRotateCompleted = true;
//Debug.Log(index + "Ðý×ªÒÆ¶¯½áÊø" + Time.time);
NavigateCompleted();
},
null
)
);
}
}
private void NavigateCompleted()
{
if (currentExMoveCompleted && currentExRotateCompleted && BaseController.IsAutoNavigating)
{
if (!string.IsNullOrEmpty(currentPoint.StopAudioName))
{
AudioManager.Instance.PlayAudio(GuiderAudioPath + currentPoint.StopAudioName);
}
Invoke("Navigate", currentPoint.StopTime);
}
}
public void StopNavigate()
{
AudioManager.Instance.StopAudio();
if (corAnim1 != null)
StopCoroutine(corAnim1);
if (corAnim2 != null)
StopCoroutine(corAnim2);
BaseController.IsAutoNavigating = false;
currentExMoveCompleted = true;
currentExRotateCompleted = true;
}
}
static class TransformExtender
{
/// <summary>
/// µ±Ç°ÎïÌåÒÔÖ¸¶¨µÄËÙ¶ÈÔÈËÙÒÆ¶¯ÖÁÄ¿±êµã
/// </summary>
/// <param name="self"></param>
/// <param name="target"></param>
/// <param name="speed"></param>
/// <param name="onComplete"></param>
/// <param name="onUpdate"></param>
/// <returns></returns>
public static IEnumerator ExMove(this Transform self, Vector3 target, float speed, Action onComplete = null, Action onUpdate = null)
{
while (self.position != target)
{
self.position = Vector3.MoveTowards(
self.position,
target,
speed * Time.deltaTime
);
onUpdate?.Invoke();
yield return 0;
}
onComplete?.Invoke();
}
/// <summary>
/// µ±Ç°ÎïÌåÒÔÖ¸¶¨µÄËÙ¶ÈÔÈËÙÒÆ¶¯ÖÁÄ¿±êµã
/// </summary>
/// <param name="self"></param>
/// <param name="target"></param>
/// <param name="speed"></param>
/// <param name="onComplete"></param>
/// <param name="onUpdate"></param>
/// <returns></returns>
public static IEnumerator ExRotate(this Transform self, Vector3 target, float speed, Action onComplete = null, Action onUpdate = null)
{
while (self.eulerAngles != target)
{
self.localRotation =
Quaternion.RotateTowards(
self.localRotation,
Quaternion.Euler(target),
speed * Time.deltaTime
);
//Debug.Log(self.localRotation.eulerAngles);
onUpdate?.Invoke();
yield return 0;
}
onComplete?.Invoke();
}
}