diff --git a/Ragon.Client/Sources/Entity/RagonEntity.cs b/Ragon.Client/Sources/Entity/RagonEntity.cs index e2915c9..39ae162 100644 --- a/Ragon.Client/Sources/Entity/RagonEntity.cs +++ b/Ragon.Client/Sources/Entity/RagonEntity.cs @@ -54,12 +54,14 @@ namespace Ragon.Client public ushort Id { get; private set; } public ushort Type { get; private set; } - public bool Replication { get; private set; } + public RagonAuthority Authority { get; private set; } public RagonPlayer Owner { get; private set; } public RagonEntityState State { get; private set; } + public bool IsStatic => SceneId > 0; + public bool IsReplicated { get; private set; } public bool IsAttached { get; private set; } public bool HasAuthority { get; private set; } @@ -80,39 +82,33 @@ namespace Ragon.Client private readonly Dictionary>> _localListeners = new Dictionary>>(); private readonly Dictionary>> _listeners = new Dictionary>>(); - public RagonEntity(ushort type = 0, ushort sceneId = 0) + public RagonEntity(ushort type = 0, ushort sceneId = 0, bool replicated = true) { State = new RagonEntityState(this); Type = type; + IsReplicated = replicated; _spawnPayload = new RagonPayload(0); _destroyPayload = new RagonPayload(0); _sceneId = sceneId; } - internal void Attach(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer owner) + internal void Attach() { - Type = entityType; - Id = entityId; - Owner = owner; IsAttached = true; - Replication = true; - HasAuthority = hasAuthority; - - _client = client; - + Attached?.Invoke(this); } - internal void SetReplication(bool enabled) + public void SetReplication(bool enabled) { - Replication = enabled; + IsReplicated = enabled; } - + internal void Detach(RagonPayload payload) { _destroyPayload = payload; - + Detached?.Invoke(); } @@ -120,20 +116,27 @@ namespace Ragon.Client { var payload = new T(); if (data.Size <= 0) return payload; - + var buffer = new RagonBuffer(); - + data.Write(buffer); - + payload.Deserialize(buffer); - + return payload; } - - public void AttachPayload(RagonPayload payload) + + public void Prepare(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer player, RagonPayload payload) { + Type = entityType; + Id = entityId; + HasAuthority = hasAuthority; + + _client = client; _spawnPayload = payload; - } + + Owner = player; + } public T GetAttachPayload() where T : IRagonPayload, new() { @@ -193,6 +196,7 @@ namespace Ragon.Client listener.Invoke(_client.Room.Local, evnt); return; } + if (replicationMode == RagonReplicationMode.LocalAndServer) { var localListeners = _localListeners[eventCode]; @@ -202,7 +206,7 @@ namespace Ragon.Client } var buffer = _client.Buffer; - + buffer.Clear(); buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); buffer.WriteUShort(Id); @@ -259,8 +263,7 @@ namespace Ragon.Client _propertiesChanged = false; } - - + internal void Read(RagonBuffer buffer) { State.ReadState(buffer); @@ -268,6 +271,8 @@ namespace Ragon.Client internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer) { + if (!IsReplicated) return; + if (_events.TryGetValue(eventCode, out var evnt)) evnt?.Invoke(caller, buffer); else diff --git a/Ragon.Client/Sources/Entity/RagonPayload.cs b/Ragon.Client/Sources/Entity/RagonPayload.cs index 1d13161..7a0acb7 100644 --- a/Ragon.Client/Sources/Entity/RagonPayload.cs +++ b/Ragon.Client/Sources/Entity/RagonPayload.cs @@ -21,6 +21,8 @@ namespace Ragon.Client; public class RagonPayload { + public static RagonPayload Empty = new RagonPayload(0); + private readonly uint[] _data = new uint[128]; private readonly int _size = 0; diff --git a/Ragon.Client/Sources/Handler/EntityCreateHandler.cs b/Ragon.Client/Sources/Handler/EntityCreateHandler.cs index 46f2515..375e78f 100644 --- a/Ragon.Client/Sources/Handler/EntityCreateHandler.cs +++ b/Ragon.Client/Sources/Handler/EntityCreateHandler.cs @@ -57,11 +57,11 @@ internal class EntityCreateHandler : IHandler var hasAuthority = _playerCache.Local.Id == player.Id; var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated); - entity.AttachPayload(payload); + entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload); if (hasCreated) _entityListener.OnEntityCreated(entity); - entity.Attach(_client, entityId, entityType, hasAuthority, player); + entity.Attach(); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/SnapshotHandler.cs b/Ragon.Client/Sources/Handler/SnapshotHandler.cs index 5ce1a7c..789aeca 100644 --- a/Ragon.Client/Sources/Handler/SnapshotHandler.cs +++ b/Ragon.Client/Sources/Handler/SnapshotHandler.cs @@ -27,7 +27,7 @@ internal class SnapshotHandler : IHandler private readonly RagonListenerList _listenerList; private readonly RagonEntityCache _entityCache; private readonly RagonPlayerCache _playerCache; - + public SnapshotHandler( RagonClient ragonClient, RagonListenerList listenerList, @@ -76,19 +76,19 @@ internal class SnapshotHandler : IHandler var hasAuthority = _playerCache.Local.Id == player.Id; var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _); - + var payload = RagonPayload.Empty; if (payloadSize > 0) { - var payload = new RagonPayload(payloadSize); + payload = new RagonPayload(payloadSize); payload.Read(buffer); - - entity.AttachPayload(payload); } + entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload); + _entityListener.OnEntityCreated(entity); entity.Read(buffer); - entity.Attach(_client, entityId, entityType, hasAuthority, player); + entity.Attach(); } var staticEntities = buffer.ReadUShort(); @@ -111,8 +111,10 @@ internal class SnapshotHandler : IHandler var hasAuthority = _playerCache.Local.Id == player.Id; var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _); + entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty); + entity.Read(buffer); - entity.Attach(_client, entityId, entityType, hasAuthority, player); + entity.Attach(); } if (_client.Status == RagonStatus.LOBBY) diff --git a/Ragon.Client/Sources/RagonClient.cs b/Ragon.Client/Sources/RagonClient.cs index 0886aed..7e7644c 100644 --- a/Ragon.Client/Sources/RagonClient.cs +++ b/Ragon.Client/Sources/RagonClient.cs @@ -47,8 +47,7 @@ namespace Ragon.Client public RagonEntityCache Entity => _entityCache; public NetworkStatistics Statistics => _stats; public RagonRoom Room => _room; - - + internal RagonBuffer Buffer => _writeBuffer; internal INetworkChannel Reliable => _connection.Reliable; internal INetworkChannel Unreliable => _connection.Unreliable; diff --git a/Ragon.Client/Sources/RagonEntityCache.cs b/Ragon.Client/Sources/RagonEntityCache.cs index ea27049..3ca406d 100644 --- a/Ragon.Client/Sources/RagonEntityCache.cs +++ b/Ragon.Client/Sources/RagonEntityCache.cs @@ -116,7 +116,7 @@ public sealed class RagonEntityCache foreach (var ent in _entityList) { if (!ent.IsAttached || - !ent.Replication || + !ent.IsReplicated || !ent.PropertiesChanged) continue; ent.Write(buffer); diff --git a/Ragon.Simulation/Sources/Client/IO/RagonENetConnection.cs b/Ragon.Simulation/Sources/Client/IO/RagonENetConnection.cs index dce87d9..44ab26f 100644 --- a/Ragon.Simulation/Sources/Client/IO/RagonENetConnection.cs +++ b/Ragon.Simulation/Sources/Client/IO/RagonENetConnection.cs @@ -127,5 +127,10 @@ namespace Ragon.Client if (_libraryLoaded) Library.Deinitialize(); } + + public void Close() + { + + } } } \ No newline at end of file diff --git a/Ragon.Simulation/Sources/Client/IO/RagonNullConnection.cs b/Ragon.Simulation/Sources/Client/IO/RagonNullConnection.cs index 3ab94ef..5376ddf 100644 --- a/Ragon.Simulation/Sources/Client/IO/RagonNullConnection.cs +++ b/Ragon.Simulation/Sources/Client/IO/RagonNullConnection.cs @@ -128,5 +128,10 @@ namespace Ragon.Client if (_libraryLoaded) Library.Deinitialize(); } + + public void Close() + { + + } } } \ No newline at end of file diff --git a/Ragon.Simulation/Sources/Game.cs b/Ragon.Simulation/Sources/Game.cs index 0eb7ecb..517001a 100644 --- a/Ragon.Simulation/Sources/Game.cs +++ b/Ragon.Simulation/Sources/Game.cs @@ -1,5 +1,4 @@ - - +using Ragon.Client.Property; using Ragon.Protocol; namespace Ragon.Client.Simulation;