125 lines
4.1 KiB
C#
125 lines
4.1 KiB
C#
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; }
|
|
bool isMoving { get; }
|
|
void SetGameObject(GameObject gameObject, string name, float jinbei);
|
|
void SetRoom(IRoom roo);
|
|
UniTask<bool> WaitMoveRoomAsync(IRoom roo, CancellationToken token);
|
|
UniTask<bool> MoveAsync(List<WayPoint> wayPoint, CancellationToken token);
|
|
void Init();
|
|
void Dispose();
|
|
}
|
|
|
|
[System.Serializable]
|
|
internal class Player : IPlayer
|
|
{
|
|
private GameObject _self;
|
|
private PlayerData _playerData;
|
|
private IRoom _room;
|
|
PlayerInfo playerInfo;
|
|
private bool _isMoving;
|
|
|
|
public string playerName => this._playerData.playerName;
|
|
|
|
public GameObject self => this._self;
|
|
|
|
public PlayerData playerData => this._playerData;
|
|
|
|
public IRoom room => this._room;
|
|
|
|
public bool isMoving => this._isMoving;
|
|
|
|
|
|
public void SetGameObject(GameObject gameObject, string name, float jinbei)
|
|
{
|
|
this._self = gameObject;
|
|
this._playerData = new PlayerData(name, jinbei, 100);
|
|
|
|
// view
|
|
playerInfo = this._self.GetComponent<PlayerInfo>();
|
|
playerInfo.SetPlayer(this);
|
|
this._isMoving = true;
|
|
}
|
|
|
|
public void SetRoom(IRoom roo)
|
|
{
|
|
this._room?.Quit(this);
|
|
this._room = roo;
|
|
// EventManager.Instance.FireNow(this, new PlayerMoveToRoomEventArgs(this, this._room));
|
|
}
|
|
|
|
public async UniTask<bool> WaitMoveRoomAsync(IRoom roo, CancellationToken token)
|
|
{
|
|
this._room?.Quit(this);
|
|
this._room = roo;
|
|
// 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();
|
|
|
|
await playerInfo.MoveAsync(wayPoint, endPos, token);
|
|
this._isMoving = true;
|
|
return true;
|
|
}
|
|
|
|
public virtual void Init()
|
|
{
|
|
EventManager.Instance.Subscribe(PlayerJinBeiChange_ToPlayerEventArgs.EventId, PlayerJinbeiChangeEvent);
|
|
EventManager.Instance.Subscribe(ReturnPlayerJinBeiEventArgs.EventId, ReturnPlayerJinBeiEvent);
|
|
}
|
|
|
|
public virtual void Dispose()
|
|
{
|
|
EventManager.Instance.Unsubscribe(PlayerJinBeiChange_ToPlayerEventArgs.EventId, PlayerJinbeiChangeEvent);
|
|
EventManager.Instance.Unsubscribe(ReturnPlayerJinBeiEventArgs.EventId, ReturnPlayerJinBeiEvent);
|
|
GameObject.DestroyImmediate(this.self);
|
|
}
|
|
|
|
private void ReturnPlayerJinBeiEvent(object sender, GameEventArgs e)
|
|
{
|
|
var args = e as ReturnPlayerJinBeiEventArgs;
|
|
if (args.player == this)
|
|
{
|
|
this._playerData.ReturnJinBei(args.jinBei);
|
|
EventManager.Instance.FireNow(this, new PlayerJinBeiChange_ToMainUIEventArgs(this));
|
|
}
|
|
}
|
|
|
|
private void PlayerJinbeiChangeEvent(object sender, GameEventArgs e)
|
|
{
|
|
var args = e as PlayerJinBeiChange_ToPlayerEventArgs;
|
|
var jinbei = this._playerData.jinbei - args.jinbei;
|
|
if (jinbei >= 0)
|
|
{
|
|
this._playerData.jinbei = jinbei;
|
|
args.callback?.Invoke(true);
|
|
}
|
|
else
|
|
args.callback?.Invoke(false);
|
|
}
|
|
}
|
|
} |