Frame/Assets/Scripts/Room/Room.cs

88 lines
2.2 KiB
C#

using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using Game.Player;
using UnityEngine;
namespace Game.Room;
public interface IRoom
{
RoomData roomData { get; }
RoomInfo roomInfo { get; }
List<IPlayer> players { get; }
RoomType roomType { get; }
UniTask<bool> JoinAsync(IPlayer player, CancellationToken token);
UniTask<bool> Quit(IPlayer player);
float ClearAll();
void Dispose();
bool InvestmentJinbei(IPlayer player, float jinbei);
}
class Room : IRoom
{
private GameObject self;
private RoomInfo _roomInfo;
private RoomData _roomData;
public RoomType roomType => this._roomData.roomType;
public RoomData roomData => this._roomData;
public RoomInfo roomInfo => this._roomInfo;
public List<IPlayer> players => this._roomData.players;
public Room(RoomType roomType, GameObject roomGo)
{
this._roomData = new RoomData(roomType);
// view
this._roomInfo = roomGo.GetComponent<RoomInfo>();
this._roomInfo.SetRoom(this);
this.self = roomGo;
}
public async UniTask<bool> JoinAsync(IPlayer player, CancellationToken token)
{
if (this.players.Contains(player))
return false;
Debug.Log($"{player.playerName} join {roomType} !!");
bool res = await player.WaitMoveRoomAsync(this, token);
this.players.Add(player);
return true;
}
public async UniTask<bool> Quit(IPlayer player)
{
if (!this.players.Contains(player))
return false;
Debug.Log($"{player.playerName} quit {roomType} !!");
// player.SetRoom(null);
this.players.Remove(player);
await UniTask.Yield();
return true;
}
public float ClearAll()
{
this.players.Clear();
return this._roomData.jinbei;
}
public void Dispose()
{
foreach (var player in this.players)
{
player.Dispose();
}
}
public bool InvestmentJinbei(IPlayer player, float jinbei)
{
if (!this.players.Contains(player))
return false;
this._roomData.jinbei += jinbei;
return true;
}
}