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 WaitMoveRoomAsync(IRoom roo, CancellationToken token); UniTask MoveAsync(List 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.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 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 MoveAsync(List 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(PlayerJinbeiChangeEventArgs.EventId, PlayerJinbeiChangeEvent); } public virtual void Dispose() { EventManager.Instance.Unsubscribe(PlayerJinbeiChangeEventArgs.EventId, PlayerJinbeiChangeEvent); GameObject.DestroyImmediate(this.self); } private void PlayerJinbeiChangeEvent(object sender, GameEventArgs e) { var args = e as PlayerJinbeiChangeEventArgs; var jinbei = this._playerData.jinbei - args.jinbei; if (jinbei >= 0) { this._playerData.jinbei = jinbei; args.callback?.Invoke(true); } else args.callback?.Invoke(false); } }