Files
Ragon/Ragon/Sources/Entity/Entity.cs
T

73 lines
2.0 KiB
C#
Raw Normal View History

2022-08-25 23:12:33 +04:00
using System;
2022-09-06 22:39:52 +04:00
using System.Collections.Generic;
using Ragon.Common;
2022-04-24 09:05:15 +04:00
namespace Ragon.Core;
public class Entity
{
2022-08-13 18:54:02 +04:00
private static ushort _idGenerator = 0;
public ushort EntityId { get; private set; }
public ushort StaticId { get; private set; }
2022-05-08 00:40:11 +04:00
public ushort EntityType { get; private set; }
2022-08-14 12:36:12 +04:00
public ushort OwnerId { get; private set; }
public RagonAuthority Authority { get; private set; }
2022-08-13 18:54:02 +04:00
public EntityProperty[] Properties { get; private set; }
2022-09-06 22:39:52 +04:00
public List<EntityEvent> BufferedEvents = new List<EntityEvent>();
2022-08-13 23:22:46 +04:00
public byte[] Payload { get; set; }
2022-04-24 09:05:15 +04:00
2022-09-06 22:39:52 +04:00
public Entity(ushort ownerId, ushort entityType, ushort staticId, RagonAuthority eventAuthority, int props)
2022-04-24 09:05:15 +04:00
{
OwnerId = ownerId;
2022-07-13 07:56:44 +04:00
StaticId = staticId;
2022-05-08 00:40:11 +04:00
EntityType = entityType;
2022-04-24 09:05:15 +04:00
EntityId = _idGenerator++;
2022-08-13 18:54:02 +04:00
Properties = new EntityProperty[props];
2022-08-25 23:12:33 +04:00
Payload = Array.Empty<byte>();
Authority = eventAuthority;
2022-04-24 09:05:15 +04:00
}
2022-09-04 14:43:17 +04:00
public void ReplicateProperties(RagonSerializer serializer)
{
serializer.WriteUShort(EntityId);
for (int propertyIndex = 0; propertyIndex < Properties.Length; propertyIndex++)
{
var property = Properties[propertyIndex];
if (property.IsDirty)
{
serializer.WriteBool(true);
var span = serializer.GetWritableData(property.Size);
var data = property.Read();
data.CopyTo(span);
property.Clear();
}
else
{
serializer.WriteBool(false);
}
}
}
public void Snapshot(RagonSerializer serializer)
{
for (int propertyIndex = 0; propertyIndex < Properties.Length; propertyIndex++)
{
var property = Properties[propertyIndex];
var hasPayload = property.IsFixed || property.Size > 0 && !property.IsFixed;
if (hasPayload)
{
serializer.WriteBool(true);
var span = serializer.GetWritableData(property.Size);
var data = property.Read();
data.CopyTo(span);
}
else
{
serializer.WriteBool(false);
}
}
}
2022-04-24 09:05:15 +04:00
}