Compare commits

..

6 Commits

Author SHA1 Message Date
cosmonaut 2a84d0a36a lol microsoft
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details
2021-01-29 16:33:08 -08:00
cosmonaut 730d0729bb whoops again
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/pr Build is failing Details
2021-01-29 16:24:45 -08:00
cosmonaut e91eac5967 whoops
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/pr Build is failing Details
2021-01-29 16:23:21 -08:00
cosmonaut 54e3e2b7e5 bump docker image to net 5
continuous-integration/drone/push Build encountered an error Details
continuous-integration/drone/pr Build encountered an error Details
2021-01-29 16:22:33 -08:00
cosmonaut 0817efe037 remove TODO
continuous-integration/drone/pr Build is failing Details
continuous-integration/drone/push Build is failing Details
2021-01-29 16:20:51 -08:00
cosmonaut 6f9dfd5cd1 remove some dependencies 2021-01-29 16:18:46 -08:00
33 changed files with 756 additions and 218 deletions

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30717.126
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "encompass-cs", "encompass-cs\encompass-cs.csproj", "{B862FC25-0740-4CEA-BC53-3C5F43DCD985}"
EndProject
@ -9,28 +9,38 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "test", "test\test.csproj",
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x64.ActiveCfg = Debug|x64
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x64.Build.0 = Debug|x64
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x86.ActiveCfg = Debug|x86
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x86.Build.0 = Debug|x86
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x64.ActiveCfg = Release|x64
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x64.Build.0 = Release|x64
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x86.ActiveCfg = Release|x86
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x86.Build.0 = Release|x86
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x64.ActiveCfg = Debug|x64
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x64.Build.0 = Debug|x64
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x86.ActiveCfg = Debug|x86
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x86.Build.0 = Debug|x86
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x64.ActiveCfg = Release|x64
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x64.Build.0 = Release|x64
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x86.ActiveCfg = Release|x86
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x86.Build.0 = Release|x86
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x64.ActiveCfg = Debug|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x64.Build.0 = Debug|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x86.ActiveCfg = Debug|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x86.Build.0 = Debug|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|Any CPU.Build.0 = Release|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x64.ActiveCfg = Release|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x64.Build.0 = Release|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x86.ActiveCfg = Release|Any CPU
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x86.Build.0 = Release|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x64.ActiveCfg = Debug|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x64.Build.0 = Debug|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x86.ActiveCfg = Debug|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x86.Build.0 = Debug|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|Any CPU.Build.0 = Release|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x64.ActiveCfg = Release|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x64.Build.0 = Release|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x86.ActiveCfg = Release|Any CPU
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
namespace Encompass
{
[AttributeUsage(AttributeTargets.Class)]
public class Adds : Attribute
{
public readonly HashSet<Type> addTypes;
public Adds(params Type[] addTypes)
{
this.addTypes = new HashSet<Type>(addTypes);
}
}
}

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
@ -10,6 +12,15 @@ namespace Encompass
public Receives(params Type[] receiveTypes)
{
foreach (var receiveType in receiveTypes)
{
var isMessage = receiveType.GetInterfaces().Contains(typeof(IMessage));
if (!isMessage)
{
throw new IllegalSendTypeException("{0} must be a Message", receiveType.Name);
}
}
ReceiveTypes = new HashSet<Type>(receiveTypes);
}
}

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
@ -10,6 +12,15 @@ namespace Encompass
public Sends(params Type[] sendTypes)
{
foreach (var sendType in sendTypes)
{
var isMessage = sendType.GetInterfaces().Contains(typeof(IMessage));
if (!isMessage)
{
throw new IllegalSendTypeException("{0} must be a Message", sendType.Name);
}
}
this.SendTypes = new HashSet<Type>(sendTypes);
}
}

View File

@ -8,60 +8,60 @@ namespace Encompass
{
private readonly Dictionary<Type, TypedMessageStore> _stores = new Dictionary<Type, TypedMessageStore>(512);
private void RegisterMessageType<TMessage>() where TMessage : struct
private void RegisterMessageType<TMessage>() where TMessage : struct, IMessage
{
_stores.Add(typeof(TMessage), new TypedMessageStore<TMessage>());
}
private TypedMessageStore<TMessage> Lookup<TMessage>() where TMessage : struct
private TypedMessageStore<TMessage> Lookup<TMessage>() where TMessage : struct, IMessage
{
if (!_stores.ContainsKey(typeof(TMessage))) { RegisterMessageType<TMessage>(); }
return _stores[typeof(TMessage)] as TypedMessageStore<TMessage>;
}
public void AddMessage<TMessage>(in TMessage message) where TMessage : struct
public void AddMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
{
Lookup<TMessage>().Add(message);
}
public void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct
public void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{
Lookup<TMessage>().Add(message, time);
}
public void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct
public void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{
Lookup<TMessage>().AddIgnoringTimeDilation(message, time);
}
public ref readonly TMessage First<TMessage>() where TMessage : struct
public ref readonly TMessage First<TMessage>() where TMessage : struct, IMessage
{
if (!Any<TMessage>()) { throw new NoMessageOfTypeException("No Message of type {0} exists", typeof(TMessage).Name); }
return ref Lookup<TMessage>().First();
}
public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : struct
public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : struct, IMessage
{
return Lookup<TMessage>().All();
}
public bool Any<TMessage>() where TMessage : struct
public bool Any<TMessage>() where TMessage : struct, IMessage
{
return Lookup<TMessage>().Any();
}
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
return Lookup<TMessage>().WithEntity(entityID);
}
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage
{
return ref Lookup<TMessage>().FirstWithEntity(entityID);
}
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
return Lookup<TMessage>().SomeWithEntity(entityID);
}

View File

@ -78,8 +78,8 @@ namespace Encompass
{
if (!_priorities.ContainsKey(entityID) || priority <= _priorities[entityID]) // if priorities are equal that means it's the same engine
{
ForceRemove(entityID);
_priorities[entityID] = priority;
ForceRemove(entityID);
return true;
}
@ -92,6 +92,7 @@ namespace Encompass
{
var storageIndex = _entityIDToStorageIndex[entityID];
_entityIDToStorageIndex.Remove(entityID);
_priorities.Remove(entityID);
// move a component into the hole to maintain contiguous memory
if (_nextID > 1 && storageIndex != _nextID - 1)

View File

@ -9,7 +9,7 @@ namespace Encompass
public abstract void Clear();
}
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct, IMessage
{
// messages are placed in a contiguous region
// so we can return the collection as a Span

View File

@ -5,6 +5,8 @@ namespace Encompass
{
internal class ComponentManager
{
private readonly DrawLayerManager _drawLayerManager;
private readonly ComponentStore _existingComponentStore;
private readonly ComponentStore _immediateComponentStore;
private readonly ComponentDeltaStore _replayStore;
@ -17,8 +19,9 @@ namespace Encompass
internal ComponentBitSet ImmediateBits { get { return _immediateComponentStore.ComponentBitSet; } }
internal ComponentBitSet ExistingBits { get { return _existingComponentStore.ComponentBitSet; } }
public ComponentManager(Dictionary<Type, int> typeToIndex)
public ComponentManager(DrawLayerManager drawLayerManager, Dictionary<Type, int> typeToIndex)
{
this._drawLayerManager = drawLayerManager;
_existingComponentStore = new ComponentStore(typeToIndex);
_immediateComponentStore = new ComponentStore(typeToIndex);
_replayStore = new ComponentDeltaStore(typeToIndex);
@ -44,6 +47,11 @@ namespace Encompass
_upToDateComponentStore.SwapWith(componentStore);
}
internal void RegisterDrawableComponent<TComponent>(int entityID, int layer) where TComponent : struct
{
_drawLayerManager.RegisterComponentWithLayer<TComponent>(entityID, layer);
}
internal void WriteComponents()
{
_existingComponentStore.UpdateUsing(_replayStore);
@ -256,6 +264,7 @@ namespace Encompass
_immediateComponentStore.Remove(entityID);
_replayStore.Remove(entityID);
_upToDateComponentStore.Remove(entityID);
_drawLayerManager.UnRegisterEntityWithLayer(entityID);
}
_entitiesMarkedForRemoval.Clear();
@ -267,6 +276,7 @@ namespace Encompass
{
_replayStore.Remove<TComponent>(entityID, priority);
_upToDateComponentStore.Remove<TComponent>(entityID, priority);
_drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
return true;
}
return false;
@ -277,6 +287,7 @@ namespace Encompass
if (_upToDateComponentStore.Remove<TComponent>(entityID, priority))
{
_replayStore.Remove<TComponent>(entityID, priority);
_drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
}
}

View File

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
internal class DrawLayerManager
{
private readonly SortedList<int, int> _layerOrder = new SortedList<int, int>();
private readonly Dictionary<int, Dictionary<Type, HashSet<int>>> _layerIndexToTypeToID = new Dictionary<int, Dictionary<Type, HashSet<int>>>();
private readonly Dictionary<int, HashSet<GeneralRenderer>> _layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>(512);
private readonly Dictionary<Type, Dictionary<int, int>> _typeToEntityToLayer = new Dictionary<Type, Dictionary<int, int>>(512);
public IEnumerable<int> LayerOrder { get { return _layerOrder.Values; } }
public DrawLayerManager()
{
RegisterDrawLayer(0);
}
public void RegisterDrawLayer(int layer)
{
if (!_layerIndexToTypeToID.ContainsKey(layer))
{
_layerOrder.Add(layer, layer);
_layerIndexToGeneralRenderers.Add(layer, new HashSet<GeneralRenderer>());
_layerIndexToTypeToID.Add(layer, new Dictionary<Type, HashSet<int>>());
}
}
public void RegisterOrderedDrawable<TComponent>() where TComponent : struct
{
if (!_typeToEntityToLayer.ContainsKey(typeof(TComponent)))
{
_typeToEntityToLayer.Add(typeof(TComponent), new Dictionary<int, int>(128));
}
foreach (var pair in _layerIndexToTypeToID)
{
if (!pair.Value.ContainsKey(typeof(TComponent)))
{
pair.Value.Add(typeof(TComponent), new HashSet<int>());
}
}
}
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{
RegisterDrawLayer(layer);
var set = _layerIndexToGeneralRenderers[layer];
set.Add(renderer);
}
public void RegisterComponentWithLayer<TComponent>(int entityID, int layer) where TComponent : struct
{
if (!_layerIndexToTypeToID.ContainsKey(layer))
{
throw new UndefinedLayerException("Layer {0} is not defined. Use WorldBuilder.RegisterDrawLayer to register the layer.", layer);
}
if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID))
{
if (_typeToEntityToLayer[typeof(TComponent)][entityID] != layer)
{
UnRegisterComponentWithLayer<TComponent>(entityID);
var set = _layerIndexToTypeToID[layer][typeof(TComponent)];
set.Add(entityID);
_typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
}
}
else
{
var set = _layerIndexToTypeToID[layer][typeof(TComponent)];
set.Add(entityID);
_typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
}
}
public void UnRegisterComponentWithLayer<TComponent>(int entityID) where TComponent : struct
{
if (!_typeToEntityToLayer.ContainsKey(typeof(TComponent))) { return; }
if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID))
{
var layer = _typeToEntityToLayer[typeof(TComponent)][entityID];
_layerIndexToTypeToID[layer][typeof(TComponent)].Remove(entityID);
}
_typeToEntityToLayer[typeof(TComponent)].Remove(entityID);
}
public void UnRegisterEntityWithLayer(int entityID)
{
foreach (var store in _layerIndexToTypeToID.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)
{
return _layerIndexToGeneralRenderers.ContainsKey(layer) ?
_layerIndexToGeneralRenderers[layer] :
Enumerable.Empty<GeneralRenderer>();
}
public IEnumerable<(int, Type)> AllInLayer(int layer)
{
foreach (var kvp in _layerIndexToTypeToID[layer])
{
foreach (var id in kvp.Value)
{
yield return (id, kvp.Key);
}
}
}
}
}

View File

@ -23,7 +23,6 @@ namespace Encompass
internal readonly HashSet<Type> WriteImmediateTypes = new HashSet<Type>();
internal readonly HashSet<Type> QueryWithTypes = new HashSet<Type>();
internal readonly HashSet<Type> QueryWithoutTypes = new HashSet<Type>();
internal readonly HashSet<Type> AddTypes = new HashSet<Type>();
internal readonly Dictionary<Type, int> WritePriorities = new Dictionary<Type, int>();
internal readonly int DefaultWritePriority = 0;
@ -59,12 +58,6 @@ namespace Encompass
{
_id = Guid.NewGuid();
var addsAttribute = GetType().GetCustomAttribute<Adds>(false);
if (addsAttribute != null)
{
AddTypes = addsAttribute.addTypes;
}
var sendsAttribute = GetType().GetCustomAttribute<Sends>(false);
if (sendsAttribute != null)
{
@ -176,7 +169,7 @@ namespace Encompass
_trackingManager = trackingManager;
}
internal void CheckMessageRead<TMessage>() where TMessage : struct
internal void CheckMessageRead<TMessage>() where TMessage : struct, IMessage
{
if (!ReceiveTypes.Contains(typeof(TMessage)))
{
@ -229,7 +222,7 @@ namespace Encompass
/// <summary>
/// Returns all Entities containing the specified Component type.
/// </summary>
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -254,7 +247,7 @@ namespace Encompass
/// <summary>
/// Returns an Entity containing the specified Component type.
/// </summary>
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -279,7 +272,7 @@ namespace Encompass
/// <summary>
/// Returns all of the Components with the specified Component Type.
/// </summary>
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -304,7 +297,7 @@ namespace Encompass
/// <summary>
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
/// </summary>
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -329,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
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -351,7 +344,7 @@ namespace Encompass
}
}
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -382,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>(in Entity entity) where TComponent : struct
protected ref readonly TComponent GetComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{
return ref GetComponentHelper<TComponent>(entity.ID);
}
@ -393,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>(in Entity entity) where TComponent : struct
protected bool HasComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -451,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>(in Entity entity, in TComponent component) where TComponent : struct
protected void SetComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct, IComponent
{
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
@ -478,6 +471,11 @@ namespace Encompass
{
_trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
}
if (written && component is IDrawableComponent drawableComponent)
{
_componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
}
}
/// <summary>
@ -486,13 +484,8 @@ 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>(in Entity entity, in TComponent component) where TComponent : struct
protected void AddComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct, IComponent
{
if (!AddTypes.Contains(typeof(TComponent)))
{
throw new IllegalWriteException("Engine {0} tried to add undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
if (!EntityCreatedThisFrame(entity.ID))
{
throw new IllegalWriteException("AddComponent used on Entity that was not created in this context. Use SetComponent instead.");
@ -509,6 +502,11 @@ namespace Encompass
}
_trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
if (component is IDrawableComponent drawableComponent)
{
_componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
}
}
/// <summary>
@ -517,7 +515,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalSendException">
/// Thrown when the Engine does not declare that it Sends the Message Type.
/// </exception>
protected void SendMessage<TMessage>(in TMessage message) where TMessage : struct
protected void SendMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
{
if (!SendTypes.Contains(typeof(TMessage)))
{
@ -531,7 +529,7 @@ namespace Encompass
/// Sends a message after the specified number of seconds, respecting time dilation.
/// </summary>
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
protected void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct
protected void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{
_messageManager.AddMessage(message, time);
}
@ -540,7 +538,7 @@ namespace Encompass
/// Sends a message after the specified number of seconds, ignoring time dilation.
/// </summary>
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
protected void SendMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct
protected void SendMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{
_messageManager.AddMessageIgnoringTimeDilation(message, time);
}
@ -551,7 +549,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
/// </exception>
protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : struct
protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage
{
CheckMessageRead<TMessage>();
return _messageManager.GetMessagesByType<TMessage>();
@ -563,7 +561,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
/// </exception>
protected ref readonly TMessage ReadMessage<TMessage>() where TMessage : struct
protected ref readonly TMessage ReadMessage<TMessage>() where TMessage : struct, IMessage
{
CheckMessageRead<TMessage>();
return ref _messageManager.First<TMessage>();
@ -575,7 +573,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
/// </exception>
protected bool SomeMessage<TMessage>() where TMessage : struct
protected bool SomeMessage<TMessage>() where TMessage : struct, IMessage
{
CheckMessageRead<TMessage>();
return _messageManager.Any<TMessage>();
@ -594,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
protected void DestroyWith<TComponent>() where TComponent : struct, IComponent
{
Destroy(ReadEntity<TComponent>());
}
@ -603,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
protected void DestroyAllWith<TComponent>() where TComponent : struct, IComponent
{
foreach (var entity in ReadEntities<TComponent>())
{
@ -616,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>(in Entity entity) where TComponent : struct
protected void RemoveComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
@ -709,7 +707,7 @@ namespace Encompass
/// <typeparam name="TMessage">The Message subtype.</typeparam>
/// <param name="entity">The entity that all messages in the IEnumerable refer to.</param>
/// <returns></returns>
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : struct, IMessage, IHasEntity
{
CheckMessageRead<TMessage>();
return _messageManager.WithEntity<TMessage>(entity.ID);
@ -719,7 +717,7 @@ namespace Encompass
/// Efficiently reads a single Message of a given type that references a given Entity.
/// It is recommended to use this method in conjunction with SomeMessageWithEntity to prevent errors.
/// </summary>
protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IMessage, IHasEntity
{
CheckMessageRead<TMessage>();
return ref _messageManager.WithEntitySingular<TMessage>(entity.ID);
@ -728,7 +726,7 @@ namespace Encompass
/// <summary>
/// Efficiently checks if any Message of a given type referencing a given Entity exists.
/// </summary>
protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IMessage, IHasEntity
{
CheckMessageRead<TMessage>();
return _messageManager.SomeWithEntity<TMessage>(entity.ID);

View File

@ -6,7 +6,7 @@ namespace Encompass
/// A Spawner is a special type of Engine that runs a Spawn method in response to each Message it receives.
/// Spawners are useful for organizing the building of new Entities in your game.
/// </summary>
public abstract class Spawner<TMessage> : Engine where TMessage : struct
public abstract class Spawner<TMessage> : Engine where TMessage : struct, IMessage
{
protected Spawner() : base()
{

View File

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

7
encompass-cs/IMessage.cs Normal file
View File

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

View File

@ -0,0 +1,4 @@
namespace Encompass
{
public interface IComponent { }
}

View File

@ -0,0 +1,7 @@
namespace Encompass
{
public interface IDrawableComponent
{
int Layer { get; }
}
}

View File

@ -13,17 +13,17 @@ namespace Encompass
_timeManager = timeManager;
}
internal void AddMessage<TMessage>(in TMessage message) where TMessage : struct
internal void AddMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
{
_messageStore.AddMessage(message);
}
internal void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct
internal void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{
_messageStore.AddMessage(message, time);
}
internal void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct
internal void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{
_messageStore.AddMessageIgnoringTimeDilation(message, time);
}
@ -38,32 +38,32 @@ namespace Encompass
_messageStore.ProcessDelayedMessages(dt * _timeManager.TimeDilationFactor, dt);
}
internal ReadOnlySpan<TMessage> GetMessagesByType<TMessage>() where TMessage : struct
internal ReadOnlySpan<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage
{
return _messageStore.All<TMessage>();
}
internal bool Any<TMessage>() where TMessage : struct
internal bool Any<TMessage>() where TMessage : struct, IMessage
{
return _messageStore.Any<TMessage>();
}
internal ref readonly TMessage First<TMessage>() where TMessage : struct
internal ref readonly TMessage First<TMessage>() where TMessage : struct, IMessage
{
return ref _messageStore.First<TMessage>();
}
internal IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
internal IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
return _messageStore.WithEntity<TMessage>(entityID);
}
internal ref readonly TMessage WithEntitySingular<TMessage>(int entityID) where TMessage : struct, IHasEntity
internal ref readonly TMessage WithEntitySingular<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
return ref _messageStore.FirstWithEntity<TMessage>(entityID);
}
internal bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
internal bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
return _messageStore.SomeWithEntity<TMessage>(entityID);
}

View File

@ -1,27 +1,51 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace Encompass
{
internal class RenderManager
{
private readonly EntityManager _entityManager;
private readonly List<Renderer> renderers = new List<Renderer>();
private readonly DrawLayerManager _drawLayerManager;
public RenderManager(EntityManager entityManager)
private readonly Dictionary<Type, Action<Entity>> _drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Entity>>(256);
public RenderManager(EntityManager entityManager, DrawLayerManager drawLayerManager)
{
_entityManager = entityManager;
_drawLayerManager = drawLayerManager;
}
public void AddRenderer(Renderer renderer)
public void RegisterOrderedRenderer<TComponent>(Action<Entity> renderAction) where TComponent : struct
{
renderers.Add(renderer);
_drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction);
_drawLayerManager.RegisterOrderedDrawable<TComponent>();
}
public void Draw(double dt, double alpha)
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{
foreach (var renderer in renderers)
_drawLayerManager.RegisterGeneralRendererWithLayer(renderer, layer);
}
public void Draw()
{
foreach (var layer in _drawLayerManager.LayerOrder)
{
renderer.Render(dt, alpha);
var generalRendererSet = _drawLayerManager.GeneralRenderersByLayer(layer);
foreach (var (entityID, componentType) in _drawLayerManager.AllInLayer(layer))
{
if (_drawComponentTypeToOrderedRenderer.ContainsKey(componentType))
{
var internalRenderAction = _drawComponentTypeToOrderedRenderer[componentType];
internalRenderAction(_entityManager.GetEntity(entityID));
}
}
foreach (var generalRenderer in generalRendererSet)
{
generalRenderer.Render();
}
}
}
}

View File

@ -18,49 +18,47 @@ namespace Encompass
_componentManager = componentManager;
}
public abstract void Render(double dt, double alpha);
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.GetExistingEntities<TComponent>();
}
protected IEnumerable<Entity> ReadEntitiesAsEnumerable<TComponent>() where TComponent : struct
protected IEnumerable<Entity> ReadEntitiesAsEnumerable<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.GetExistingEntitiesAsEnumerable<TComponent>();
}
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
return ref _componentManager.ExistingSingularEntity<TComponent>();
}
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.GetComponentsByType<TComponent>();
}
protected IEnumerable<TComponent> ReadComponentsAsEnumerable<TComponent>() where TComponent : struct
protected IEnumerable<TComponent> ReadComponentsAsEnumerable<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.GetComponentsByTypeEnumerable<TComponent>();
}
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{
return ref _componentManager.ExistingSingular<TComponent>();
}
protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct
protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return ref _componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
}
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return _componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
}
protected bool SomeComponent<TComponent>() where TComponent : struct
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.SomeExistingComponent<TComponent>();
}

View File

@ -0,0 +1,11 @@
namespace Encompass
{
/// <summary>
/// GeneralRenderer is a Renderer which generically reads the game state in order to draw elements to the screen.
/// GeneralRenderers have a layer specified when they are added to the World.
/// </summary>
public abstract class GeneralRenderer : Renderer
{
public abstract void Render();
}
}

View File

@ -0,0 +1,18 @@
using System;
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 void Render(Entity entity, in TComponent drawComponent);
internal void InternalRender(Entity entity)
{
ref readonly var component = ref GetComponent<TComponent>(entity);
Render(entity, component);
}
}
}

View File

@ -14,7 +14,6 @@ namespace Encompass
{
_componentTypes = componentTypes;
_messageTypes = messageTypes;
AddTypes.UnionWith(componentTypes);
ReadTypes.UnionWith(componentTypes);
WriteTypes.UnionWith(componentTypes);
SendTypes.UnionWith(messageTypes);
@ -55,7 +54,7 @@ namespace Encompass
// we can't reflect invoke on byref returns or Span returns right now... so we have non-return wrapper methods
protected void CallAllComponentMethods<TComponent>() where TComponent : struct
protected void CallAllComponentMethods<TComponent>() where TComponent : struct, IComponent
{
ReadComponent<TComponent>();
ReadComponents<TComponent>();
@ -70,7 +69,7 @@ namespace Encompass
AddComponent<TComponent>(Entity, default);
}
protected void CallAllMessageMethods<TMessage>() where TMessage : struct
protected void CallAllMessageMethods<TMessage>() where TMessage : struct, IMessage
{
SendMessageIgnoringTimeDilation<TMessage>(default, 0.1);
SendMessage<TMessage>(default);
@ -80,7 +79,7 @@ namespace Encompass
SomeMessage<TMessage>();
}
protected void CallAllEntityMessageMethods<TMessage>() where TMessage : struct, IHasEntity
protected void CallAllEntityMessageMethods<TMessage>() where TMessage : struct, IMessage, IHasEntity
{
ReadMessagesWithEntity<TMessage>(Entity);
ReadMessageWithEntity<TMessage>(Entity);

View File

@ -20,7 +20,7 @@ namespace Encompass
}
// can't reflect invoke on Span returns...
public override void Render(double dt, double alpha)
public void Render()
{
foreach (var type in _componentTypes)
{
@ -28,7 +28,7 @@ namespace Encompass
}
}
protected void CallAllComponentMethods<TComponent>() where TComponent : struct
protected void CallAllComponentMethods<TComponent>() where TComponent : struct, IComponent
{
ReadEntity<TComponent>();
ReadEntities<TComponent>();

View File

@ -69,9 +69,9 @@ namespace Encompass
/// <summary>
/// Causes the Renderers to draw.
/// </summary>
public void Draw(double dt, double alpha)
public void Draw()
{
_renderManager.Draw(dt, alpha);
_renderManager.Draw();
}
}
}

View File

@ -26,6 +26,7 @@ namespace Encompass
private readonly EntityManager _entityManager;
private readonly MessageManager _messageManager;
private readonly TimeManager _timeManager;
private readonly DrawLayerManager _drawLayerManager;
private readonly RenderManager _renderManager;
private readonly TrackingManager _trackingManager;
@ -35,15 +36,18 @@ namespace Encompass
private readonly HashSet<Type> _messageTypes = new HashSet<Type>();
private readonly Dictionary<Type, int> _typeToIndex = new Dictionary<Type, int>();
private bool _rendererRegistered = false;
public WorldBuilder(int entityCapacity = 32768)
{
_entityCapacity = entityCapacity;
_drawLayerManager = new DrawLayerManager();
_timeManager = new TimeManager();
_trackingManager = new TrackingManager();
_componentManager = new ComponentManager(_typeToIndex);
_componentManager = new ComponentManager(_drawLayerManager, _typeToIndex);
_messageManager = new MessageManager(_timeManager);
_entityManager = new EntityManager(_componentManager, entityCapacity);
_renderManager = new RenderManager(_entityManager);
_renderManager = new RenderManager(_entityManager, _drawLayerManager);
_startingExistingComponentStore = new ComponentStore(_typeToIndex);
_startingUpToDateComponentStore = new ComponentStore(_typeToIndex);
@ -60,7 +64,7 @@ namespace Encompass
/// <summary>
/// Specifies that the given Message should be sent immediately on the first World Update.
/// </summary>
public void SendMessage<TMessage>(in TMessage message) where TMessage : struct
public void SendMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
{
_messageManager.AddMessage(message);
}
@ -68,7 +72,7 @@ namespace Encompass
/// <summary>
/// Specifies that the given Message should be sent after the specified number of seconds after the first World Update.
/// </summary>
public void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct
public void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{
_messageManager.AddMessage<TMessage>(message, time);
}
@ -76,21 +80,20 @@ namespace Encompass
/// <summary>
/// Sets Component data for the specified Component Type on the specified Entity.
/// </summary>
public void SetComponent<TComponent>(Entity entity, in TComponent component) where TComponent : struct
public void SetComponent<TComponent>(Entity entity, in TComponent component) where TComponent : struct, IComponent
{
RegisterComponentType<TComponent>();
_startingExistingComponentStore.Set(entity.ID, component);
_startingUpToDateComponentStore.Set(entity.ID, component);
if (component is IDrawableComponent drawableComponent)
{
_componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
_drawLayerManager.RegisterOrderedDrawable<TComponent>();
}
}
internal void RegisterComponentTypeNonGeneric(Type type)
{
var method = GetType().GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
var generic = method.MakeGenericMethod(type);
generic.Invoke(this, null);
}
internal void RegisterComponentType<TComponent>() where TComponent : struct
internal void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{
if (!_typeToIndex.ContainsKey(typeof(TComponent)))
{
@ -155,26 +158,6 @@ namespace Encompass
_senders.Add(engine);
}
foreach (var componentType in engine.ReadTypes)
{
RegisterComponentTypeNonGeneric(componentType);
}
foreach (var componentType in engine.AddTypes)
{
RegisterComponentTypeNonGeneric(componentType);
}
foreach (var componentType in engine.WriteTypes)
{
RegisterComponentTypeNonGeneric(componentType);
}
foreach (var componentType in engine.WriteImmediateTypes)
{
RegisterComponentTypeNonGeneric(componentType);
}
foreach (var componentType in engine.QueryWithTypes)
{
_trackingManager.RegisterComponentTypeToEngine(componentType, engine);
@ -217,13 +200,44 @@ namespace Encompass
}
/// <summary>
/// Adds the specified Renderer to the World.
/// Registers a draw layer. This must be done before any Renderers are registered.
/// </summary>
public void AddRenderer(Renderer renderer)
/// <param name="layer">The draw layer to register.</param>
public void RegisterDrawLayer(int layer)
{
renderer.AssignComponentManager(_componentManager);
if (_rendererRegistered)
{
throw new IllegalDrawLayerException("Cannot register a draw layer after a Renderer has been registered.");
}
_drawLayerManager.RegisterDrawLayer(layer);
}
/// <summary>
/// Adds the specified OrderedRenderer to the World.
/// </summary>
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent
{
RegisterComponentType<TComponent>();
renderer.AssignEntityManager(_entityManager);
_renderManager.AddRenderer(renderer);
renderer.AssignComponentManager(_componentManager);
_renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender);
_rendererRegistered = true;
return renderer;
}
/// <summary>
/// Adds the specified GeneralRenderer to the World at the specified layer.
/// Higher layer numbers draw on top of lower layer numbers.
/// </summary>
/// <param name="renderer">An instance of a GeneralRenderer.</param>
/// <param name="layer">The layer at which the GeneralRenderer should render. Higher numbers draw over lower numbers.</param>
public TRenderer AddGeneralRenderer<TRenderer>(TRenderer renderer, int layer) where TRenderer : GeneralRenderer
{
renderer.AssignEntityManager(_entityManager);
renderer.AssignComponentManager(_componentManager);
_renderManager.RegisterGeneralRendererWithLayer(renderer, layer);
_rendererRegistered = true;
return renderer;
}
private void BuildEngineGraph()
@ -427,20 +441,44 @@ namespace Encompass
{
var dummyTimeManager = new TimeManager();
var dummyMessageManager = new MessageManager(dummyTimeManager);
var dummyDrawLayerManager = new DrawLayerManager();
var dummyTrackingManager = new TrackingManager();
var dummyComponentManager = new ComponentManager(_typeToIndex);
var dummyComponentManager = new ComponentManager(dummyDrawLayerManager, _typeToIndex);
var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity);
var dummyRenderManager = new RenderManager(dummyEntityManager);
var dummyRenderManager = new RenderManager(dummyEntityManager, dummyDrawLayerManager);
// 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);
var dummyRegisterMethod = typeof(ComponentManager).GetMethod("RegisterComponentType", BindingFlags.Public | BindingFlags.Instance);
var dummyGeneric = dummyRegisterMethod.MakeGenericMethod(componentType);
dummyGeneric.Invoke(dummyComponentManager, null);
}
if (componentType.GetInterface("IDrawableComponent") != null)
{
// register draw layer using property value
var instance = Activator.CreateInstance(componentType);
var layerPropertyInfo = componentType.GetProperty("Layer");
dummyDrawLayerManager.RegisterDrawLayer((int)layerPropertyInfo.GetValue(instance));
var drawLayerManagerRegisterMethod = typeof(DrawLayerManager).GetMethod("RegisterOrderedDrawable");
var drawLayerManagerRegisterGenericMethod = drawLayerManagerRegisterMethod.MakeGenericMethod(componentType);
drawLayerManagerRegisterGenericMethod.Invoke(dummyDrawLayerManager, null);
}
}
}
var prepEngineOrder = new List<Engine>();
foreach (var componentType in _componentTypesToPreload)
{
var dummyRegisterMethod = typeof(ComponentManager).GetMethod("RegisterComponentType", BindingFlags.Public | BindingFlags.Instance);
var dummyGeneric = dummyRegisterMethod.MakeGenericMethod(componentType);
dummyGeneric.Invoke(dummyComponentManager, null);
}
var uberEngine = new UberEngine(_componentTypesToPreload, messageTypes);
uberEngine.AssignEntityManager(dummyEntityManager);
@ -474,7 +512,7 @@ namespace Encompass
dummyComponentManager.WriteComponents();
uberRenderer.SetEntity(uberEngine.Entity);
uberRenderer.Render(1, 0);
uberRenderer.Render();
}
}
}

View File

@ -17,7 +17,6 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyName>EncompassECS.Framework</AssemblyName>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Platforms>x64;x86</Platforms>
</PropertyGroup>
<ItemGroup>
<None Include="..\LICENSE">

View File

@ -8,12 +8,12 @@ namespace Tests
{
public class ComponentTests
{
struct MockComponent
struct MockComponent : IComponent
{
public int myInt;
}
struct EntityMessage
struct EntityMessage : IMessage
{
public Entity entity;
}
@ -33,7 +33,7 @@ namespace Tests
}
}
struct AddComponentTestMessage
struct AddComponentTestMessage : IMessage
{
public Entity entity;
public MockComponent mockComponent;
@ -62,6 +62,8 @@ namespace Tests
var entity = worldBuilder.CreateEntity();
const string MyString = "hello";
MockComponent mockComponent;
mockComponent.myInt = 3;
@ -172,7 +174,7 @@ namespace Tests
Assert.DoesNotThrow(() => world.Update(0.01));
}
struct AddMockComponentMessage
struct AddMockComponentMessage : IMessage
{
public Entity entity;
public MockComponent mockComponent;
@ -269,7 +271,7 @@ namespace Tests
Assert.AreEqual(mockComponent, gottenMockComponent);
}
struct HasComponentTestMessage
struct HasComponentTestMessage : IMessage
{
public Entity entity;
}
@ -367,7 +369,7 @@ namespace Tests
Assert.IsFalse(hasComponentRuntimeTypeResult);
}
struct RemoveComponentTestMessage
struct RemoveComponentTestMessage : IMessage
{
public Entity entity;
}
@ -461,7 +463,7 @@ namespace Tests
hasComponentResult.Should().BeFalse();
}
struct CheckHasMockComponentMessage
struct CheckHasMockComponentMessage : IMessage
{
public Entity entity;
public bool shouldHaveComponent;

View File

@ -10,7 +10,7 @@ using Encompass.Exceptions;
namespace Tests
{
struct MockComponent
struct MockComponent : IComponent
{
public int myInt;
}
@ -271,7 +271,7 @@ namespace Tests
Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredUpdateComponentTestEngine tried to update undeclared Component MockComponent"));
}
struct MockMessage
struct MockMessage : IMessage
{
public string myString;
}
@ -435,7 +435,7 @@ namespace Tests
Assert.Throws<IllegalReadException>(() => world.Update(0.01f));
}
struct EntityMessage : IHasEntity
struct EntityMessage : IMessage, IHasEntity
{
public EntityMessage(Entity entity, int myInt)
{
@ -650,7 +650,7 @@ namespace Tests
world.Update(0.01f);
}
struct DestroyerComponent { }
struct DestroyerComponent : IComponent { }
[Reads(typeof(DestroyerComponent))]
class DestroyerEngine : Engine
@ -955,7 +955,7 @@ namespace Tests
resultComponents.Should().BeEmpty();
}
struct DestroyComponentMessage { public Entity entity; }
struct DestroyComponentMessage : IMessage { public Entity entity; }
[Reads(typeof(MockComponent))]
[Writes(typeof(MockComponent))]
@ -1007,7 +1007,7 @@ namespace Tests
Assert.Throws<NoComponentOfTypeException>(() => world.Update(0.01), "No component of type MockComponent exists");
}
struct MockComponentB
struct MockComponentB : IComponent
{
private int value;
@ -1324,7 +1324,6 @@ namespace Tests
undilatedDeltaTime.Should().Be(0.5);
}
[Adds(typeof(MockComponent))]
class AddComponentWithoutPriorityEngine : Engine
{
public override void Update(double dt)
@ -1381,7 +1380,6 @@ namespace Tests
Assert.Throws<IllegalWriteException>(() => world.Update(0.01));
}
[Adds(typeof(MockComponentB))]
[WritesImmediate(typeof(MockComponentB))]
class AddImmediateComponentEngine : Engine
{
@ -1452,9 +1450,9 @@ namespace Tests
public class QueryTests
{
struct MockComponentB { }
struct MockComponentC { }
struct MockComponentD { }
struct MockComponentB : IComponent { }
struct MockComponentC : IComponent { }
struct MockComponentD : IComponent { }
[Writes(typeof(MockComponentB))]
[QueryWith(typeof(MockComponent), typeof(MockComponentB))]
@ -1904,7 +1902,7 @@ namespace Tests
_components.Should().NotBeEmpty();
}
struct MockTimerComponent
struct MockTimerComponent : IComponent
{
public double Timer { get; }

View File

@ -5,15 +5,15 @@ namespace Tests
{
public static class GeneralRendererTest
{
struct AComponent { }
struct AComponent : IComponent { }
public class SingletonRead
{
static (AComponent, Entity) result;
class TestRenderer : Renderer
class TestRenderer : GeneralRenderer
{
public override void Render(double dt, double alpha)
public override void Render()
{
ref readonly var entity = ref ReadEntity<AComponent>();
result = (GetComponent<AComponent>(entity), entity);
@ -24,7 +24,7 @@ namespace Tests
public void SingletonComponent()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddRenderer(new TestRenderer());
worldBuilder.AddGeneralRenderer(new TestRenderer(), 1);
AComponent aComponent;
@ -34,10 +34,10 @@ namespace Tests
var world = worldBuilder.Build();
world.Update(0.01f);
world.Draw(0.01f, 0);
world.Draw();
world.Update(0.01);
world.Draw(0.01f, 0);
world.Draw();
Assert.That(result, Is.EqualTo((aComponent, entity)));
}
@ -46,7 +46,7 @@ namespace Tests
public void MultipleComponents()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddRenderer(new TestRenderer());
worldBuilder.AddGeneralRenderer(new TestRenderer(), 1);
AComponent aComponent;
AComponent aComponentTwo;
@ -59,10 +59,10 @@ namespace Tests
var world = worldBuilder.Build();
world.Update(0.01f);
world.Draw(0.01f, 0);
world.Draw();
world.Update(0.01f);
world.Draw(0.01f, 0);
world.Draw();
Assert.That(result, Is.EqualTo((aComponent, entity)).Or.EqualTo((aComponentTwo, entityB)));
}

109
test/OrderedRendererTest.cs Normal file
View File

@ -0,0 +1,109 @@
using System;
using NUnit.Framework;
using FluentAssertions;
using Encompass;
using System.Collections.Generic;
namespace Tests
{
public class OrderedRendererTest
{
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct CComponent : IComponent { }
struct TestDrawComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
}
class TestRenderer : OrderedRenderer<TestDrawComponent>
{
public override void Render(Entity entity, in TestDrawComponent testDrawComponent) { }
}
static bool called = false;
class DeactivatedRenderer : TestRenderer
{
public override void Render(Entity entity, in TestDrawComponent testDrawComponent)
{
called = true;
}
}
static bool calledOnDraw = false;
static (TestDrawComponent, Entity) resultComponent;
class CalledRenderer : OrderedRenderer<TestDrawComponent>
{
public override void Render(Entity entity, in TestDrawComponent testDrawComponent)
{
resultComponent = (testDrawComponent, entity);
calledOnDraw = true;
}
}
[Test]
public void RenderMethodCalledOnWorldDraw()
{
var worldBuilder = new WorldBuilder();
worldBuilder.RegisterDrawLayer(2);
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
AComponent aComponent;
CComponent cComponent;
var testDrawComponent = new TestDrawComponent { Layer = 2 };
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, aComponent);
worldBuilder.SetComponent(entity, cComponent);
worldBuilder.SetComponent(entity, testDrawComponent);
var world = worldBuilder.Build();
world.Update(0.01f);
world.Draw();
Assert.IsTrue(calledOnDraw);
resultComponent.Should().BeEquivalentTo((testDrawComponent, entity));
}
[Reads(typeof(TestDrawComponent))]
class DestroyerEngine : Engine
{
public override void Update(double dt)
{
foreach (var entity in ReadEntities<TestDrawComponent>())
{
Destroy(entity);
}
}
}
[Test]
public void RenderMethodNotCalledAfterDestroy()
{
calledOnDraw = false;
var worldBuilder = new WorldBuilder();
worldBuilder.RegisterDrawLayer(1);
worldBuilder.AddEngine(new DestroyerEngine());
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
TestDrawComponent testDrawComponent = new TestDrawComponent { Layer = 1 };
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, testDrawComponent);
var world = worldBuilder.Build();
world.Update(0.01);
world.Draw();
Assert.IsFalse(calledOnDraw);
}
}
}

View File

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

View File

@ -13,8 +13,8 @@ namespace Tests
{
public class EngineCycleSimple
{
struct AMessage { }
struct BMessage { }
struct AMessage : IMessage { }
struct BMessage : IMessage { }
[Receives(typeof(AMessage))]
[Sends(typeof(BMessage))]
@ -51,10 +51,10 @@ namespace Tests
public class EngineCycleComplex
{
struct AMessage { }
struct BMessage { }
struct CMessage { }
struct DMessage { }
struct AMessage : IMessage { }
struct BMessage : IMessage { }
struct CMessage : IMessage { }
struct DMessage : IMessage { }
[Receives(typeof(AMessage))]
[Sends(typeof(BMessage))]
@ -142,12 +142,12 @@ namespace Tests
public class MultipleEngineWriteWithPriority
{
struct SetMessage
struct SetMessage : IMessage
{
public Entity entity;
}
struct AComponent
struct AComponent : IComponent
{
public int myInt;
}
@ -211,12 +211,12 @@ namespace Tests
public class DefaultWritePriority
{
struct SetMessage
struct SetMessage : IMessage
{
public Entity entity;
}
struct AComponent
struct AComponent : IComponent
{
public int myInt;
}
@ -298,7 +298,7 @@ namespace Tests
public class EngineMessageSelfCycle
{
struct AMessage { }
struct AMessage : IMessage { }
[Receives(typeof(AMessage))]
[Sends(typeof(AMessage))]
@ -319,6 +319,28 @@ namespace Tests
}
}
public class IllegalWriteType
{
struct ANonMessage { }
[Sends(typeof(ANonMessage))]
class MyEngine : Engine
{
public override void Update(double dt)
{
}
}
[Test]
public void ThrowsError()
{
var worldBuilder = new WorldBuilder();
Assert.Throws<IllegalSendTypeException>(() => worldBuilder.AddEngine(new MyEngine()), "ANonMessage must be a Message or Component");
}
}
public class PriorityConflict
{
[Writes(typeof(MockComponent), 2)]
@ -385,13 +407,13 @@ namespace Tests
{
static List<Engine> order = new List<Engine>();
struct AComponent { }
struct BComponent { }
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct AMessage { }
struct BMessage { }
struct CMessage { }
struct DMessage { }
struct AMessage : IMessage { }
struct BMessage : IMessage { }
struct CMessage : IMessage { }
struct DMessage : IMessage { }
[Sends(typeof(AMessage))]
class AEngine : Engine
@ -498,8 +520,8 @@ namespace Tests
{
static List<Engine> order = new List<Engine>();
struct AMessage { }
struct BMessage { }
struct AMessage : IMessage { }
struct BMessage : IMessage { }
[Sends(typeof(AMessage), typeof(BMessage))]
class AEngine : Engine
@ -536,5 +558,57 @@ namespace Tests
Assert.That(order.IndexOf(engineA), Is.LessThan(order.IndexOf(engineB)));
}
}
public class DrawLayerRegister
{
struct AComponent : IComponent, IDrawableComponent
{
public int Layer { get; }
}
struct BComponent : IComponent, IDrawableComponent
{
public int Layer { get => 3; }
}
class ARenderer : OrderedRenderer<AComponent>
{
public override void Render(Entity entity, in AComponent drawComponent) { }
}
class BRenderer : OrderedRenderer<BComponent>
{
public override void Render(Entity entity, in BComponent drawComponent) { }
}
[Test]
public void DrawLayerRegisterAfterOrderedRendererRegisterThrows()
{
var worldBuilder = new WorldBuilder();
var rendererA = worldBuilder.AddOrderedRenderer(new ARenderer());
Assert.Throws<IllegalDrawLayerException>(() => worldBuilder.RegisterDrawLayer(1));
}
[Test]
public void DrawLayerRegisterBeforeOrderedRendererDoesNotThrow()
{
var worldBuilder = new WorldBuilder();
Assert.DoesNotThrow(() => worldBuilder.RegisterDrawLayer(1));
Assert.DoesNotThrow(() => worldBuilder.AddOrderedRenderer(new ARenderer()));
}
[Test]
public void DrawLayerWithProperty()
{
var worldBuilder = new WorldBuilder();
var rendererB = worldBuilder.AddOrderedRenderer(new BRenderer());
Assert.DoesNotThrow(() => worldBuilder.Build());
}
}
}
}

81
test/WorldTest.cs Normal file
View File

@ -0,0 +1,81 @@
using NUnit.Framework;
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Text;
using Encompass;
namespace Tests
{
public class WorldTest
{
struct TestComponent : IComponent { }
struct TestDrawComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
}
static List<object> drawOrder = new List<object>();
class TestEntityRenderer : OrderedRenderer<TestDrawComponent>
{
public override void Render(Entity entity, in TestDrawComponent testDrawComponent)
{
drawOrder.Add(entity);
}
}
class TestGeneralRenderer : GeneralRenderer
{
public override void Render()
{
drawOrder.Add(this);
}
}
[Test]
public void DrawOrder()
{
var worldBuilder = new WorldBuilder();
worldBuilder.RegisterDrawLayer(1);
worldBuilder.RegisterDrawLayer(2);
worldBuilder.RegisterDrawLayer(3);
worldBuilder.RegisterDrawLayer(4);
worldBuilder.RegisterDrawLayer(7);
worldBuilder.AddOrderedRenderer(new TestEntityRenderer());
var testGeneralRenderer = worldBuilder.AddGeneralRenderer(new TestGeneralRenderer(), 7);
TestComponent testComponent;
TestDrawComponent drawComponentThree = new TestDrawComponent { Layer = 3 };
var drawComponentTwo = new TestDrawComponent { Layer = 2 };
var drawComponentOne = new TestDrawComponent { Layer = 1 };
var drawComponentFour = new TestDrawComponent { Layer = 4 };
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, testComponent);
worldBuilder.SetComponent(entity, drawComponentThree);
var entityTwo = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entityTwo, testComponent);
worldBuilder.SetComponent(entityTwo, drawComponentTwo);
var entityThree = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entityThree, testComponent);
worldBuilder.SetComponent(entityThree, drawComponentThree);
var entityFour = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entityFour, testComponent);
worldBuilder.SetComponent(entityFour, drawComponentFour);
var world = worldBuilder.Build();
world.Update(0.01f);
world.Draw();
drawOrder.Should().BeEquivalentTo(entityFour, entityTwo, entity, entityThree, testGeneralRenderer);
}
}
}

View File

@ -6,7 +6,6 @@
<RootNamespace>Tests</RootNamespace>
<AssemblyName>EncompassECS.Framework.Tests</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x64;x86</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.7.0" />