Frame/Assets/Scripts/Dinosaurs/Player/Player.cs

197 lines
6.2 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
2024-04-06 11:59:18 +08:00
{
public enum RoleType
{
Player,
Robot,
}
public interface IPlayer
{
2024-05-05 17:02:33 +08:00
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);
2024-05-05 17:02:33 +08:00
void MoneyRefresh(float money);
void Init();
void Dispose();
}
2024-04-06 11:59:18 +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
private IBFSManager _bfsManager;
private RoleType _roleType;
2024-05-05 17:02:33 +08:00
private long _uId;
public long uId => this._uId;
public string playerName => this._playerData.playerName;
2024-04-06 11:59:18 +08:00
public RoleType roleType => this._roleType;
public GameObject self => this._self;
2024-04-06 11:59:18 +08:00
public PlayerData playerData => this._playerData;
2024-04-06 11:59:18 +08:00
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;
}
}
2024-05-05 17:02:33 +08:00
public Player(string name, long uId, GameObject go, IBFSManager bfs, RoleType roleType)
{
2024-05-05 17:02:33 +08:00
this._playerData = new PlayerData(name, 100);
this._bfsManager = bfs;
this._roleType = roleType;
2024-05-05 17:02:33 +08:00
this._uId = uId;
2024-04-06 11:59:18 +08:00
// 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;
2024-04-08 18:20:55 +08:00
// EventManager.Instance.FireNow(this, new PlayerMoveToRoomEventArgs(this, this._room));
var wayPoints = this._bfsManager.FindPath(this._self.transform.position, _room.roomInfo.room_Center);
2024-04-08 18:20:55 +08:00
await UniTask.Yield();
var moveAsync = await this.MoveAsync(wayPoints, token);
2024-04-11 14:44:39 +08:00
// Debug.Log("Move finish !!!");
return moveAsync;
}
2024-04-08 18:20:55 +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());
}
private async UniTask<bool> MoveAsync(List<WayPoint> wayPoint, CancellationToken token)
{
if (this._isMoving) return false;
2024-04-08 18:20:55 +08:00
_isMoving = true;
var endPos = this._room.roomInfo.GetJoinPosition();
2024-04-06 11:59:18 +08:00
await playerInfo.MoveAsync(wayPoint, endPos, token);
this._isMoving = false;
return true;
}
2024-04-06 11:59:18 +08:00
public virtual void Init()
{
EventManager.Instance.Subscribe(PlayerJinBeiChange_ToPlayerEventArgs.EventId, PlayerJinbeiChangeEvent);
EventManager.Instance.Subscribe(PlayerJinBeiChange_VictoryEventArgs.EventId, PlayerJinBeiChange_VictoryEvent);
2024-05-05 17:02:33 +08:00
// 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);
2024-05-05 17:02:33 +08:00
// 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;
}
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
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);
}
}
2024-04-06 11:59:18 +08:00
}