using System; using System.Collections.Generic; using System.Threading; using Cysharp.Threading.Tasks; using DG.Tweening; using Game.Pathfinding; using Game.Spine; using Sirenix.OdinInspector; using UnityEngine; namespace Game.Boss { [RequireComponent(typeof(SpineAnimator))] public class BossInfo : MonoBehaviour { [SerializeField] [ReadOnly] private IBoss _boss; [SerializeField] private float speed = 1f; private static readonly int _kill = Animator.StringToHash("Kill"); [SerializeField] private Vector2 _birthPoint; private SpineAnimator _animator; public Vector2 birthPoint => this._birthPoint; private void Awake() { this._animator = this.GetComponent(); } public void SetBoss(IBoss boss) { this._boss = boss; this.gameObject.name = this._boss.name; transform.position = _birthPoint; } // public async UniTask MoveAsync(List wayPoint, Vector2 endPos, CancellationToken token) // { // // enter this room // foreach (var point in wayPoint) // { // await this.MoveAsync(point.position, token); // } // // // enter room a point // await this.MoveAsync(endPos, token); // // return true; // } public async UniTask MoveAsync(List wayPoint, CancellationToken token) { this._animator.PlayAni("walk", true); // enter this room foreach (var point in wayPoint) { await this.MoveAsync(point.position, token); } this._animator.PlayAni("idle", true); // enter room a point // await this.MoveAsync(endPos, token); return true; } async UniTask MoveAsync(Vector2 endPos, CancellationToken token) { var position = this.transform.position; var distance = Vector3.Distance(position, endPos); var time = distance / this.speed; CheckIsNeedTurn(position, endPos); this.transform.DOMove(endPos, time).SetEase(Ease.Linear); float delayTimeSpan = time * 1000; await UniTask.Delay((int)delayTimeSpan); // (int)delayTimeSpan // Debug.Log($"time is {time}, await time is {delayTimeSpan}"); } #region Add Test private float startTime; // 开始移动的时间 private float journeyLength; // 移动的总距离 async UniTask MoveToAsync(Vector2 end, CancellationToken token) { journeyLength = Vector3.Distance(transform.position, end); startTime = Time.time; while (true) { // 计算从开始移动到现在所经过的时间 float journeyTime = Time.time - startTime; // 计算当前位置所在的百分比 float fracJourney = journeyTime / journeyLength; // 使用线性插值计算当前位置 transform.position = Vector2.Lerp(transform.position, end, fracJourney * this.speed); // 如果当前位置接近于目标位置,停止移动 if (fracJourney >= 0.1f) { // 将物体位置设置为目标位置,确保准确到达目标点 transform.position = end; break; } await UniTask.Yield(); } } #endregion public async UniTask Kill(CancellationToken token) { this._animator.PlayAni("atk"); await UniTask.Delay(500); this._animator.PlayAni("idle", true); } public void Dispose() { this._animator = null; this._boss = null; GameObject.DestroyImmediate(this.gameObject); } void CheckIsNeedTurn(Vector2 str, Vector2 end) { if (str.x > end.x) { transform.localEulerAngles = new Vector3(0, 180, 0); } else if (str.x < end.x) { transform.localEulerAngles = new Vector3(0, 0, 0); } } } }