forked from zxl/Frame
1
0
Fork 0
Frame/Assets/Scripts/Player/PlayerInfo.cs

48 lines
1.4 KiB
C#

using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Game.Pathfinding;
using Sirenix.OdinInspector;
using UnityEngine;
namespace Game.Player
{
class PlayerInfo : MonoBehaviour
{
[SerializeField] [ReadOnly] private IPlayer _player;
[SerializeField] private float speed = 1f;
public void SetPlayer(IPlayer player)
{
this._player = player;
this.gameObject.name = this._player.playerName;
}
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;
}
async UniTask MoveAsync(Vector2 endPos, CancellationToken token)
{
var distance = Vector3.Distance(this.transform.position, endPos);
var time = distance / this.speed;
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}");
}
}
}