Frame/Assets/Scripts/Boss/BossInfo.cs

109 lines
3.1 KiB
C#

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<SpineAnimator>();
}
public void SetBoss(IBoss boss)
{
this._boss = boss;
this.gameObject.name = this._boss.name;
transform.position = _birthPoint;
}
// 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;
// }
public async UniTask<bool> MoveAsync(List<WayPoint> 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}");
}
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);
}
}
}
}