Frame/Assets/Scripts/Player/PlayerInfo.cs

78 lines
2.2 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 Spine.Unity;
using UnityEngine;
namespace Game.Player
{
[RequireComponent(typeof(SpineAnimator))]
class PlayerInfo : MonoBehaviour
{
[SerializeField] [ReadOnly] private Player _player;
[SerializeField] private float speed = 1f;
[SerializeField] private Vector2 birthPoint;
[SerializeField] private SpineAnimator _animator;
private void Awake()
{
_animator = this.GetComponent<SpineAnimator>();
}
public void SetPlayer(Player player)
{
this._player = player;
this.gameObject.name = this._player.playerName;
transform.position = birthPoint;
}
public async UniTask<bool> MoveAsync(List<WayPoint> wayPoint, Vector2 endPos, CancellationToken token)
{
_animator.PlayAni("walk", true);
// enter this room
foreach (var point in wayPoint)
{
await this.MoveAsync(point.position, token);
}
// enter room a point
await this.MoveAsync(endPos, token);
_animator.PlayAni("idle", true);
return true;
}
async UniTask MoveAsync(Vector2 endPos, CancellationToken token)
{
var position = transform.position;
var distance = Vector3.Distance(position, endPos);
var time = distance / this.speed;
CheckIsNeedTurn(position, endPos);
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}");
}
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);
}
}
}
}