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

112 lines
3.1 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 Game.Pathfinding;
using Game.Room;
using UnityEngine;
namespace Game.Player;
public interface IPlayer
{
string playerName { get; }
GameObject self { get; }
PlayerData playerData { get; }
IRoom room { get; }
2024-04-08 18:20:55 +08:00
bool isMoving { get; }
2024-04-06 11:59:18 +08:00
void SetGameObject(GameObject gameObject, string name, float jinbei);
2024-04-08 18:20:55 +08:00
void SetRoom(IRoom roo);
UniTask<bool> WaitMoveRoomAsync(IRoom roo, CancellationToken token);
UniTask<bool> MoveAsync(List<WayPoint> wayPoint, CancellationToken token);
void Init();
2024-04-06 11:59:18 +08:00
void Dispose();
}
2024-04-08 18:20:55 +08:00
[System.Serializable]
2024-04-06 11:59:18 +08:00
internal class Player : IPlayer
{
private GameObject _self;
private PlayerData _playerData;
private IRoom _room;
PlayerInfo playerInfo;
2024-04-08 18:20:55 +08:00
private bool _isMoving;
2024-04-06 11:59:18 +08:00
public string playerName => this._playerData.playerName;
public GameObject self => this._self;
public PlayerData playerData => this._playerData;
public IRoom room => this._room;
2024-04-08 18:20:55 +08:00
public bool isMoving => this._isMoving;
2024-04-06 11:59:18 +08:00
public void SetGameObject(GameObject gameObject, string name, float jinbei)
{
this._self = gameObject;
this._playerData = new PlayerData(name, jinbei, 100);
// view
2024-04-06 11:59:18 +08:00
playerInfo = this._self.GetComponent<PlayerInfo>();
playerInfo.SetPlayer(this);
2024-04-08 18:20:55 +08:00
this._isMoving = true;
2024-04-06 11:59:18 +08:00
}
2024-04-08 18:20:55 +08:00
public void SetRoom(IRoom roo)
2024-04-06 11:59:18 +08:00
{
2024-04-08 18:20:55 +08:00
this._room?.Quit(this);
this._room = roo;
// EventManager.Instance.FireNow(this, new PlayerMoveToRoomEventArgs(this, this._room));
}
2024-04-08 18:20:55 +08:00
public async UniTask<bool> WaitMoveRoomAsync(IRoom roo, CancellationToken token)
{
this._room?.Quit(this);
2024-04-06 11:59:18 +08:00
this._room = roo;
2024-04-08 18:20:55 +08:00
// EventManager.Instance.FireNow(this, new PlayerMoveToRoomEventArgs(this, this._room));
var wayPoints = Game.bfsManager.FindPath(_room.roomInfo.room_Center);
await UniTask.Yield();
var moveAsync = await this.MoveAsync(wayPoints, token);
Debug.Log("Move finish !!!");
return moveAsync;
}
public async UniTask<bool> MoveAsync(List<WayPoint> wayPoint, CancellationToken token)
{
if (!this._isMoving) return false;
_isMoving = false;
var endPos = this._room.roomInfo.GetJoinPosition();
2024-04-06 11:59:18 +08:00
await playerInfo.MoveAsync(wayPoint, endPos, token);
2024-04-08 18:20:55 +08:00
this._isMoving = true;
2024-04-06 11:59:18 +08:00
return true;
}
public virtual void Init()
2024-04-06 11:59:18 +08:00
{
EventManager.Instance.Subscribe(PlayerJinbeiChangeEventArgs.EventId, PlayerJinbeiChangeEvent);
}
public virtual void Dispose()
{
EventManager.Instance.Unsubscribe(PlayerJinbeiChangeEventArgs.EventId, PlayerJinbeiChangeEvent);
2024-04-06 11:59:18 +08:00
GameObject.DestroyImmediate(this.self);
}
private void PlayerJinbeiChangeEvent(object sender, GameEventArgs e)
{
var args = e as PlayerJinbeiChangeEventArgs;
var jinbei = this._playerData.jinbei - args.jinbei;
if (jinbei >= 0)
{
this._playerData.jinbei = jinbei;
args.callback?.Invoke(true);
}
else
args.callback?.Invoke(false);
}
2024-04-06 11:59:18 +08:00
}