diff --git a/Ragon.Client/Sources/Entity/RagonEntity.cs b/Ragon.Client/Sources/Entity/RagonEntity.cs index 29b1250..6a6ad6a 100644 --- a/Ragon.Client/Sources/Entity/RagonEntity.cs +++ b/Ragon.Client/Sources/Entity/RagonEntity.cs @@ -49,8 +49,8 @@ namespace Ragon.Client private RagonPayload _spawnPayload; private RagonPayload _destroyPayload; - private Dictionary _events = new(); - private Dictionary> _localEvents = new(); + private readonly Dictionary _events = new Dictionary(); + private readonly Dictionary> _localEvents = new Dictionary>(); public RagonEntity(ushort type = 0, ushort sceneId = 0) { @@ -150,29 +150,26 @@ namespace Ragon.Client return; } + var eventCode = _client.Event.GetEventCode(evnt); if (target != RagonTarget.ExceptOwner) { if (replicationMode == RagonReplicationMode.Local) { - var eventCode = _client.Event.GetEventCode(evnt); _localEvents[eventCode].Invoke(_client.Room.Local, evnt); return; } - if (replicationMode == RagonReplicationMode.LocalAndServer) { - var eventCode = _client.Event.GetEventCode(evnt); _localEvents[eventCode].Invoke(_client.Room.Local, evnt); } } - var evntId = _client.Event.GetEventCode(evnt); var buffer = _client.Buffer; - + buffer.Clear(); buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); buffer.WriteUShort(Id); - buffer.WriteUShort(evntId); + buffer.WriteUShort(eventCode); buffer.WriteByte((byte)replicationMode); buffer.WriteByte((byte)target); diff --git a/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs b/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs index 9a4a2d8..bf88717 100644 --- a/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs +++ b/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs @@ -22,9 +22,13 @@ namespace Ragon.Client; internal class AuthorizeSuccessHandler: Handler { private readonly RagonListenerList _listenerList; + private readonly RagonClient _client; - public AuthorizeSuccessHandler(RagonListenerList listenerList) + public AuthorizeSuccessHandler( + RagonClient client, + RagonListenerList listenerList) { + _client = client; _listenerList = listenerList; } @@ -34,6 +38,7 @@ internal class AuthorizeSuccessHandler: Handler var playerName = buffer.ReadString(); var playerPayload = buffer.ReadString(); + _client.SetStatus(RagonStatus.LOBBY); _listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/JoinSuccessHandler.cs b/Ragon.Client/Sources/Handler/JoinSuccessHandler.cs index 5aaf3dc..41a7aa7 100644 --- a/Ragon.Client/Sources/Handler/JoinSuccessHandler.cs +++ b/Ragon.Client/Sources/Handler/JoinSuccessHandler.cs @@ -43,7 +43,6 @@ internal class JoinSuccessHandler : Handler private readonly RagonPlayerCache _playerCache; private readonly RagonEntityCache _entityCache; private readonly RagonClient _client; - private readonly RagonBuffer _buffer; public JoinSuccessHandler( RagonClient client, @@ -53,7 +52,6 @@ internal class JoinSuccessHandler : Handler RagonEntityCache entityCache ) { - _buffer = buffer; _client = client; _listenerList = listenerList; _entityCache = entityCache; diff --git a/Ragon.Client/Sources/Handler/SnapshotHandler.cs b/Ragon.Client/Sources/Handler/SnapshotHandler.cs index eb3d52d..fd56559 100644 --- a/Ragon.Client/Sources/Handler/SnapshotHandler.cs +++ b/Ragon.Client/Sources/Handler/SnapshotHandler.cs @@ -117,9 +117,13 @@ internal class SnapshotHandler : Handler entity.Attach(_client, entityId, entityType, hasAuthority, player); } - if (_client.Status != RagonStatus.ROOM) - _listenerList.OnJoined(); - + if (_client.Status == RagonStatus.LOBBY) + { + _client.SetStatus(RagonStatus.ROOM); + + _listenerList.OnJoined(); + } + _listenerList.OnSceneLoaded(); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/RagonClient.cs b/Ragon.Client/Sources/RagonClient.cs index a145c0a..582c2b4 100644 --- a/Ragon.Client/Sources/RagonClient.cs +++ b/Ragon.Client/Sources/RagonClient.cs @@ -34,7 +34,7 @@ namespace Ragon.Client private RagonEntityCache _entityCache; private RagonEventCache _eventCache; private RagonStatus _status; - + private float _replicationRate = 0; private float _replicationTime = 0; @@ -58,9 +58,9 @@ namespace Ragon.Client _connection.OnData += OnData; _connection.OnConnected += OnConnected; _connection.OnDisconnected += OnDisconnected; - + listeners = new RagonListenerList(this); - + _replicationRate = (1000.0f / rate) / 1000.0f; _replicationTime = 0; @@ -79,7 +79,7 @@ namespace Ragon.Client { _entityListener = listener; } - + public void Connect(string address, ushort port, string protocol) { if (_sceneCollector == null) @@ -93,7 +93,7 @@ namespace Ragon.Client RagonLog.Error("Entity Listener is not defined!"); return; } - + _writeBuffer = new RagonBuffer(); _readBuffer = new RagonBuffer(); _session = new RagonSession(this, _readBuffer); @@ -102,7 +102,7 @@ namespace Ragon.Client _entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector); _handlers = new Handler[byte.MaxValue]; - _handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(listeners); + _handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, listeners); _handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(listeners); _handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _readBuffer, listeners, _playerCache, _entityCache); _handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(listeners); @@ -127,7 +127,7 @@ namespace Ragon.Client _status = RagonStatus.DISCONNECTED; _room.Cleanup(); _connection.Disconnect(); - + OnDisconnected(RagonDisconnect.MANUAL); } @@ -144,7 +144,7 @@ namespace Ragon.Client _stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt); } - + listeners.Update(); _connection.Update(); } @@ -179,7 +179,7 @@ namespace Ragon.Client public void RemoveListener(IRagonPlayerLeftListener listener) => listeners.Remove(listener); public void RemoveListener(IRagonSceneListener listener) => listeners.Remove(listener); public void RemoveListener(IRagonSceneRequestListener listener) => listeners.Remove(listener); - + #endregion #region INTERNAL @@ -187,7 +187,11 @@ namespace Ragon.Client internal void AssignRoom(RagonRoom room) { _room = room; - _status = RagonStatus.ROOM; + } + + internal void SetStatus(RagonStatus status) + { + _status = status; } #endregion