From d873c114b8c6f8a18a47e25f1eaff87701540ac4 Mon Sep 17 00:00:00 2001 From: sebas77 Date: Sun, 5 May 2019 19:09:33 +0100 Subject: [PATCH] update svelto ECS 2.8 --- Svelto.Common | 2 +- .../Components/ExtensionMethods.cs | 5 +- .../Components/ExtensionMethodsUECS.cs | 69 ++++++++++++ Svelto.ECS/DataStructures/GroupsList.cs | 106 ------------------ .../DataStructures/TypeSafeDictionary.cs | 4 +- Svelto.ECS/Dispatcher/DispatchOnSet.cs | 35 ++---- Svelto.ECS/EnginesRoot.Entities.cs | 2 +- Svelto.ECS/ExecuteOnEntitiesDB.cs | 12 +- .../Extensions/Unity/SveltoGUIHelper.cs | 15 +-- .../GenericentityStreamConsumerFactory.cs | 4 +- Svelto.ECS/IEntitiesDB.cs | 7 +- Svelto.ECS/IReactOnSwap.cs | 2 +- 12 files changed, 99 insertions(+), 164 deletions(-) create mode 100644 Svelto.ECS.Components/Components/ExtensionMethodsUECS.cs delete mode 100644 Svelto.ECS/DataStructures/GroupsList.cs diff --git a/Svelto.Common b/Svelto.Common index b43c986..09e2b89 160000 --- a/Svelto.Common +++ b/Svelto.Common @@ -1 +1 @@ -Subproject commit b43c9866240e40da0caacdf0f717b37d6e5a6cf0 +Subproject commit 09e2b8967e24eb94c701ceb8f4907e45afb64d6d diff --git a/Svelto.ECS.Components/Components/ExtensionMethods.cs b/Svelto.ECS.Components/Components/ExtensionMethods.cs index b3c5b2f..ea70e97 100644 --- a/Svelto.ECS.Components/Components/ExtensionMethods.cs +++ b/Svelto.ECS.Components/Components/ExtensionMethods.cs @@ -92,10 +92,9 @@ public static partial class ExtensionMethods float wz = rotation.w * z; return new ECSVector3((1F - (yy + zz)) * point.x + (xy - wz) * point.y + (xz + wy) * point.z, - (xy + wz) * point.x + (1F - (xx + zz)) * point.y + (yz - wx) * point.z, - (xz - wy) * point.x + (yz + wx) * point.y + (1F - (xx + yy)) * point.z); + (xy + wz) * point.x + (1F - (xx + zz)) * point.y + (yz - wx) * point.z, + (xz - wy) * point.x + (yz + wx) * point.y + (1F - (xx + yy)) * point.z); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Swap(ref this ECSVector3 vector, ref ECSVector3 vectorS) { diff --git a/Svelto.ECS.Components/Components/ExtensionMethodsUECS.cs b/Svelto.ECS.Components/Components/ExtensionMethodsUECS.cs new file mode 100644 index 0000000..71f17a3 --- /dev/null +++ b/Svelto.ECS.Components/Components/ExtensionMethodsUECS.cs @@ -0,0 +1,69 @@ +#if UNITY_MATHEMATICS +using Svelto.ECS.Components; +using Unity.Mathematics; + +public static partial class ExtensionMethods +{ + public static float3 ToFloat3(in this ECSVector3 vector) + { + return new float3(vector.x, vector.y, vector.z); + } + + public static quaternion ToQuaternion(in this ECSVector4 vector) + { + return new quaternion(vector.x, vector.y, vector.z, vector.w); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Mul(ref this float3 vector1, float value) + { + vector1.x *= value; + vector1.y *= value; + vector1.z *= value; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Div(ref this float3 vector1, float value) + { + vector1.x /= value; + vector1.y /= value; + vector1.z /= value; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ProjectOnPlane(ref this float3 vector, in float3 planeNormal) + { + var num1 = math.dot(planeNormal,planeNormal); + if ((double) num1 < (double) Mathf.Epsilon) + return; + var num2 = math.dot(vector,planeNormal) / num1; + + vector.x -= planeNormal.x * num2; + vector.y -= planeNormal.y * num2; + vector.z -= planeNormal.z * num2; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Normalize(ref this float3 x) + { + x.Mul(math.rsqrt(math.dot(x,x))); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NormalizeSafe(ref this float3 x) + { + var len = math.dot(x,x); + x = len > math.FLT_MIN_NORMAL ? x * math.rsqrt(len) : float3.zero; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Add(ref this float3 a, in float3 b) + { + a.x += b.x; + a.y += b.y; + a.z += b.z; + } + +} + +#endif \ No newline at end of file diff --git a/Svelto.ECS/DataStructures/GroupsList.cs b/Svelto.ECS/DataStructures/GroupsList.cs deleted file mode 100644 index 932df98..0000000 --- a/Svelto.ECS/DataStructures/GroupsList.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using Svelto.DataStructures; - -namespace Svelto.ECS.Internal -{ - class GroupList - { - public int Count => _list.Count; - - public GroupList() - { - _list = new FasterList(); - } - - public ref T this[uint index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => ref _list[index]; - } - - public GroupListEnumerator GetEnumerator() - { - return new GroupListEnumerator(_list.ToArrayFast(), _list.Count); - } - - public T GetOrAdd(uint location) where TC:class, T, new() - { - if (location >= _list.Count || this[location] == null) - { - var item = new TC(); - _list.Add(location, item); - return item; - } - - return this[location]; - } - - public void Clear() { _list.Clear(); } - public void FastClear() { _list.ResetCountToAvoidGC(); } - - public bool TryGetValue(uint index, out T value) - { - if (default(T) == null) - { - if (index < _list.Count && this[index] != null) - { - value = this[index]; - return true; - } - - value = default(T); - return false; - } - else - { - if (index < _list.Count) - { - value = this[index]; - return true; - } - - value = default(T); - return false; - } - } - - public void Add(uint location, T value) { _list.Add(location, value); } - - readonly FasterList _list; - - } - - public struct GroupListEnumerator - { - public ref readonly T Current => ref _buffer[_counter -1]; - public uint index => _counter - 1; - - public GroupListEnumerator(T[] buffer, int size) - { - _size = size; - _counter = 0; - _buffer = buffer; - } - - public bool MoveNext() - { - if (default(T) == null) - { - while (_counter < _size) - { - if (_buffer[_counter] == null) - _counter++; - else - break; - } - } - - return _counter++ < _size; - } - - readonly T[] _buffer; - uint _counter; - readonly int _size; - } -} \ No newline at end of file diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index 83b78f4..e30b0f2 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -38,7 +38,7 @@ namespace Svelto.ECS.Internal static readonly string _typeName = _type.Name; static readonly bool HasEgid = typeof(INeedEGID).IsAssignableFrom(_type); - public TypeSafeDictionary(uint size) : base((uint) size) { } + public TypeSafeDictionary(uint size) : base(size) { } public TypeSafeDictionary() {} public void AddEntitiesFromDictionary(ITypeSafeDictionary entitiesToSubmit, uint groupId) @@ -203,7 +203,7 @@ namespace Svelto.ECS.Internal try { using (profiler.Sample(entityViewsEngines[i], _typeName)) - (entityViewsEngines[i] as IReactOnSwap).MovedFrom(ref entity, previousGroup.Value); + (entityViewsEngines[i] as IReactOnSwap).MovedFrom(ref entity); } catch (Exception e) { diff --git a/Svelto.ECS/Dispatcher/DispatchOnSet.cs b/Svelto.ECS/Dispatcher/DispatchOnSet.cs index b0f2deb..120ce32 100644 --- a/Svelto.ECS/Dispatcher/DispatchOnSet.cs +++ b/Svelto.ECS/Dispatcher/DispatchOnSet.cs @@ -18,10 +18,9 @@ namespace Svelto.ECS { _value = value; - _subscribers.Invoke(_senderID, value); + if(_paused == false) + _subscribers.Invoke(_senderID, value); } - - get => _value; } public void NotifyOnValueSet(Action action) @@ -34,33 +33,13 @@ namespace Svelto.ECS _subscribers -= action; } + public void PauseNotify() { _paused = true; } + public void ResumeNotify() { _paused = false; } + protected T _value; - internal EGID _senderID; + readonly EGID _senderID; WeakEvent _subscribers; - } - - public static class DispatchExtensions - { - public static DispatchOnSet Setup(DispatchOnSet dispatcher, EGID entity) where T : struct - { - if (dispatcher == null) - dispatcher = new DispatchOnSet(entity); - else - dispatcher._senderID = entity; - - return dispatcher; - } - - public static DispatchOnChange Setup(DispatchOnChange dispatcher, EGID entity) - where T : struct, IEquatable - { - if (dispatcher == null) - dispatcher = new DispatchOnChange(entity); - else - dispatcher._senderID = entity; - - return dispatcher; - } + bool _paused; } } diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 76355be..e25591f 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -57,7 +57,7 @@ namespace Svelto.ECS /// public IEntityStreamConsumerFactory GenerateConsumerFactory() { - return new GenericEntityStreamConsumerFactory(new DataStructures.WeakReference(this)); + return new GenericentityStreamConsumerFactory(new DataStructures.WeakReference(this)); } public IEntityFactory GenerateEntityFactory() diff --git a/Svelto.ECS/ExecuteOnEntitiesDB.cs b/Svelto.ECS/ExecuteOnEntitiesDB.cs index 9b9cb2e..8b7d507 100644 --- a/Svelto.ECS/ExecuteOnEntitiesDB.cs +++ b/Svelto.ECS/ExecuteOnEntitiesDB.cs @@ -5,7 +5,7 @@ namespace Svelto.ECS.Internal partial class EntitiesDB { public void ExecuteOnAllEntities(Action action) - where T : struct, IEntityStruct + where T : struct, IEntityStruct { var type = typeof(T); @@ -13,11 +13,10 @@ namespace Svelto.ECS.Internal { foreach (var pair in dic) { - var entities = - (pair.Value as TypeSafeDictionary).GetValuesArray(out var innerCount); + var entities = (pair.Value as TypeSafeDictionary).GetValuesArray(out var innerCount); - if (innerCount > 0) - action(entities, new ExclusiveGroup.ExclusiveGroupStruct(pair.Key), innerCount, this); + if (innerCount > 0) + action(entities, new ExclusiveGroup.ExclusiveGroupStruct(pair.Key), innerCount, this); } } } @@ -32,8 +31,7 @@ namespace Svelto.ECS.Internal { foreach (var pair in dic) { - var entities = - (pair.Value as TypeSafeDictionary).GetValuesArray(out var innerCount); + var entities = (pair.Value as TypeSafeDictionary).GetValuesArray(out var innerCount); if (innerCount > 0) action(entities, new ExclusiveGroup.ExclusiveGroupStruct(pair.Key), innerCount, this, value); diff --git a/Svelto.ECS/Extensions/Unity/SveltoGUIHelper.cs b/Svelto.ECS/Extensions/Unity/SveltoGUIHelper.cs index 2f6cc9a..a52efe7 100644 --- a/Svelto.ECS/Extensions/Unity/SveltoGUIHelper.cs +++ b/Svelto.ECS/Extensions/Unity/SveltoGUIHelper.cs @@ -5,10 +5,9 @@ namespace Svelto.ECS.Unity { public static class SveltoGUIHelper { - public static T CreateFromPrefab(uint startIndex, Transform contextHolder, IEntityFactory factory, ExclusiveGroup group) where T : MonoBehaviour, IEntityDescriptorHolder + public static T CreateFromPrefab(ref uint startIndex, Transform contextHolder, IEntityFactory factory, ExclusiveGroup group) where T : MonoBehaviour, IEntityDescriptorHolder { var holder = Create(new EGID(startIndex++, group), contextHolder, factory); - var childs = contextHolder.GetComponentsInChildren(true); foreach (var child in childs) @@ -16,7 +15,7 @@ namespace Svelto.ECS.Unity if (child.GetType() != typeof(T)) { var childImplementors = (child as MonoBehaviour).GetComponents(); - InternalBuildAll(startIndex, child, factory, group, childImplementors); + startIndex = InternalBuildAll(startIndex, child, factory, group, childImplementors); } } @@ -43,13 +42,13 @@ namespace Svelto.ECS.Unity { var implementors = holder.GetComponents(); - InternalBuildAll(startIndex, holder, factory, group, implementors); + startIndex = InternalBuildAll(startIndex, holder, factory, group, implementors); } return startIndex; } - static void InternalBuildAll(uint startIndex, IEntityDescriptorHolder descriptorHolder, IEntityFactory factory, ExclusiveGroup group, IImplementor[] implementors) + static uint InternalBuildAll(uint startIndex, IEntityDescriptorHolder descriptorHolder, IEntityFactory factory, ExclusiveGroup group, IImplementor[] implementors) { ExclusiveGroup.ExclusiveGroupStruct realGroup = group; @@ -62,10 +61,12 @@ namespace Svelto.ECS.Unity egid = new EGID(startIndex++, realGroup); else egid = new EGID(holderId, realGroup); - + var init = factory.BuildEntity(egid, descriptorHolder.GetDescriptor(), implementors); - init.Init(new EntityHierarchyStruct(group)); + init.Init(new EntityHierarchyStruct(group)); + + return startIndex; } } } diff --git a/Svelto.ECS/GenericentityStreamConsumerFactory.cs b/Svelto.ECS/GenericentityStreamConsumerFactory.cs index 25232eb..0aa06d5 100644 --- a/Svelto.ECS/GenericentityStreamConsumerFactory.cs +++ b/Svelto.ECS/GenericentityStreamConsumerFactory.cs @@ -1,8 +1,8 @@ namespace Svelto.ECS { - class GenericEntityStreamConsumerFactory : IEntityStreamConsumerFactory + class GenericentityStreamConsumerFactory : IEntityStreamConsumerFactory { - public GenericEntityStreamConsumerFactory(DataStructures.WeakReference weakReference) + public GenericentityStreamConsumerFactory(DataStructures.WeakReference weakReference) { _enginesRoot = weakReference; } diff --git a/Svelto.ECS/IEntitiesDB.cs b/Svelto.ECS/IEntitiesDB.cs index 33639c3..400192d 100644 --- a/Svelto.ECS/IEntitiesDB.cs +++ b/Svelto.ECS/IEntitiesDB.cs @@ -14,7 +14,6 @@ namespace Svelto.ECS /// /// bool TryQueryEntitiesAndIndex(EGID entityGid, out uint index, out T[] array) where T : struct, IEntityStruct; - bool TryQueryEntitiesAndIndex (uint id, ExclusiveGroup.ExclusiveGroupStruct group, out uint index, out T[] array) where T : struct, IEntityStruct; @@ -29,7 +28,6 @@ namespace Svelto.ECS /// /// T[] QueryEntitiesAndIndex(EGID entityGid, out uint index) where T : struct, IEntityStruct; - T[] QueryEntitiesAndIndex(uint id, ExclusiveGroup.ExclusiveGroupStruct group, out uint index) where T : struct, IEntityStruct; @@ -49,7 +47,6 @@ namespace Svelto.ECS /// /// ref T QueryEntity(EGID entityGid) where T : struct, IEntityStruct; - ref T QueryEntity(uint id, ExclusiveGroup.ExclusiveGroupStruct group) where T : struct, IEntityStruct; /// @@ -62,13 +59,11 @@ namespace Svelto.ECS /// T[] QueryEntities(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out uint count) where T : struct, IEntityStruct; - (T1[], T2[]) QueryEntities(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out uint count) where T1 : struct, IEntityStruct where T2 : struct, IEntityStruct; - (T1[], T2[], T3[]) QueryEntities(ExclusiveGroup.ExclusiveGroupStruct groupStruct, out uint count) where T1 : struct, IEntityStruct where T2 : struct, IEntityStruct where T3 : struct, IEntityStruct; - + EntityCollection QueryEntities(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T : struct, IEntityStruct; diff --git a/Svelto.ECS/IReactOnSwap.cs b/Svelto.ECS/IReactOnSwap.cs index 3a5b4cd..0d5722c 100644 --- a/Svelto.ECS/IReactOnSwap.cs +++ b/Svelto.ECS/IReactOnSwap.cs @@ -5,6 +5,6 @@ namespace Svelto.ECS public interface IReactOnSwap : IReactOnSwap where T : IEntityStruct { void MovedTo(ref T entityView, ExclusiveGroup.ExclusiveGroupStruct previousGroup); - void MovedFrom(ref T entityView, ExclusiveGroup.ExclusiveGroupStruct previousGroupValue); + void MovedFrom(ref T entityView); } } \ No newline at end of file