角色控制和房间进入推送

This commit is contained in:
2025-09-07 23:51:17 +08:00
parent 61d20b5729
commit 7dad49bf5f
28 changed files with 432 additions and 221 deletions

View File

@@ -11,7 +11,7 @@ public class C2Game_GetRoleInfoRequestHandler : RouteRPC<Player, C2Game_GetRoleI
protected override async FTask Run(Player entity, C2Game_GetRoleInfoRequest request,
Game2C_GetRoleInfoResponse response, Action reply)
{
response.RoomId = 0;
response.RoomCode = "13AHVL";
response.RoleInfo = entity.GetRoleInfo();
await FTask.CompletedTask;

View File

@@ -97,7 +97,8 @@ public class C2G_EnterRoomRequestHandler : MessageRPC<C2G_EnterRoomRequest, G2C_
}
gateUnit.RoomCode = request.RoomCode;
response.Units = roomResponse.Units;
response.RoomCode = request.RoomCode;
response.ErrorCode = ErrorCode.Successful;
}

View File

@@ -39,7 +39,7 @@ public sealed class C2G_LoginRequestHandler : MessageRPC<C2G_LoginRequest, G2C_L
return;
}
response.RoleId = gateUnit.AccountID;
response.ErrorCode = await GateLoginHelper.Online(gateUnit);
Log.Debug($"当前的Gate服务器:{session.Scene.SceneConfigId} accountId:{accountId}");
}

View File

@@ -24,19 +24,14 @@ public class C2Map_CreateRoomRequestHandler : RouteRPC<MapUnit, C2Map_CreateRoom
return;
}
var roomId = roomManageComponent.AllocateId();
if (roomId < 1)
var room = roomManageComponent.Create(entity.Id);
if (room == null)
{
response.ErrorCode = ErrorCode.MapCreateRoomMax;
return;
}
// roomManageComponent.on
var room = Entity.Create<MapRoom>(entity.Scene, true, true);
room.Owner = entity.Id;
room.RoomId = roomId;
room.Code = RoomHelper.GenerateCode(entity.Scene.SceneConfigId, roomId);
roomManageComponent.Add(room);
Log.Info(
$"创建房间=== sId={entity.Scene.SceneConfigId} map:{request.MapId} id={room.RoomId} code={room.Code}");

View File

@@ -0,0 +1,33 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Map;
public class C2Map_LookHandler : Route<MapUnit, C2Map_Look>
{
protected override async FTask Run(MapUnit entity, C2Map_Look message)
{
var roomManageComponent = entity.Scene.GetComponent<RoomManageComponent>();
var notifyMessage = new Map2C_LookeNotify()
{
Id = entity.Id,
Rotation = message.Rotation,
Timestamp = message.Timestamp
};
entity.Rotation.x = message.Rotation.x;
entity.Rotation.y = message.Rotation.y;
entity.Rotation.z = message.Rotation.z;
var room = roomManageComponent.Get(entity.MapId);
// foreach (var (_, unit) in mapUnitManageComponent.Units)
// {
// entity.Scene.NetworkMessagingComponent.SendInnerRoute(unit.GateRouteId, notifyMessage);
// }
await FTask.CompletedTask;
}
}

View File

@@ -0,0 +1,38 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Helper;
using Fantasy.Network.Interface;
namespace NB.Map;
public class C2Map_MoveHandler : Route<MapUnit, C2Map_Move>
{
protected override async FTask Run(MapUnit entity, C2Map_Move message)
{
var mapUnitManageComponent = entity.Scene.GetComponent<MapUnitManageComponent>();
var notifyMessage = new Map2C_MoveNotify()
{
Id = entity.Id,
Position = message.Position,
Rotation = message.Rotation,
IsStop = message.IsStop,
Direction = message.Direction,
Timestamp = TimeHelper.Now
};
entity.Position.x = message.Position.x;
entity.Position.y = message.Position.y;
entity.Position.z = message.Position.z;
entity.Rotation.x = message.Rotation.x;
entity.Rotation.y = message.Rotation.y;
entity.Rotation.z = message.Rotation.z;
foreach (var (_, unit) in mapUnitManageComponent.Units)
{
entity.Scene.NetworkMessagingComponent.SendInnerRoute(unit.GateRouteId, notifyMessage);
}
await FTask.CompletedTask;
}
}

View File

@@ -4,7 +4,7 @@ using Fantasy.Network.Interface;
namespace NB.Map.Inner;
public class G2Map_EnterRequestHandler : RouteRPC<Scene, G2Map_EnterRoomRequest, Map2G_EnterRoomResponse>
public class G2Map_EnterRoomRequestHandler : RouteRPC<Scene, G2Map_EnterRoomRequest, Map2G_EnterRoomResponse>
{
protected override async FTask Run(Scene entity, G2Map_EnterRoomRequest request, Map2G_EnterRoomResponse response,
Action reply)
@@ -54,9 +54,16 @@ public class G2Map_EnterRequestHandler : RouteRPC<Scene, G2Map_EnterRoomRequest,
return;
}
}
response.ErrorCode = await room.Enter(request.AccountId);
response.ErrorCode = await room.Enter(mapUnit);
response.RoomCode = room.Code;
if (response.ErrorCode == ErrorCode.Successful)
{
mapUnit.RoomId = room.Id;
response.Units = room.ToMapUnitInfo();
}
}
}

View File

@@ -0,0 +1,13 @@
using Fantasy;
using Fantasy.Async;
using Fantasy.Network.Interface;
namespace NB.Map.Inner;
public class G2Map_ExitRoomRequestHandler : RouteRPC<Scene, G2Map_ExitRoomRequest, Map2G_ExiRoomResponse>
{
protected override async FTask Run(Scene entity, G2Map_ExitRoomRequest request, Map2G_ExiRoomResponse response,
Action reply)
{
}
}

View File

@@ -18,9 +18,34 @@ public class MapRoomDestroySystem : DestroySystem<MapRoom>
public static class MapRoomSystem
{
public static async FTask<uint> Enter(this MapRoom self, long unitId)
public static async FTask<uint> Enter(this MapRoom self, MapUnit unit)
{
self.Units.TryAdd(unit.Id, unit);
var notifyMessage = new Map2C_RoleEnterRoomNotify()
{
Info = unit.ToMapUnitInfo(),
};
foreach (var (_, roomUnit) in self.Units)
{
// if (roomUnit.Id == unit.Id) continue;
// 同步其他客户端
self.Scene.NetworkMessagingComponent.SendInnerRoute(roomUnit.GateRouteId, notifyMessage);
}
await FTask.CompletedTask;
return ErrorCode.Successful;
}
public static List<MapUnitInfo> ToMapUnitInfo(this MapRoom self)
{
List<MapUnitInfo> ret = new List<MapUnitInfo>();
foreach (var (_, unit) in self.Units)
{
ret.Add(unit.ToMapUnitInfo());
}
return ret;
}
}

View File

@@ -11,7 +11,7 @@ public class MapUnitSystemDestroySystem : DestroySystem<MapUnit>
{
self.MapId = 0;
self.Position = float3.zero;
self.Rotation = float4.zero;
self.Rotation = float3.zero;
self.GateRouteId = 0L;
}
}
@@ -40,4 +40,25 @@ public static class MapUnitSystem
return ErrorCode.Successful;
}
public static MapUnitInfo ToMapUnitInfo(this MapUnit self)
{
var ret = new MapUnitInfo()
{
Id = self.Id,
Position = Vector3Info.Create(self.Scene),
Rotation = Vector3Info.Create(self.Scene),
};
ret.Position.x = self.Position.x;
ret.Position.y = self.Position.y;
ret.Position.z = self.Position.z;
ret.Rotation.x = self.Rotation.x;
ret.Rotation.y = self.Rotation.y;
ret.Rotation.z = self.Rotation.z;
return ret;
}
}

View File

@@ -1,4 +1,5 @@
using Fantasy.Entitas;
using Fantasy;
using Fantasy.Entitas;
using Fantasy.Entitas.Interface;
namespace NB.Map;
@@ -31,6 +32,24 @@ public static class RoomManageComponentSystem
{
#region
public static MapRoom? Create(this RoomManageComponent self, long ownerId)
{
var roomId = self.AllocateId();
if (roomId < 1)
{
return null;
}
// roomManageComponent.on
var room = Entity.Create<MapRoom>(self.Scene, true, true);
room.Owner = ownerId;
room.RoomId = roomId;
room.Code = RoomHelper.GenerateCode(self.Scene.SceneConfigId, roomId);
self.Add(room);
return room;
}
public static void Add(this RoomManageComponent self, MapRoom room)
{
self.Rooms[room.RoomId] = room;
@@ -46,10 +65,12 @@ public static class RoomManageComponentSystem
return true;
}
public static MapRoom? Get(this RoomManageComponent self, int roomId)
{
return self.Rooms.GetValueOrDefault(roomId);
}
#endregion
#region Id
@@ -81,6 +102,4 @@ public static class RoomManageComponentSystem
}
#endregion
}

View File

@@ -67,6 +67,9 @@ public sealed class OnCreateSceneEvent : AsyncEventSystem<OnCreateScene>
case SceneType.Map:
{
Log.Debug($"Map Scene SceneRuntimeId:{scene.RuntimeId}");
var roomCode = RoomHelper.GenerateCode(scene.SceneConfigId, 1);
Log.Info($"测试 roomCode{roomCode}");
// uint serverId = 25255;
// for (int i = 1; i < 65535; i++)
// {

View File

@@ -52,8 +52,16 @@ public class OnSceneCreate_Init : AsyncEventSystem<OnCreateScene>
{
Log.Info("创建地图场景===");
scene.AddComponent<MapUnitManageComponent>();
scene.AddComponent<RoomManageComponent>();
var roomManageComponent = scene.AddComponent<RoomManageComponent>();
scene.AddComponent<MapManageComponent>();
var room = roomManageComponent.Create(361499030775398402);
if (room != null)
{
roomManageComponent.TestRoomCode = room.Code;
Log.Info($"测试房间代码 = {room.Code}");
}
break;
}
}