diff --git a/ECS/Dispatcher/DispatcherOnChange.cs b/ECS/Dispatcher/DispatcherOnChange.cs index 2a265d6..0601df8 100644 --- a/ECS/Dispatcher/DispatcherOnChange.cs +++ b/ECS/Dispatcher/DispatcherOnChange.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; namespace Svelto.ECS { - public class DispatcherOnChange : DispatcherOnSet + public class DispatchOnChange : DispatchOnSet { - public DispatcherOnChange(int senderID) : base(senderID) + public DispatchOnChange(int senderID) : base(senderID) { } public new T value diff --git a/ECS/Dispatcher/DispatcherOnSet.cs b/ECS/Dispatcher/DispatcherOnSet.cs index 0514064..2c9f069 100644 --- a/ECS/Dispatcher/DispatcherOnSet.cs +++ b/ECS/Dispatcher/DispatcherOnSet.cs @@ -1,11 +1,13 @@ +using BetterWeakEvents; + namespace Svelto.ECS { - public class DispatcherOnSet + public class DispatchOnSet { - public DispatcherOnSet(int senderID) + public DispatchOnSet(int senderID) { _senderID = senderID; - _subscribers = new System.Collections.Generic.HashSet>(); + _subscribers = new WeakEvent(); } public T value @@ -14,14 +16,7 @@ namespace Svelto.ECS { _value = value; - if (_subscribers != null) - { - using (var enumerator = _subscribers.GetEnumerator()) - { - while (enumerator.MoveNext() == true) - enumerator.Current.Invoke(_senderID, _value); - } - } + _subscribers.Invoke(_senderID, value); } get @@ -30,19 +25,19 @@ namespace Svelto.ECS } } - public void NotifyOnDataChange(System.Action action) + public void NotifyOnValueSet(System.Action action) { - _subscribers.Add(new WeakAction(action)); + _subscribers += action; } - public void StopNotifyOnDataChange(System.Action action) + public void StopNotify(System.Action action) { - _subscribers.Remove(new WeakAction(action)); + _subscribers -= action; } protected T _value; protected int _senderID; - protected System.Collections.Generic.HashSet> _subscribers; + protected WeakEvent _subscribers; } } diff --git a/ECS/GenericEntityDescriptor.cs b/ECS/GenericEntityDescriptor.cs new file mode 100644 index 0000000..16c2687 --- /dev/null +++ b/ECS/GenericEntityDescriptor.cs @@ -0,0 +1,127 @@ +namespace Svelto.ECS +{ + class GenericEntityDescriptor : EntityDescriptor + where T : NodeWithID, new() + { + static GenericEntityDescriptor() + { + _nodesToBuild = new INodeBuilder[] + { + new NodeBuilder() + }; + } + public GenericEntityDescriptor(params object[] componentsImplementor) : base(_nodesToBuild, componentsImplementor) + { + } + static INodeBuilder[] _nodesToBuild; + } + + class GenericEntityDescriptor : EntityDescriptor + where T : NodeWithID, new() + where U : NodeWithID, new() + { + static GenericEntityDescriptor() + { + _nodesToBuild = new INodeBuilder[] + { + new NodeBuilder(), + new NodeBuilder() + }; + } + public GenericEntityDescriptor(params object[] componentsImplementor) : base(_nodesToBuild, componentsImplementor) + { + } + static INodeBuilder[] _nodesToBuild; + } + + class GenericEntityDescriptor : EntityDescriptor + where T : NodeWithID, new() + where U : NodeWithID, new() + where V : NodeWithID, new() + { + static GenericEntityDescriptor() + { + _nodesToBuild = new INodeBuilder[] + { + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder() + }; + } + public GenericEntityDescriptor(params object[] componentsImplementor) : base(_nodesToBuild, componentsImplementor) + { + } + static INodeBuilder[] _nodesToBuild; + } + + class GenericEntityDescriptor : EntityDescriptor + where T : NodeWithID, new() + where U : NodeWithID, new() + where V : NodeWithID, new() + where W : NodeWithID, new() + { + static GenericEntityDescriptor() + { + _nodesToBuild = new INodeBuilder[] + { + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder() + }; + } + public GenericEntityDescriptor(params object[] componentsImplementor) : base(_nodesToBuild, componentsImplementor) + { + } + static INodeBuilder[] _nodesToBuild; + } + + class GenericEntityDescriptor : EntityDescriptor + where T : NodeWithID, new() + where U : NodeWithID, new() + where V : NodeWithID, new() + where W : NodeWithID, new() + where X : NodeWithID, new() + { + static GenericEntityDescriptor() + { + _nodesToBuild = new INodeBuilder[] + { + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder() + }; + } + public GenericEntityDescriptor(params object[] componentsImplementor) : base(_nodesToBuild, componentsImplementor) + { + } + static INodeBuilder[] _nodesToBuild; + } + class GenericEntityDescriptor : EntityDescriptor + where T : NodeWithID, new() + where U : NodeWithID, new() + where V : NodeWithID, new() + where W : NodeWithID, new() + where X : NodeWithID, new() + where Y : NodeWithID, new() + { + static GenericEntityDescriptor() + { + _nodesToBuild = new INodeBuilder[] + { + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder(), + new NodeBuilder() + }; + } + public GenericEntityDescriptor(params object[] componentsImplementor) : base(_nodesToBuild, componentsImplementor) + { + } + static INodeBuilder[] _nodesToBuild; + } +} diff --git a/Utilities/WeakAction.cs.meta b/ECS/GenericEntityDescriptor.cs.meta similarity index 75% rename from Utilities/WeakAction.cs.meta rename to ECS/GenericEntityDescriptor.cs.meta index eb12418..840728d 100644 --- a/Utilities/WeakAction.cs.meta +++ b/ECS/GenericEntityDescriptor.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: eaffccbdbcacfa449b997dc1f7181bb4 -timeCreated: 1461942385 +guid: fd70d1b3107162f4790b1b71a129818f +timeCreated: 1484485852 licenseType: Pro MonoImporter: serializedVersion: 2 diff --git a/ECS/Sequencer.cs b/ECS/Sequencer.cs new file mode 100644 index 0000000..f9d0efd --- /dev/null +++ b/ECS/Sequencer.cs @@ -0,0 +1,52 @@ +using System; +using Steps = System.Collections.Generic.Dictionary>; + +namespace Svelto.ECS +{ + public interface IStep + { } + + public interface IStep:IStep + { + void Step(ref T token, Enum condition); + } + + public class Sequencer + { + public Sequencer() + {} + + public void SetSequence(Steps steps) + { + _steps = steps; + } + + public void Next(IEngine engine, ref T param) + { + Next(engine, ref param, Condition.always); + } + + public void Next(IEngine engine, ref T param, Enum condition) + { + var tos = _steps[engine]; + var steps = tos[condition]; + + if (steps != null) + for (int i = 0; i < steps.Length; i++) + ((IStep)steps[i]).Step(ref param, condition); + } + + Steps _steps; + } + + public enum Condition + { + always + } + + public enum DamageCondition + { + damage, + dead + } +} \ No newline at end of file diff --git a/ECS/Sequencer.cs.meta b/ECS/Sequencer.cs.meta new file mode 100644 index 0000000..87596cb --- /dev/null +++ b/ECS/Sequencer.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 4e1f40937ca5027428ed7193729be1a5 +timeCreated: 1484487023 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ECS/SingleNodeEngine.cs b/ECS/SingleNodeEngine.cs index 6b43243..f888ee2 100644 --- a/ECS/SingleNodeEngine.cs +++ b/ECS/SingleNodeEngine.cs @@ -12,7 +12,9 @@ Remove(obj as TNodeType); } - protected abstract void Add(TNodeType node); - protected abstract void Remove(TNodeType node); + protected virtual void Add(TNodeType node) + {} + protected virtual void Remove(TNodeType node) + {} } } diff --git a/Utilities/WeakAction.cs b/Utilities/WeakAction.cs deleted file mode 100644 index 0264419..0000000 --- a/Utilities/WeakAction.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -public struct WeakAction:IEquatable> -{ - public WeakAction(Action listener) - { - ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak); - Method = listener.Method; - - if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0) - throw new ArgumentException("Cannot create weak event to anonymous method with closure."); - } - - public bool Invoke(T1 data1, T2 data2) - { - if (ObjectRef.IsAllocated && ObjectRef.Target != null) - { - Method.Invoke(ObjectRef.Target, new object[] { data1, data2 }); - return true; - } - - return false; - } - - public bool Equals(WeakAction other) - { - return (Method.Equals(other.Method)); - } - - readonly GCHandle ObjectRef; - readonly MethodInfo Method; -} - -public struct WeakAction:IEquatable> -{ - public WeakAction(Action listener) - { - ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak); - Method = listener.Method; - - if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0) - throw new ArgumentException("Cannot create weak event to anonymous method with closure."); - } - - public bool Invoke(T data) - { - if (ObjectRef.IsAllocated && ObjectRef.Target != null) - { - Method.Invoke(ObjectRef.Target, new object[] { data }); - return true; - } - - return false; - } - - public bool Equals(WeakAction other) - { - return (Method.Equals(other.Method)); - } - - readonly GCHandle ObjectRef; - readonly MethodInfo Method; -} - -public struct WeakAction:IEquatable -{ - public WeakAction(Action listener) - { - ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak); - Method = listener.Method; - - if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0) - throw new ArgumentException("Cannot create weak event to anonymous method with closure."); - } - - public bool Invoke() - { - if (ObjectRef.IsAllocated && ObjectRef.Target != null) - { - Method.Invoke(ObjectRef.Target, null); - return true; - } - - return false; - } - - public bool Equals(WeakAction other) - { - return (Method.Equals(other.Method)); - } - - readonly GCHandle ObjectRef; - readonly MethodInfo Method; -} - - diff --git a/Utilities/WeakActionStruct.cs b/Utilities/WeakActionStruct.cs new file mode 100644 index 0000000..17158e1 --- /dev/null +++ b/Utilities/WeakActionStruct.cs @@ -0,0 +1,122 @@ +using System; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +//careful, you must handle the destruction of the GCHandles! + +namespace BetterWeakEvents +{ + public struct WeakAction : IEquatable> + { + public WeakAction(Action listener) + { + ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak); + Method = listener.Method; + + if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0) + throw new ArgumentException("Cannot create weak event to anonymous method with closure."); + } + + public bool Invoke(T1 data1, T2 data2) + { + if (ObjectRef.IsAllocated && ObjectRef.Target != null) + { + Method.Invoke(ObjectRef.Target, new object[] { data1, data2 }); + return true; + } + + Release(); + return false; + } + + public bool Equals(WeakAction other) + { + return (Method.Equals(other.Method)); + } + + public void Release() + { + ObjectRef.Free(); + } + + readonly GCHandle ObjectRef; + readonly MethodInfo Method; + } + + public struct WeakAction : IEquatable> + { + public WeakAction(Action listener) + { + ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak); + Method = listener.Method; + + if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0) + throw new ArgumentException("Cannot create weak event to anonymous method with closure."); + } + + public bool Invoke(T data) + { + if (ObjectRef.IsAllocated && ObjectRef.Target != null) + { + Method.Invoke(ObjectRef.Target, new object[] { data }); + return true; + } + + Release(); + return false; + } + + public bool Equals(WeakAction other) + { + return (Method.Equals(other.Method)); + } + + public void Release() + { + ObjectRef.Free(); + } + + readonly GCHandle ObjectRef; + readonly MethodInfo Method; + } + + public struct WeakAction : IEquatable + { + public WeakAction(Action listener) + { + ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak); + Method = listener.Method; + + if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0) + throw new ArgumentException("Cannot create weak event to anonymous method with closure."); + } + + public bool Invoke() + { + if (ObjectRef.IsAllocated && ObjectRef.Target != null) + { + Method.Invoke(ObjectRef.Target, null); + return true; + } + + Release(); + return false; + } + + public bool Equals(WeakAction other) + { + return (Method.Equals(other.Method)); + } + + public void Release() + { + ObjectRef.Free(); + } + + readonly GCHandle ObjectRef; + readonly MethodInfo Method; + } + + +} \ No newline at end of file diff --git a/Utilities/WeakActionStruct.cs.meta b/Utilities/WeakActionStruct.cs.meta new file mode 100644 index 0000000..49656b3 --- /dev/null +++ b/Utilities/WeakActionStruct.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 1662357721652524593d7b0f731aef45 +timeCreated: 1484483913 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Utilities/WeakEvent.cs b/Utilities/WeakEvent.cs new file mode 100644 index 0000000..e912490 --- /dev/null +++ b/Utilities/WeakEvent.cs @@ -0,0 +1,35 @@ +using Svelto.DataStructures; +using System; + +namespace BetterWeakEvents +{ + public class WeakEvent + { + public static WeakEvent operator+(WeakEvent c1, Action x) + { + c1._subscribers.Add(new WeakAction(x)); + return c1; + } + + public static WeakEvent operator-(WeakEvent c1, Action x) + { + c1._subscribers.UnorderredRemove(new WeakAction(x)); + return c1; + } + + public void Invoke(T1 arg1, T2 arg2) + { + for (int i = 0; i < _subscribers.Count; i++) + if (_subscribers[i].Invoke(arg1, arg2) == false) + _subscribers.UnorderredRemoveAt(i--); + } + + ~WeakEvent() + { + for (int i = 0; i < _subscribers.Count; i++) + _subscribers[i].Release(); + } + + protected FasterList> _subscribers = new FasterList>(); + } +} \ No newline at end of file diff --git a/Utilities/WeakEvent.cs.meta b/Utilities/WeakEvent.cs.meta new file mode 100644 index 0000000..884293e --- /dev/null +++ b/Utilities/WeakEvent.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: e2ee3763e7dffd4459c588eda6867ddd +timeCreated: 1484480295 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: