changed scene loading flow

This commit is contained in:
2023-07-30 21:46:42 +03:00
parent 92062cd708
commit fb58dedfaf
5 changed files with 32 additions and 24 deletions
+5 -8
View File
@@ -49,8 +49,8 @@ namespace Ragon.Client
private RagonPayload _spawnPayload;
private RagonPayload _destroyPayload;
private Dictionary<int, OnEventDelegate> _events = new();
private Dictionary<int, Action<RagonPlayer, IRagonEvent>> _localEvents = new();
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
private readonly Dictionary<int, Action<RagonPlayer, IRagonEvent>> _localEvents = new Dictionary<int, Action<RagonPlayer, IRagonEvent>>();
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);
@@ -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);
}
}
@@ -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;
@@ -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();
}
}
+14 -10
View File
@@ -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