Update WIP

This commit is contained in:
Mikhail 2023-02-07 17:11:56 +08:00
parent 2c6fac98fb
commit cab282ac43
38 changed files with 608 additions and 184 deletions

8
src/Builtin.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f9f604669d2092e4a92d397e9c7db287
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,15 @@
namespace DCFApixels.DragonECS
{
public class DestroyProcessor : IEcsDo<_Run>
{
void IEcsDo<_Run>.Do(EcsSession session)
{
}
}
public class DestroyedTable : EcsTable
{
private EcsIncTag _destroyedTag;
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: dc514cbb93c3f3049adf666ad237c6ee guid: c48ecb209046a944e9602d18f3fd5237
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -9,29 +9,29 @@ namespace DCFApixels.DragonECS
public class EcsEntityTableManager public class EcsEntityTableManager
{ {
private int _count; private int _count;
private IEcsFieldPool[] _fieldPools; private IEcsPool[] _fieldPools;
private int _idIncrement; private int _idIncrement;
private Dictionary<IDKey, int> _ids; private Dictionary<IDKey, int> _ids;
public EcsEntityTableManager(int capacity) public EcsEntityTableManager(int capacity)
{ {
_fieldPools = new IEcsFieldPool[capacity]; _fieldPools = new IEcsPool[capacity];
_ids = new Dictionary<IDKey, int>(capacity); _ids = new Dictionary<IDKey, int>(capacity);
_count = 0; _count = 0;
} }
public EcsFieldPool<T> GetFieldPool<T>(int id) public EcsPool<T> GetFieldPool<T>(int id)
{ {
if(id < _count) if(id < _count)
return (EcsFieldPool<T>)_fieldPools[id]; return (EcsPool<T>)_fieldPools[id];
_count++; _count++;
if(_fieldPools.Length < _count) if(_fieldPools.Length < _count)
{ {
Array.Resize(ref _fieldPools, _fieldPools.Length << 1); Array.Resize(ref _fieldPools, _fieldPools.Length << 1);
} }
EcsFieldPool<T> newPool = new EcsFieldPool<T>(7); EcsPool<T> newPool = null;// new EcsFieldPool<T>(7);
_fieldPools[id] = newPool; _fieldPools[id] = newPool;
return newPool; return newPool;
} }

122
src/EcsFilter.cs Normal file
View File

@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace DCFApixels.DragonECS
{
public interface IEcsFilter
{
public EcsWorld World { get; }
public int EntitiesCount { get; }
}
public class EcsFilter : IEcsFilter
{
private readonly EcsWorld _source;
private readonly EcsWorld.Mask _mask;
private readonly SparseSet _entities;
private DelayedOp[] _delayedOps;
private int _lockCount;
#region Properties
public EcsWorld World => _source;
public int EntitiesCount => _entities.Count;
#endregion
#region Constrcutors
internal EcsFilter(EcsWorld source, EcsWorld.Mask mask, int capasity)
{
_source = source;
_mask = mask;
_entities = new SparseSet(capasity);
_delayedOps = new DelayedOp[512];
_lockCount = 0;
}
#endregion
internal void Change(int entityID, bool isAdd)
{
if (isAdd)
Add(entityID);
else
Del(entityID);
}
internal void Add(int entityID)
{
if (_lockCount > 0)
AddDelayedOp(entityID, false);
_entities.Add(entityID);
}
internal void Del(int entityID)
{
if (_lockCount > 0)
AddDelayedOp(entityID, false);
_entities.Remove(entityID);
}
private void AddDelayedOp(int entityID, bool isAdd)
{
}
#region GetEnumerator
private void Unlock()
{
#if DEBUG
if (_lockCount <= 0) { throw new Exception($"Invalid lock-unlock balance for {nameof(EcsFilter)}."); }
#endif
_lockCount--;
}
public Enumerator GetEnumerator()
{
_lockCount++;
return new Enumerator(this);
}
#endregion
#region Utils
public ref struct Enumerator
{
readonly EcsFilter _source;
readonly SparseSet _entities;
int _index;
public Enumerator(EcsFilter filter)
{
_source = filter;
_entities = filter._entities;
_index = -1;
}
public int Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entities[_index];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
return ++_index < _entities.Count;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
_source.Unlock();
}
}
struct DelayedOp
{
public bool Added;
public int Entity;
}
#endregion
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 2b523e51b8d5f4c4c969e0ffec1b8b6f guid: c8a9a76233fda6e478be27aed0079971
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -10,8 +10,8 @@ namespace DCFApixels.DragonECS
public class EcsSession public class EcsSession
{ {
private List<IEcsSystem> _allSystems; private List<IEcsProcessor> _allProcessors;
private ReadOnlyCollection<IEcsSystem> _ecsSystemsSealed; private ReadOnlyCollection<IEcsProcessor> _allProcessorsSealed;
private bool _isInit = false; private bool _isInit = false;
private bool _isDestoryed = false; private bool _isDestoryed = false;
@ -20,49 +20,49 @@ namespace DCFApixels.DragonECS
private Dictionary<string, EcsWorld> _worldsDict = new Dictionary<string, EcsWorld>(); private Dictionary<string, EcsWorld> _worldsDict = new Dictionary<string, EcsWorld>();
private List<EcsWorld> _worlds = new List<EcsWorld>(); private List<EcsWorld> _worlds = new List<EcsWorld>();
private Dictionary<Type, IEcsSystemsRunner> _runners; private Dictionary<Type, IEcsProcessorsRunner> _runners;
private Dictionary<Type, IEcsSystemsMessenger> _messengers; private Dictionary<Type, IEcsProcessorsMessenger> _messengers;
private EcsSystemsRunner<_Run> _runRunnerCache; private EcsProcessorsRunner<_Run> _runRunnerCache;
#region Properties #region Properties
public ReadOnlyCollection<IEcsSystem> AllSystems => _ecsSystemsSealed; public ReadOnlyCollection<IEcsProcessor> AllProcessors => _allProcessorsSealed;
#endregion #endregion
#region React Runners/Messengers #region React Runners/Messengers
public EcsSystemsRunner<TDoTag> GetRunner<TDoTag>() public EcsProcessorsRunner<TDoTag> GetRunner<TDoTag>()
where TDoTag : IEcsDoTag where TDoTag : IEcsDoTag
{ {
Type type = typeof(TDoTag); Type type = typeof(TDoTag);
if (_runners.TryGetValue(type, out IEcsSystemsRunner result)) if (_runners.TryGetValue(type, out IEcsProcessorsRunner result))
{ {
return (EcsSystemsRunner<TDoTag>)result; return (EcsProcessorsRunner<TDoTag>)result;
} }
result = new EcsSystemsRunner<TDoTag>(this); result = new EcsProcessorsRunner<TDoTag>(this);
_runners.Add(type, result); _runners.Add(type, result);
return (EcsSystemsRunner<TDoTag>)result; return (EcsProcessorsRunner<TDoTag>)result;
} }
public EcsSystemsMessenger<TMessege> GetMessenger<TMessege>() public EcsProcessorsMessenger<TMessege> GetMessenger<TMessege>()
where TMessege : IEcsMessage where TMessege : IEcsMessage
{ {
Type type = typeof(TMessege); Type type = typeof(TMessege);
if (_messengers.TryGetValue(type, out IEcsSystemsMessenger result)) if (_messengers.TryGetValue(type, out IEcsProcessorsMessenger result))
{ {
return (EcsSystemsMessenger<TMessege>)result; return (EcsProcessorsMessenger<TMessege>)result;
} }
result = new EcsSystemsMessenger<TMessege>(this); result = new EcsProcessorsMessenger<TMessege>(this);
_messengers.Add(type, result); _messengers.Add(type, result);
return (EcsSystemsMessenger<TMessege>)result; return (EcsProcessorsMessenger<TMessege>)result;
} }
#endregion #endregion
#region Configuration #region Configuration
public EcsSession Add(IEcsSystem system) public EcsSession Add(IEcsProcessor system)
{ {
CheckInitForMethod(nameof(AddWorld)); CheckInitForMethod(nameof(AddWorld));
_allSystems.Add(system); _allProcessors.Add(system);
return this; return this;
} }
public EcsSession AddWorld(string name) public EcsSession AddWorld(string name)
@ -79,7 +79,7 @@ namespace DCFApixels.DragonECS
public void Init() public void Init()
{ {
CheckInitForMethod(nameof(Init)); CheckInitForMethod(nameof(Init));
_ecsSystemsSealed = _allSystems.AsReadOnly(); _allProcessorsSealed = _allProcessors.AsReadOnly();
_isInit = true; _isInit = true;
GetRunner<_PreInit>().Run(); GetRunner<_PreInit>().Run();

16
src/EcsTable.cs Normal file
View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace DCFApixels.DragonECS
{
public abstract class EcsTable
{
internal EcsFilter _filter;
public EcsFilter Filter
{
get => _filter;
}
}
}

View File

@ -1,68 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
public abstract class EcsTableCore
{
internal EcsWorld _source;
internal ent entity;
private bool _enabled = false;
[EditorBrowsable(EditorBrowsableState.Never)]
public ent Entity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => entity;
}
internal void Enable()
{
_enabled = true;
}
internal void Disable()
{
_enabled = false;
entity = ent.NULL;
}
internal void MoveToNextEntity()
{
}
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
public ref struct Enumerator
{
private readonly EcsTableCore _source;
public Enumerator(EcsTableCore source)
{
_source = source;
}
public EcsTableCore Current => _source;
public bool MoveNext()
{
throw new NotImplementedException();
}
public void Reset()
{
throw new NotImplementedException();
}
}
}
}

View File

@ -13,10 +13,12 @@ namespace DCFApixels.DragonECS
private byte _id = DEAD_WORLD_ID; private byte _id = DEAD_WORLD_ID;
private Dictionary<Type, IEcsFieldPool> _pools; private Dictionary<Type, IEcsPool> _pools;
private SparseSet _entities = new SparseSet(); private SparseSet _entities = new SparseSet();
private short[] _gens; private short[] _gens;
private byte[] _components;
private List<EcsFilter>[] _filtersByIncludedComponents;
private List<EcsFilter>[] _filtersByExcludedComponents;
//private Dictionary<Type, IEcsEntityTable> _tables; //private Dictionary<Type, IEcsEntityTable> _tables;
@ -28,7 +30,7 @@ namespace DCFApixels.DragonECS
#region Constructors #region Constructors
public EcsWorld() public EcsWorld()
{ {
_pools = new Dictionary<Type, IEcsFieldPool>(); _pools = new Dictionary<Type, IEcsPool>();
_entities = new SparseSet(); _entities = new SparseSet();
} }
#endregion #endregion
@ -41,17 +43,17 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region GetPool #region GetPool
public EcsFieldPool<T> GetPool<T>() public EcsPool<T> GetPool<T>()
{ {
Type type = typeof(T); Type type = typeof(T);
if (_pools.TryGetValue(type, out IEcsFieldPool pool)) if (_pools.TryGetValue(type, out IEcsPool pool))
{ {
return (EcsFieldPool<T>)pool; return (EcsPool<T>)pool;
} }
//pool = new EcsPool<T>(); //pool = new EcsPool<T>();
_pools.Add(type, pool); _pools.Add(type, pool);
return (EcsFieldPool<T>)pool; return (EcsPool<T>)pool;
} }
#endregion #endregion
@ -60,11 +62,10 @@ namespace DCFApixels.DragonECS
{ {
int entityID = _entities.GetFree(); int entityID = _entities.GetFree();
_entities.Normalize(ref _gens); _entities.Normalize(ref _gens);
_entities.Normalize(ref _components);
_gens[entityID]++; _gens[entityID]++;
return new ent(entityID, _gens[entityID], _id, _components[entityID]); return new ent(entityID, _gens[entityID], _id);
} }
#endregion #endregion
@ -75,7 +76,19 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
private void Resize() internal void OnEntityFieldAdd(int entityID, EcsType chaangedField)
{
}
internal void OnEntityFieldDel(int entityID, EcsType chaangedField)
{
}
public class Mask
{ {
} }

8
src/Exceptions.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4b5f469ccf287fd4294a51cc0bec2ba1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,16 @@
using System;
namespace DCFApixels.DragonECS
{
[Serializable]
public class EcsFrameworkException : Exception
{
private const string MESSAGE_SUFFIX = "[DragonECS] ";
public EcsFrameworkException() { }
public EcsFrameworkException(string message) : base(MESSAGE_SUFFIX + message) { }
public EcsFrameworkException(string message, Exception inner) : base(MESSAGE_SUFFIX + message, inner) { }
protected EcsFrameworkException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 37fabd05090af0843a67e6c8046ad374 guid: 309c604e79678054aaa9506dded971da
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,6 +1,6 @@
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsSystem { } public interface IEcsProcessor { }
public interface IEcsDoTag { } public interface IEcsDoTag { }
public struct _PreInit : IEcsDoTag { } public struct _PreInit : IEcsDoTag { }
@ -8,22 +8,23 @@
public struct _Run : IEcsDoTag { } public struct _Run : IEcsDoTag { }
public struct _Destroy : IEcsDoTag { } public struct _Destroy : IEcsDoTag { }
public struct _PostDestroy : IEcsDoTag { } public struct _PostDestroy : IEcsDoTag { }
public interface IEcsDo<TTag> : IEcsSystem public interface IEcsDo<TTag> : IEcsProcessor
where TTag : IEcsDoTag where TTag : IEcsDoTag
{ {
public void Do(EcsSession engine); public void Do(EcsSession session);
} }
public interface IEcsSimpleCycleProcessor :
public interface IEcsMessage { }
public interface IEcsDoMessege<TMessage> : IEcsSystem
where TMessage : IEcsMessage
{
public void Do(EcsSession engine, in TMessage message);
}
public interface IEcsSimpleCycleSystem :
IEcsDo<_Init>, IEcsDo<_Init>,
IEcsDo<_Run>, IEcsDo<_Run>,
IEcsDo<_Destroy> IEcsDo<_Destroy>
{ } { }
public interface IEcsMessage { }
public interface IEcsDoMessege<TMessage> : IEcsProcessor
where TMessage : IEcsMessage
{
public void Do(EcsSession session, in TMessage message);
}
} }

8
src/Pools.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 712e0669e3984c0479174a12942e4db6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -5,31 +5,37 @@ using UnityEngine;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsFieldPool public class EcsPool<T> : IEcsPool
{
public bool Has(int index);
public void Add(int index);
}
public class EcsFieldPool<T> : IEcsFieldPool
{ {
private int _id; private int _id;
private SparseSet _sparseSet; private readonly EcsWorld _source;
private readonly EcsType _type;
private readonly SparseSet _sparseSet;
private T[] _denseItems; private T[] _denseItems;
#region Properites
public EcsWorld World => _source;
public int ID => _id; public int ID => _id;
public EcsType Type => _type;
public ref T this[int index] public ref T this[int index]
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _denseItems[_sparseSet[index]]; get => ref _denseItems[_sparseSet[index]];
} }
public EcsFieldPool(int capacity) #endregion
#region Constructors
public EcsPool(EcsWorld source, EcsType type, int capacity)
{ {
_source = source;
_type = type;
_denseItems = new T[capacity]; _denseItems = new T[capacity];
_sparseSet = new SparseSet(capacity); _sparseSet = new SparseSet(capacity);
} }
#endregion
#region Add/Has/Get/Del
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T Add(int index) public ref T Add(int index)
{ {
@ -44,8 +50,15 @@ namespace DCFApixels.DragonECS
return _sparseSet.Contains(index); return _sparseSet.Contains(index);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Del(int index)
{
_sparseSet.Remove(index);
}
#endregion
#region IEcsFieldPool #region IEcsFieldPool
void IEcsFieldPool.Add(int index) void IEcsPool.Add(int index)
{ {
Add(index); Add(index);
} }

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 1d395d007b9c7c24c9f1f4c09f16e04c guid: ea5495513fa22a54987d75f5cc4fea42
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

52
src/Pools/EcsTagsPool.cs Normal file
View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace DCFApixels.DragonECS
{
public class EcsTagsPool : IEcsPool
{
private int _id;
private readonly EcsWorld _source;
private readonly EcsType _type;
private readonly SparseSet _sparseSet;
#region Properites
public EcsWorld World => _source;
public int ID => _id;
public EcsType Type => _type;
#endregion
#region Constructors
public EcsTagsPool(EcsWorld source, EcsType type, int capacity)
{
_source = source;
_type = type;
_sparseSet = new SparseSet(capacity);
}
#endregion
#region Add/Has/Del
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(int index)
{
_sparseSet.Add(index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Del(int index)
{
_sparseSet.Add(index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Has(int index)
{
return _sparseSet.Contains(index);
}
#endregion
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 01fdcaed7dcb78b4eb4560c068cb0b27
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

18
src/Pools/IEcsPool.cs Normal file
View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DCFApixels.DragonECS
{
public interface IEcsPool
{
public EcsWorld World { get; }
public int ID { get; }
public EcsType Type { get; }
public bool Has(int index);
public void Add(int index);
public void Del(int index);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 82d72237ee8aeb64c9e5455be807896a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,37 +2,37 @@
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsSystemsMessenger public interface IEcsProcessorsMessenger
{ {
public EcsSession Source { get; } public EcsSession Source { get; }
} }
public class EcsSystemsMessenger<TMessage> : IEcsSystemsMessenger public class EcsProcessorsMessenger<TMessage> : IEcsProcessorsMessenger
where TMessage : IEcsMessage where TMessage : IEcsMessage
{ {
private EcsSession _source; private EcsSession _source;
private IEcsDoMessege<TMessage>[] _systems; private IEcsDoMessege<TMessage>[] _targets;
public EcsSession Source => _source; public EcsSession Source => _source;
public IReadOnlyList<IEcsDoMessege<TMessage>> Systems => _systems; public IReadOnlyList<IEcsDoMessege<TMessage>> Systems => _targets;
internal EcsSystemsMessenger(EcsSession source) internal EcsProcessorsMessenger(EcsSession source)
{ {
_source = source; _source = source;
List<IEcsDoMessege<TMessage>> list = new List<IEcsDoMessege<TMessage>>(); List<IEcsDoMessege<TMessage>> list = new List<IEcsDoMessege<TMessage>>();
foreach (var item in _source.AllSystems) foreach (var item in _source.AllProcessors)
{ {
if (item is IEcsDoMessege<TMessage> targetItem) if (item is IEcsDoMessege<TMessage> targetItem)
{ {
list.Add(targetItem); list.Add(targetItem);
} }
} }
_systems = list.ToArray(); _targets = list.ToArray();
} }
public void Send(in TMessage message) public void Send(in TMessage message)
{ {
foreach (var item in _systems) foreach (var item in _targets)
{ {
item.Do(_source, message); item.Do(_source, message);
} }

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5fb1c6ecbb7c4fd4692f54a92b7d198e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,38 +2,38 @@
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsSystemsRunner public interface IEcsProcessorsRunner
{ {
public EcsSession Source { get; } public EcsSession Source { get; }
public void Run(); public void Run();
} }
public class EcsSystemsRunner<TDoTag> : IEcsSystemsRunner public class EcsProcessorsRunner<TDoTag> : IEcsProcessorsRunner
where TDoTag : IEcsDoTag where TDoTag : IEcsDoTag
{ {
private EcsSession _source; private EcsSession _source;
private IEcsDo<TDoTag>[] _systems; private IEcsDo<TDoTag>[] _targets;
public EcsSession Source => _source; public EcsSession Source => _source;
public IReadOnlyList<IEcsDo<TDoTag>> Systems => _systems; public IReadOnlyList<IEcsDo<TDoTag>> Systems => _targets;
internal EcsSystemsRunner(EcsSession source) internal EcsProcessorsRunner(EcsSession source)
{ {
_source = source; _source = source;
List<IEcsDo<TDoTag>> list = new List<IEcsDo<TDoTag>>(); List<IEcsDo<TDoTag>> list = new List<IEcsDo<TDoTag>>();
foreach (var item in _source.AllSystems) foreach (var item in _source.AllProcessors)
{ {
if (item is IEcsDo<TDoTag> targetItem) if (item is IEcsDo<TDoTag> targetItem)
{ {
list.Add(targetItem); list.Add(targetItem);
} }
} }
_systems = list.ToArray(); _targets = list.ToArray();
} }
public void Run() public void Run()
{ {
foreach (var item in _systems) foreach (var item in _targets)
{ {
item.Do(_source); item.Do(_source);
} }

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c8311d8200e99504693251ecdb1877c8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -7,10 +7,10 @@ namespace DCFApixels.DragonECS
{ {
public readonly struct EcsField<T> : IEcsMemberCachePool<EcsField<T>, T> public readonly struct EcsField<T> : IEcsMemberCachePool<EcsField<T>, T>
{ {
private readonly EcsFieldPool<T> _pool; private readonly EcsPool<T> _pool;
private readonly int _poolID; private readonly int _poolID;
public EcsFieldPool<T> Pool => _pool; public EcsPool<T> Pool => _pool;
public int PoolID => _poolID; public int PoolID => _poolID;
private EcsField(int poolID) private EcsField(int poolID)
@ -18,7 +18,7 @@ namespace DCFApixels.DragonECS
_pool = null; _pool = null;
_poolID = poolID; _poolID = poolID;
} }
internal EcsField(EcsFieldPool<T> pool) internal EcsField(EcsPool<T> pool)
{ {
_pool = pool; _pool = pool;
_poolID = pool.ID; _poolID = pool.ID;
@ -32,21 +32,25 @@ namespace DCFApixels.DragonECS
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasValue(int entityID) public bool Has(int entityID)
{ {
return _pool.Has(entityID); return _pool.Has(entityID);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T New(int entityID) public ref T Add(int entityID)
{ {
return ref _pool.Add(entityID); return ref _pool.Add(entityID);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Del(int entityID)
{
_pool.Del(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator EcsField<T>(in int poolID) => new EcsField<T>(poolID); public static implicit operator EcsField<T>(in int poolID) => new EcsField<T>(poolID);
void IEcsMemberCachePool<EcsField<T>, T>.Inject(out EcsField<T> self, EcsFieldPool<T> pool) void IEcsMemberCachePool<EcsField<T>, T>.Inject(out EcsField<T> self, EcsPool<T> pool)
{ {
self = new EcsField<T>(pool); self = new EcsField<T>(pool);
} }
@ -54,10 +58,10 @@ namespace DCFApixels.DragonECS
public readonly struct EcsIncField<T> : IEcsMemberCachePool<EcsIncField<T>, T> public readonly struct EcsIncField<T> : IEcsMemberCachePool<EcsIncField<T>, T>
{ {
private readonly EcsFieldPool<T> _pool; private readonly EcsPool<T> _pool;
private readonly int _poolID; private readonly int _poolID;
public EcsFieldPool<T> Pool => _pool; public EcsPool<T> Pool => _pool;
public int PoolID => _poolID; public int PoolID => _poolID;
private EcsIncField(int poolID) private EcsIncField(int poolID)
@ -65,7 +69,7 @@ namespace DCFApixels.DragonECS
_pool = null; _pool = null;
_poolID = poolID; _poolID = poolID;
} }
internal EcsIncField(EcsFieldPool<T> pool) internal EcsIncField(EcsPool<T> pool)
{ {
_pool = pool; _pool = pool;
_poolID = pool.ID; _poolID = pool.ID;
@ -78,26 +82,52 @@ namespace DCFApixels.DragonECS
get => ref _pool[entityID]; get => ref _pool[entityID];
} }
[EditorBrowsable(EditorBrowsableState.Never)]
public void Del(int entityID)
{
_pool.Del(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator EcsIncField<T>(in int poolID) => new EcsIncField<T>(poolID); public static implicit operator EcsIncField<T>(in int poolID) => new EcsIncField<T>(poolID);
void IEcsMemberCachePool<EcsIncField<T>, T>.Inject(out EcsIncField<T> self, EcsFieldPool<T> pool) void IEcsMemberCachePool<EcsIncField<T>, T>.Inject(out EcsIncField<T> self, EcsPool<T> pool)
{ {
self = new EcsIncField<T>(pool); self = new EcsIncField<T>(pool);
} }
} }
public struct EcsExcField<T> : IEcsTableMember public struct EcsExcField<T> : IEcsMemberCachePool<EcsExcField<T>, T>
{ {
private readonly EcsPool<T> _pool;
private readonly int _poolID; private readonly int _poolID;
public EcsPool<T> Pool => _pool;
public int PoolID => _poolID; public int PoolID => _poolID;
private EcsExcField(int poolID) private EcsExcField(int poolID)
{ {
_pool = null;
_poolID = poolID; _poolID = poolID;
} }
internal EcsExcField(EcsPool<T> pool)
{
_pool = pool;
_poolID = pool.ID;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T Add(int entityID)
{
return ref _pool.Add(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator EcsExcField<T>(in int poolID) => new EcsExcField<T>(poolID); public static implicit operator EcsExcField<T>(in int poolID) => new EcsExcField<T>(poolID);
void IEcsMemberCachePool<EcsExcField<T>, T>.Inject(out EcsExcField<T> self, EcsPool<T> pool)
{
self = new EcsExcField<T>(pool);
}
} }
} }

View File

@ -5,12 +5,12 @@ using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public readonly struct EcsTag<T> : IEcsMemberCachePool<EcsTag<T>, T> public readonly struct EcsTag : IEcsMemberCachePool<EcsTag, TagType>
{ {
private readonly EcsFieldPool<T> _pool; private readonly EcsPool<TagType> _pool;
private readonly int _poolID; private readonly int _poolID;
public EcsFieldPool<T> Pool => _pool; public EcsPool<TagType> Pool => _pool;
public int PoolID => _poolID; public int PoolID => _poolID;
private EcsTag(int poolID) private EcsTag(int poolID)
@ -18,7 +18,7 @@ namespace DCFApixels.DragonECS
_pool = null; _pool = null;
_poolID = poolID; _poolID = poolID;
} }
internal EcsTag(EcsFieldPool<T> pool) internal EcsTag(EcsPool<TagType> pool)
{ {
_pool = pool; _pool = pool;
_poolID = pool.ID; _poolID = pool.ID;
@ -30,37 +30,65 @@ namespace DCFApixels.DragonECS
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator EcsTag<T>(in int poolID) => new EcsTag<T>(poolID); public static implicit operator EcsTag(in int poolID) => new EcsTag(poolID);
void IEcsMemberCachePool<EcsTag<T>, T>.Inject(out EcsTag<T> self, EcsFieldPool<T> pool) void IEcsMemberCachePool<EcsTag, TagType>.Inject(out EcsTag self, EcsPool<TagType> pool)
{ {
self = new EcsTag<T>(pool); self = new EcsTag(pool);
} }
} }
public readonly struct EcsIncTag<T> : IEcsTableMember public readonly struct EcsIncTag : IEcsMemberCachePool<EcsIncTag, TagType>
{ {
private readonly EcsPool<TagType> _pool;
private readonly int _poolID; private readonly int _poolID;
public EcsPool<TagType> Pool => _pool;
public int PoolID => _poolID; public int PoolID => _poolID;
private EcsIncTag(int poolID) private EcsIncTag(int poolID)
{ {
_pool = null;
_poolID = poolID; _poolID = poolID;
} }
internal EcsIncTag(EcsPool<TagType> pool)
{
_pool = pool;
_poolID = pool.ID;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator EcsIncTag<T>(in int poolID) => new EcsIncTag<T>(poolID); public static implicit operator EcsIncTag(in int poolID) => new EcsIncTag(poolID);
}
public readonly struct EcsExcTag<T> : IEcsTableMember void IEcsMemberCachePool<EcsIncTag, TagType>.Inject(out EcsIncTag self, EcsPool<TagType> pool)
{ {
self = new EcsIncTag(pool);
}
}
public readonly struct EcsExcTag : IEcsMemberCachePool<EcsExcTag, TagType>
{
private readonly EcsPool<TagType> _pool;
private readonly int _poolID; private readonly int _poolID;
public EcsPool<TagType> Pool => _pool;
public int PoolID => _poolID; public int PoolID => _poolID;
private EcsExcTag(int poolID) private EcsExcTag(int poolID)
{ {
_pool = null;
_poolID = poolID; _poolID = poolID;
} }
internal EcsExcTag(EcsPool<TagType> pool)
{
_pool = pool;
_poolID = pool.ID;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator EcsExcTag<T>(in int poolID) => new EcsExcTag<T>(poolID); public static implicit operator EcsExcTag(in int poolID) => new EcsExcTag(poolID);
void IEcsMemberCachePool<EcsExcTag, TagType>.Inject(out EcsExcTag self, EcsPool<TagType> pool)
{
self = new EcsExcTag(pool);
}
} }
} }

View File

@ -10,7 +10,7 @@ namespace DCFApixels.DragonECS
public interface IEcsMemberCachePool<TSelf, T> : IEcsTableMember public interface IEcsMemberCachePool<TSelf, T> : IEcsTableMember
where TSelf: struct, IEcsTableMember where TSelf: struct, IEcsTableMember
{ {
public EcsFieldPool<T> Pool { get; } public EcsPool<T> Pool { get; }
public void Inject(out TSelf self, EcsFieldPool<T> pool); public void Inject(out TSelf self, EcsPool<T> pool);
} }
} }

View File

@ -6,6 +6,5 @@ using System.Threading.Tasks;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsTable { } public struct TagType { }
} }

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 73f5e1a2319dcb644818c9bd14dcbc5d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,12 +0,0 @@
namespace DCFApixels.DragonECS
{
public class ComponentTypeID
{
protected static int _incerement = 0;
}
public class TypeID<T> : ComponentTypeID
where T : struct
{
public static readonly int id = _incerement++;
}
}

77
src/Utils/EcsType.cs Normal file
View File

@ -0,0 +1,77 @@
using System.Collections.Generic;
using System;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
public class EcsType : IEquatable<EcsType>
{
private static int _idIncrement = 0;
//свободно еще 4 байта, возможно можно выделить под айдишники
private string _name;
private int _uniqueID;
#region Constructors
internal EcsType(string name)
{
_name = name;
_uniqueID = _idIncrement++;
#if DEBUG
if (_idIncrement == 0)
{
throw new EcsFrameworkException($"The maximum number of identifiers is allocated. Max:{uint.MaxValue}");
}
#endif
}
#endregion
#region Equals
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
return obj is EcsType key && _name == key._name;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(EcsType other)
{
return _uniqueID == other._uniqueID;
}
#endregion
#region GetHashCode/ToString
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => _uniqueID;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() => "EcsType." + _name;
#endregion
#region operators
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(in EcsType left, in EcsType right) => left.Equals(right);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(in EcsType left, in EcsType right) => !left.Equals(right);
#endregion
}
public static class EcsTypeMap
{
private static Dictionary<string, EcsType> _types = new Dictionary<string, EcsType>(256);
public static EcsType GetEcsType(string name)
{
if (_types.TryGetValue(name, out EcsType type))
return type;
type = new EcsType(name);
_types.Add(name, type);
return type;
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DCFApixels.DragonECS
{
public class entityArraysPool
{
//public int[]
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 50cf3219cac6cc84faf71a80230f8539
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -14,6 +14,7 @@ namespace DCFApixels.DragonECS
// id - 32 bits // id - 32 bits
// gen - 16 bits // gen - 16 bits
// world - 8 bits // world - 8 bits
// empty - 8 bits
public readonly long _full; public readonly long _full;
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]
@ -48,12 +49,12 @@ namespace DCFApixels.DragonECS
} }
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]
public ent(int id, short gen, byte world, byte com) public ent(int id, short gen, byte world)
{ {
_full = ((long)id) << 32; _full = ((long)id) << 32;
_full += ((long)gen) << 16; _full += ((long)gen) << 16;
_full += ((long)(++world)) << 8; // сдвиг айдишников + 1 _full += ((long)(++world)) << 8; // сдвиг айдишников + 1
_full += com; //_full += ...;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]