forked from zxl/Frame
1
0
Fork 0
Frame/Assets/Scripts/Room/Room.cs

96 lines
2.4 KiB
C#
Raw Normal View History

2024-04-06 11:59:18 +08:00
using System.Collections.Generic;
2024-04-08 18:20:55 +08:00
using System.Threading;
using Cysharp.Threading.Tasks;
2024-04-06 11:59:18 +08:00
using Game.Player;
using UnityEngine;
namespace Game.Room;
public interface IRoom
{
2024-04-09 13:22:52 +08:00
string roomName { get; }
2024-04-06 11:59:18 +08:00
RoomData roomData { get; }
RoomInfo roomInfo { get; }
List<IPlayer> players { get; }
2024-04-08 18:20:55 +08:00
RoomType roomType { get; }
2024-04-06 11:59:18 +08:00
2024-04-08 18:20:55 +08:00
UniTask<bool> JoinAsync(IPlayer player, CancellationToken token);
UniTask<bool> Quit(IPlayer player);
2024-04-06 11:59:18 +08:00
float ClearAll();
void Dispose();
bool InvestmentJinbei(IPlayer player, float jinbei);
}
class Room : IRoom
{
private GameObject self;
private RoomInfo _roomInfo;
private RoomData _roomData;
2024-04-09 13:22:52 +08:00
private string _roomName;
2024-04-06 11:59:18 +08:00
public RoomType roomType => this._roomData.roomType;
2024-04-09 13:22:52 +08:00
public string roomName => this._roomName;
2024-04-06 11:59:18 +08:00
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);
2024-04-09 13:22:52 +08:00
_roomName = roomType.ToString();
// view
2024-04-06 11:59:18 +08:00
this._roomInfo = roomGo.GetComponent<RoomInfo>();
2024-04-08 18:20:55 +08:00
this._roomInfo.SetRoom(this);
2024-04-06 11:59:18 +08:00
this.self = roomGo;
}
2024-04-08 18:20:55 +08:00
public async UniTask<bool> JoinAsync(IPlayer player, CancellationToken token)
2024-04-06 11:59:18 +08:00
{
if (this.players.Contains(player))
return false;
2024-04-08 18:20:55 +08:00
Debug.Log($"{player.playerName} join {roomType} !!");
bool res = await player.WaitMoveRoomAsync(this, token);
2024-04-06 11:59:18 +08:00
this.players.Add(player);
return true;
}
2024-04-08 18:20:55 +08:00
public async UniTask<bool> Quit(IPlayer player)
2024-04-06 11:59:18 +08:00
{
if (!this.players.Contains(player))
return false;
2024-04-08 18:20:55 +08:00
Debug.Log($"{player.playerName} quit {roomType} !!");
// player.SetRoom(null);
2024-04-06 11:59:18 +08:00
this.players.Remove(player);
2024-04-08 18:20:55 +08:00
await UniTask.Yield();
2024-04-06 11:59:18 +08:00
return true;
}
public float ClearAll()
{
2024-04-09 13:22:52 +08:00
// TODO:
// EventManager.Instance.FireNow(this, new InputNameFinishEventArgs());
2024-04-06 11:59:18 +08:00
this.players.Clear();
return this._roomData.jinbei;
}
public void Dispose()
{
2024-04-08 18:20:55 +08:00
foreach (var player in this.players)
{
player.Dispose();
}
2024-04-06 11:59:18 +08:00
}
public bool InvestmentJinbei(IPlayer player, float jinbei)
{
if (!this.players.Contains(player))
return false;
this._roomData.jinbei += jinbei;
return true;
}
}