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;
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
namespace Game.Player
|
2024-04-06 11:59:18 +08:00
|
|
|
|
{
|
2024-04-27 11:37:11 +08:00
|
|
|
|
public enum RoleType
|
|
|
|
|
{
|
|
|
|
|
Player,
|
|
|
|
|
Robot,
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public interface IPlayer
|
|
|
|
|
{
|
2024-05-05 17:02:33 +08:00
|
|
|
|
long uId { get; }
|
2024-04-11 16:44:25 +08:00
|
|
|
|
string playerName { get; }
|
2024-04-27 11:37:11 +08:00
|
|
|
|
RoleType roleType { get; }
|
2024-04-11 16:44:25 +08:00
|
|
|
|
GameObject self { get; }
|
|
|
|
|
PlayerData playerData { get; }
|
|
|
|
|
IRoom room { get; }
|
|
|
|
|
bool isMoving { get; }
|
2024-04-24 17:52:34 +08:00
|
|
|
|
bool IsCanDelete { get; }
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
UniTask<bool> WaitMoveRoomAsync(IRoom roo, CancellationToken token);
|
2024-04-24 17:52:34 +08:00
|
|
|
|
void JumpToRoom(IRoom room);
|
2024-05-05 17:02:33 +08:00
|
|
|
|
void MoneyRefresh(float money);
|
2024-04-11 16:44:25 +08:00
|
|
|
|
void Init();
|
|
|
|
|
void Dispose();
|
|
|
|
|
}
|
2024-04-06 11:59:18 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
[System.Serializable]
|
|
|
|
|
internal class Player : IPlayer
|
|
|
|
|
{
|
|
|
|
|
private GameObject _self;
|
|
|
|
|
private PlayerData _playerData;
|
|
|
|
|
private IRoom _room;
|
|
|
|
|
PlayerInfo playerInfo;
|
|
|
|
|
private bool _isMoving;
|
2024-04-06 11:59:18 +08:00
|
|
|
|
|
2024-04-24 17:52:34 +08:00
|
|
|
|
private IBFSManager _bfsManager;
|
2024-04-27 11:37:11 +08:00
|
|
|
|
private RoleType _roleType;
|
2024-05-05 17:02:33 +08:00
|
|
|
|
private long _uId;
|
|
|
|
|
|
|
|
|
|
public long uId => this._uId;
|
2024-04-24 17:52:34 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public string playerName => this._playerData.playerName;
|
2024-04-06 11:59:18 +08:00
|
|
|
|
|
2024-04-27 11:37:11 +08:00
|
|
|
|
public RoleType roleType => this._roleType;
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public GameObject self => this._self;
|
2024-04-06 11:59:18 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public PlayerData playerData => this._playerData;
|
2024-04-06 11:59:18 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public IRoom room => this._room;
|
2024-04-06 18:15:42 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public bool isMoving => this._isMoving;
|
2024-04-06 18:15:42 +08:00
|
|
|
|
|
2024-04-24 17:52:34 +08:00
|
|
|
|
public bool IsCanDelete
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (this.isMoving || this._room.roomType != RoomType.出生点)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-06 18:15:42 +08:00
|
|
|
|
|
2024-05-05 17:02:33 +08:00
|
|
|
|
public Player(string name, long uId, GameObject go, IBFSManager bfs, RoleType roleType)
|
2024-04-11 16:44:25 +08:00
|
|
|
|
{
|
2024-05-05 17:02:33 +08:00
|
|
|
|
this._playerData = new PlayerData(name, 100);
|
2024-04-24 17:52:34 +08:00
|
|
|
|
this._bfsManager = bfs;
|
2024-04-27 11:37:11 +08:00
|
|
|
|
this._roleType = roleType;
|
2024-05-05 17:02:33 +08:00
|
|
|
|
this._uId = uId;
|
2024-04-06 11:59:18 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
// view
|
2024-04-24 17:52:34 +08:00
|
|
|
|
this._self = go;
|
2024-04-11 16:44:25 +08:00
|
|
|
|
playerInfo = this._self.GetComponent<PlayerInfo>();
|
|
|
|
|
playerInfo.SetPlayer(this);
|
2024-04-12 17:32:37 +08:00
|
|
|
|
this._isMoving = false;
|
2024-04-11 16:44:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 17:52:34 +08:00
|
|
|
|
// public void SetGo(GameObject gameObject)
|
|
|
|
|
// {
|
|
|
|
|
// this._self = gameObject;
|
|
|
|
|
//
|
|
|
|
|
// // view
|
|
|
|
|
// playerInfo = this._self.GetComponent<PlayerInfo>();
|
|
|
|
|
// playerInfo.SetPlayer(this);
|
|
|
|
|
// this._isMoving = false;
|
|
|
|
|
// }
|
2024-04-06 18:15:42 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public async UniTask<bool> WaitMoveRoomAsync(IRoom roo, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
this._room?.Quit(this);
|
|
|
|
|
this._room = roo;
|
2024-04-08 18:20:55 +08:00
|
|
|
|
// EventManager.Instance.FireNow(this, new PlayerMoveToRoomEventArgs(this, this._room));
|
|
|
|
|
|
2024-04-24 17:52:34 +08:00
|
|
|
|
var wayPoints = this._bfsManager.FindPath(this._self.transform.position, _room.roomInfo.room_Center);
|
2024-04-08 18:20:55 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
await UniTask.Yield();
|
|
|
|
|
var moveAsync = await this.MoveAsync(wayPoints, token);
|
2024-04-11 14:44:39 +08:00
|
|
|
|
// Debug.Log("Move finish !!!");
|
2024-04-11 16:44:25 +08:00
|
|
|
|
return moveAsync;
|
|
|
|
|
}
|
2024-04-08 18:20:55 +08:00
|
|
|
|
|
2024-04-24 17:52:34 +08:00
|
|
|
|
public void JumpToRoom(IRoom room)
|
|
|
|
|
{
|
|
|
|
|
this._room?.Quit(this);
|
|
|
|
|
this._room = room;
|
|
|
|
|
|
|
|
|
|
var endPos = this._room.roomInfo.GetJoinPosition();
|
|
|
|
|
playerInfo.JumpTo(endPos);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-05 17:02:33 +08:00
|
|
|
|
public void MoneyRefresh(float money)
|
|
|
|
|
{
|
|
|
|
|
this._playerData.RefreshMoney(money);
|
|
|
|
|
EventManager.Instance.FireNow(this, new PlayerJinBeiChange_ToMainUIEventArgs());
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 17:52:34 +08:00
|
|
|
|
private async UniTask<bool> MoveAsync(List<WayPoint> wayPoint, CancellationToken token)
|
2024-04-11 16:44:25 +08:00
|
|
|
|
{
|
2024-04-12 17:32:37 +08:00
|
|
|
|
if (this._isMoving) return false;
|
2024-04-08 18:20:55 +08:00
|
|
|
|
|
2024-04-12 17:32:37 +08:00
|
|
|
|
_isMoving = true;
|
2024-04-11 16:44:25 +08:00
|
|
|
|
var endPos = this._room.roomInfo.GetJoinPosition();
|
2024-04-06 11:59:18 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
await playerInfo.MoveAsync(wayPoint, endPos, token);
|
2024-04-12 17:32:37 +08:00
|
|
|
|
this._isMoving = false;
|
2024-04-11 16:44:25 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-04-06 11:59:18 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public virtual void Init()
|
|
|
|
|
{
|
|
|
|
|
EventManager.Instance.Subscribe(PlayerJinBeiChange_ToPlayerEventArgs.EventId, PlayerJinbeiChangeEvent);
|
2024-04-27 17:58:27 +08:00
|
|
|
|
EventManager.Instance.Subscribe(PlayerJinBeiChange_VictoryEventArgs.EventId, PlayerJinBeiChange_VictoryEvent);
|
2024-05-05 17:02:33 +08:00
|
|
|
|
// EventManager.Instance.Subscribe(ReturnPlayerJinBeiEventArgs.EventId, ReturnPlayerJinBeiEvent);
|
2024-04-11 16:44:25 +08:00
|
|
|
|
}
|
2024-04-06 18:15:42 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public virtual void Dispose()
|
|
|
|
|
{
|
|
|
|
|
EventManager.Instance.Unsubscribe(PlayerJinBeiChange_ToPlayerEventArgs.EventId, PlayerJinbeiChangeEvent);
|
2024-04-27 17:58:27 +08:00
|
|
|
|
EventManager.Instance.Unsubscribe(PlayerJinBeiChange_VictoryEventArgs.EventId, PlayerJinBeiChange_VictoryEvent);
|
2024-05-05 17:02:33 +08:00
|
|
|
|
// EventManager.Instance.Unsubscribe(ReturnPlayerJinBeiEventArgs.EventId, ReturnPlayerJinBeiEvent);
|
2024-04-24 17:52:34 +08:00
|
|
|
|
|
2024-04-27 11:37:11 +08:00
|
|
|
|
if (this._room != null)
|
|
|
|
|
this._room.Quit(this);
|
|
|
|
|
|
2024-04-24 17:52:34 +08:00
|
|
|
|
this._room = null;
|
|
|
|
|
this.playerInfo = null;
|
|
|
|
|
this._isMoving = false;
|
|
|
|
|
this._playerData = null;
|
2024-04-11 16:44:25 +08:00
|
|
|
|
GameObject.DestroyImmediate(this.self);
|
2024-04-24 17:52:34 +08:00
|
|
|
|
this._self = null;
|
2024-04-11 16:44:25 +08:00
|
|
|
|
}
|
2024-04-06 18:15:42 +08:00
|
|
|
|
|
2024-05-05 17:02:33 +08:00
|
|
|
|
// 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));
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-04-10 16:18:32 +08:00
|
|
|
|
|
2024-04-27 17:58:27 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
private void PlayerJinbeiChangeEvent(object sender, GameEventArgs e)
|
2024-04-06 18:15:42 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
var args = e as PlayerJinBeiChange_ToPlayerEventArgs;
|
2024-04-27 17:58:27 +08:00
|
|
|
|
if (args == null || args.player != this)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
var jinbei = this._playerData.jinbei - args.jinbei;
|
2024-04-27 17:58:27 +08:00
|
|
|
|
// Debug.Log($"{playerName} 下注了 {args.jinbei} 金杯 , 剩余 {jinbei} 金杯");
|
2024-04-11 16:44:25 +08:00
|
|
|
|
if (jinbei >= 0)
|
|
|
|
|
{
|
|
|
|
|
this._playerData.jinbei = jinbei;
|
|
|
|
|
args.callback?.Invoke(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
args.callback?.Invoke(false);
|
2024-04-06 18:15:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-06 11:59:18 +08:00
|
|
|
|
}
|