NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/BSplineCurve/FlareLevelCar.cs

126 lines
3.2 KiB
C#

using GameWrapper.Curve;
using Sirenix.OdinInspector;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace GameWrapper.FlareLevel
{
public sealed class FlareLevelCar : MonoBehaviour
{
/// <summary>
/// 移动时间
/// </summary>
private const float MoveStepTime = 1f;
#region 私有变量
/// <summary>
/// 当前位置
/// </summary>
private int curIndex;
/// <summary>
/// 下一个目标点位
/// </summary>
private BSplineCurve nextTarget;
/// <summary>
/// 目标位置
/// </summary>
private int targetIndex;
/// <summary>
/// 移动计时
/// </summary>
private float timer;
/// <summary>
/// 点位列表
/// </summary>
private List<GameObject> listPoint;
#endregion
/// <summary>
/// 点位根节点
/// </summary>
public Transform TsPointRoot;
private void Awake()
{
if (null == TsPointRoot)
{
listPoint = new List<GameObject>(1);
return;
}
listPoint = new List<GameObject>(TsPointRoot.childCount);
for (int i = 0; i < TsPointRoot.childCount; ++i)
{
listPoint.Add(TsPointRoot.GetChild(i).gameObject);
}
curIndex = -1;
targetIndex = curIndex;
SetPos(0);
}
private void Update()
{
if (curIndex == targetIndex)
return;
timer += Time.deltaTime;
transform.position = nextTarget.GetPointOnCurve(timer / MoveStepTime);
if (timer >= MoveStepTime)
{
curIndex = nextTarget.Index;
MoveTo(targetIndex);
}
}
/// <summary>
/// 设置移动位置
/// </summary>
[Button]
public void MoveTo(int index)
{
if (listPoint.Count <= index)
{
DebugUtil.LogError("设置移动点位越界");
return;
}
if (index <= curIndex)
return;
targetIndex = index;
GameObject goCur = listPoint[curIndex];
nextTarget = goCur.GetComponent<BSplineCurve>();
if (null == nextTarget)
{
DebugUtil.LogError($"点位没有挂载{nameof(BSplineCurve)}脚本");
return;
}
timer = 0f;
}
/// <summary>
/// 设置位置
/// </summary>
/// <param name="index"></param>
[Button]
public void SetPos(int index)
{
if (listPoint.Count <= index)
{
DebugUtil.LogError("设置移动点位越界");
return;
}
if (curIndex == index)
return;
curIndex = index;
targetIndex = curIndex;
GameObject goTarget = listPoint[index];
transform.position = goTarget.transform.position;
}
}
}