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

59 lines
1.6 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;
2024-04-07 17:20:48 +08:00
namespace Game.Player
2024-04-05 18:12:04 +08:00
{
2024-04-07 17:20:48 +08:00
class PlayerInfo : MonoBehaviour
2024-04-05 18:12:04 +08:00
{
2024-04-07 17:20:48 +08:00
[SerializeField] [ReadOnly] private IPlayer _player;
[SerializeField] private float speed = 1f;
2024-04-06 11:59:18 +08:00
2024-04-07 17:20:48 +08:00
public void SetPlayer(IPlayer player)
2024-04-06 11:59:18 +08:00
{
2024-04-07 17:20:48 +08:00
this._player = player;
this.gameObject.name = this._player.playerName;
2024-04-06 11:59:18 +08:00
}
2024-04-07 17:20:48 +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);
}
2024-04-06 11:59:18 +08:00
2024-04-07 17:20:48 +08:00
// enter room a point
await Move(endPos, token);
2024-04-06 11:59:18 +08:00
2024-04-07 17:20:48 +08:00
return true;
}
async UniTask Move(Vector2 endPos, CancellationToken token)
{
var distance = Vector3.Distance(this.transform.position, endPos);
var time = distance / this.speed;
2024-04-06 11:59:18 +08:00
// bool isMoveing = true;
// while (isMoveing)
// {
// await UniTask.Yield(token);
// if (Vector3.Distance(transform.position, endPos) <= 0.1f)
// {
// isMoveing = false;
// break;
// }
//
// transform.DOMove(endPos, time);
// }
2024-04-07 17:20:48 +08:00
transform.DOMove(endPos, time);
float delayTimeSpan = time * 1000;
await UniTask.Delay((int)delayTimeSpan);
}
2024-04-06 11:59:18 +08:00
}
2024-04-05 18:12:04 +08:00
}