Compare commits

...

10 Commits
0.21.3 ... main

Author SHA1 Message Date
cosmonaut 00614661ec fix broken test
continuous-integration/drone/push Build is passing Details
2021-03-23 17:50:05 -07:00
cosmonaut 36e98b0376 add dt and alpha params to renderers
continuous-integration/drone/push Build is failing Details
2021-03-23 17:37:47 -07:00
cosmonaut 6858ec1b0f Mega Refactor (#20)
continuous-integration/drone/push Build is passing Details
Co-Authored-By: cosmonaut <evan@moonside.games>
Co-Committed-By: cosmonaut <evan@moonside.games>
2021-03-12 08:25:25 +00:00
cosmonaut df0e251650 Merge pull request 'fix configuration' (#19) from fix_sln into main
continuous-integration/drone/push Build is passing Details
Reviewed-on: #19
2021-01-30 01:09:35 +00:00
cosmonaut 5183c04632 fix configuration
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
2021-01-29 17:04:13 -08:00
cosmonaut d5186e6011 remove some dependencies (#18)
continuous-integration/drone/push Build is passing Details
Co-Authored-By: cosmonaut <evan@moonside.games>
Co-Committed-By: cosmonaut <evan@moonside.games>
2021-01-30 00:36:42 +00:00
cosmonaut 49ea9e7a5d 0.23.0 (#16)
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2020-11-16 07:24:37 +00:00
cosmonaut 40e6d75638 Expose some enumerable iterators + some tweaks (#15)
continuous-integration/drone/push Build is passing Details
2020-11-16 07:21:51 +00:00
cosmonaut 327a458f17 Update 'encompass-cs/encompass-cs.csproj'
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2020-07-22 01:10:45 +00:00
Evan Hemsley d661ed5b17 Spawner.Spawn now requires in param
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details
2020-07-21 18:05:47 -07:00
42 changed files with 901 additions and 802 deletions

View File

@ -7,18 +7,18 @@
steps:
- name: test
image: mcr.microsoft.com/dotnet/core/sdk:3.1
image: mcr.microsoft.com/dotnet/sdk:5.0.102-ca-patch-buster-slim
commands:
- dotnet build -c Release
- dotnet test -c Release
- name: deploy
image: mcr.microsoft.com/dotnet/core/sdk:3.1
image: mcr.microsoft.com/dotnet/sdk:5.0.102-ca-patch-buster-slim
environment:
API_KEY:
from_secret: API_KEY
commands:
- dotnet nuget push /build/encompass-cs/bin/Release/EncompassECS.Framework.*.nupkg -s https://api.nuget.org/v3/index.json -k $API_KEY
- dotnet nuget push /build/encompass-cs/bin/Release/EncompassECS.Framework.*.nupkg -s https://api.nuget.org/v3/index.json -k $API_KEY
when:
ref:
- refs/tags/*.*.*

7
TODO
View File

@ -1,7 +0,0 @@
- immutable component system?
- method to remove all components of a type without destroying Entities
- method to remove a component of a type without destroying entity
- look at test coverage
- docs

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.30717.126
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,38 +9,28 @@ 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|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
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,16 @@
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,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
@ -12,15 +10,6 @@ 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,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
@ -12,15 +10,6 @@ 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

@ -0,0 +1,156 @@
using System;
namespace Encompass
{
public static unsafe class MemoryHelper
{
public static void Copy(uint* src, uint* dest, int count)
{
for (; count != 0; count--) *dest++ = *src++;
}
public static void Fill(uint* p, int count, uint value)
{
for (; count != 0; count--) *p++ = value;
}
public static void And(uint* p, uint* q, uint* result, int count)
{
for (; count != 0; count--) *result++ = *p++ & *q++;
}
public static void Or(uint* p, uint* q, uint* result, int count)
{
for (; count != 0; count--) *result++ = *p++ | *q++;
}
public static void Not(uint* p, uint* result, int count)
{
for (; count != 0; count--) *result++ = ~*p++;
}
public static bool Equal(uint* p, uint* q, int count)
{
for (; count != 0; count--) if (*p++ != *q++) { return false; }
return true;
}
}
public unsafe struct BitSet512 : IEquatable<BitSet512>
{
public static BitSet512 Zero { get; } = new BitSet512(0);
public static BitSet512 Ones { get; } = new BitSet512(uint.MaxValue);
private const int _uintLength = 16;
private fixed uint _buffer[_uintLength];
public BitSet512(uint value)
{
fixed (uint* p = _buffer) MemoryHelper.Fill(p, _uintLength, value);
}
public BitSet512(uint* src)
{
fixed (uint* dest = _buffer) MemoryHelper.Copy(src, dest, _uintLength);
}
public static BitSet512 operator &(BitSet512 a, BitSet512 b)
{
var tmp = stackalloc uint[_uintLength];
MemoryHelper.And(a._buffer, b._buffer, tmp, _uintLength);
return new BitSet512(tmp);
}
public static BitSet512 operator |(BitSet512 a, BitSet512 b)
{
var tmp = stackalloc uint[_uintLength];
MemoryHelper.Or(a._buffer, b._buffer, tmp, _uintLength);
return new BitSet512(tmp);
}
public static BitSet512 operator ~(BitSet512 a)
{
var tmp = stackalloc uint[_uintLength];
MemoryHelper.Not(a._buffer, tmp, _uintLength);
return new BitSet512(tmp);
}
public static bool operator ==(BitSet512 left, BitSet512 right)
{
return left.Equals(right);
}
public static bool operator !=(BitSet512 left, BitSet512 right)
{
return !(left == right);
}
public BitSet512 Set(int index)
{
var tmp = stackalloc uint[_uintLength];
fixed (uint* p = _buffer) MemoryHelper.Copy(p, tmp, _uintLength);
tmp[index / 32] |= (uint)(1 << index % 32);
return new BitSet512(tmp);
}
public BitSet512 UnSet(int index)
{
var tmp = stackalloc uint[_uintLength];
fixed (uint* p = _buffer) MemoryHelper.Copy(p, tmp, _uintLength);
tmp[index / 32] &= ~(uint)(1 << index % 32);
return new BitSet512(tmp);
}
public bool Get(int bitIndex)
{
var bitInt = (uint)(1 << bitIndex % 32);
return (_buffer[bitIndex / 32] & bitInt) == bitInt;
}
public bool AllTrue()
{
return this == Ones;
}
public bool AllFalse()
{
return this == Zero;
}
public static BitSet512 BitwiseAnd(BitSet512 left, BitSet512 right)
{
return left & right;
}
public static BitSet512 BitwiseOr(BitSet512 left, BitSet512 right)
{
return left | right;
}
public static BitSet512 OnesComplement(BitSet512 bitSet)
{
return ~bitSet;
}
public override bool Equals(object obj)
{
return obj is BitSet512 set && Equals(set);
}
public bool Equals(BitSet512 other)
{
fixed (uint* p = _buffer) return MemoryHelper.Equal(p, other._buffer, _uintLength);
}
public override int GetHashCode()
{
var hc = 0;
for (var i = 0; i < _uintLength; i++)
{
hc ^= _buffer[i].GetHashCode();
}
return hc;
}
}
}

View File

@ -1,4 +1,3 @@
using MoonTools.FastCollections;
using System;
using System.Collections.Generic;

View File

@ -74,7 +74,7 @@ namespace Encompass
}
}
public Span<Entity> AllEntities<TComponent>() where TComponent : struct
public ReadOnlySpan<Entity> AllEntities<TComponent>() where TComponent : struct
{
return _store.AllEntities<TComponent>();
}

View File

@ -1,5 +1,4 @@
using Encompass.Exceptions;
using MoonTools.FastCollections;
using System;
using System.Collections.Generic;
@ -113,16 +112,26 @@ namespace Encompass
return Lookup<TComponent>().Count > 0;
}
public Span<TComponent> All<TComponent>() where TComponent : struct
public ReadOnlySpan<TComponent> All<TComponent>() where TComponent : struct
{
return Lookup<TComponent>().AllComponents();
}
public Span<Entity> AllEntities<TComponent>() where TComponent : struct
public IEnumerable<TComponent> AllAsEnumerable<TComponent>() where TComponent : struct
{
return Lookup<TComponent>().AllComponentsAsEnumerable();
}
public ReadOnlySpan<Entity> AllEntities<TComponent>() where TComponent : struct
{
return Lookup<TComponent>().AllEntities();
}
public IEnumerable<Entity> AllEntitiesAsEnumerable<TComponent>() where TComponent : struct
{
return Lookup<TComponent>().AllEntitiesAsEnumerable();
}
public void Clear<TComponent>() where TComponent : struct
{
Lookup<TComponent>().Clear();

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, IMessage
private void RegisterMessageType<TMessage>() where TMessage : struct
{
_stores.Add(typeof(TMessage), new TypedMessageStore<TMessage>());
}
private TypedMessageStore<TMessage> Lookup<TMessage>() where TMessage : struct, IMessage
private TypedMessageStore<TMessage> Lookup<TMessage>() where TMessage : struct
{
if (!_stores.ContainsKey(typeof(TMessage))) { RegisterMessageType<TMessage>(); }
return _stores[typeof(TMessage)] as TypedMessageStore<TMessage>;
}
public void AddMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
public void AddMessage<TMessage>(in TMessage message) where TMessage : struct
{
Lookup<TMessage>().Add(message);
}
public void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
public void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct
{
Lookup<TMessage>().Add(message, time);
}
public void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
public void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct
{
Lookup<TMessage>().AddIgnoringTimeDilation(message, time);
}
public ref readonly TMessage First<TMessage>() where TMessage : struct, IMessage
public ref readonly TMessage First<TMessage>() where TMessage : struct
{
if (!Any<TMessage>()) { throw new NoMessageOfTypeException("No Message of type {0} exists", typeof(TMessage).Name); }
return ref Lookup<TMessage>().First();
}
public Span<TMessage> All<TMessage>() where TMessage : struct, IMessage
public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : struct
{
return Lookup<TMessage>().All();
}
public bool Any<TMessage>() where TMessage : struct, IMessage
public bool Any<TMessage>() where TMessage : struct
{
return Lookup<TMessage>().Any();
}
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
{
return Lookup<TMessage>().WithEntity(entityID);
}
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct
{
return ref Lookup<TMessage>().FirstWithEntity(entityID);
}
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, 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
{
_priorities[entityID] = priority;
ForceRemove(entityID);
_priorities[entityID] = priority;
return true;
}
@ -92,7 +92,6 @@ 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)
@ -127,14 +126,24 @@ namespace Encompass
_priorities.Clear();
}
public Span<Entity> AllEntities()
public ReadOnlySpan<Entity> AllEntities()
{
return new Span<Entity>(_storageIndexToEntities, 0, _nextID);
return new ReadOnlySpan<Entity>(_storageIndexToEntities, 0, _nextID);
}
public Span<TComponent> AllComponents()
public IEnumerable<Entity> AllEntitiesAsEnumerable()
{
return new Span<TComponent>(_components, 0, _nextID);
return new ArraySegment<Entity>(_storageIndexToEntities, 0, _nextID);
}
public ReadOnlySpan<TComponent> AllComponents()
{
return new ReadOnlySpan<TComponent>(_components, 0, _nextID);
}
public IEnumerable<TComponent> AllComponentsAsEnumerable()
{
return new ArraySegment<TComponent>(_components, 0, _nextID);
}
}
}

View File

@ -9,7 +9,7 @@ namespace Encompass
public abstract void Clear();
}
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct, IMessage
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct
{
// messages are placed in a contiguous region
// so we can return the collection as a Span
@ -92,9 +92,9 @@ namespace Encompass
return _nextIndex != 0;
}
public Span<TMessage> All()
public ReadOnlySpan<TMessage> All()
{
return new Span<TMessage>(_store, 0, _nextIndex);
return new ReadOnlySpan<TMessage>(_store, 0, _nextIndex);
}
public IEnumerable<TMessage> WithEntity(int entityID)

View File

@ -5,8 +5,6 @@ namespace Encompass
{
internal class ComponentManager
{
private readonly DrawLayerManager _drawLayerManager;
private readonly ComponentStore _existingComponentStore;
private readonly ComponentStore _immediateComponentStore;
private readonly ComponentDeltaStore _replayStore;
@ -19,9 +17,8 @@ namespace Encompass
internal ComponentBitSet ImmediateBits { get { return _immediateComponentStore.ComponentBitSet; } }
internal ComponentBitSet ExistingBits { get { return _existingComponentStore.ComponentBitSet; } }
public ComponentManager(DrawLayerManager drawLayerManager, Dictionary<Type, int> typeToIndex)
public ComponentManager(Dictionary<Type, int> typeToIndex)
{
this._drawLayerManager = drawLayerManager;
_existingComponentStore = new ComponentStore(typeToIndex);
_immediateComponentStore = new ComponentStore(typeToIndex);
_replayStore = new ComponentDeltaStore(typeToIndex);
@ -47,11 +44,6 @@ 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);
@ -98,7 +90,7 @@ namespace Encompass
// existing or immediate reads
internal Span<TComponent> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct
internal ReadOnlySpan<TComponent> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct
{
return _upToDateComponentStore.All<TComponent>();
}
@ -108,7 +100,7 @@ namespace Encompass
return ref _upToDateComponentStore.Singular<TComponent>();
}
internal Span<Entity> GetExistingAndImmediateEntities<TComponent>() where TComponent : struct
internal ReadOnlySpan<Entity> GetExistingAndImmediateEntities<TComponent>() where TComponent : struct
{
return _upToDateComponentStore.AllEntities<TComponent>();
}
@ -125,7 +117,7 @@ namespace Encompass
// existing reads
internal Span<TComponent> GetExistingComponents<TComponent>() where TComponent : struct
internal ReadOnlySpan<TComponent> GetExistingComponents<TComponent>() where TComponent : struct
{
return _existingComponentStore.All<TComponent>();
}
@ -135,11 +127,16 @@ namespace Encompass
return ref _existingComponentStore.Singular<TComponent>();
}
internal Span<Entity> GetExistingEntities<TComponent>() where TComponent : struct
internal ReadOnlySpan<Entity> GetExistingEntities<TComponent>() where TComponent : struct
{
return _existingComponentStore.AllEntities<TComponent>();
}
internal IEnumerable<Entity> GetExistingEntitiesAsEnumerable<TComponent>() where TComponent : struct
{
return _existingComponentStore.AllEntitiesAsEnumerable<TComponent>();
}
internal ref readonly Entity ExistingSingularEntity<TComponent>() where TComponent : struct
{
return ref _existingComponentStore.SingularEntity<TComponent>();
@ -152,7 +149,7 @@ namespace Encompass
// immediate reads
internal Span<TComponent> ReadImmediateComponentsByType<TComponent>() where TComponent : struct
internal ReadOnlySpan<TComponent> ReadImmediateComponentsByType<TComponent>() where TComponent : struct
{
return _immediateComponentStore.All<TComponent>();
}
@ -162,7 +159,7 @@ namespace Encompass
return ref _immediateComponentStore.Singular<TComponent>();
}
internal Span<Entity> GetImmediateEntities<TComponent>() where TComponent : struct
internal ReadOnlySpan<Entity> GetImmediateEntities<TComponent>() where TComponent : struct
{
return _immediateComponentStore.AllEntities<TComponent>();
}
@ -226,11 +223,16 @@ namespace Encompass
return _immediateComponentStore.Has(type, entityID);
}
internal Span<TComponent> GetComponentsByType<TComponent>() where TComponent : struct
internal ReadOnlySpan<TComponent> GetComponentsByType<TComponent>() where TComponent : struct
{
return _existingComponentStore.All<TComponent>();
}
internal IEnumerable<TComponent> GetComponentsByTypeEnumerable<TComponent>() where TComponent : struct
{
return _existingComponentStore.AllAsEnumerable<TComponent>();
}
internal ref readonly TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{
return ref _existingComponentStore.Get<TComponent>(entityID);
@ -254,7 +256,6 @@ namespace Encompass
_immediateComponentStore.Remove(entityID);
_replayStore.Remove(entityID);
_upToDateComponentStore.Remove(entityID);
_drawLayerManager.UnRegisterEntityWithLayer(entityID);
}
_entitiesMarkedForRemoval.Clear();
@ -266,7 +267,6 @@ namespace Encompass
{
_replayStore.Remove<TComponent>(entityID, priority);
_upToDateComponentStore.Remove<TComponent>(entityID, priority);
_drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
return true;
}
return false;
@ -277,7 +277,6 @@ namespace Encompass
if (_upToDateComponentStore.Remove<TComponent>(entityID, priority))
{
_replayStore.Remove<TComponent>(entityID, priority);
_drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
}
}

View File

@ -1,132 +0,0 @@
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

@ -3,7 +3,6 @@ using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
using MoonTools.FastCollections;
namespace Encompass
{
@ -24,6 +23,7 @@ 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,6 +59,12 @@ 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)
{
@ -170,7 +176,7 @@ namespace Encompass
_trackingManager = trackingManager;
}
internal void CheckMessageRead<TMessage>() where TMessage : struct, IMessage
internal void CheckMessageRead<TMessage>() where TMessage : struct
{
if (!ReceiveTypes.Contains(typeof(TMessage)))
{
@ -223,7 +229,7 @@ namespace Encompass
/// <summary>
/// Returns all Entities containing the specified Component type.
/// </summary>
protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -248,7 +254,7 @@ namespace Encompass
/// <summary>
/// Returns an Entity containing the specified Component type.
/// </summary>
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -273,7 +279,7 @@ namespace Encompass
/// <summary>
/// Returns all of the Components with the specified Component Type.
/// </summary>
protected Span<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -298,7 +304,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, IComponent
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -323,7 +329,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 : struct
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -345,7 +351,7 @@ namespace Encompass
}
}
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct, IComponent
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -376,7 +382,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, IComponent
protected ref readonly TComponent GetComponent<TComponent>(in Entity entity) where TComponent : struct
{
return ref GetComponentHelper<TComponent>(entity.ID);
}
@ -387,7 +393,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, IComponent
protected bool HasComponent<TComponent>(in Entity entity) where TComponent : struct
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -445,7 +451,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, IComponent
protected void SetComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct
{
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
@ -472,11 +478,6 @@ namespace Encompass
{
_trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
}
if (written && component is IDrawableComponent drawableComponent)
{
_componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
}
}
/// <summary>
@ -485,8 +486,13 @@ 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, IComponent
protected void AddComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct
{
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.");
@ -503,11 +509,6 @@ namespace Encompass
}
_trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
if (component is IDrawableComponent drawableComponent)
{
_componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
}
}
/// <summary>
@ -516,7 +517,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, IMessage
protected void SendMessage<TMessage>(in TMessage message) where TMessage : struct
{
if (!SendTypes.Contains(typeof(TMessage)))
{
@ -530,7 +531,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, IMessage
protected void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct
{
_messageManager.AddMessage(message, time);
}
@ -539,7 +540,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, IMessage
protected void SendMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct
{
_messageManager.AddMessageIgnoringTimeDilation(message, time);
}
@ -550,7 +551,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
/// </exception>
protected Span<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage
protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : struct
{
CheckMessageRead<TMessage>();
return _messageManager.GetMessagesByType<TMessage>();
@ -562,7 +563,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, IMessage
protected ref readonly TMessage ReadMessage<TMessage>() where TMessage : struct
{
CheckMessageRead<TMessage>();
return ref _messageManager.First<TMessage>();
@ -574,7 +575,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, IMessage
protected bool SomeMessage<TMessage>() where TMessage : struct
{
CheckMessageRead<TMessage>();
return _messageManager.Any<TMessage>();
@ -593,7 +594,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 : struct
{
Destroy(ReadEntity<TComponent>());
}
@ -602,7 +603,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 : struct
{
foreach (var entity in ReadEntities<TComponent>())
{
@ -615,7 +616,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, IComponent
protected void RemoveComponent<TComponent>(in Entity entity) where TComponent : struct
{
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
@ -708,7 +709,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, IMessage, IHasEntity
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
{
CheckMessageRead<TMessage>();
return _messageManager.WithEntity<TMessage>(entity.ID);
@ -718,7 +719,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, IMessage, IHasEntity
protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
{
CheckMessageRead<TMessage>();
return ref _messageManager.WithEntitySingular<TMessage>(entity.ID);
@ -727,7 +728,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, IMessage, IHasEntity
protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, 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, IMessage
public abstract class Spawner<TMessage> : Engine where TMessage : struct
{
protected Spawner() : base()
{
@ -21,12 +21,12 @@ namespace Encompass
public override void Update(double dt)
{
foreach (var message in ReadMessages<TMessage>())
foreach (ref readonly var message in ReadMessages<TMessage>())
{
Spawn(message);
}
}
protected abstract void Spawn(TMessage message);
protected abstract void Spawn(in TMessage message);
}
}

View File

@ -1,6 +1,4 @@
using MoonTools.FastCollections;
namespace Encompass
namespace Encompass
{
internal struct EntitySetQuery
{

View File

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

View File

@ -0,0 +1,390 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Encompass
{
internal class DirectedGraph<TNode> where TNode : IEquatable<TNode>
{
protected HashSet<TNode> nodes = new HashSet<TNode>();
protected Dictionary<TNode, HashSet<TNode>> neighbors = new Dictionary<TNode, HashSet<TNode>>();
protected HashSet<(TNode, TNode)> edges = new HashSet<(TNode, TNode)>();
public IEnumerable<TNode> Nodes => nodes;
public IEnumerable<(TNode, TNode)> Edges => edges;
public void AddNode(TNode node)
{
if (!Exists(node))
{
nodes.Add(node);
neighbors.Add(node, new HashSet<TNode>());
}
}
public void AddNodes(params TNode[] nodes)
{
foreach (var node in nodes)
{
AddNode(node);
}
}
public void RemoveNode(TNode node)
{
CheckNodes(node);
var edgesToRemove = new List<(TNode, TNode)>();
foreach (var entry in neighbors)
{
if (entry.Value.Contains(node))
{
edgesToRemove.Add((entry.Key, node));
}
}
foreach (var edge in edgesToRemove)
{
RemoveEdge(edge.Item1, edge.Item2);
}
nodes.Remove(node);
neighbors.Remove(node);
}
public void RemoveEdge(TNode v, TNode u)
{
CheckEdge(v, u);
neighbors[v].Remove(u);
edges.Remove((v, u));
}
public void AddEdge(TNode v, TNode u)
{
CheckNodes(v, u);
if (Exists(v, u)) { throw new ArgumentException($"Edge between {v} and {u} already exists in the graph"); }
if (v.Equals(u)) { throw new ArgumentException("Self-edges are not allowed in a simple graph. Use a multigraph instead"); }
neighbors[v].Add(u);
edges.Add((v, u));
}
public bool Exists(TNode node)
{
return nodes.Contains(node);
}
public bool Exists(TNode v, TNode u)
{
CheckNodes(v, u);
return edges.Contains((v, u));
}
protected void CheckNodes(params TNode[] givenNodes)
{
foreach (var node in givenNodes)
{
if (!Exists(node))
{
throw new System.ArgumentException($"Vertex {node} does not exist in the graph");
}
}
}
protected void CheckEdge(TNode v, TNode u)
{
CheckNodes(v, u);
if (!Exists(v, u)) { throw new ArgumentException($"Edge between vertex {v} and vertex {u} does not exist in the graph"); }
}
public IEnumerable<TNode> Neighbors(TNode node)
{
CheckNodes(node);
return neighbors[node];
}
public DirectedGraph<TNode> Clone()
{
var clone = new DirectedGraph<TNode>();
clone.AddNodes(Nodes.ToArray());
foreach (var v in Nodes)
{
foreach (var n in Neighbors(v))
{
clone.AddEdge(v, n);
}
}
return clone;
}
public DirectedGraph<TNode> SubGraph(params TNode[] subVertices)
{