♻️ plugin api
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
using Ragon.Protocol;
|
||||
using Ragon.Server.Room;
|
||||
|
||||
namespace Ragon.Server.Entity;
|
||||
|
||||
public interface IRagonEntity
|
||||
{
|
||||
public ushort Id { get; }
|
||||
public ushort Type { get; }
|
||||
public ushort StaticId { get; }
|
||||
public ushort AttachId { get; }
|
||||
public RagonRoomPlayer Owner { get; }
|
||||
public RagonAuthority Authority { get; }
|
||||
public RagonPayload Payload { get; }
|
||||
public IRagonEntityState State { get; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server.Entity;
|
||||
|
||||
public interface IRagonEntityState
|
||||
{
|
||||
|
||||
}
|
||||
@@ -20,7 +20,7 @@ using Ragon.Server.Room;
|
||||
|
||||
namespace Ragon.Server.Entity;
|
||||
|
||||
public class RagonEntity
|
||||
public class RagonEntity: IRagonEntity
|
||||
{
|
||||
private static ushort _idGenerator = 100;
|
||||
public ushort Id { get; private set; }
|
||||
@@ -30,8 +30,10 @@ public class RagonEntity
|
||||
public RagonRoomPlayer Owner { get; private set; }
|
||||
public RagonAuthority Authority { get; private set; }
|
||||
public RagonPayload Payload { get; private set; }
|
||||
public RagonEntityState State { get; private set; }
|
||||
public IRagonEntityState State => _state;
|
||||
|
||||
private readonly List<RagonEvent> _bufferedEvents;
|
||||
private readonly RagonEntityState _state;
|
||||
|
||||
public RagonEntity(RagonEntityParameters parameters)
|
||||
{
|
||||
@@ -41,10 +43,9 @@ public class RagonEntity
|
||||
Type = parameters.Type;
|
||||
AttachId = parameters.AttachId;
|
||||
Authority = parameters.Authority;
|
||||
|
||||
State = new RagonEntityState(this);
|
||||
Payload = new RagonPayload();
|
||||
|
||||
_state = new RagonEntityState(this);
|
||||
_bufferedEvents = new List<RagonEvent>();
|
||||
}
|
||||
|
||||
@@ -121,7 +122,8 @@ public class RagonEntity
|
||||
|
||||
buffer.WriteUShort(Payload.Size);
|
||||
Payload.Write(buffer);
|
||||
State.Snapshot(buffer);
|
||||
|
||||
_state.Snapshot(buffer);
|
||||
}
|
||||
|
||||
public void ReplicateEvent(
|
||||
@@ -215,16 +217,23 @@ public class RagonEntity
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(RagonBuffer writer)
|
||||
public void AddProperty(RagonProperty property)
|
||||
{
|
||||
State.Write(writer);
|
||||
_state.AddProperty(property);
|
||||
}
|
||||
|
||||
public void WriteState(RagonBuffer writer)
|
||||
{
|
||||
_state.Write(writer);
|
||||
}
|
||||
|
||||
public void Read(RagonRoomPlayer player, RagonBuffer reader)
|
||||
public bool TryReadState(RagonRoomPlayer player, RagonBuffer reader)
|
||||
{
|
||||
if (Owner.Connection.Id != player.Connection.Id)
|
||||
return;
|
||||
return false;
|
||||
|
||||
State.Read(reader);
|
||||
_state.Read(reader);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -19,14 +19,16 @@ using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server.Entity;
|
||||
|
||||
public class RagonEntityState
|
||||
public class RagonEntityState: IRagonEntityState
|
||||
{
|
||||
private List<RagonProperty> _properties;
|
||||
private RagonEntity _entity;
|
||||
|
||||
private RagonBuffer _buffer;
|
||||
|
||||
public RagonEntityState(RagonEntity entity, int capacity = 10)
|
||||
{
|
||||
_entity = entity;
|
||||
_buffer = new RagonBuffer(8);
|
||||
_properties = new List<RagonProperty>(capacity);
|
||||
}
|
||||
|
||||
@@ -65,8 +67,8 @@ public class RagonEntityState
|
||||
{
|
||||
foreach (var property in _properties)
|
||||
{
|
||||
var hasPayload = property.IsFixed || !property.IsFixed && property.Size > 0;
|
||||
if (hasPayload)
|
||||
var hasPayloadOrFixed = property.IsFixed || property is { IsFixed: false, Size: > 0 };
|
||||
if (hasPayloadOrFixed)
|
||||
{
|
||||
buffer.WriteBool(true);
|
||||
property.Write(buffer);
|
||||
|
||||
Reference in New Issue
Block a user