refactor: entity structure changes

This commit is contained in:
2022-12-17 14:05:53 +04:00
parent fa6ace4dc8
commit 5a4bf0c24e
4 changed files with 33 additions and 25 deletions
+1
View File
@@ -6,6 +6,7 @@ public class Loop
public Loop()
{
_tasks = new List<IAction>(35);
}
+6 -12
View File
@@ -46,8 +46,8 @@ public class Entity
writer.Clear();
writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
writer.WriteUShort(bufferedEvent.EventId);
writer.WriteUShort(bufferedEvent.PeerId);
writer.WriteByte((byte) RagonReplicationMode.Server);
writer.WriteUShort(bufferedEvent.Invoker.Connection.Id);
writer.WriteByte((byte)RagonReplicationMode.Server);
writer.WriteUShort(Id);
ReadOnlySpan<byte> data = bufferedEvent.EventData.AsSpan();
@@ -70,7 +70,7 @@ public class Entity
serializer.WriteUShort(Owner.Connection.Id);
ReadOnlySpan<byte> entityPayload = Payload.AsSpan();
serializer.WriteUShort((ushort) entityPayload.Length);
serializer.WriteUShort((ushort)entityPayload.Length);
serializer.WriteData(ref entityPayload);
var sendData = serializer.ToArray();
@@ -109,7 +109,7 @@ public class Entity
serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
serializer.WriteUShort(eventId);
serializer.WriteUShort(caller.Connection.Id);
serializer.WriteByte((byte) eventMode);
serializer.WriteByte((byte)eventMode);
serializer.WriteUShort(Id);
serializer.WriteData(ref payload);
@@ -134,13 +134,7 @@ public class Entity
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner)
{
var bufferedEvent = new EntityEvent()
{
EventData = payload.ToArray(),
Target = targetMode,
EventId = eventId,
PeerId = caller.Connection.Id,
};
var bufferedEvent = new EntityEvent(caller, eventId, payload.ToArray(), targetMode);
_bufferedEvents.Add(bufferedEvent);
}
@@ -151,7 +145,7 @@ public class Entity
serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
serializer.WriteUShort(eventId);
serializer.WriteUShort(caller.Connection.Id);
serializer.WriteByte((byte) eventMode);
serializer.WriteByte((byte)eventMode);
serializer.WriteUShort(Id);
serializer.WriteData(ref payload);
+17 -4
View File
@@ -4,8 +4,21 @@ namespace Ragon.Core.Game;
public class EntityEvent
{
public ushort PeerId { get; set; }
public ushort EventId { get; set; }
public byte[] EventData { get; set; }
public RagonTarget Target { set; get; }
public RoomPlayer Invoker { get; private set; }
public ushort EventId { get; private set; }
public byte[] EventData { get; private set; }
public RagonTarget Target { set; private get; }
public EntityEvent(
RoomPlayer invoker,
ushort eventId,
byte[] payload,
RagonTarget target
)
{
Invoker = invoker;
EventId = eventId;
EventData = payload;
Target = target;
}
}
+6 -6
View File
@@ -2,18 +2,18 @@ namespace Ragon.Core.Game;
public class EntityList
{
private List<Entity> _dynamicEntitiesList = new List<Entity>();
private List<Entity> _staticEntitesList = new List<Entity>();
private Dictionary<ushort, Entity> _entitiesMap = new Dictionary<ushort, Entity>();
private readonly List<Entity> _dynamicEntitiesList = new List<Entity>();
private readonly List<Entity> _staticEntitiesList = new List<Entity>();
private readonly Dictionary<ushort, Entity> _entitiesMap = new Dictionary<ushort, Entity>();
public IReadOnlyList<Entity> StaticList => _staticEntitesList;
public IReadOnlyList<Entity> StaticList => _staticEntitiesList;
public IReadOnlyList<Entity> DynamicList => _dynamicEntitiesList;
public IReadOnlyDictionary<ushort, Entity> Map => _entitiesMap;
public void Add(Entity entity)
{
if (entity.StaticId != 0)
_staticEntitesList.Add(entity);
_staticEntitiesList.Add(entity);
else
_dynamicEntitiesList.Add(entity);
@@ -24,7 +24,7 @@ public class EntityList
{
if (_entitiesMap.Remove(entity.Id, out var existEntity))
{
_staticEntitesList.Remove(entity);
_staticEntitiesList.Remove(entity);
_dynamicEntitiesList.Remove(entity);
return existEntity;