unmanaged structs
continuous-integration/drone/push Build is passing Details

pull/3/head
Evan Hemsley 2020-03-20 15:45:58 -07:00
parent 6d699e4b17
commit 408dd9bfeb
27 changed files with 132 additions and 249 deletions

View File

@ -14,13 +14,6 @@ namespace Encompass
{
foreach (var queryWithType in queryWithTypes)
{
var isComponent = queryWithType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", queryWithType.Name);
}
QueryWithTypes.Add(queryWithType);
}
}

View File

@ -14,14 +14,7 @@ namespace Encompass
{
foreach (var type in queryWithoutTypes)
{
var isComponent = type.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", type.Name);
}
this.QueryWithoutTypes.Add(type);
QueryWithoutTypes.Add(type);
}
}
}

View File

@ -14,14 +14,7 @@ namespace Encompass
{
foreach (var readType in readTypes)
{
var isComponent = readType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", readType.Name);
}
this.ReadTypes.Add(readType);
ReadTypes.Add(readType);
}
}
}

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
@ -14,14 +12,7 @@ namespace Encompass
{
foreach (var readImmediateType in readImmediateTypes)
{
var isComponent = readImmediateType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", readImmediateType.Name);
}
this.ReadImmediateTypes.Add(readImmediateType);
ReadImmediateTypes.Add(readImmediateType);
}
}
}

View File

@ -15,24 +15,12 @@ namespace Encompass
{
foreach (var writeType in writeTypes)
{
var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name);
}
this.WriteTypes.Add(writeType);
WriteTypes.Add(writeType);
}
}
public Writes(Type writeType, int priority)
{
var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name);
}
WriteTypes.Add(writeType);
Priorities.Add(writeType, priority);
}

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
@ -14,13 +12,7 @@ namespace Encompass
{
foreach (var writeImmediateType in writeImmediateTypes)
{
var isComponent = writeImmediateType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalWriteImmediateTypeException("{0} must be a Component", writeImmediateType.Name);
}
this.WriteImmediateTypes.Add(writeImmediateType);
WriteImmediateTypes.Add(writeImmediateType);
}
}
}

View File

@ -24,13 +24,13 @@ namespace Encompass
_entities.Add(entityID, BitSet512.Zero);
}
public void Set<TComponent>(int entityID) where TComponent : struct, IComponent
public void Set<TComponent>(int entityID) where TComponent : struct
{
if (!_entities.ContainsKey(entityID)) { AddEntity(entityID); }
_entities[entityID] = _entities[entityID].Set(_typeToIndex[typeof(TComponent)]);
}
public void RemoveComponent<TComponent>(int entityID) where TComponent : struct, IComponent
public void RemoveComponent<TComponent>(int entityID) where TComponent : struct
{
if (_entities.ContainsKey(entityID))
{

View File

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

View File

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

View File

@ -14,7 +14,7 @@ namespace Encompass
public abstract void ClearPriorities();
}
internal class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct, IComponent
internal class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : unmanaged
{
private readonly Dictionary<int, int> _indices = new Dictionary<int, int>(512);
private readonly Dictionary<int, int> _priorities = new Dictionary<int, int>(512);

View File

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

View File

@ -30,7 +30,7 @@ namespace Encompass
}
}
public void RegisterOrderedDrawable<TComponent>() where TComponent : struct, IComponent
public void RegisterOrderedDrawable<TComponent>() where TComponent : struct
{
if (!_typeToEntityToLayer.ContainsKey(typeof(TComponent)))
{
@ -53,7 +53,7 @@ namespace Encompass
set.Add(renderer);
}
public void RegisterComponentWithLayer<TComponent>(int entityID, int layer) where TComponent : struct, IComponent
public void RegisterComponentWithLayer<TComponent>(int entityID, int layer) where TComponent : struct
{
if (!_layerIndexToTypeToID.ContainsKey(layer))
{
@ -68,7 +68,7 @@ namespace Encompass
_typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
}
public void UnRegisterComponentWithLayer<TComponent>(int entityID) where TComponent : struct, IComponent
public void UnRegisterComponentWithLayer<TComponent>(int entityID) where TComponent : struct
{
if (!_typeToEntityToLayer.ContainsKey(typeof(TComponent))) { return; }

View File

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

View File

@ -1,12 +0,0 @@
using System;
namespace Encompass.Exceptions
{
public class IllegalReadTypeException : Exception
{
public IllegalReadTypeException(
string format,
params object[] args
) : base(string.Format(format, args)) { }
}
}

View File

@ -1,7 +0,0 @@
namespace Encompass
{
/// <summary>
/// Structs that implement IComponent are considered to be Components.
/// </summary>
public interface IComponent { }
}

View File

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

View File

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

View File

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

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, IComponent
public void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : unmanaged
{
RegisterComponentType<TComponent>();
_startingExistingComponentStore.Set(entity.ID, component);
@ -97,7 +97,7 @@ namespace Encompass
}
}
internal void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
internal void RegisterComponentType<TComponent>() where TComponent : unmanaged
{
if (!_typeToIndex.ContainsKey(typeof(TComponent)))
{
@ -183,7 +183,7 @@ namespace Encompass
/// <summary>
/// Adds the specified OrderedRenderer to the World.
/// </summary>
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : unmanaged, IDrawableComponent
{
RegisterComponentType<TComponent>();
renderer.AssignEntityManager(_entityManager);
@ -354,20 +354,6 @@ namespace Encompass
throw new EngineWriteConflictException(errorString);
}
// doing reflection to grab all component types, because not all writes need to be declared
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var componentType in assembly.GetTypes())
{
if (typeof(IComponent).IsAssignableFrom(componentType) && componentType.IsValueType && !componentType.IsEnum && !componentType.IsPrimitive)
{
var method = typeof(WorldBuilder).GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
var generic = method.MakeGenericMethod(componentType);
generic.Invoke(this, null);
}
}
}
PreloadJIT(_componentTypesToPreload, _messageTypes);
var engineOrder = new List<Engine>();

View File

@ -2,14 +2,14 @@ using NUnit.Framework;
using FluentAssertions;
using Encompass;
using System.Runtime.CompilerServices;
namespace Tests
{
public class ComponentTests
{
struct MockComponent : Encompass.IComponent
struct MockComponent
{
public string myString;
public int myInt;
}
@ -54,16 +54,17 @@ namespace Tests
}
[Test]
public void AddComponent()
public unsafe void AddComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AddComponentTestEngine());
var entity = worldBuilder.CreateEntity();
const string MyString = "hello";
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent);
@ -85,16 +86,15 @@ namespace Tests
worldBuilder.AddEngine(new ReadMockComponentEngine());
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent { myInt = 20, myString = "what" });
worldBuilder.SetComponent(entity, new MockComponent { myInt = 50, myString = "hi" });
worldBuilder.SetComponent(entity, new MockComponent { myInt = 40, myString = "wassup" });
worldBuilder.SetComponent(entity, new MockComponent { myInt = 20 });
worldBuilder.SetComponent(entity, new MockComponent { myInt = 50 });
worldBuilder.SetComponent(entity, new MockComponent { myInt = 40 });
var world = worldBuilder.Build();
world.Update(0.01);
Assert.That(gottenMockComponent.myInt, Is.EqualTo(40));
Assert.That(gottenMockComponent.myString, Is.EqualTo("wassup"));
}
[Reads(typeof(MockComponent))]
@ -191,7 +191,6 @@ namespace Tests
{
MockComponent mockComponent;
mockComponent.myInt = 10;
mockComponent.myString = "four";
AddMockComponentMessage addMockComponentMessage;
addMockComponentMessage.entity = entity;
@ -255,9 +254,8 @@ namespace Tests
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent<MockComponent>(entity, mockComponent);
worldBuilder.SetComponent(entity, mockComponent);
EntityMessage entityMessage;
entityMessage.entity = entity;
@ -298,7 +296,6 @@ namespace Tests
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent);
@ -336,7 +333,6 @@ namespace Tests
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent);
@ -446,7 +442,6 @@ namespace Tests
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent);

View File

@ -10,10 +10,9 @@ using Encompass.Exceptions;
namespace Tests
{
struct MockComponent : IComponent
struct MockComponent
{
public int myInt;
public string myString;
}
public class EngineTest
@ -72,12 +71,10 @@ namespace Tests
var entityB = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 0;
mockComponent.myString = "hello";
mockComponent.myInt = 2;
MockComponent mockComponentB;
mockComponentB.myInt = 1;
mockComponentB.myString = "howdy";
worldBuilder.SetComponent(entity, mockComponent);
worldBuilder.SetComponent(entityB, mockComponentB);
@ -100,12 +97,10 @@ namespace Tests
var entityB = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 0;
mockComponent.myString = "hello";
mockComponent.myInt = 2;
MockComponent mockComponentB;
mockComponentB.myInt = 1;
mockComponentB.myString = "howdy";
worldBuilder.SetComponent(entity, mockComponent);
worldBuilder.SetComponent(entityB, mockComponentB);
@ -135,8 +130,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 0;
mockComponent.myString = "hello";
mockComponent.myInt = 3;
worldBuilder.SetComponent(entity, mockComponent);
@ -157,12 +151,10 @@ namespace Tests
var entityB = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 0;
mockComponent.myString = "hello";
mockComponent.myInt = 2;
MockComponent mockComponentB;
mockComponentB.myInt = 1;
mockComponentB.myString = "howdy";
worldBuilder.SetComponent(entity, mockComponent);
worldBuilder.SetComponent(entityB, mockComponentB);
@ -183,8 +175,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 0;
mockComponent.myString = "hello";
mockComponent.myInt = 2;
worldBuilder.SetComponent(entity, mockComponent);
@ -204,7 +195,6 @@ namespace Tests
var (component, entity) = ReadComponentIncludingEntity<MockComponent>();
component.myInt = 420;
component.myString = "blaze it";
SetComponent(entity, component);
}
}
@ -222,8 +212,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 0;
mockComponent.myString = "hello";
mockComponent.myInt = 3;
worldBuilder.SetComponent(entity, mockComponent);
@ -233,7 +222,6 @@ namespace Tests
world.Update(0.01);
Assert.AreEqual(420, resultComponent.myInt);
Assert.AreEqual("blaze it", resultComponent.myString);
}
[Reads(typeof(MockComponent))]
@ -244,7 +232,6 @@ namespace Tests
var (component, entity) = ReadComponentIncludingEntity<MockComponent>();
component.myInt = 420;
component.myString = "blaze it";
SetComponent(entity, component);
component = ReadComponent<MockComponent>();
@ -260,8 +247,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 0;
mockComponent.myString = "hello";
mockComponent.myInt = 3;
worldBuilder.SetComponent(entity, mockComponent);
@ -594,11 +580,9 @@ namespace Tests
MockComponent componentA;
componentA.myInt = 20;
componentA.myString = "hello";
MockComponent componentB;
componentB.myInt = 20;
componentB.myString = "hello";
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, componentA);
@ -636,7 +620,7 @@ namespace Tests
Assert.That(emptyComponentReadResult, Is.Empty);
}
struct DestroyerComponent : IComponent { }
struct DestroyerComponent { }
[Reads(typeof(DestroyerComponent))]
class DestroyerEngine : Engine
@ -675,7 +659,6 @@ namespace Tests
DestroyerComponent destroyerComponent;
MockComponent mockComponent;
mockComponent.myInt = 2;
mockComponent.myString = "blah";
worldBuilder.SetComponent(entity, destroyerComponent);
worldBuilder.SetComponent(entity, mockComponent);
@ -975,14 +958,14 @@ namespace Tests
entity.Should().BeEquivalentTo(readEntity);
}
struct MockComponentB : IComponent
struct MockComponentB
{
private int value;
public MockComponentB(int value)
{
this.value = value;
}
int value;
}
static MockComponentB getComponentResult;
@ -1420,9 +1403,9 @@ namespace Tests
public class QueryTests
{
struct MockComponentB : IComponent { }
struct MockComponentC : IComponent { }
struct MockComponentD : IComponent { }
struct MockComponentB { }
struct MockComponentC { }
struct MockComponentD { }
[Reads(typeof(MockComponent), typeof(MockComponentB))]
[Writes(typeof(MockComponentB))]
@ -1877,14 +1860,14 @@ namespace Tests
_components.Should().NotBeEmpty();
}
struct MockTimerComponent : IComponent
struct MockTimerComponent
{
public double Timer { get; set; }
public MockTimerComponent(double time)
{
Timer = time;
}
public double Timer { get; set; }
}
[Reads(typeof(MockTimerComponent))]

View File

@ -5,7 +5,7 @@ namespace Tests
{
public static class GeneralRendererTest
{
struct AComponent : IComponent { }
struct AComponent { }
public class SingletonRead
{

View File

@ -9,11 +9,11 @@ namespace Tests
{
public class OrderedRendererTest
{
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct CComponent : IComponent { }
struct AComponent { }
struct BComponent { }
struct CComponent { }
struct TestDrawComponent : IComponent, IDrawableComponent
struct TestDrawComponent : IDrawableComponent
{
public int Layer { get; set; }
}

View File

@ -5,7 +5,7 @@ namespace Tests
{
public class SpawnerTest
{
struct TestComponent : IComponent { }
struct TestComponent { }
struct SpawnMessageA : IMessage { }
static Entity resultEntity;

View File

@ -114,7 +114,7 @@ namespace Tests
public class MultipleEngineWriteConflict
{
struct AComponent : IComponent { }
struct AComponent { }
[Writes(typeof(AComponent))]
class AEngine : Engine
@ -146,7 +146,7 @@ namespace Tests
public Entity entity;
}
struct AComponent : IComponent
struct AComponent
{
public int myInt;
}
@ -215,7 +215,7 @@ namespace Tests
public Entity entity;
}
struct AComponent : IComponent
struct AComponent
{
public int myInt;
}
@ -318,28 +318,6 @@ namespace Tests
}
}
public class IllegalReadType
{
struct ANonMessage { }
[Reads(typeof(ANonMessage))]
class MyEngine : Engine
{
public override void Update(double dt)
{
}
}
[Test]
public void ThrowsError()
{
var worldBuilder = new WorldBuilder();
Assert.Throws<IllegalReadTypeException>(() => worldBuilder.AddEngine(new MyEngine()), "ANonMessage must be a Message or Component");
}
}
public class IllegalWriteType
{
struct ANonMessage { }
@ -428,8 +406,8 @@ namespace Tests
{
static List<Engine> order = new List<Engine>();
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct AComponent { }
struct BComponent { }
struct AMessage : IMessage { }
struct BMessage : IMessage { }

View File

@ -11,8 +11,8 @@ namespace Tests
{
public class WorldTest
{
struct TestComponent : IComponent { }
struct TestDrawComponent : IComponent, IDrawableComponent
struct TestComponent { }
struct TestDrawComponent : IDrawableComponent
{
public int Layer { get; set; }
}

View File

@ -5,6 +5,7 @@
<IsPackable>false</IsPackable>
<RootNamespace>Tests</RootNamespace>
<AssemblyName>EncompassECS.Framework.Tests</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.7.0" />