Frame/Assets/Scripts/Player/PlayerInfo.cs

54 lines
1.3 KiB
C#
Raw Normal View History

2024-04-06 11:59:18 +08:00
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Game.Pathfinding;
using Sirenix.OdinInspector;
2024-04-05 18:12:04 +08:00
using UnityEngine;
namespace Game.Player;
class PlayerInfo : MonoBehaviour
{
2024-04-06 11:59:18 +08:00
[SerializeField] [ReadOnly] private IPlayer _player;
[SerializeField] private float speed = 1f;
2024-04-05 18:12:04 +08:00
2024-04-06 11:59:18 +08:00
public void SetPlayer(IPlayer player)
2024-04-05 18:12:04 +08:00
{
this._player = player;
this.gameObject.name = this._player.playerName;
}
2024-04-06 11:59:18 +08:00
public async UniTask<bool> MoveAsync(List<WayPoint> wayPoint, Vector2 endPos, CancellationToken token)
{
// enter this room
foreach (var point in wayPoint)
{
await Move(point.position, token);
}
// enter room a point
await Move(endPos, token);
return true;
}
async UniTask Move(Vector2 endPos, CancellationToken token)
{
var distance = Vector3.Distance(this.transform.position, endPos);
var time = distance / this.speed;
bool isMoveing = true;
while (isMoveing)
{
await UniTask.Yield(token);
if (Vector3.Distance(transform.position, endPos) <= 0.05f)
{
isMoveing = false;
break;
}
transform.DOMove(endPos, time);
}
}
2024-04-05 18:12:04 +08:00
}