68 lines
1.7 KiB
C#
68 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using Game.Player;
|
|
using UnityEngine;
|
|
|
|
namespace Game.Room;
|
|
|
|
public interface IRoomManager
|
|
{
|
|
IRoom CreateRoom(RoomType roomType);
|
|
IRoom GetRoom(RoomType roomType);
|
|
UniTask<bool> JoinRoomAsync(RoomType roomType, IPlayer player, CancellationToken token);
|
|
bool QuitRoom(RoomType roomType, IPlayer player);
|
|
void DeleteRoom(RoomType roomType);
|
|
}
|
|
|
|
public class RoomManager : ManagerBase, IRoomManager
|
|
{
|
|
private Dictionary<RoomType, IRoom> _rooms = new Dictionary<RoomType, IRoom>();
|
|
|
|
public IRoom CreateRoom(RoomType roomType)
|
|
{
|
|
if (this._rooms.TryGetValue(roomType, out var room))
|
|
return room;
|
|
|
|
var gameObject = Game.resourceManager.LoadGameObjectSync(roomType.ToString());
|
|
room = new Room(roomType, gameObject);
|
|
this._rooms.Add(roomType, room);
|
|
return room;
|
|
}
|
|
|
|
public IRoom GetRoom(RoomType roomType)
|
|
{
|
|
return this._rooms.GetValueOrDefault(roomType);
|
|
}
|
|
|
|
public async UniTask<bool> JoinRoomAsync(RoomType roomType, IPlayer player, CancellationToken token)
|
|
{
|
|
if (!player.isMoving)
|
|
return false;
|
|
|
|
if (this._rooms.TryGetValue(roomType, out var room))
|
|
{
|
|
await room.JoinAsync(player, token);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool QuitRoom(RoomType roomType, IPlayer player)
|
|
{
|
|
if (this._rooms.TryGetValue(roomType, out var room))
|
|
{
|
|
room.Quit(player);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void DeleteRoom(RoomType roomType)
|
|
{
|
|
if (this._rooms.TryGetValue(roomType, out var room))
|
|
room.Dispose();
|
|
}
|
|
} |