wip
This commit is contained in:
@@ -1,35 +1,34 @@
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityList
|
||||
{
|
||||
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 => _staticEntitiesList;
|
||||
public IReadOnlyList<Entity> DynamicList => _dynamicEntitiesList;
|
||||
public IReadOnlyDictionary<ushort, Entity> Map => _entitiesMap;
|
||||
|
||||
public void Add(Entity entity)
|
||||
{
|
||||
if (entity.StaticId != 0)
|
||||
_staticEntitiesList.Add(entity);
|
||||
else
|
||||
_dynamicEntitiesList.Add(entity);
|
||||
|
||||
_entitiesMap.Add(entity.Id, entity);
|
||||
}
|
||||
|
||||
public Entity Remove(Entity entity)
|
||||
{
|
||||
if (_entitiesMap.Remove(entity.Id, out var existEntity))
|
||||
{
|
||||
_staticEntitiesList.Remove(entity);
|
||||
_dynamicEntitiesList.Remove(entity);
|
||||
|
||||
return existEntity;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityList
|
||||
{
|
||||
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 => _staticEntitiesList;
|
||||
public IReadOnlyList<Entity> DynamicList => _dynamicEntitiesList;
|
||||
public IReadOnlyDictionary<ushort, Entity> Map => _entitiesMap;
|
||||
|
||||
public void Add(Entity entity)
|
||||
{
|
||||
if (entity.StaticId != 0)
|
||||
_staticEntitiesList.Add(entity);
|
||||
else
|
||||
_dynamicEntitiesList.Add(entity);
|
||||
|
||||
_entitiesMap.Add(entity.Id, entity);
|
||||
}
|
||||
|
||||
public bool Remove(Entity entity)
|
||||
{
|
||||
if (_entitiesMap.Remove(entity.Id, out var existEntity))
|
||||
{
|
||||
_staticEntitiesList.Remove(entity);
|
||||
_dynamicEntitiesList.Remove(entity);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,100 +1,100 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityState
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private List<EntityStateProperty> _properties;
|
||||
private Entity _entity;
|
||||
|
||||
public EntityState(Entity entity, int capacity = 10)
|
||||
{
|
||||
_entity = entity;
|
||||
_properties = new List<EntityStateProperty>(10);
|
||||
}
|
||||
|
||||
public void AddProperty(EntityStateProperty property)
|
||||
{
|
||||
_properties.Add(property);
|
||||
}
|
||||
|
||||
public void Write(RagonSerializer serializer)
|
||||
{
|
||||
serializer.WriteUShort(_entity.Id);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < _properties.Count; 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 Read(RagonSerializer serializer)
|
||||
{
|
||||
for (var i = 0; i < _properties.Count; i++)
|
||||
{
|
||||
if (serializer.ReadBool())
|
||||
{
|
||||
var property = _properties[i];
|
||||
var size = property.Size;
|
||||
if (!property.IsFixed)
|
||||
size = serializer.ReadUShort();
|
||||
|
||||
if (size > property.Capacity)
|
||||
{
|
||||
Console.WriteLine($"Property {i} payload too large, size: {size}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var propertyPayload = serializer.ReadData(size);
|
||||
property.Write(ref propertyPayload);
|
||||
property.Size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Snapshot(RagonSerializer serializer)
|
||||
{
|
||||
ReadOnlySpan<byte> payload = _entity.Payload.AsSpan();
|
||||
|
||||
serializer.WriteUShort(_entity.Type);
|
||||
serializer.WriteUShort(_entity.Id);
|
||||
|
||||
if (_entity.StaticId != 0)
|
||||
serializer.WriteUShort(_entity.StaticId);
|
||||
|
||||
serializer.WriteUShort(_entity.Owner.Connection.Id);
|
||||
serializer.WriteUShort((ushort) payload.Length);
|
||||
serializer.WriteData(ref payload);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < _properties.Count; 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityState
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private List<EntityStateProperty> _properties;
|
||||
private Entity _entity;
|
||||
|
||||
public EntityState(Entity entity, int capacity = 10)
|
||||
{
|
||||
_entity = entity;
|
||||
_properties = new List<EntityStateProperty>(10);
|
||||
}
|
||||
|
||||
public void AddProperty(EntityStateProperty property)
|
||||
{
|
||||
_properties.Add(property);
|
||||
}
|
||||
|
||||
public void Write(RagonSerializer serializer)
|
||||
{
|
||||
serializer.WriteUShort(_entity.Id);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < _properties.Count; 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 Read(RagonSerializer serializer)
|
||||
{
|
||||
for (var i = 0; i < _properties.Count; i++)
|
||||
{
|
||||
if (serializer.ReadBool())
|
||||
{
|
||||
var property = _properties[i];
|
||||
var size = property.Size;
|
||||
if (!property.IsFixed)
|
||||
size = serializer.ReadUShort();
|
||||
|
||||
if (size > property.Capacity)
|
||||
{
|
||||
Console.WriteLine($"Property {i} payload too large, size: {size}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var propertyPayload = serializer.ReadData(size);
|
||||
property.Write(ref propertyPayload);
|
||||
property.Size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Snapshot(RagonSerializer serializer)
|
||||
{
|
||||
ReadOnlySpan<byte> payload = _entity.Payload.AsSpan();
|
||||
|
||||
serializer.WriteUShort(_entity.Type);
|
||||
serializer.WriteUShort(_entity.Id);
|
||||
|
||||
if (_entity.StaticId != 0)
|
||||
serializer.WriteUShort(_entity.StaticId);
|
||||
|
||||
serializer.WriteUShort(_entity.Owner.Connection.Id);
|
||||
serializer.WriteUShort((ushort) payload.Length);
|
||||
serializer.WriteData(ref payload);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < _properties.Count; 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+165
-168
@@ -1,169 +1,166 @@
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Time;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class Room: IAction
|
||||
{
|
||||
public string Id { get; }
|
||||
public RoomInformation Info { get; }
|
||||
public RoomPlayer Owner { get; set; }
|
||||
|
||||
public Dictionary<ushort, RoomPlayer> Players { get; }
|
||||
public List<RoomPlayer> WaitPlayersList { get; private set; }
|
||||
public List<RoomPlayer> ReadyPlayersList { get; private set; }
|
||||
public List<RoomPlayer> PlayerList { get; private set; }
|
||||
|
||||
public Dictionary<ushort, Entity> Entities { get; private set; }
|
||||
public List<Entity> DynamicEntitiesList { get; private set; }
|
||||
public List<Entity> StaticEntitiesList { get; private set; }
|
||||
public List<Entity> EntityList { get; private set; }
|
||||
|
||||
private HashSet<Entity> _entitiesDirtySet;
|
||||
private RagonSerializer _writer;
|
||||
|
||||
public RagonSerializer Writer => _writer;
|
||||
|
||||
public Room(string roomId, RoomInformation info)
|
||||
{
|
||||
Id = roomId;
|
||||
Info = info;
|
||||
|
||||
Players = new Dictionary<ushort, RoomPlayer>(info.Max);
|
||||
WaitPlayersList = new List<RoomPlayer>(info.Max);
|
||||
ReadyPlayersList = new List<RoomPlayer>(info.Max);
|
||||
PlayerList = new List<RoomPlayer>(info.Max);
|
||||
|
||||
Entities = new Dictionary<ushort, Entity>();
|
||||
DynamicEntitiesList = new List<Entity>();
|
||||
StaticEntitiesList = new List<Entity>();
|
||||
EntityList = new List<Entity>();
|
||||
|
||||
_entitiesDirtySet = new HashSet<Entity>();
|
||||
_writer = new RagonSerializer(512);
|
||||
}
|
||||
|
||||
public void AttachEntity(RoomPlayer newOwner, Entity entity)
|
||||
{
|
||||
Entities.Add(entity.Id, entity);
|
||||
EntityList.Add(entity);
|
||||
|
||||
if (entity.StaticId == 0)
|
||||
DynamicEntitiesList.Add(entity);
|
||||
else
|
||||
StaticEntitiesList.Add(entity);
|
||||
|
||||
entity.Create();
|
||||
|
||||
newOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
public void DetachEntity(RoomPlayer currentOwner, Entity entity, byte[] payload)
|
||||
{
|
||||
Entities.Remove(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
StaticEntitiesList.Remove(entity);
|
||||
DynamicEntitiesList.Remove(entity);
|
||||
_entitiesDirtySet.Remove(entity);
|
||||
|
||||
entity.Destroy(payload);
|
||||
currentOwner.Entities.Remove(entity);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
var entities = (ushort) _entitiesDirtySet.Count;
|
||||
if (entities > 0)
|
||||
{
|
||||
_writer.Clear();
|
||||
_writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
_writer.WriteUShort(entities);
|
||||
|
||||
foreach (var entity in _entitiesDirtySet)
|
||||
entity.State.Write(_writer);
|
||||
|
||||
_entitiesDirtySet.Clear();
|
||||
|
||||
var sendData = _writer.ToArray();
|
||||
foreach (var roomPlayer in ReadyPlayersList)
|
||||
roomPlayer.Connection.UnreliableChannel.Send(sendData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPlayer(RoomPlayer player)
|
||||
{
|
||||
if (Players.Count == 0)
|
||||
Owner = player;
|
||||
|
||||
player.Attach(this);
|
||||
|
||||
PlayerList.Add(player);
|
||||
Players.Add(player.Connection.Id, player);
|
||||
}
|
||||
|
||||
public void RemovePlayer(RoomPlayer roomPlayer)
|
||||
{
|
||||
if (Players.Remove(roomPlayer.Connection.Id, out var player))
|
||||
{
|
||||
PlayerList.Remove(player);
|
||||
|
||||
{
|
||||
_writer.Clear();
|
||||
_writer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
||||
_writer.WriteString(player.Id);
|
||||
|
||||
var entitiesToDelete = player.Entities.DynamicList;
|
||||
_writer.WriteUShort((ushort) entitiesToDelete.Count);
|
||||
foreach (var entity in entitiesToDelete)
|
||||
{
|
||||
_writer.WriteUShort(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
}
|
||||
|
||||
var sendData = _writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
|
||||
if (roomPlayer.Connection.Id == Owner.Connection.Id && PlayerList.Count > 0)
|
||||
{
|
||||
var nextOwner = PlayerList[0];
|
||||
|
||||
Owner = nextOwner;
|
||||
|
||||
var entitiesToUpdate = roomPlayer.Entities.StaticList;
|
||||
|
||||
_writer.Clear();
|
||||
_writer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
||||
_writer.WriteString(Owner.Id);
|
||||
_writer.WriteUShort((ushort) entitiesToUpdate.Count);
|
||||
|
||||
foreach (var entity in entitiesToUpdate)
|
||||
{
|
||||
_writer.WriteUShort(entity.Id);
|
||||
|
||||
entity.SetOwner(nextOwner);
|
||||
nextOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
var sendData = _writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateReadyPlayerList()
|
||||
{
|
||||
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
|
||||
}
|
||||
|
||||
public void Track(Entity entity)
|
||||
{
|
||||
_entitiesDirtySet.Add(entity);
|
||||
}
|
||||
|
||||
public void Broadcast(byte[] data)
|
||||
{
|
||||
foreach (var readyPlayer in ReadyPlayersList)
|
||||
readyPlayer.Connection.ReliableChannel.Send(data);
|
||||
}
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Time;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class Room: IAction
|
||||
{
|
||||
public string Id { get; private set; }
|
||||
public RoomInformation Info { get; private set; }
|
||||
public RoomPlayer Owner { get; private set; }
|
||||
public RagonSerializer Writer { get; }
|
||||
public Dictionary<ushort, RoomPlayer> Players { get; private set; }
|
||||
public List<RoomPlayer> WaitPlayersList { get; private set; }
|
||||
public List<RoomPlayer> ReadyPlayersList { get; private set; }
|
||||
public List<RoomPlayer> PlayerList { get; private set; }
|
||||
|
||||
public Dictionary<ushort, Entity> Entities { get; private set; }
|
||||
public List<Entity> DynamicEntitiesList { get; private set; }
|
||||
public List<Entity> StaticEntitiesList { get; private set; }
|
||||
public List<Entity> EntityList { get; private set; }
|
||||
|
||||
private readonly HashSet<Entity> _entitiesDirtySet;
|
||||
|
||||
public Room(string roomId, RoomInformation info)
|
||||
{
|
||||
Id = roomId;
|
||||
Info = info;
|
||||
|
||||
Players = new Dictionary<ushort, RoomPlayer>(info.Max);
|
||||
WaitPlayersList = new List<RoomPlayer>(info.Max);
|
||||
ReadyPlayersList = new List<RoomPlayer>(info.Max);
|
||||
PlayerList = new List<RoomPlayer>(info.Max);
|
||||
|
||||
Entities = new Dictionary<ushort, Entity>();
|
||||
DynamicEntitiesList = new List<Entity>();
|
||||
StaticEntitiesList = new List<Entity>();
|
||||
EntityList = new List<Entity>();
|
||||
|
||||
_entitiesDirtySet = new HashSet<Entity>();
|
||||
Writer = new RagonSerializer(512);
|
||||
}
|
||||
|
||||
public void AttachEntity(RoomPlayer newOwner, Entity entity)
|
||||
{
|
||||
Entities.Add(entity.Id, entity);
|
||||
EntityList.Add(entity);
|
||||
|
||||
if (entity.StaticId == 0)
|
||||
DynamicEntitiesList.Add(entity);
|
||||
else
|
||||
StaticEntitiesList.Add(entity);
|
||||
|
||||
entity.Create();
|
||||
|
||||
newOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
public void DetachEntity(RoomPlayer currentOwner, Entity entity, byte[] payload)
|
||||
{
|
||||
Entities.Remove(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
StaticEntitiesList.Remove(entity);
|
||||
DynamicEntitiesList.Remove(entity);
|
||||
_entitiesDirtySet.Remove(entity);
|
||||
|
||||
entity.Destroy(payload);
|
||||
currentOwner.Entities.Remove(entity);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
var entities = (ushort) _entitiesDirtySet.Count;
|
||||
if (entities > 0)
|
||||
{
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
Writer.WriteUShort(entities);
|
||||
|
||||
foreach (var entity in _entitiesDirtySet)
|
||||
entity.State.Write(Writer);
|
||||
|
||||
_entitiesDirtySet.Clear();
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
foreach (var roomPlayer in ReadyPlayersList)
|
||||
roomPlayer.Connection.UnreliableChannel.Send(sendData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPlayer(RoomPlayer player)
|
||||
{
|
||||
if (Players.Count == 0)
|
||||
Owner = player;
|
||||
|
||||
player.Attach(this);
|
||||
|
||||
PlayerList.Add(player);
|
||||
Players.Add(player.Connection.Id, player);
|
||||
}
|
||||
|
||||
public void RemovePlayer(RoomPlayer roomPlayer)
|
||||
{
|
||||
if (Players.Remove(roomPlayer.Connection.Id, out var player))
|
||||
{
|
||||
PlayerList.Remove(player);
|
||||
|
||||
{
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
||||
Writer.WriteString(player.Id);
|
||||
|
||||
var entitiesToDelete = player.Entities.DynamicList;
|
||||
Writer.WriteUShort((ushort) entitiesToDelete.Count);
|
||||
foreach (var entity in entitiesToDelete)
|
||||
{
|
||||
Writer.WriteUShort(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
}
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
|
||||
if (roomPlayer.Connection.Id == Owner.Connection.Id && PlayerList.Count > 0)
|
||||
{
|
||||
var nextOwner = PlayerList[0];
|
||||
|
||||
Owner = nextOwner;
|
||||
|
||||
var entitiesToUpdate = roomPlayer.Entities.StaticList;
|
||||
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
||||
Writer.WriteString(Owner.Id);
|
||||
Writer.WriteUShort((ushort) entitiesToUpdate.Count);
|
||||
|
||||
foreach (var entity in entitiesToUpdate)
|
||||
{
|
||||
Writer.WriteUShort(entity.Id);
|
||||
|
||||
entity.SetOwner(nextOwner);
|
||||
nextOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateReadyPlayerList()
|
||||
{
|
||||
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
|
||||
}
|
||||
|
||||
public void Track(Entity entity)
|
||||
{
|
||||
_entitiesDirtySet.Add(entity);
|
||||
}
|
||||
|
||||
public void Broadcast(byte[] data)
|
||||
{
|
||||
foreach (var readyPlayer in ReadyPlayersList)
|
||||
readyPlayer.Connection.ReliableChannel.Send(data);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class RoomInformation
|
||||
{
|
||||
public string Map { get; set; }
|
||||
public int Min { get; set; }
|
||||
public int Max { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Map: {Map} Count: {Min}/{Max}";
|
||||
}
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class RoomInformation
|
||||
{
|
||||
public string Map { get; init; } = "none";
|
||||
public int Min { get; init; }
|
||||
public int Max { get; init; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Map: {Map} Count: {Min}/{Max}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user