Frame/Assets/Scripts/Player/PlayerInfo.cs

48 lines
1.4 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)
{
2024-04-08 18:20:55 +08:00
await this.MoveAsync(point.position, token);
2024-04-07 17:20:48 +08:00
}
2024-04-06 11:59:18 +08:00
2024-04-07 17:20:48 +08:00
// enter room a point
2024-04-08 18:20:55 +08:00
await this.MoveAsync(endPos, token);
2024-04-06 11:59:18 +08:00
2024-04-07 17:20:48 +08:00
return true;
}
2024-04-08 18:20:55 +08:00
async UniTask MoveAsync(Vector2 endPos, CancellationToken token)
2024-04-07 17:20:48 +08:00
{
var distance = Vector3.Distance(this.transform.position, endPos);
var time = distance / this.speed;
2024-04-06 11:59:18 +08:00
2024-04-08 18:20:55 +08:00
transform.DOMove(endPos, time).SetEase(Ease.Linear);
2024-04-07 17:20:48 +08:00
float delayTimeSpan = time * 1000;
2024-04-08 18:20:55 +08:00
await UniTask.Delay((int)delayTimeSpan); // (int)delayTimeSpan
Debug.Log($"time is {time}, await time is {delayTimeSpan}");
2024-04-07 17:20:48 +08:00
}
2024-04-06 11:59:18 +08:00
}
2024-04-05 18:12:04 +08:00
}