This commit is contained in:
Mikhail 2023-03-12 20:45:18 +08:00
parent 73090da11a
commit 5371c0473c
24 changed files with 325 additions and 70 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace DCFApixels.DragonECS
{
public interface IEcsInject<T> : IEcsProcessor
public interface IEcsInject<T> : IEcsSystem
{
public void Inject(T obj);
}

View File

@ -1,18 +1,18 @@
namespace DCFApixels.DragonECS
{
public interface IEcsPreInitSystem : IEcsProcessor
public interface IEcsPreInitSystem : IEcsSystem
{
public void PreInit(EcsSession session);
}
public interface IEcsInitSystem : IEcsProcessor
public interface IEcsInitSystem : IEcsSystem
{
public void Init(EcsSession session);
}
public interface IEcsRunSystem : IEcsProcessor
public interface IEcsRunSystem : IEcsSystem
{
public void Run(EcsSession session);
}
public interface IEcsDestroySystem : IEcsProcessor
public interface IEcsDestroySystem : IEcsSystem
{
public void Destroy(EcsSession session);
}

View File

@ -110,11 +110,13 @@ namespace DCFApixels.DragonECS
internal static int capacity = 512;
protected BakedMask(int[] inc, int[] exc, Mask mask) : base(inc, exc, mask) { }
}
public sealed class BakedMask<TWorldArchetype, TMask> : BakedMask<TWorldArchetype>
where TWorldArchetype : IWorldArchetype
where TMask : MaskSingleton<TMask>
where TMask : Mask, new()
{
public static readonly int uniqueID;
@ -127,6 +129,7 @@ namespace DCFApixels.DragonECS
#endif
if (increment > capacity)
capacity <<= 1;
_instance = new BakedMask<TWorldArchetype, TMask>();
}
@ -136,19 +139,17 @@ namespace DCFApixels.DragonECS
MaskSingleton<TMask>.Instance)
{ }
private static readonly BakedMask<TWorldArchetype, TMask> _instance = new BakedMask<TWorldArchetype, TMask>();
private static readonly BakedMask<TWorldArchetype, TMask> _instance;
public static BakedMask<TWorldArchetype, TMask> Instance
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _instance;
}
internal override int UniqueID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => uniqueID;
}
internal override Type WorldArchetypeType
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -164,47 +165,43 @@ namespace DCFApixels.DragonECS
internal abstract int[] MakeExc<TWorldArchetype>() where TWorldArchetype : IWorldArchetype;
public abstract BakedMask GetBaked<TWorldArchetype>() where TWorldArchetype : IWorldArchetype;
}
public abstract class MaskSingleton<TSelf> : Mask
where TSelf : Mask
public abstract class MaskSingleton<TSelf>
where TSelf : Mask, new()
{
protected static TSelf _instance;
protected static TSelf _instance = new TSelf();
internal static TSelf Instance
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _instance;
}
}
public class Mask<TInc> : MaskSingleton<Mask<TInc>>
where TInc : struct, IInc
public class Mask<TInc> : Mask where TInc : struct, IInc
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override int[] MakeInc<TWorldArchetype>() => new TInc().GetComponentsIDs<TWorldArchetype>();
internal override int[] MakeInc<TWorldArchetype>() => new TInc().GetComponentsIDs<TWorldArchetype>().Sort();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override int[] MakeExc<TWorldArchetype>() => Array.Empty<int>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override BakedMask GetBaked<TWorldArchetype>()
{
return BakedMask<TWorldArchetype, Mask<TInc>>.Instance;
}
static Mask() { _instance = new Mask<TInc>(); }
public override BakedMask GetBaked<TWorldArchetype>() => BakedMask<TWorldArchetype, Mask<TInc>>.Instance;
}
public class Mask<TInc, TExc> : MaskSingleton<Mask<TInc, TExc>>
where TInc : struct, IInc
where TExc : struct, IExc
public class Mask<TInc, TExc> : Mask where TInc : struct, IInc where TExc : struct, IExc
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override int[] MakeInc<TWorldArchetype>() => new TInc().GetComponentsIDs<TWorldArchetype>();
internal override int[] MakeInc<TWorldArchetype>() => new TInc().GetComponentsIDs<TWorldArchetype>().Sort();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal override int[] MakeExc<TWorldArchetype>() => new TExc().GetComponentsIDs<TWorldArchetype>();
internal override int[] MakeExc<TWorldArchetype>() => new TExc().GetComponentsIDs<TWorldArchetype>().Sort();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override BakedMask GetBaked<TWorldArchetype>()
{
return BakedMask<TWorldArchetype, Mask<TInc, TExc>>.Instance;
}
public override BakedMask GetBaked<TWorldArchetype>() => BakedMask<TWorldArchetype, Mask<TInc, TExc>>.Instance;
}
static Mask() { _instance = new Mask<TInc, TExc>(); }
internal static class ArrayExt
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T[] Sort<T>(this T[] self)
{
Array.Sort(self);
return self;
}
}
#endregion

View File

@ -40,8 +40,8 @@ namespace DCFApixels.DragonECS
private int _id;
private readonly List<IEcsProcessor> _allSystems;
private ReadOnlyCollection<IEcsProcessor> _allSystemsSealed;
private readonly List<IEcsSystem> _allSystems;
private ReadOnlyCollection<IEcsSystem> _allSystemsSealed;
private bool _isInit = false;
private bool _isDestoryed = false;
@ -53,19 +53,19 @@ namespace DCFApixels.DragonECS
private readonly EcsWorldMap _worldMap;
#region Properties
public ReadOnlyCollection<IEcsProcessor> AllProcessors => _allSystemsSealed;
public ReadOnlyCollection<IEcsSystem> AllProcessors => _allSystemsSealed;
#endregion
public EcsSession()
{
_allSystems = new List<IEcsProcessor>(128);
_allSystems = new List<IEcsSystem>(128);
_runners = new Dictionary<Type, IEcsRunner>();
_worldMap = new EcsWorldMap();
}
#region React Runners/Messengers
public T GetRunner<T>() where T : IEcsProcessor
#region Runners
public T GetRunner<T>() where T : IEcsSystem
{
Type type = typeof(T);
if (_runners.TryGetValue(type, out IEcsRunner result))
@ -79,7 +79,7 @@ namespace DCFApixels.DragonECS
#endregion
#region Configuration
public EcsSession Add(IEcsProcessor system)
public EcsSession Add(IEcsSystem system)
{
CheckInitForMethod(nameof(AddWorld));
_allSystems.Add(system);
@ -92,7 +92,6 @@ namespace DCFApixels.DragonECS
_worldMap.Add(world, name);
return this;
}
#endregion
#region LifeCycle
@ -155,8 +154,6 @@ namespace DCFApixels.DragonECS
}
#endregion
#region Utils
#endregion
}

View File

@ -21,7 +21,8 @@ namespace DCFApixels.DragonECS
#endregion
public EcsPool<T> GetPool<T>() where T : struct;
public EcsFilter GetFilter<TMask>() where TMask : MaskSingleton<TMask>;
public EcsFilter GetFilter<TInc>() where TInc : struct, IInc;
public EcsFilter GetFilter<TInc, TExc>() where TInc : struct, IInc where TExc : struct, IExc;
public ent NewEntity();
public void Destroy();
@ -74,8 +75,11 @@ namespace DCFApixels.DragonECS
{
_entityDispenser = new IntDispenser();
_pools = new IEcsPool[512];
_gens = new short[512];
_filters = new EcsFilter[64];
_entities = new EcsGroup(this, 512);
_filtersByIncludedComponents = new List<EcsFilter>[16];
_filtersByExcludedComponents = new List<EcsFilter>[16];
}
#endregion
@ -100,7 +104,16 @@ namespace DCFApixels.DragonECS
#endregion
#region GetFilter
public EcsFilter GetFilter<TMask>() where TMask : MaskSingleton<TMask>
public EcsFilter GetFilter<TInc>() where TInc : struct, IInc
{
return GetFilterInternal<Mask<TInc>>();
}
public EcsFilter GetFilter<TInc, TExc>() where TInc : struct, IInc where TExc : struct, IExc
{
return GetFilterInternal<Mask<TInc, TExc>>();
}
private EcsFilter GetFilterInternal<TMask>() where TMask : Mask, new()
{
var bakedmask = BakedMask<TArchetype, TMask>.Instance;
@ -111,10 +124,41 @@ namespace DCFApixels.DragonECS
if (_filters[bakedmask.UniqueID] == null)
{
_filters[bakedmask.UniqueID] = new EcsFilter(this, bakedmask, 512);
_filters[bakedmask.UniqueID] = NewFilter(bakedmask);
}
return _filters[bakedmask.UniqueID];
}
private EcsFilter NewFilter(BakedMask mask, int capacirty = 512)
{
var newFilter = new EcsFilter(this, mask, capacirty);
for (int i = 0; i < mask.IncCount; i++)
{
int poolid = mask.Inc[i];
var list = _filtersByIncludedComponents[poolid];
if (list == null)
{
list = new List<EcsFilter>(8);
_filtersByIncludedComponents[poolid] = list;
}
list.Add(newFilter);
}
for (int i = 0; i < mask.ExcCount; i++)
{
int poolid = mask.Exc[i];
var list = _filtersByExcludedComponents[poolid];
if (list == null)
{
list = new List<EcsFilter>(8);
_filtersByExcludedComponents[poolid] = list;
}
list.Add(newFilter);
}
return newFilter;
}
#endregion
#region IsMaskCompatible/IsMaskCompatibleWithout

View File

@ -16,13 +16,19 @@ namespace DCFApixels.DragonECS
this.interfaceType = interfaceType;
this.filter = filter;
}
public EcsRunnerFilterAttribute(object filter)
{
interfaceType = null;
this.filter = filter;
}
}
public interface IEcsProcessor { }
public interface IEcsSystem { }
public static class IEcsProcessorExtensions
{
public static bool IsRunner(this IEcsProcessor self)
public static bool IsRunner(this IEcsSystem self)
{
return self is IEcsRunner;
}
@ -59,7 +65,7 @@ namespace DCFApixels.DragonECS
_runnerTypes = new Dictionary<Guid, Type>();
foreach (var item in newRunnerTypes)
{
Type intrf = item.GetInterfaces().Where(o => o != typeof(IEcsRunner) && o != typeof(IEcsProcessor)).First(); //TODO оптимизировать это место
Type intrf = item.GetInterfaces().Where(o => o != typeof(IEcsRunner) && o != typeof(IEcsSystem)).First(); //TODO оптимизировать это место
_runnerTypes.Add(intrf.GUID, item);
}
@ -84,7 +90,7 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InitFor<TInterface>() where TInterface : IEcsProcessor
public static void InitFor<TInterface>() where TInterface : IEcsSystem
{
Type interfaceType = typeof(TInterface);
Type nonGenericInterfaceType = interfaceType;
@ -109,8 +115,8 @@ namespace DCFApixels.DragonECS
public interface IEcsRunner { }
public abstract class EcsRunner<TInterface> : IEcsProcessor, IEcsRunner
where TInterface : IEcsProcessor
public abstract class EcsRunner<TInterface> : IEcsSystem, IEcsRunner
where TInterface : IEcsSystem
{
internal static void Init(Type subclass)
{
@ -128,19 +134,19 @@ namespace DCFApixels.DragonECS
{
throw new ArgumentException($"{typeof(TInterface).FullName} is not interface");
}
if (interfaces.Length != 1 || interfaces[0] != typeof(IEcsProcessor))
if (interfaces.Length != 1 || interfaces[0] != typeof(IEcsSystem))
{
throw new ArgumentException($"{typeof(TInterface).FullName} does not directly inherit the {nameof(IEcsProcessor)} interface");
throw new ArgumentException($"{typeof(TInterface).FullName} does not directly inherit the {nameof(IEcsSystem)} interface");
}
#endif
_subclass = subclass;
}
public static TInterface Instantiate(IEnumerable<IEcsProcessor> targets, object filter)
public static TInterface Instantiate(IEnumerable<IEcsSystem> targets, object filter)
{
Type interfaceType = typeof(TInterface);
IEnumerable<IEcsProcessor> newTargets;
IEnumerable<IEcsSystem> newTargets;
if (filter != null)
{
@ -163,7 +169,7 @@ namespace DCFApixels.DragonECS
return Instantiate(newTargets.Select(o => (TInterface)o).ToArray());
}
public static TInterface Instantiate(IEnumerable<IEcsProcessor> targets)
public static TInterface Instantiate(IEnumerable<IEcsSystem> targets)
{
return Instantiate(targets.Where(o => o is TInterface).Select(o => (TInterface)o).ToArray());
}
@ -173,7 +179,7 @@ namespace DCFApixels.DragonECS
EcsRunnerActivator.InitFor<TInterface>();
var instance = (EcsRunner<TInterface>)Activator.CreateInstance(_subclass);
return (TInterface)(IEcsProcessor)instance.Set(targets);
return (TInterface)(IEcsSystem)instance.Set(targets);
}
private static Type _subclass;

8
test/Components.meta Normal file
View File

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

View File

@ -0,0 +1,7 @@

namespace DCFApixels.DragonECS
{
public struct EnemyTag
{
}
}

View File

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

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DCFApixels.DragonECS
{
public struct PlayerTag
{
}
}

View File

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

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public struct TransfromCom
{
public Vector3 position;
}
}

View File

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

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public struct Velocity
{
public Vector3 value;
}
}

View File

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

14
test/Components/View.cs Normal file
View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public struct View
{
public Transform Ref;
}
}

View File

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

View File

@ -3,10 +3,14 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DCFApixels.DragonECS
{
[Serializable]
public class SharedData
{
public Transform view1;
public Transform view2;
}
}

View File

@ -9,13 +9,18 @@ namespace DCFApixels.DragonECS
{
private EcsSession _ecsSession;
[SerializeField]
public SharedData _data = new SharedData();
private void Start()
{
_ecsSession = new EcsSession()
.Add(new TestSystem())
.Inject(_data)
.AddWorld(new EcsWorld<DefaultWorld>())
.Add(new TestSystem())
.Add(new VelocitySystem())
.Add(new ViewSystem())
.Init();
}

View File

@ -1,28 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public class TestSystem : IEcsInject<SharedData>, IEcsSimpleCycleSystem
public class TestSystem : IEcsInject<SharedData>, IEcsInject<EcsWorldMap>, IEcsInitSystem
{
private SharedData _sharedData;
private EcsWorld<DefaultWorld> _world;
public void Inject(SharedData obj) => _sharedData = obj;
public void Inject(EcsWorldMap obj) { _world = obj.Get<DefaultWorld>(); }
public void Init(EcsSession session)
{
Debug.Log("Init");
}
public void Run(EcsSession session)
{
Debug.Log("Run");
}
public void Destroy(EcsSession session)
{
Debug.Log("Destroy");
var x1 = _world.GetFilter<Inc<TransfromCom, Velocity>>();
var x2 = _world.GetFilter<Inc<TransfromCom, View>>();
var x3 = _world.GetFilter<Inc<TransfromCom, Velocity>>();
var x4 = _world.GetFilter<Inc<TransfromCom, Velocity>>();
var x5 = _world.GetFilter<Inc<Velocity, TransfromCom>>();
int has1 = x1.GetHashCode();
int has2 = x2.GetHashCode();
int has3 = x3.GetHashCode();
int has4 = x4.GetHashCode();
int has5 = x5.GetHashCode();
var e = _world.NewEntity();
e.Write<TransfromCom>().position = Vector3.zero;
e.Write<View>().Ref = _sharedData.view1;
e.Write<EnemyTag>();
e = _world.NewEntity();
e.Write<TransfromCom>().position = Vector3.zero;
e.Write<Velocity>().value = Vector3.one;
e.Write<View>().Ref = _sharedData.view2;
e.Write<PlayerTag>();
}
}
}

21
test/VelocitySystem.cs Normal file
View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public class VelocitySystem : IEcsInject<EcsWorldMap>, IEcsRunSystem
{
private EcsWorld<DefaultWorld> _world;
public void Inject(EcsWorldMap obj) { _world = obj.Get<DefaultWorld>(); }
public void Run(EcsSession session)
{
foreach (var item in _world.GetFilter<Inc<TransfromCom, Velocity>>().Entities)
{
item.Write<TransfromCom>().position += item.Read<Velocity>().value * Time.deltaTime;
}
}
}
}

View File

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

22
test/ViewSystem.cs Normal file
View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace DCFApixels.DragonECS
{
public class ViewSystem : IEcsInject<EcsWorldMap>, IEcsRunSystem
{
private EcsWorld<DefaultWorld> _world;
public void Inject(EcsWorldMap obj) { _world = obj.Get<DefaultWorld>(); }
public void Run(EcsSession session)
{
foreach (var item in _world.GetFilter<Inc<TransfromCom, View>>().Entities)
{
item.Write<View>().Ref.position = item.Read<TransfromCom>().position;
}
}
}
}

11
test/ViewSystem.cs.meta Normal file
View File

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