房间创建和进入相关逻辑和协议
This commit is contained in:
93
Hotfix/Map/System/RoomManageComponentSystem.cs
Normal file
93
Hotfix/Map/System/RoomManageComponentSystem.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Fantasy.Entitas;
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace NB.Map;
|
||||
|
||||
public class RoomManageComponentAwakeSystem : AwakeSystem<RoomManageComponent>
|
||||
{
|
||||
protected override void Awake(RoomManageComponent self)
|
||||
{
|
||||
for (int i = 1; i <= 65535; i++)
|
||||
{
|
||||
self.FreeIds.Enqueue(i, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class RoomManageComponentDestroySystem : DestroySystem<RoomManageComponent>
|
||||
{
|
||||
protected override void Destroy(RoomManageComponent self)
|
||||
{
|
||||
foreach (var (_, room) in self.Rooms)
|
||||
{
|
||||
room.Dispose();
|
||||
}
|
||||
|
||||
self.Rooms.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static class RoomManageComponentSystem
|
||||
{
|
||||
#region 增删
|
||||
|
||||
public static void Add(this RoomManageComponent self, MapRoom room)
|
||||
{
|
||||
self.Rooms[room.RoomId] = room;
|
||||
}
|
||||
|
||||
public static bool Remove(this RoomManageComponent self, int roomId)
|
||||
{
|
||||
if (self.Rooms.TryGetValue(roomId, out var room))
|
||||
{
|
||||
self.ReleaseId(room.RoomId);
|
||||
room.Dispose();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 房间Id
|
||||
|
||||
/// <summary>
|
||||
/// 分配一个新的房间ID
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static int AllocateId(this RoomManageComponent self)
|
||||
{
|
||||
if (self.FreeIds.Count == 0) return 0;
|
||||
int id = self.FreeIds.Dequeue();
|
||||
self.InUseID.Add(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放房间ID
|
||||
/// </summary>
|
||||
/// <param name="self"></param>
|
||||
/// <param name="id"></param>
|
||||
public static void ReleaseId(this RoomManageComponent self, int id)
|
||||
{
|
||||
if (self.InUseID.Remove(id))
|
||||
{
|
||||
self.FreeIds.Enqueue(id, id);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 进入退出
|
||||
|
||||
public static void Enter(this RoomManageComponent self, MapUnit unit, long roomId)
|
||||
{
|
||||
}
|
||||
|
||||
public static void Exit(this RoomManageComponent self, MapUnit unit)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user