refactoring multiplayer
This commit is contained in:
@@ -23,62 +23,72 @@ namespace RebootKit.Engine.Simulation {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ActorCommand : INetworkSerializable {
|
||||
public struct ActorCommand : ISerializableEntity {
|
||||
public ulong ActorID;
|
||||
public ulong ClientID;
|
||||
public ushort CommandID;
|
||||
public NativeArray<byte> Data;
|
||||
|
||||
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter {
|
||||
serializer.SerializeValue(ref ActorID);
|
||||
serializer.SerializeValue(ref ClientID);
|
||||
serializer.SerializeValue(ref CommandID);
|
||||
|
||||
if (serializer.IsWriter) {
|
||||
bool hasData = Data.IsCreated;
|
||||
serializer.SerializeValue(ref hasData);
|
||||
|
||||
if (hasData) {
|
||||
serializer.SerializeValue(ref Data, Allocator.Temp);
|
||||
}
|
||||
} else if (serializer.IsReader) {
|
||||
bool hasData = false;
|
||||
serializer.SerializeValue(ref hasData);
|
||||
|
||||
if (hasData) {
|
||||
serializer.SerializeValue(ref Data, Allocator.Temp);
|
||||
}
|
||||
|
||||
public int GetMaxBytes() {
|
||||
return sizeof(ulong) + // ActorID
|
||||
sizeof(ulong) + // ClientID
|
||||
sizeof(ushort) + // CommandID
|
||||
sizeof(ushort) + // Data length
|
||||
sizeof(byte) * Data.Length; // Data
|
||||
}
|
||||
|
||||
public void Serialize(NetworkBufferWriter writer) {
|
||||
writer.Write(ActorID);
|
||||
writer.Write(ClientID);
|
||||
writer.Write(CommandID);
|
||||
writer.Write((ushort) Data.Length);
|
||||
if (Data.IsCreated) {
|
||||
writer.Write(Data);
|
||||
}
|
||||
}
|
||||
|
||||
public void Deserialize(NetworkBufferReader reader) {
|
||||
reader.Read(out ActorID);
|
||||
reader.Read(out ClientID);
|
||||
reader.Read(out CommandID);
|
||||
reader.Read(out ushort dataLength);
|
||||
if (dataLength > 0) {
|
||||
reader.Read(out Data, dataLength, Allocator.Temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @NOTE: ActorEvent is used to send events from the server to clients and only clients.
|
||||
// Server should not receive ActorEvents.
|
||||
public struct ActorEvent : INetworkSerializable {
|
||||
public struct ActorEvent : ISerializableEntity {
|
||||
public ulong ActorID;
|
||||
public ulong ClientID;
|
||||
public ushort EventID;
|
||||
public NativeArray<byte> Data;
|
||||
|
||||
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter {
|
||||
serializer.SerializeValue(ref ActorID);
|
||||
serializer.SerializeValue(ref ClientID);
|
||||
serializer.SerializeValue(ref EventID);
|
||||
public int GetMaxBytes() {
|
||||
return sizeof(ulong) + // ActorID
|
||||
sizeof(ushort) + // EventID
|
||||
sizeof(ushort) + // Data length
|
||||
sizeof(byte) * Data.Length; // Data
|
||||
}
|
||||
|
||||
if (serializer.IsWriter) {
|
||||
bool hasData = Data.IsCreated;
|
||||
serializer.SerializeValue(ref hasData);
|
||||
public void Serialize(NetworkBufferWriter writer) {
|
||||
writer.Write(ActorID);
|
||||
writer.Write(EventID);
|
||||
|
||||
if (hasData) {
|
||||
serializer.SerializeValue(ref Data, Allocator.Temp);
|
||||
}
|
||||
} else if (serializer.IsReader) {
|
||||
bool hasData = false;
|
||||
serializer.SerializeValue(ref hasData);
|
||||
writer.Write((ushort) Data.Length);
|
||||
if (Data.IsCreated) {
|
||||
writer.Write(Data);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasData) {
|
||||
serializer.SerializeValue(ref Data, Allocator.Temp);
|
||||
}
|
||||
public void Deserialize(NetworkBufferReader reader) {
|
||||
reader.Read(out ActorID);
|
||||
reader.Read(out EventID);
|
||||
reader.Read(out ushort dataLength);
|
||||
|
||||
if (dataLength > 0) {
|
||||
reader.Read(out Data, dataLength, Allocator.Temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,7 +352,6 @@ namespace RebootKit.Engine.Simulation {
|
||||
// Override this method to implement client-side logic
|
||||
public virtual void OnClientTick(float deltaTime) { }
|
||||
protected virtual void OnActorEventClient(ActorEvent actorEvent) { }
|
||||
protected virtual void OnClientFinishedInitialSync() { }
|
||||
|
||||
//
|
||||
// @MARK: Server API
|
||||
@@ -479,7 +488,7 @@ namespace RebootKit.Engine.Simulation {
|
||||
Data = data
|
||||
};
|
||||
|
||||
Manager.SendActorCommandToServerRpc(command);
|
||||
Manager.SendActorCommand(command);
|
||||
}
|
||||
|
||||
protected void SendActorEvent<TEventData>(ushort eventID, ref TEventData eventData)
|
||||
@@ -501,7 +510,6 @@ namespace RebootKit.Engine.Simulation {
|
||||
|
||||
ActorEvent actorEvent = new ActorEvent {
|
||||
ActorID = ActorID,
|
||||
ClientID = NetworkManager.Singleton.LocalClientId,
|
||||
EventID = eventID,
|
||||
Data = data
|
||||
};
|
||||
@@ -712,10 +720,6 @@ namespace RebootKit.Engine.Simulation {
|
||||
return CreateActorData();
|
||||
}
|
||||
|
||||
internal void InitialSyncFinished() {
|
||||
OnClientFinishedInitialSync();
|
||||
}
|
||||
|
||||
internal void HandleActorCommand(ActorCommand actorCommand) {
|
||||
if (!RR.IsServer()) {
|
||||
s_ActorLogger.Error($"Only the server can handle actor commands. Actor: {name} (ID: {ActorID})");
|
||||
|
||||
Reference in New Issue
Block a user