197 lines
6.2 KiB
C#
197 lines
6.2 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 enum RoleType
|
||
{
|
||
Player,
|
||
Robot,
|
||
}
|
||
|
||
public interface IPlayer
|
||
{
|
||
long uId { get; }
|
||
string playerName { get; }
|
||
RoleType roleType { get; }
|
||
GameObject self { get; }
|
||
PlayerData playerData { get; }
|
||
IRoom room { get; }
|
||
bool isMoving { get; }
|
||
bool IsCanDelete { get; }
|
||
|
||
UniTask<bool> WaitMoveRoomAsync(IRoom roo, CancellationToken token);
|
||
void JumpToRoom(IRoom room);
|
||
void MoneyRefresh(float money);
|
||
void Init();
|
||
void Dispose();
|
||
}
|
||
|
||
[System.Serializable]
|
||
internal class Player : IPlayer
|
||
{
|
||
private GameObject _self;
|
||
private PlayerData _playerData;
|
||
private IRoom _room;
|
||
PlayerInfo playerInfo;
|
||
private bool _isMoving;
|
||
|
||
private IBFSManager _bfsManager;
|
||
private RoleType _roleType;
|
||
private long _uId;
|
||
|
||
public long uId => this._uId;
|
||
|
||
public string playerName => this._playerData.playerName;
|
||
|
||
public RoleType roleType => this._roleType;
|
||
|
||
public GameObject self => this._self;
|
||
|
||
public PlayerData playerData => this._playerData;
|
||
|
||
public IRoom room => this._room;
|
||
|
||
public bool isMoving => this._isMoving;
|
||
|
||
public bool IsCanDelete
|
||
{
|
||
get
|
||
{
|
||
if (this.isMoving || this._room.roomType != RoomType.出生点)
|
||
return false;
|
||
return true;
|
||
}
|
||
}
|
||
|
||
public Player(string name, long uId, GameObject go, IBFSManager bfs, RoleType roleType)
|
||
{
|
||
this._playerData = new PlayerData(name, 100);
|
||
this._bfsManager = bfs;
|
||
this._roleType = roleType;
|
||
this._uId = uId;
|
||
|
||
// view
|
||
this._self = go;
|
||
playerInfo = this._self.GetComponent<PlayerInfo>();
|
||
playerInfo.SetPlayer(this);
|
||
this._isMoving = false;
|
||
}
|
||
|
||
// public void SetGo(GameObject gameObject)
|
||
// {
|
||
// this._self = gameObject;
|
||
//
|
||
// // view
|
||
// playerInfo = this._self.GetComponent<PlayerInfo>();
|
||
// playerInfo.SetPlayer(this);
|
||
// this._isMoving = false;
|
||
// }
|
||
|
||
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 = this._bfsManager.FindPath(this._self.transform.position, _room.roomInfo.room_Center);
|
||
|
||
await UniTask.Yield();
|
||
var moveAsync = await this.MoveAsync(wayPoints, token);
|
||
// Debug.Log("Move finish !!!");
|
||
return moveAsync;
|
||
}
|
||
|
||
public void JumpToRoom(IRoom room)
|
||
{
|
||
this._room?.Quit(this);
|
||
this._room = room;
|
||
|
||
var endPos = this._room.roomInfo.GetJoinPosition();
|
||
playerInfo.JumpTo(endPos);
|
||
}
|
||
|
||
public void MoneyRefresh(float money)
|
||
{
|
||
this._playerData.RefreshMoney(money);
|
||
EventManager.Instance.FireNow(this, new PlayerJinBeiChange_ToMainUIEventArgs());
|
||
}
|
||
|
||
private async UniTask<bool> MoveAsync(List<WayPoint> wayPoint, CancellationToken token)
|
||
{
|
||
if (this._isMoving) return false;
|
||
|
||
_isMoving = true;
|
||
var endPos = this._room.roomInfo.GetJoinPosition();
|
||
|
||
await playerInfo.MoveAsync(wayPoint, endPos, token);
|
||
this._isMoving = false;
|
||
return true;
|
||
}
|
||
|
||
public virtual void Init()
|
||
{
|
||
EventManager.Instance.Subscribe(PlayerJinBeiChange_ToPlayerEventArgs.EventId, PlayerJinbeiChangeEvent);
|
||
EventManager.Instance.Subscribe(PlayerJinBeiChange_VictoryEventArgs.EventId, PlayerJinBeiChange_VictoryEvent);
|
||
// EventManager.Instance.Subscribe(ReturnPlayerJinBeiEventArgs.EventId, ReturnPlayerJinBeiEvent);
|
||
}
|
||
|
||
public virtual void Dispose()
|
||
{
|
||
EventManager.Instance.Unsubscribe(PlayerJinBeiChange_ToPlayerEventArgs.EventId, PlayerJinbeiChangeEvent);
|
||
EventManager.Instance.Unsubscribe(PlayerJinBeiChange_VictoryEventArgs.EventId, PlayerJinBeiChange_VictoryEvent);
|
||
// EventManager.Instance.Unsubscribe(ReturnPlayerJinBeiEventArgs.EventId, ReturnPlayerJinBeiEvent);
|
||
|
||
if (this._room != null)
|
||
this._room.Quit(this);
|
||
|
||
this._room = null;
|
||
this.playerInfo = null;
|
||
this._isMoving = false;
|
||
this._playerData = null;
|
||
GameObject.DestroyImmediate(this.self);
|
||
this._self = null;
|
||
}
|
||
|
||
// private void ReturnPlayerJinBeiEvent(object sender, GameEventArgs e)
|
||
// {
|
||
// var args = e as ReturnPlayerJinBeiEventArgs;
|
||
// if (args != null && args.player == this)
|
||
// {
|
||
// this._playerData.ReturnJinBei(args.jinBei);
|
||
// EventManager.Instance.FireNow(this, new PlayerJinBeiChange_ToMainUIEventArgs(this));
|
||
// }
|
||
// }
|
||
|
||
private void PlayerJinBeiChange_VictoryEvent(object sender, GameEventArgs e)
|
||
{
|
||
var args = e as PlayerJinBeiChange_VictoryEventArgs;
|
||
if (args == null || args.player != this)
|
||
return;
|
||
|
||
var jin = this._playerData.jinbei + args.jinbei;
|
||
this._playerData.jinbei = jin;
|
||
}
|
||
|
||
private void PlayerJinbeiChangeEvent(object sender, GameEventArgs e)
|
||
{
|
||
var args = e as PlayerJinBeiChange_ToPlayerEventArgs;
|
||
if (args == null || args.player != this)
|
||
return;
|
||
|
||
var jinbei = this._playerData.jinbei - args.jinbei;
|
||
// Debug.Log($"{playerName} 下注了 {args.jinbei} 金杯 , 剩余 {jinbei} 金杯");
|
||
if (jinbei >= 0)
|
||
{
|
||
this._playerData.jinbei = jinbei;
|
||
args.callback?.Invoke(true);
|
||
}
|
||
else
|
||
args.callback?.Invoke(false);
|
||
}
|
||
}
|
||
} |