房间创建和进入相关逻辑和协议

This commit is contained in:
2025-08-27 00:04:40 +08:00
parent 7325e268ce
commit f6d85a1e0a
45 changed files with 1246 additions and 729 deletions

View File

@@ -0,0 +1,19 @@
using Fantasy.Entitas.Interface;
namespace NB.Map;
public class MapRoomDestroySystem : DestroySystem<MapRoom>
{
protected override void Destroy(MapRoom self)
{
self.Map = 0;
self.Password = string.Empty;
self.CreateTime = 0;
self.Owner = 0;
self.Units.Clear();
}
}
public static class MapRoomSystem
{
}

View File

@@ -0,0 +1,84 @@
using Fantasy;
using Fantasy.Async;
namespace NB.Map;
public static class MapUnitManageComponentSystem
{
#region 线线
/// <summary>
/// 玩家上线
/// </summary>
/// <param name="self"></param>
/// <param name="scene"></param>
/// <param name="accountId"></param>
/// <param name="gateRouteId"></param>
public static async FTask<MapUnit?> Online(this MapUnitManageComponent self, Scene scene,
long accountId,
long gateRouteId)
{
if (!self.TryGet(accountId, out var unit))
{
unit = MapUnitFactory.Create(scene, accountId);
self.Add(unit);
}
if (unit != null)
{
unit.GateRouteId = gateRouteId;
}
await FTask.CompletedTask;
return unit;
}
public static async FTask Offline(this MapUnitManageComponent self, Scene scene, long accountId, long gateRouteId)
{
if (self.TryGet(accountId, out var unit) && unit != null)
{
if (unit.GateRouteId == gateRouteId)
{
self.Remove(accountId); //如果当前网关和下线的网关一致
}
}
await FTask.CompletedTask;
}
#endregion
#region &
public static void Add(this MapUnitManageComponent self, MapUnit unit)
{
self.Units.Add(unit.Id, unit);
}
public static MapUnit? Get(this MapUnitManageComponent self, long accountId)
{
return self.Units.GetValueOrDefault(accountId);
}
public static bool TryGet(this MapUnitManageComponent self, long accountId, out MapUnit? unit)
{
return self.Units.TryGetValue(accountId, out unit);
}
public static void Remove(this MapUnitManageComponent self, long accountId, bool isDispose = true)
{
if (!self.Units.Remove(accountId, out var account))
{
return;
}
if (!isDispose)
{
return;
}
account.Dispose();
}
#endregion
}

View 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
}