using GameWrapper.Curve; using Sirenix.OdinInspector; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace GameWrapper.FlareLevel { public sealed class FlareLevelCar : MonoBehaviour { /// /// 移动时间 /// private const float MoveStepTime = 1f; #region 私有变量 /// /// 当前位置 /// private int curIndex; /// /// 下一个目标点位 /// private BSplineCurve nextTarget; /// /// 目标位置 /// private int targetIndex; /// /// 移动计时 /// private float timer; /// /// 点位列表 /// private List listPoint; #endregion /// /// 点位根节点 /// public Transform TsPointRoot; private void Awake() { if (null == TsPointRoot) { listPoint = new List(1); return; } listPoint = new List(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); } } /// /// 设置移动位置 /// [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(); if (null == nextTarget) { DebugUtil.LogError($"点位没有挂载{nameof(BSplineCurve)}脚本"); return; } timer = 0f; } /// /// 设置位置 /// /// [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; } } }