feat: added safe get entity by id
This commit is contained in:
@@ -22,6 +22,7 @@ namespace Ragon.Client
|
||||
public sealed class RagonEntity
|
||||
{
|
||||
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
|
||||
|
||||
private RagonClient _client;
|
||||
|
||||
public ushort Id { get; private set; }
|
||||
@@ -92,6 +93,15 @@ namespace Ragon.Client
|
||||
return payload;
|
||||
}
|
||||
|
||||
public void PreAttach(IRagonPayload payload)
|
||||
{
|
||||
var buffer = new RagonBuffer();
|
||||
payload.Serialize(buffer);
|
||||
|
||||
_spawnPayload = new RagonPayload();
|
||||
_spawnPayload.Read(buffer);
|
||||
}
|
||||
|
||||
public T GetAttachPayload<T>() where T : IRagonPayload, new()
|
||||
{
|
||||
return GetPayload<T>(_spawnPayload);
|
||||
|
||||
@@ -21,8 +21,8 @@ namespace Ragon.Client
|
||||
public sealed class RagonClient
|
||||
{
|
||||
private readonly INetworkConnection _connection;
|
||||
private readonly IRagonEntityListener _entityListener;
|
||||
private readonly IRagonSceneCollector _sceneCollector;
|
||||
private IRagonEntityListener _entityListener;
|
||||
private IRagonSceneCollector _sceneCollector;
|
||||
private Handler[] _handlers;
|
||||
private RagonBuffer _readBuffer;
|
||||
private RagonBuffer _writeBuffer;
|
||||
@@ -52,21 +52,15 @@ namespace Ragon.Client
|
||||
|
||||
#region PUBLIC
|
||||
|
||||
public RagonClient(
|
||||
INetworkConnection connection,
|
||||
IRagonEntityListener entityListener,
|
||||
IRagonSceneCollector sceneCollector,
|
||||
int rate)
|
||||
public RagonClient(INetworkConnection connection, int rate)
|
||||
{
|
||||
_listenerList = new RagonListenerList(this);
|
||||
_entityListener = entityListener;
|
||||
_sceneCollector = sceneCollector;
|
||||
|
||||
_connection = connection;
|
||||
_connection.OnData += OnData;
|
||||
_connection.OnConnected += OnConnected;
|
||||
_connection.OnDisconnected += OnDisconnected;
|
||||
|
||||
_listenerList = new RagonListenerList(this);
|
||||
|
||||
_replicationRate = (1000.0f / rate) / 1000.0f;
|
||||
_replicationTime = 0;
|
||||
|
||||
@@ -75,8 +69,31 @@ namespace Ragon.Client
|
||||
_status = RagonStatus.DISCONNECTED;
|
||||
}
|
||||
|
||||
|
||||
public void Configure(IRagonSceneCollector sceneCollector)
|
||||
{
|
||||
_sceneCollector = sceneCollector;
|
||||
}
|
||||
|
||||
public void Configure(IRagonEntityListener listener)
|
||||
{
|
||||
_entityListener = listener;
|
||||
}
|
||||
|
||||
public void Connect(string address, ushort port, string protocol)
|
||||
{
|
||||
if (_sceneCollector == null)
|
||||
{
|
||||
RagonLog.Error("Scene collector is not defined!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_entityListener == null)
|
||||
{
|
||||
RagonLog.Error("Entity Listener is not defined!");
|
||||
return;
|
||||
}
|
||||
|
||||
_writeBuffer = new RagonBuffer();
|
||||
_readBuffer = new RagonBuffer();
|
||||
_session = new RagonSession(this, _readBuffer);
|
||||
|
||||
@@ -45,9 +45,9 @@ public sealed class RagonEntityCache
|
||||
_playerCache = playerCache;
|
||||
}
|
||||
|
||||
public RagonEntity FindById(ushort id)
|
||||
public bool TryGetEntity(ushort id, out RagonEntity entity)
|
||||
{
|
||||
return _entityMap[id];
|
||||
return _entityMap.TryGetValue(id, out entity);
|
||||
}
|
||||
|
||||
public void Create(RagonEntity entity, IRagonPayload? spawnPayload)
|
||||
|
||||
@@ -54,8 +54,8 @@ public sealed class EntityCreateOperation : IRagonOperation
|
||||
if (reader.Capacity > 0)
|
||||
entity.Payload.Read(reader);
|
||||
|
||||
var roomPlugin = room.Plugin;
|
||||
if (!roomPlugin.OnEntityCreate(player, entity))
|
||||
var plugin = room.Plugin;
|
||||
if (!plugin.OnEntityCreate(player, entity))
|
||||
return;
|
||||
|
||||
entity.Attach(player);
|
||||
|
||||
@@ -44,16 +44,15 @@ public sealed class EntityEventOperation : IRagonOperation
|
||||
if (targetMode == RagonTarget.Player)
|
||||
targetPlayerPeerId = reader.ReadUShort();
|
||||
|
||||
var ragonEvent = new RagonEvent(player, eventId);
|
||||
ragonEvent.Read(reader);
|
||||
var @event = new RagonEvent(player, eventId);
|
||||
@event.Read(reader);
|
||||
|
||||
if (targetMode == RagonTarget.Player &&
|
||||
context.Room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
|
||||
if (targetMode == RagonTarget.Player && room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
|
||||
{
|
||||
ent.ReplicateEvent(player, ragonEvent, eventMode, targetPlayer);
|
||||
ent.ReplicateEvent(player, @event, eventMode, targetPlayer);
|
||||
return;
|
||||
}
|
||||
|
||||
ent.ReplicateEvent(player, ragonEvent, eventMode, targetMode);
|
||||
ent.ReplicateEvent(player, @event, eventMode, targetMode);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user