refactoring multiplayer

This commit is contained in:
2025-07-21 19:58:07 +02:00
parent a0a0f6303d
commit ea99249fe2
6 changed files with 238 additions and 114 deletions

View File

@@ -295,16 +295,29 @@ namespace RebootKit.Engine.Simulation {
s_Logger.Error($"Failed to find actor with ID {header.ActorID} for event handling.");
return;
}
using NetworkBufferReader reader = new NetworkBufferReader(data);
ActorEvent actorEvent = new ActorEvent();
actorEvent.Deserialize(reader);
throw new NotImplementedException();
actor.HandleActorEvent(actorEvent);
} else if (header.Type == NetworkDataType.ActorCommand) {
if (!RR.IsServer()) {
s_Logger.Error($"Received ActorCommand on client, but this should only be handled on the server.");
return;
}
Actor actor = FindActorByID(header.ActorID);
if (actor == null) {
s_Logger.Error($"Failed to find actor with ID {header.ActorID} for command handling.");
return;
}
using NetworkBufferReader reader = new NetworkBufferReader(data);
ActorCommand actorCommand = new ActorCommand();
actorCommand.Deserialize(reader);
throw new NotImplementedException();
actor.HandleActorCommand(actorCommand);
} else if (header.Type == NetworkDataType.SynchronizeActor) {
Actor actor = FindActorByID(header.ActorID);
if (actor == null) {
@@ -381,48 +394,17 @@ namespace RebootKit.Engine.Simulation {
//
// @MARK: Actor Commands and Events
//
[Rpc(SendTo.Server, Delivery = RpcDelivery.Reliable)]
internal void SendActorCommandToServerRpc(ActorCommand cmd) {
if (!IsServer) {
s_Logger.Error("Only the server can handle actor events.");
return;
}
Actor actor = FindActorByID(cmd.ActorID);
if (actor is null) {
s_Logger.Error($"Actor with ID {cmd.ActorID} not found for command {cmd.CommandID}");
return;
}
actor.HandleActorCommand(cmd);
}
internal void SendActorEvent(ActorEvent actorEvent) {
if (!RR.IsServer()) {
s_Logger.Error("Only the server can send actor events.");
return;
}
foreach ((ulong clientID, NetworkClientState state) in RR.NetworkSystemInstance.Clients) {
if (NetworkManager.Singleton.LocalClientId == clientID) {
continue;
}
if (state.IsReady) {
SendActorEventRpc(actorEvent, RpcTarget.Single(clientID, RpcTargetUse.Temp));
}
}
RR.NetworkSystemInstance.WriteActorEvent(NetworkPacketTarget.AllClients(), actorEvent);
}
[Rpc(SendTo.SpecifiedInParams)]
void SendActorEventRpc(ActorEvent actorEvent, RpcParams rpcParams) {
Actor actor = FindActorByID(actorEvent.ActorID);
if (actor is null) {
s_Logger.Error($"Actor with ID {actorEvent.ActorID} not found for event {actorEvent.EventID}");
return;
}
actor.HandleActorEvent(actorEvent);
internal void SendActorCommand(ActorCommand actorCommand) {
RR.NetworkSystemInstance.WriteActorCommand(actorCommand);
}
}
}