Frame/Assets/Scripts/Boss/BossInfo.cs

93 lines
2.5 KiB
C#
Raw Normal View History

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;
using Game.Spine;
2024-04-09 13:22:52 +08:00
using Sirenix.OdinInspector;
using UnityEngine;
namespace Game.Boss;
[RequireComponent(typeof(SpineAnimator))]
2024-04-09 13:22:52 +08:00
public class BossInfo : MonoBehaviour
{
[SerializeField] [ReadOnly] private IBoss _boss;
[SerializeField] private float speed = 1f;
private static readonly int _kill = Animator.StringToHash("Kill");
2024-04-09 18:16:37 +08:00
[SerializeField] private Vector2 _birthPoint;
private SpineAnimator _animator;
2024-04-09 18:16:37 +08:00
public Vector2 birthPoint => this._birthPoint;
2024-04-09 13:22:52 +08:00
private void Awake()
{
this._animator = this.GetComponent<SpineAnimator>();
2024-04-09 13:22:52 +08:00
}
2024-04-09 18:16:37 +08:00
public void SetBoss(IBoss boss)
2024-04-09 13:22:52 +08:00
{
this._boss = boss;
this.gameObject.name = this._boss.name;
2024-04-09 18:16:37 +08:00
transform.position = _birthPoint;
2024-04-09 13:22:52 +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
public async UniTask<bool> MoveAsync(List<WayPoint> wayPoint, CancellationToken token)
{
this._animator.PlayAni("walk", true);
2024-04-09 13:22:52 +08:00
// enter this room
foreach (var point in wayPoint)
{
await this.MoveAsync(point.position, token);
}
this._animator.PlayAni("idle", true);
2024-04-09 13:22:52 +08:00
// enter room a point
// await this.MoveAsync(endPos, token);
return true;
}
async UniTask MoveAsync(Vector2 endPos, CancellationToken token)
{
var distance = Vector3.Distance(this.transform.position, endPos);
var time = distance / this.speed;
this.transform.DOMove(endPos, time).SetEase(Ease.Linear);
float delayTimeSpan = time * 1000;
await UniTask.Delay((int)delayTimeSpan); // (int)delayTimeSpan
2024-04-09 18:16:37 +08:00
// Debug.Log($"time is {time}, await time is {delayTimeSpan}");
2024-04-09 13:22:52 +08:00
}
public async UniTask Kill(CancellationToken token)
{
this._animator.PlayAni("atk");
2024-04-09 13:22:52 +08:00
await UniTask.Delay(500);
this._animator.PlayAni("idle", true);
2024-04-09 13:22:52 +08:00
}
public void Dispose()
{
this._animator = null;
this._boss = null;
GameObject.DestroyImmediate(this.gameObject);
}
}