remove unmanaged constraint from component storage
continuous-integration/drone/push Build is passing Details

pull/3/head
Evan Hemsley 2020-03-21 17:38:35 -07:00
parent 255760a3d7
commit 812fe10ca1
10 changed files with 73 additions and 142 deletions

View File

@ -16,7 +16,7 @@ namespace Encompass
ComponentBitSet = new ComponentBitSet(typeToIndex); ComponentBitSet = new ComponentBitSet(typeToIndex);
} }
public virtual void RegisterComponentType<TComponent>() where TComponent : unmanaged public virtual void RegisterComponentType<TComponent>() where TComponent : struct
{ {
if (!_stores.ContainsKey(typeof(TComponent))) if (!_stores.ContainsKey(typeof(TComponent)))
{ {
@ -27,13 +27,13 @@ namespace Encompass
if (!_typeToIndex.ContainsKey(typeof(TComponent))) { _typeToIndex.Add(typeof(TComponent), _typeToIndex.Count); } if (!_typeToIndex.ContainsKey(typeof(TComponent))) { _typeToIndex.Add(typeof(TComponent), _typeToIndex.Count); }
} }
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : unmanaged private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct
{ {
RegisterComponentType<TComponent>(); RegisterComponentType<TComponent>();
return _stores[typeof(TComponent)] as TypedComponentStore<TComponent>; return _stores[typeof(TComponent)] as TypedComponentStore<TComponent>;
} }
public bool Has<TComponent>(int entityID) where TComponent : unmanaged public bool Has<TComponent>(int entityID) where TComponent : struct
{ {
return Lookup<TComponent>().Has(entityID); return Lookup<TComponent>().Has(entityID);
} }
@ -48,18 +48,18 @@ namespace Encompass
return ComponentBitSet.EntityBitArray(entityID); return ComponentBitSet.EntityBitArray(entityID);
} }
public ref readonly TComponent Get<TComponent>(int entityID) where TComponent : unmanaged public ref readonly TComponent Get<TComponent>(int entityID) where TComponent : struct
{ {
return ref Lookup<TComponent>().Get(entityID); return ref Lookup<TComponent>().Get(entityID);
} }
public virtual void Set<TComponent>(int entityID, TComponent component) where TComponent : unmanaged public virtual void Set<TComponent>(int entityID, TComponent component) where TComponent : struct
{ {
Lookup<TComponent>().Set(entityID, component); Lookup<TComponent>().Set(entityID, component);
ComponentBitSet.Set<TComponent>(entityID); ComponentBitSet.Set<TComponent>(entityID);
} }
public virtual bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : unmanaged public virtual bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
{ {
if (Lookup<TComponent>().Set(entityID, component, priority)) if (Lookup<TComponent>().Set(entityID, component, priority))
{ {
@ -69,7 +69,7 @@ namespace Encompass
return false; return false;
} }
public virtual bool Remove<TComponent>(int entityID, int priority) where TComponent : unmanaged public virtual bool Remove<TComponent>(int entityID, int priority) where TComponent : struct
{ {
if (Lookup<TComponent>().Remove(entityID, priority)) if (Lookup<TComponent>().Remove(entityID, priority))
{ {
@ -79,7 +79,7 @@ namespace Encompass
return false; return false;
} }
public void ForceRemove<TComponent>(int entityID) where TComponent : unmanaged public void ForceRemove<TComponent>(int entityID) where TComponent : struct
{ {
Lookup<TComponent>().ForceRemove(entityID); Lookup<TComponent>().ForceRemove(entityID);
ComponentBitSet.RemoveComponent<TComponent>(entityID); ComponentBitSet.RemoveComponent<TComponent>(entityID);
@ -94,17 +94,17 @@ namespace Encompass
ComponentBitSet.RemoveEntity(entityID); ComponentBitSet.RemoveEntity(entityID);
} }
public bool Any<TComponent>() where TComponent : unmanaged public bool Any<TComponent>() where TComponent : struct
{ {
return Lookup<TComponent>().Count > 0; return Lookup<TComponent>().Count > 0;
} }
public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : unmanaged public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : struct
{ {
return Lookup<TComponent>().All(); return Lookup<TComponent>().All();
} }
public void Clear<TComponent>() where TComponent : unmanaged public void Clear<TComponent>() where TComponent : struct
{ {
Lookup<TComponent>().Clear(); Lookup<TComponent>().Clear();
} }

View File

@ -1,69 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
internal unsafe class FixedArray<T> : IDisposable where T : unmanaged
{
private void* _buffer;
public int Length { get; private set; }
public T this[int i]
{
get { return ReadElement(_buffer, i); }
set { WriteElement(_buffer, i, value); }
}
public unsafe FixedArray(int length, bool clearMemory = false)
{
var bufferSize = Marshal.SizeOf<T>() * length;
_buffer = (void*)Alloc(bufferSize);
if (clearMemory)
{
Memset(_buffer, 0, bufferSize);
}
Length = length;
}
public ref T ByRef(int i)
{
return ref *(T*)((byte*)_buffer + i * sizeof(T));
}
void IDisposable.Dispose()
{
Free((IntPtr)_buffer);
_buffer = null;
Length = 0;
}
private static T ReadElement(void* buffer, int i)
{
return *(T*)((byte*)buffer + i * sizeof(T));
}
private static void WriteElement(void* buffer, int i, T value)
{
*(T*)((byte*)buffer + i * sizeof(T)) = value;
}
private unsafe static IntPtr Alloc(int size)
{
return Marshal.AllocHGlobal(size);
}
public unsafe static void Free(IntPtr ptr)
{
if (ptr != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptr);
}
}
private unsafe static void Memset(void* ptr, byte value, int count)
{
Unsafe.InitBlock(ptr, value, (uint)count);
}
}

View File

@ -10,7 +10,7 @@ namespace Encompass
public abstract void Clear(); public abstract void Clear();
} }
internal class Replayer<TComponent> : Replayer where TComponent : unmanaged internal class Replayer<TComponent> : Replayer where TComponent : struct
{ {
private readonly ComponentDeltaStore _deltaStore; private readonly ComponentDeltaStore _deltaStore;
private readonly HashSet<int> _removals = new HashSet<int>(); private readonly HashSet<int> _removals = new HashSet<int>();

View File

@ -14,11 +14,11 @@ namespace Encompass
public abstract void ClearPriorities(); public abstract void ClearPriorities();
} }
internal unsafe class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : unmanaged internal unsafe class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct
{ {
private readonly Dictionary<int, int> _indices = new Dictionary<int, int>(512); private readonly Dictionary<int, int> _indices = new Dictionary<int, int>(512);
private readonly Dictionary<int, int> _priorities = new Dictionary<int, int>(512); private readonly Dictionary<int, int> _priorities = new Dictionary<int, int>(512);
private readonly FixedArray<TComponent> _components = new FixedArray<TComponent>(512); // TODO: allow specify size private readonly TComponent[] _components = new TComponent[512]; // TODO: allow specify size
private readonly IDManager _idManager = new IDManager(); private readonly IDManager _idManager = new IDManager();
public override int Count { get => _indices.Count; } public override int Count { get => _indices.Count; }
@ -26,7 +26,7 @@ namespace Encompass
public unsafe ref readonly TComponent Get(int entityID) public unsafe ref readonly 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); } 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.ByRef(_indices[entityID]); return ref _components[_indices[entityID]];
} }
public unsafe void Set(int entityID, TComponent component) public unsafe void Set(int entityID, TComponent component)

View File

@ -29,7 +29,7 @@ namespace Encompass
TypeToIndex = typeToIndex; TypeToIndex = typeToIndex;
} }
public void RegisterComponentType<TComponent>() where TComponent : unmanaged public void RegisterComponentType<TComponent>() where TComponent : struct
{ {
_existingComponentStore.RegisterComponentType<TComponent>(); _existingComponentStore.RegisterComponentType<TComponent>();
_immediateComponentStore.RegisterComponentType<TComponent>(); _immediateComponentStore.RegisterComponentType<TComponent>();
@ -47,7 +47,7 @@ namespace Encompass
_upToDateComponentStore.SwapWith(componentStore); _upToDateComponentStore.SwapWith(componentStore);
} }
internal void RegisterDrawableComponent<TComponent>(int entityID, int layer) where TComponent : unmanaged internal void RegisterDrawableComponent<TComponent>(int entityID, int layer) where TComponent : struct
{ {
_drawLayerManager.RegisterComponentWithLayer<TComponent>(entityID, layer); _drawLayerManager.RegisterComponentWithLayer<TComponent>(entityID, layer);
} }
@ -61,7 +61,7 @@ namespace Encompass
_replayStore.ClearAll(); _replayStore.ClearAll();
} }
internal bool AddImmediateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : unmanaged internal bool AddImmediateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
{ {
if (_immediateComponentStore.Set(entityID, component, priority)) if (_immediateComponentStore.Set(entityID, component, priority))
{ {
@ -73,14 +73,14 @@ namespace Encompass
return false; return false;
} }
internal void AddImmediateComponent<TComponent>(int entityID, TComponent component) where TComponent : unmanaged internal void AddImmediateComponent<TComponent>(int entityID, TComponent component) where TComponent : struct
{ {
_immediateComponentStore.Set(entityID, component); _immediateComponentStore.Set(entityID, component);
_replayStore.Set(entityID, component); _replayStore.Set(entityID, component);
_upToDateComponentStore.Set(entityID, component); _upToDateComponentStore.Set(entityID, component);
} }
internal bool UpdateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : unmanaged internal bool UpdateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
{ {
var result = _upToDateComponentStore.Set(entityID, component, priority); var result = _upToDateComponentStore.Set(entityID, component, priority);
if (result) if (result)
@ -90,7 +90,7 @@ namespace Encompass
return result; return result;
} }
internal void AddComponent<TComponent>(int entityID, TComponent component) where TComponent : unmanaged internal void AddComponent<TComponent>(int entityID, TComponent component) where TComponent : struct
{ {
_upToDateComponentStore.Set(entityID, component); _upToDateComponentStore.Set(entityID, component);
_replayStore.Set(entityID, component); _replayStore.Set(entityID, component);
@ -98,12 +98,12 @@ namespace Encompass
// existing or immediate reads // existing or immediate reads
internal IEnumerable<(TComponent, int)> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : unmanaged internal IEnumerable<(TComponent, int)> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct
{ {
return _upToDateComponentStore.All<TComponent>(); return _upToDateComponentStore.All<TComponent>();
} }
internal (TComponent, int) ReadFirstExistingOrImmediateComponentByType<TComponent>() where TComponent : unmanaged internal (TComponent, int) ReadFirstExistingOrImmediateComponentByType<TComponent>() where TComponent : struct
{ {
if (!SomeExistingOrImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } if (!SomeExistingOrImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
var enumerator = ReadExistingAndImmediateComponentsByType<TComponent>().GetEnumerator(); var enumerator = ReadExistingAndImmediateComponentsByType<TComponent>().GetEnumerator();
@ -111,14 +111,14 @@ namespace Encompass
return enumerator.Current; return enumerator.Current;
} }
internal bool SomeExistingOrImmediateComponent<TComponent>() where TComponent : unmanaged internal bool SomeExistingOrImmediateComponent<TComponent>() where TComponent : struct
{ {
return _upToDateComponentStore.Any<TComponent>(); return _upToDateComponentStore.Any<TComponent>();
} }
// existing reads // existing reads
internal (TComponent, int) ReadFirstExistingComponentByType<TComponent>() where TComponent : unmanaged internal (TComponent, int) ReadFirstExistingComponentByType<TComponent>() where TComponent : struct
{ {
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
var enumerator = GetComponentsIncludingEntity<TComponent>().GetEnumerator(); var enumerator = GetComponentsIncludingEntity<TComponent>().GetEnumerator();
@ -126,19 +126,19 @@ namespace Encompass
return enumerator.Current; return enumerator.Current;
} }
internal bool SomeExistingComponent<TComponent>() where TComponent : unmanaged internal bool SomeExistingComponent<TComponent>() where TComponent : struct
{ {
return _existingComponentStore.Any<TComponent>(); return _existingComponentStore.Any<TComponent>();
} }
// immediate reads // immediate reads
internal IEnumerable<(TComponent, int)> ReadImmediateComponentsByType<TComponent>() where TComponent : unmanaged internal IEnumerable<(TComponent, int)> ReadImmediateComponentsByType<TComponent>() where TComponent : struct
{ {
return _immediateComponentStore.All<TComponent>(); return _immediateComponentStore.All<TComponent>();
} }
internal (TComponent, int) ReadFirstImmediateComponentByType<TComponent>() where TComponent : unmanaged internal (TComponent, int) ReadFirstImmediateComponentByType<TComponent>() where TComponent : struct
{ {
if (!SomeImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } if (!SomeImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
var enumerator = ReadImmediateComponentsByType<TComponent>().GetEnumerator(); var enumerator = ReadImmediateComponentsByType<TComponent>().GetEnumerator();
@ -146,31 +146,31 @@ namespace Encompass
return enumerator.Current; return enumerator.Current;
} }
internal bool SomeImmediateComponent<TComponent>() where TComponent : unmanaged internal bool SomeImmediateComponent<TComponent>() where TComponent : struct
{ {
return _immediateComponentStore.Any<TComponent>(); return _immediateComponentStore.Any<TComponent>();
} }
// component getters // component getters
internal ref readonly TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : unmanaged internal ref readonly TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return ref _upToDateComponentStore.Get<TComponent>(entityID); return ref _upToDateComponentStore.Get<TComponent>(entityID);
} }
internal ref readonly TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : unmanaged internal ref readonly TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return ref _existingComponentStore.Get<TComponent>(entityID); return ref _existingComponentStore.Get<TComponent>(entityID);
} }
internal ref readonly TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : unmanaged internal ref readonly TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return ref _immediateComponentStore.Get<TComponent>(entityID); return ref _immediateComponentStore.Get<TComponent>(entityID);
} }
// has checkers // has checkers
internal bool HasExistingOrImmediateComponent<TComponent>(int entityID) where TComponent : unmanaged internal bool HasExistingOrImmediateComponent<TComponent>(int entityID) where TComponent : struct
{ {
return _upToDateComponentStore.Has<TComponent>(entityID); return _upToDateComponentStore.Has<TComponent>(entityID);
} }
@ -180,7 +180,7 @@ namespace Encompass
return _upToDateComponentStore.Has(type, entityID); return _upToDateComponentStore.Has(type, entityID);
} }
internal bool HasExistingComponent<TComponent>(int entityID) where TComponent : unmanaged internal bool HasExistingComponent<TComponent>(int entityID) where TComponent : struct
{ {
return _existingComponentStore.Has<TComponent>(entityID); return _existingComponentStore.Has<TComponent>(entityID);
} }
@ -190,7 +190,7 @@ namespace Encompass
return _existingComponentStore.Has(type, entityID); return _existingComponentStore.Has(type, entityID);
} }
internal bool HasImmediateComponent<TComponent>(int entityID) where TComponent : unmanaged internal bool HasImmediateComponent<TComponent>(int entityID) where TComponent : struct
{ {
return _immediateComponentStore.Has<TComponent>(entityID); return _immediateComponentStore.Has<TComponent>(entityID);
} }
@ -200,12 +200,12 @@ namespace Encompass
return _immediateComponentStore.Has(type, entityID); return _immediateComponentStore.Has(type, entityID);
} }
internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity<TComponent>() where TComponent : unmanaged internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity<TComponent>() where TComponent : struct
{ {
return _existingComponentStore.All<TComponent>(); return _existingComponentStore.All<TComponent>();
} }
internal IEnumerable<TComponent> GetComponentsByType<TComponent>() where TComponent : unmanaged internal IEnumerable<TComponent> GetComponentsByType<TComponent>() where TComponent : struct
{ {
foreach (var pair in _existingComponentStore.All<TComponent>()) foreach (var pair in _existingComponentStore.All<TComponent>())
{ {
@ -213,12 +213,12 @@ namespace Encompass
} }
} }
internal ref readonly TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : unmanaged internal ref readonly TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return ref _existingComponentStore.Get<TComponent>(entityID); return ref _existingComponentStore.Get<TComponent>(entityID);
} }
internal bool EntityHasComponentOfType<TComponent>(int entityID) where TComponent : unmanaged internal bool EntityHasComponentOfType<TComponent>(int entityID) where TComponent : struct
{ {
return _existingComponentStore.Has<TComponent>(entityID); return _existingComponentStore.Has<TComponent>(entityID);
} }
@ -242,7 +242,7 @@ namespace Encompass
_entitiesMarkedForRemoval.Clear(); _entitiesMarkedForRemoval.Clear();
} }
public bool RemoveImmediate<TComponent>(int entityID, int priority) where TComponent : unmanaged public bool RemoveImmediate<TComponent>(int entityID, int priority) where TComponent : struct
{ {
if (_immediateComponentStore.Remove<TComponent>(entityID, priority)) if (_immediateComponentStore.Remove<TComponent>(entityID, priority))
{ {
@ -254,7 +254,7 @@ namespace Encompass
return false; return false;
} }
public void Remove<TComponent>(int entityID, int priority) where TComponent : unmanaged public void Remove<TComponent>(int entityID, int priority) where TComponent : struct
{ {
if (_upToDateComponentStore.Remove<TComponent>(entityID, priority)) if (_upToDateComponentStore.Remove<TComponent>(entityID, priority))
{ {

View File

@ -213,7 +213,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns an Entity containing the specified Component type. /// Returns an Entity containing the specified Component type.
/// </summary> /// </summary>
protected Entity ReadEntity<TComponent>() where TComponent : unmanaged protected Entity ReadEntity<TComponent>() where TComponent : struct
{ {
return _entityManager.GetEntity(ReadComponentHelper<TComponent>().Item2); return _entityManager.GetEntity(ReadComponentHelper<TComponent>().Item2);
} }
@ -221,7 +221,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns all Entities containing the specified Component type. /// Returns all Entities containing the specified Component type.
/// </summary> /// </summary>
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : unmanaged protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct
{ {
foreach (var pair in ReadComponentsHelper<TComponent>()) foreach (var pair in ReadComponentsHelper<TComponent>())
{ {
@ -231,12 +231,12 @@ namespace Encompass
// these next two are for the ComponentMessageEmitter only // these next two are for the ComponentMessageEmitter only
internal IEnumerable<TComponent> ReadComponentsFromWorld<TComponent>() where TComponent : unmanaged internal IEnumerable<TComponent> ReadComponentsFromWorld<TComponent>() where TComponent : struct
{ {
return _componentManager.GetComponentsByType<TComponent>(); return _componentManager.GetComponentsByType<TComponent>();
} }
private IEnumerable<(TComponent, int)> ReadComponentsHelper<TComponent>() where TComponent : unmanaged private IEnumerable<(TComponent, int)> ReadComponentsHelper<TComponent>() where TComponent : struct
{ {
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -261,7 +261,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns all of the Components with the specified Component Type. /// Returns all of the Components with the specified Component Type.
/// </summary> /// </summary>
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : unmanaged protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct
{ {
foreach (var pair in ReadComponentsHelper<TComponent>()) foreach (var pair in ReadComponentsHelper<TComponent>())
{ {
@ -272,7 +272,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns all of the components of the specified type including an Entity reference for each Component. /// Returns all of the components of the specified type including an Entity reference for each Component.
/// </summary> /// </summary>
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : unmanaged protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct
{ {
foreach (var (component, id) in ReadComponentsHelper<TComponent>()) foreach (var (component, id) in ReadComponentsHelper<TComponent>())
{ {
@ -280,7 +280,7 @@ namespace Encompass
} }
} }
private (TComponent, int) ReadComponentHelper<TComponent>() where TComponent : unmanaged private (TComponent, int) ReadComponentHelper<TComponent>() where TComponent : struct
{ {
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -305,7 +305,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned. /// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
/// </summary> /// </summary>
protected TComponent ReadComponent<TComponent>() where TComponent : unmanaged protected TComponent ReadComponent<TComponent>() where TComponent : struct
{ {
return ReadComponentHelper<TComponent>().Item1; return ReadComponentHelper<TComponent>().Item1;
} }
@ -313,7 +313,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns a component of the specified type including its Entity reference. If multiples exist, an arbitrary Component is returned. /// Returns a component of the specified type including its Entity reference. If multiples exist, an arbitrary Component is returned.
/// </summary> /// </summary>
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : unmanaged protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct
{ {
var (component, id) = ReadComponentHelper<TComponent>(); var (component, id) = ReadComponentHelper<TComponent>();
return (component, _entityManager.GetEntity(id)); return (component, _entityManager.GetEntity(id));
@ -322,7 +322,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns true if any Component with the specified Component Type exists. /// Returns true if any Component with the specified Component Type exists.
/// </summary> /// </summary>
protected bool SomeComponent<TComponent>() where TComponent : unmanaged protected bool SomeComponent<TComponent>() where TComponent : struct
{ {
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -344,7 +344,7 @@ namespace Encompass
} }
} }
private ref readonly TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : unmanaged private ref readonly TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct
{ {
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -375,7 +375,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException"> /// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it reads the given Component Type. /// Thrown when the Engine does not declare that it reads the given Component Type.
/// </exception> /// </exception>
protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : unmanaged protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct
{ {
return ref GetComponentHelper<TComponent>(entity.ID); return ref GetComponentHelper<TComponent>(entity.ID);
} }
@ -386,7 +386,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException"> /// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that is Reads the given Component Type. /// Thrown when the Engine does not declare that is Reads the given Component Type.
/// </exception> /// </exception>
protected bool HasComponent<TComponent>(Entity entity) where TComponent : unmanaged protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct
{ {
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -444,7 +444,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalWriteException"> /// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type. /// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception> /// </exception>
protected void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : unmanaged protected void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct
{ {
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority; var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
@ -484,7 +484,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalWriteException"> /// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type. /// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception> /// </exception>
protected void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : unmanaged protected void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct
{ {
if (!EntityCreatedThisFrame(entity.ID)) if (!EntityCreatedThisFrame(entity.ID))
{ {
@ -592,7 +592,7 @@ namespace Encompass
/// Destroys an arbitrary Entity containing a Component of the specified Type. /// Destroys an arbitrary Entity containing a Component of the specified Type.
/// Entity destruction takes place after all the Engines have been processed by World Update. /// Entity destruction takes place after all the Engines have been processed by World Update.
/// </summary> /// </summary>
protected void DestroyWith<TComponent>() where TComponent : unmanaged protected void DestroyWith<TComponent>() where TComponent : struct
{ {
Destroy(ReadEntity<TComponent>()); Destroy(ReadEntity<TComponent>());
} }
@ -601,7 +601,7 @@ namespace Encompass
/// Destroys all Entities containing a Component of the specified Type. /// Destroys all Entities containing a Component of the specified Type.
/// Entity destruction takes place after all the Engines have been processed by World Update. /// Entity destruction takes place after all the Engines have been processed by World Update.
/// </summary> /// </summary>
protected void DestroyAllWith<TComponent>() where TComponent : unmanaged protected void DestroyAllWith<TComponent>() where TComponent : struct
{ {
foreach (var entity in ReadEntities<TComponent>()) foreach (var entity in ReadEntities<TComponent>())
{ {
@ -614,7 +614,7 @@ namespace Encompass
/// Note that the Engine must Read the Component type that is being removed. /// Note that the Engine must Read the Component type that is being removed.
/// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity. /// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity.
/// </summary> /// </summary>
protected void RemoveComponent<TComponent>(Entity entity) where TComponent : unmanaged protected void RemoveComponent<TComponent>(Entity entity) where TComponent : struct
{ {
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority; var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;

View File

@ -16,7 +16,7 @@ namespace Encompass
_drawLayerManager = drawLayerManager; _drawLayerManager = drawLayerManager;
} }
public void RegisterOrderedRenderer<TComponent>(Action<Entity> renderAction) where TComponent : unmanaged public void RegisterOrderedRenderer<TComponent>(Action<Entity> renderAction) where TComponent : struct
{ {
_drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction); _drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction);
_drawLayerManager.RegisterOrderedDrawable<TComponent>(); _drawLayerManager.RegisterOrderedDrawable<TComponent>();

View File

@ -17,7 +17,7 @@ namespace Encompass
_componentManager = componentManager; _componentManager = componentManager;
} }
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : unmanaged protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct
{ {
foreach (var pair in ReadComponentsIncludingEntity<TComponent>()) foreach (var pair in ReadComponentsIncludingEntity<TComponent>())
{ {
@ -25,17 +25,17 @@ namespace Encompass
} }
} }
protected Entity ReadEntity<TComponent>() where TComponent : unmanaged protected Entity ReadEntity<TComponent>() where TComponent : struct
{ {
return ReadComponentIncludingEntity<TComponent>().Item2; return ReadComponentIncludingEntity<TComponent>().Item2;
} }
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : unmanaged protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct
{ {
return _componentManager.GetComponentsByType<TComponent>(); return _componentManager.GetComponentsByType<TComponent>();
} }
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : unmanaged protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct
{ {
foreach (var (component, id) in _componentManager.GetComponentsIncludingEntity<TComponent>()) foreach (var (component, id) in _componentManager.GetComponentsIncludingEntity<TComponent>())
{ {
@ -43,31 +43,31 @@ namespace Encompass
} }
} }
protected TComponent ReadComponent<TComponent>() where TComponent : unmanaged protected TComponent ReadComponent<TComponent>() where TComponent : struct
{ {
var enumerator = ReadComponents<TComponent>().GetEnumerator(); var enumerator = ReadComponents<TComponent>().GetEnumerator();
enumerator.MoveNext(); enumerator.MoveNext();
return enumerator.Current; return enumerator.Current;
} }
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : unmanaged protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct
{ {
var enumerator = ReadComponentsIncludingEntity<TComponent>().GetEnumerator(); var enumerator = ReadComponentsIncludingEntity<TComponent>().GetEnumerator();
enumerator.MoveNext(); enumerator.MoveNext();
return enumerator.Current; return enumerator.Current;
} }
protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : unmanaged protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct
{ {
return ref _componentManager.GetComponentByEntityAndType<TComponent>(entity.ID); return ref _componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
} }
protected bool HasComponent<TComponent>(Entity entity) where TComponent : unmanaged protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct
{ {
return _componentManager.EntityHasComponentOfType<TComponent>(entity.ID); return _componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
} }
protected bool SomeComponent<TComponent>() where TComponent : unmanaged protected bool SomeComponent<TComponent>() where TComponent : struct
{ {
return _componentManager.SomeExistingComponent<TComponent>(); return _componentManager.SomeExistingComponent<TComponent>();
} }

View File

@ -5,7 +5,7 @@ namespace Encompass
/// <summary> /// <summary>
/// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer. /// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer.
/// </summary> /// </summary>
public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : unmanaged, IDrawableComponent public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : struct, IDrawableComponent
{ {
public abstract void Render(Entity entity, in TComponent drawComponent); public abstract void Render(Entity entity, in TComponent drawComponent);

View File

@ -84,7 +84,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Sets Component data for the specified Component Type on the specified Entity. /// Sets Component data for the specified Component Type on the specified Entity.
/// </summary> /// </summary>
public void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : unmanaged public void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct
{ {
RegisterComponentType<TComponent>(); RegisterComponentType<TComponent>();
_startingExistingComponentStore.Set(entity.ID, component); _startingExistingComponentStore.Set(entity.ID, component);
@ -97,7 +97,7 @@ namespace Encompass
} }
} }
internal void RegisterComponentType<TComponent>() where TComponent : unmanaged internal void RegisterComponentType<TComponent>() where TComponent : struct
{ {
if (!_typeToIndex.ContainsKey(typeof(TComponent))) if (!_typeToIndex.ContainsKey(typeof(TComponent)))
{ {
@ -183,7 +183,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Adds the specified OrderedRenderer to the World. /// Adds the specified OrderedRenderer to the World.
/// </summary> /// </summary>
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : unmanaged, IDrawableComponent public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IDrawableComponent
{ {
RegisterComponentType<TComponent>(); RegisterComponentType<TComponent>();
renderer.AssignEntityManager(_entityManager); renderer.AssignEntityManager(_entityManager);