2024-04-09 13:22:52 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using DG.Tweening;
|
|
|
|
|
using Game.Pathfinding;
|
2024-04-10 18:18:48 +08:00
|
|
|
|
using Game.Spine;
|
2024-04-09 13:22:52 +08:00
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
namespace Game.Boss
|
2024-04-09 13:22:52 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
[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;
|
2024-04-10 18:18:48 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
private SpineAnimator _animator;
|
2024-04-10 18:18:48 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public Vector2 birthPoint => this._birthPoint;
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
this._animator = this.GetComponent<SpineAnimator>();
|
|
|
|
|
}
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public void SetBoss(IBoss boss)
|
|
|
|
|
{
|
|
|
|
|
this._boss = boss;
|
|
|
|
|
this.gameObject.name = this._boss.name;
|
|
|
|
|
transform.position = _birthPoint;
|
|
|
|
|
}
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-11 10:17:57 +08:00
|
|
|
|
// public async UniTask<bool> MoveAsync(List<WayPoint> 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;
|
|
|
|
|
// }
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public async UniTask<bool> MoveAsync(List<WayPoint> wayPoint, CancellationToken token)
|
2024-04-09 13:22:52 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
this._animator.PlayAni("walk", true);
|
|
|
|
|
// enter this room
|
|
|
|
|
foreach (var point in wayPoint)
|
|
|
|
|
{
|
|
|
|
|
await this.MoveAsync(point.position, token);
|
|
|
|
|
}
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
this._animator.PlayAni("idle", true);
|
2024-04-10 18:18:48 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
// enter room a point
|
2024-04-09 13:22:52 +08:00
|
|
|
|
// await this.MoveAsync(endPos, token);
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
async UniTask MoveAsync(Vector2 endPos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var position = this.transform.position;
|
|
|
|
|
var distance = Vector3.Distance(position, endPos);
|
|
|
|
|
var time = distance / this.speed;
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
CheckIsNeedTurn(position, endPos);
|
|
|
|
|
|
|
|
|
|
this.transform.DOMove(endPos, time).SetEase(Ease.Linear);
|
|
|
|
|
float delayTimeSpan = time * 1000;
|
|
|
|
|
await UniTask.Delay((int)delayTimeSpan); // (int)delayTimeSpan
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-09 18:16:37 +08:00
|
|
|
|
// Debug.Log($"time is {time}, await time is {delayTimeSpan}");
|
2024-04-11 16:44:25 +08:00
|
|
|
|
}
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-12 17:32:37 +08:00
|
|
|
|
#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
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public async UniTask Kill(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
this._animator.PlayAni("atk");
|
|
|
|
|
await UniTask.Delay(500);
|
|
|
|
|
this._animator.PlayAni("idle", true);
|
|
|
|
|
}
|
2024-04-09 13:22:52 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public void Dispose()
|
2024-04-11 14:44:39 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
this._animator = null;
|
|
|
|
|
this._boss = null;
|
|
|
|
|
GameObject.DestroyImmediate(this.gameObject);
|
2024-04-11 14:44:39 +08:00
|
|
|
|
}
|
2024-04-11 16:44:25 +08:00
|
|
|
|
|
|
|
|
|
void CheckIsNeedTurn(Vector2 str, Vector2 end)
|
2024-04-11 14:44:39 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
2024-04-11 14:44:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-09 13:22:52 +08:00
|
|
|
|
}
|