fix bugs in component storage and draw layer manager
continuous-integration/drone/push Build is failing Details

pull/3/head
Evan Hemsley 2020-03-21 22:19:31 -07:00
parent 4178e8f02c
commit 150043b0e5
8 changed files with 53 additions and 29 deletions

View File

@ -25,7 +25,7 @@ namespace Encompass
}
}
public void Set<TComponent>(int entityID, TComponent component) where TComponent : struct
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : struct
{
_store.Set(entityID, component);
RegisterComponentType<TComponent>();

View File

@ -48,18 +48,18 @@ namespace Encompass
return ComponentBitSet.EntityBitArray(entityID);
}
public ref readonly TComponent Get<TComponent>(int entityID) where TComponent : struct
public ref TComponent Get<TComponent>(int entityID) where TComponent : struct
{
return ref Lookup<TComponent>().Get(entityID);
}
public void Set<TComponent>(int entityID, TComponent component) where TComponent : struct
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : struct
{
Lookup<TComponent>().Set(entityID, component);
ComponentBitSet.Set<TComponent>(entityID);
}
public bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
public bool Set<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
{
if (Lookup<TComponent>().Set(entityID, component, priority))
{

View File

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
@ -14,7 +13,7 @@ namespace Encompass
public abstract void ClearPriorities();
}
internal unsafe class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct
internal class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct
{
private readonly Dictionary<int, int> _indices = new Dictionary<int, int>(512);
private readonly Dictionary<int, int> _priorities = new Dictionary<int, int>(512);
@ -23,18 +22,18 @@ namespace Encompass
public override int Count { get => _indices.Count; }
public unsafe ref readonly TComponent Get(int entityID)
public ref TComponent Get(int entityID)
{
if (!_indices.ContainsKey(entityID)) { throw new Exceptions.NoComponentOfTypeOnEntityException("No component of type {0} exists on Entity with ID {1}", typeof(TComponent), entityID); }
return ref _components[_indices[entityID]];
}
public unsafe void Set(int entityID, TComponent component)
public void Set(int entityID, in TComponent component)
{
InternalSet(entityID, component);
}
public unsafe bool Set(int entityID, TComponent component, int priority)
public bool Set(int entityID, in TComponent component, int priority)
{
if (!_priorities.ContainsKey(entityID) || priority < _priorities[entityID])
{
@ -46,7 +45,7 @@ namespace Encompass
return false;
}
private unsafe void InternalSet(int entityID, TComponent component)
private unsafe void InternalSet(int entityID, in TComponent component)
{
if (!_indices.ContainsKey(entityID))
{
@ -70,9 +69,12 @@ namespace Encompass
public override void ForceRemove(int entityID)
{
if (_indices.ContainsKey(entityID))
{
_idManager.Free(_indices[entityID]);
_indices.Remove(entityID);
_priorities.Remove(entityID);
_idManager.Free(entityID);
}
}
public override bool Has(int entityID)
@ -82,9 +84,9 @@ namespace Encompass
public override void Clear()
{
foreach (var entityID in _indices.Keys)
foreach (var mappedID in _indices.Values)
{
_idManager.Free(entityID);
_idManager.Free(mappedID);
}
_indices.Clear();
_priorities.Clear();

View File

@ -61,7 +61,7 @@ namespace Encompass
_replayStore.ClearAll();
}
internal bool AddImmediateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
internal bool AddImmediateComponent<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
{
if (_immediateComponentStore.Set(entityID, component, priority))
{
@ -73,14 +73,14 @@ namespace Encompass
return false;
}
internal void AddImmediateComponent<TComponent>(int entityID, TComponent component) where TComponent : struct
internal void AddImmediateComponent<TComponent>(int entityID, in TComponent component) where TComponent : struct
{
_immediateComponentStore.Set(entityID, component);
_replayStore.Set(entityID, component);
_upToDateComponentStore.Set(entityID, component);
}
internal bool UpdateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
internal bool UpdateComponent<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
{
var result = _upToDateComponentStore.Set(entityID, component, priority);
if (result)
@ -90,7 +90,7 @@ namespace Encompass
return result;
}
internal void AddComponent<TComponent>(int entityID, TComponent component) where TComponent : struct
internal void AddComponent<TComponent>(int entityID, in TComponent component) where TComponent : struct
{
_upToDateComponentStore.Set(entityID, component);
_replayStore.Set(entityID, component);
@ -153,17 +153,17 @@ namespace Encompass
// component getters
internal ref readonly TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
internal ref TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{
return ref _upToDateComponentStore.Get<TComponent>(entityID);
}
internal ref readonly TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
internal ref TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{
return ref _existingComponentStore.Get<TComponent>(entityID);
}
internal ref readonly TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
internal ref TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{
return ref _immediateComponentStore.Get<TComponent>(entityID);
}

View File

@ -97,12 +97,19 @@ namespace Encompass
{
foreach (var store in _layerIndexToTypeToID.Values)
{
foreach (var set in store.Values)
foreach (var typeToSet in store)
{
var type = typeToSet.Key;
var set = typeToSet.Value;
if (set.Contains(entityID))
{
_typeToEntityToLayer[type].Remove(entityID);
set.Remove(entityID);
}
}
}
}
public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer)
{

View File

@ -344,7 +344,7 @@ namespace Encompass
}
}
private ref readonly TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -380,6 +380,20 @@ namespace Encompass
return ref GetComponentHelper<TComponent>(entity.ID);
}
/// <summary>
/// Returns a Component by reference with the specified Type that exists on the Entity.
/// </summary>
/// <exception cref="Encompass.Exceptions.NoComponentOfTypeOnEntityException">
/// Thrown when the Entity does not have a Component of the specified Type
/// </exception>
/// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it reads the given Component Type.
/// </exception>
protected ref TComponent GetComponentMutable<TComponent>(Entity entity) where TComponent : struct
{
return ref GetComponentHelper<TComponent>(entity.ID);
}
/// <summary>
/// Returns true if the Entity has a Component of the given Type.
/// </summary>
@ -444,7 +458,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception>
protected void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct
protected void SetComponent<TComponent>(Entity entity, in TComponent component) where TComponent : struct
{
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
@ -484,7 +498,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception>
protected void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct
protected void AddComponent<TComponent>(Entity entity, in TComponent component) where TComponent : struct
{
if (!EntityCreatedThisFrame(entity.ID))
{

View File

@ -11,7 +11,8 @@ namespace Encompass
internal void InternalRender(Entity entity)
{
Render(entity, GetComponent<TComponent>(entity));
ref readonly var component = ref GetComponent<TComponent>(entity);
Render(entity, component);
}
}
}

View File

@ -84,7 +84,7 @@ namespace Encompass
/// <summary>
/// Sets Component data for the specified Component Type on the specified Entity.
/// </summary>
public void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct
public void SetComponent<TComponent>(Entity entity, in TComponent component) where TComponent : struct
{
RegisterComponentType<TComponent>();
_startingExistingComponentStore.Set(entity.ID, component);