diff --git a/src/Builtin/WorldRunners.cs b/src/Builtin/WorldRunners.cs index 79271e1..750f6a4 100644 --- a/src/Builtin/WorldRunners.cs +++ b/src/Builtin/WorldRunners.cs @@ -63,23 +63,23 @@ public interface IEcsWorldCreate : IEcsSystem { - public void OnWorldCreate(IEcsWorld world); + public void OnWorldCreate(EcsWorld world); } public interface IEcsWorldDestroy : IEcsSystem { - public void OnWorldDestroy(IEcsWorld world); + public void OnWorldDestroy(EcsWorld world); } public interface IEcsWorldLifecycle : IEcsWorldCreate, IEcsWorldDestroy { } public sealed class EcsWorldCreateRunner : EcsRunner, IEcsWorldCreate { - public void OnWorldCreate(IEcsWorld world) + public void OnWorldCreate(EcsWorld world) { foreach (var item in targets) item.OnWorldCreate(world); } } public sealed class EcsWorldDestryRunner : EcsRunner, IEcsWorldDestroy { - public void OnWorldDestroy(IEcsWorld world) + public void OnWorldDestroy(EcsWorld world) { foreach (var item in targets) item.OnWorldDestroy(world); } diff --git a/src/Builtin/Worlds.cs b/src/Builtin/Worlds.cs index d060658..73a3828 100644 --- a/src/Builtin/Worlds.cs +++ b/src/Builtin/Worlds.cs @@ -13,6 +13,6 @@ } public sealed class EcsEventWrold : EcsEdgeWorld { - public EcsEventWrold(IEcsWorld firstTarget, IEcsWorld secondTarget, EcsPipeline pipeline = null) : base(firstTarget, secondTarget, pipeline) { } + public EcsEventWrold(EcsWorld firstTarget, EcsWorld secondTarget, EcsPipeline pipeline = null) : base(firstTarget, secondTarget, pipeline) { } } } diff --git a/src/EcsEdgeWorld.cs b/src/EcsEdgeWorld.cs index 9e89653..a6c68e3 100644 --- a/src/EcsEdgeWorld.cs +++ b/src/EcsEdgeWorld.cs @@ -8,14 +8,14 @@ namespace DCFApixels.DragonECS { public class EcsEdgeWorld : EcsWorld where TWorldArchetype : EcsWorld { - private IEcsWorld _firstTarget; - private IEcsWorld _secondTarget; - public EcsEdgeWorld(IEcsWorld firstTarget, IEcsWorld secondTarget, EcsPipeline pipeline) : base(pipeline) + private EcsWorld _firstTarget; + private EcsWorld _secondTarget; + public EcsEdgeWorld(EcsWorld firstTarget, EcsWorld secondTarget, EcsPipeline pipeline) : base(pipeline) { _firstTarget = firstTarget; _secondTarget = secondTarget; } - public EcsEdgeWorld(IEcsWorld firstTarget, EcsPipeline pipeline) : base(pipeline) + public EcsEdgeWorld(EcsWorld firstTarget, EcsPipeline pipeline) : base(pipeline) { _firstTarget = firstTarget; } diff --git a/src/EcsGroup.cs b/src/EcsGroup.cs index 131db48..3e6ef94 100644 --- a/src/EcsGroup.cs +++ b/src/EcsGroup.cs @@ -86,7 +86,7 @@ namespace DCFApixels.DragonECS private const int DEALAYED_ADD = 0; private const int DEALAYED_REMOVE = int.MinValue; - private IEcsWorld _source; + private EcsWorld _source; private int[] _dense; private int[] _sparse; @@ -101,7 +101,7 @@ namespace DCFApixels.DragonECS private bool _isReleazed = true; #region Properties - public IEcsWorld World => _source; + public EcsWorld World => _source; public int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -131,12 +131,12 @@ namespace DCFApixels.DragonECS #region Constrcutors/Finalizer [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsGroup New(IEcsWorld world) + public static EcsGroup New(EcsWorld world) { return world.GetGroupFromPool(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal EcsGroup(IEcsWorld world, int denseCapacity = 64, int delayedOpsCapacity = 128) + internal EcsGroup(EcsWorld world, int denseCapacity = 64, int delayedOpsCapacity = 128) { _source = world; _source.RegisterGroup(this); diff --git a/src/EcsPool.cs b/src/EcsPool.cs index c659e3e..f3e856c 100644 --- a/src/EcsPool.cs +++ b/src/EcsPool.cs @@ -8,7 +8,7 @@ namespace DCFApixels.DragonECS { #region Properties public Type ComponentType { get; } - public IEcsWorld World { get; } + public EcsWorld World { get; } public int Count { get; } public int Capacity { get; } #endregion @@ -34,14 +34,14 @@ namespace DCFApixels.DragonECS public sealed class EcsNullPool : IEcsPool { public static EcsNullPool instance => new EcsNullPool(null); - private IEcsWorld _source; + private EcsWorld _source; private NullComponent fakeComponent; - private EcsNullPool(IEcsWorld source) => _source = source; + private EcsNullPool(EcsWorld source) => _source = source; #region Properties public Type ComponentType => typeof(NullComponent); public int ComponentID => -1; - public IEcsWorld World => _source; + public EcsWorld World => _source; public int Count => 0; public int Capacity => 1; #endregion @@ -61,12 +61,12 @@ namespace DCFApixels.DragonECS public sealed class EcsPool : IEcsPool where T : struct { - public static EcsPool Builder(IEcsWorld source) + public static EcsPool Builder(EcsWorld source) { return new EcsPool(source, 512, default); } - private readonly IEcsWorld _source; + private readonly EcsWorld _source; private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID private T[] _items; //dense @@ -80,12 +80,12 @@ namespace DCFApixels.DragonECS #region Properites public int Count => _itemsCount; public int Capacity => _items.Length; - public IEcsWorld World => _source; + public EcsWorld World => _source; public Type ComponentType => typeof(T); #endregion #region Constructors - internal EcsPool(IEcsWorld source, int capacity, PoolRunners poolRunnres) + internal EcsPool(EcsWorld source, int capacity, PoolRunners poolRunnres) { _source = source; diff --git a/src/EcsQuery.cs b/src/EcsQuery.cs index 83cfa5d..a48ae83 100644 --- a/src/EcsQuery.cs +++ b/src/EcsQuery.cs @@ -7,10 +7,10 @@ namespace DCFApixels.DragonECS { public abstract class EcsQueryBase { - internal IEcsWorld source; + internal EcsWorld source; internal EcsGroup groupFilter; internal EcsQueryMask mask; - public IEcsWorld World => source; + public EcsWorld World => source; #region Builder protected virtual void Init(Builder b) { } @@ -18,17 +18,17 @@ namespace DCFApixels.DragonECS public abstract void Execute(); public sealed class Builder : EcsQueryBuilderBase { - private IEcsWorld _world; + private EcsWorld _world; private List _inc; private List _exc; - private Builder(IEcsWorld world) + private Builder(EcsWorld world) { _world = world; _inc = new List(8); _exc = new List(4); } - internal static TQuery Build(IEcsWorld world) where TQuery : EcsQueryBase + internal static TQuery Build(EcsWorld world) where TQuery : EcsQueryBase { Builder builder = new Builder(world); Type queryType = typeof(TQuery); diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index be97a7c..b80f932 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -20,30 +20,14 @@ namespace DCFApixels.DragonECS #endregion } - public abstract class EcsWorld + public abstract class EcsWorld : IEcsWorld { - public static IEcsWorld[] Worlds = new IEcsWorld[8]; + public static EcsWorld[] Worlds = new EcsWorld[8]; private static IntDispenser _worldIdDispenser = new IntDispenser(0); public readonly short uniqueID; - protected EcsWorld() - { - uniqueID = (short)_worldIdDispenser.GetFree(); - if (uniqueID >= Worlds.Length) - Array.Resize(ref Worlds, Worlds.Length << 1); - Worlds[uniqueID] = (IEcsWorld)this; - } - protected void Realeze() - { - Worlds[uniqueID] = null; - _worldIdDispenser.Release(uniqueID); - } - } - - public abstract class EcsWorld : EcsWorld, IEcsWorld - where TWorldArchetype : EcsWorld - { + private const int DEL_ENT_BUFFER_SIZE_OFFSET = 2; - private readonly int _worldArchetypeID = WorldMetaStorage.GetWorldId(); + private int _worldArchetypeID; private IntDispenser _entityDispenser; private int _entitiesCount; @@ -78,7 +62,7 @@ namespace DCFApixels.DragonECS #endregion #region Properties - public Type Archetype => typeof(TWorldArchetype); + public abstract Type Archetype { get; } public int UniqueID => uniqueID; public int Count => _entitiesCount; public int Capacity => _entitesCapacity; //_denseEntities.Length; @@ -88,7 +72,14 @@ namespace DCFApixels.DragonECS #region Constructors public EcsWorld(EcsPipeline pipline) - { + { + uniqueID = (short)_worldIdDispenser.GetFree(); + if (uniqueID >= Worlds.Length) + Array.Resize(ref Worlds, Worlds.Length << 1); + Worlds[uniqueID] = this; + + _worldArchetypeID = WorldMetaStorage.GetWorldId(Archetype); + _pipeline = pipline ?? EcsPipeline.Empty; if (!_pipeline.IsInit) pipline.Init(); _entityDispenser = new IntDispenser(0); @@ -109,9 +100,14 @@ namespace DCFApixels.DragonECS _poolRunners = new PoolRunners(_pipeline); _entityCreate = _pipeline.GetRunner(); _entityDestry = _pipeline.GetRunner(); - _pipeline.GetRunner>().Inject(this); + _pipeline.GetRunner>().Inject(this); _pipeline.GetRunner().OnWorldCreate(this); } + protected void Realeze() + { + Worlds[uniqueID] = null; + _worldIdDispenser.Release(uniqueID); + } #endregion #region GetPool @@ -212,7 +208,7 @@ namespace DCFApixels.DragonECS _entitiesCount--; _entityDestry.OnEntityDestroy(entity); - if(_delEntBufferCount >= _delEntBuffer.Length) + if (_delEntBufferCount >= _delEntBuffer.Length) ReleaseDelEntBuffer(); } @@ -254,7 +250,8 @@ namespace DCFApixels.DragonECS #endregion #region Groups - void IEcsTable.RegisterGroup(EcsGroup group) + void IEcsTable.RegisterGroup(EcsGroup group) => RegisterGroup(group); + internal void RegisterGroup(EcsGroup group) { _groups.Add(new WeakReference(group)); } @@ -265,7 +262,8 @@ namespace DCFApixels.DragonECS return new EcsGroup(this); return _groupsPool.Pop(); } - void IEcsTable.ReleaseGroup(EcsGroup group) + void IEcsTable.ReleaseGroup(EcsGroup group) => ReleaseGroup(group); + internal void ReleaseGroup(EcsGroup group) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS if (group.World != this) @@ -277,6 +275,13 @@ namespace DCFApixels.DragonECS #endregion } + public abstract class EcsWorld : EcsWorld + where TWorldArchetype : EcsWorld + { + public override Type Archetype => typeof(TWorldArchetype); + public EcsWorld(EcsPipeline pipline) : base(pipline) { } + } + #region Utils [StructLayout(LayoutKind.Sequential, Pack = 8, Size = 24)] internal readonly struct PoolRunners diff --git a/src/Entities/ent.cs b/src/Entities/ent.cs index 76fd8e2..be9133e 100644 --- a/src/Entities/ent.cs +++ b/src/Entities/ent.cs @@ -27,6 +27,6 @@ namespace DCFApixels.DragonECS public struct Null { } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsEntity ToStrongID(IEcsWorld world) => world.GetEcsEntity(id); + public EcsEntity ToStrongID(EcsWorld world) => world.GetEcsEntity(id); } } diff --git a/src/TestPool.cs b/src/TestPool.cs index a0dd72a..3caeb13 100644 --- a/src/TestPool.cs +++ b/src/TestPool.cs @@ -45,7 +45,7 @@ namespace DCFApixels.DragonECS.Test { public Type ComponentType => throw new NotImplementedException(); - public IEcsWorld World => throw new NotImplementedException(); + public EcsWorld World => throw new NotImplementedException(); public int Count => throw new NotImplementedException(); @@ -86,7 +86,7 @@ namespace DCFApixels.DragonECS.Test throw new NotImplementedException(); } - void IEcsPool.Write(int entityID) + void IEcsPool.Write(int entityID, object data) { throw new NotImplementedException(); }