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; } bool IsCanDelete { get; } UniTask WaitMoveRoomAsync(IRoom roo, CancellationToken token); void JumpToRoom(IRoom room); 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; 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 bool IsCanDelete { get { if (this.isMoving || this._room.roomType != RoomType.出生点) return false; return true; } } public Player(string name, float jinBei, GameObject go, IBFSManager bfs) { this._playerData = new PlayerData(name, jinBei, 100); this._bfsManager = bfs; // view this._self = go; playerInfo = this._self.GetComponent(); playerInfo.SetPlayer(this); this._isMoving = false; } // public void SetGo(GameObject gameObject) // { // this._self = gameObject; // // // view // playerInfo = this._self.GetComponent(); // playerInfo.SetPlayer(this); // this._isMoving = false; // } public async UniTask 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); } private async UniTask MoveAsync(List 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(ReturnPlayerJinBeiEventArgs.EventId, ReturnPlayerJinBeiEvent); } public virtual void Dispose() { EventManager.Instance.Unsubscribe(PlayerJinBeiChange_ToPlayerEventArgs.EventId, PlayerJinbeiChangeEvent); EventManager.Instance.Unsubscribe(ReturnPlayerJinBeiEventArgs.EventId, ReturnPlayerJinBeiEvent); 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.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); } } }