├── .gitignore ├── Atomic.asmdef ├── Atomic.asmdef.meta ├── Elements.meta ├── Elements ├── Implementations.meta ├── Implementations │ ├── AtomicAction.cs │ ├── AtomicAction.cs.meta │ ├── AtomicEvent.cs │ ├── AtomicEvent.cs.meta │ ├── AtomicExpression.cs │ ├── AtomicExpression.cs.meta │ ├── AtomicFunction.cs │ ├── AtomicFunction.cs.meta │ ├── AtomicObservable.cs │ ├── AtomicObservable.cs.meta │ ├── AtomicProperty.cs │ ├── AtomicProperty.cs.meta │ ├── AtomicPropertyObservable.cs │ ├── AtomicPropertyObservable.cs.meta │ ├── AtomicSetter.cs │ ├── AtomicSetter.cs.meta │ ├── AtomicValue.cs │ ├── AtomicValue.cs.meta │ ├── AtomicVariable.cs │ └── AtomicVariable.cs.meta ├── Interfaces.meta ├── Interfaces │ ├── IAtomicAction.cs │ ├── IAtomicAction.cs.meta │ ├── IAtomicEvent.cs │ ├── IAtomicEvent.cs.meta │ ├── IAtomicExpression.cs │ ├── IAtomicExpression.cs.meta │ ├── IAtomicFunction.cs │ ├── IAtomicFunction.cs.meta │ ├── IAtomicObservable.cs │ ├── IAtomicObservable.cs.meta │ ├── IAtomicSetter.cs │ ├── IAtomicSetter.cs.meta │ ├── IAtomicValue.cs │ ├── IAtomicValue.cs.meta │ ├── IAtomicValueObservable.cs │ ├── IAtomicValueObservable.cs.meta │ ├── IAtomicVariable.cs │ ├── IAtomicVariable.cs.meta │ ├── IAtomicVariableObservable.cs │ └── IAtomicVariableObservable.cs.meta ├── Utils.meta └── Utils │ ├── DelegateUtils.cs │ ├── DelegateUtils.cs.meta │ ├── EqualityComparer.cs │ └── EqualityComparer.cs.meta ├── Extensions.meta ├── Extensions ├── ContractAttribute.cs ├── ContractAttribute.cs.meta ├── ElementExtensions.cs ├── ElementExtensions.cs.meta ├── Expressions.meta ├── Expressions │ ├── AtomicAnd.cs │ ├── AtomicAnd.cs.meta │ ├── AtomicFloatProduct.cs │ ├── AtomicFloatProduct.cs.meta │ ├── AtomicFloatSum.cs │ ├── AtomicFloatSum.cs.meta │ ├── AtomicIntProduct.cs │ ├── AtomicIntProduct.cs.meta │ ├── AtomicIntSum.cs │ ├── AtomicIntSum.cs.meta │ ├── AtomicOr.cs │ └── AtomicOr.cs.meta ├── ObjectExtensions.cs └── ObjectExtensions.cs.meta ├── LICENSE.md ├── LICENSE.md.meta ├── Objects.meta ├── Objects ├── AtomicCompiler.cs ├── AtomicCompiler.cs.meta ├── AtomicEntity.cs ├── AtomicEntity.cs.meta ├── AtomicObject.cs ├── AtomicObject.cs.meta ├── Attributes.meta ├── Attributes │ ├── GetAttribute.cs │ ├── GetAttribute.cs.meta │ ├── IsAttribute.cs │ └── IsAttribute.cs.meta ├── Interfaces.meta ├── Interfaces │ ├── IAtomicBehaviour.cs │ ├── IAtomicBehaviour.cs.meta │ ├── IAtomicEntity.cs │ ├── IAtomicEntity.cs.meta │ ├── IAtomicLogic.cs │ ├── IAtomicLogic.cs.meta │ ├── IAtomicObject.cs │ ├── IAtomicObject.cs.meta │ ├── IMutableAtomicEntity.cs │ ├── IMutableAtomicEntity.cs.meta │ ├── IMutableAtomicObject.cs │ ├── IMutableAtomicObject.cs.meta │ ├── IMutalbleAtomicBehaviour.cs │ └── IMutalbleAtomicBehaviour.cs.meta ├── Internal.meta ├── Internal │ ├── AtomicEntityInfo.cs │ ├── AtomicEntityInfo.cs.meta │ ├── AtomicScanner.cs │ ├── AtomicScanner.cs.meta │ ├── ReflectionUtils.cs │ ├── ReflectionUtils.cs.meta │ ├── ValueInfo.cs │ └── ValueInfo.cs.meta ├── Proxy.meta └── Proxy │ ├── AtomicEntityProxy.cs │ ├── AtomicEntityProxy.cs.meta │ ├── AtomicObjectProxy.cs │ └── AtomicObjectProxy.cs.meta ├── README.md ├── README.md.meta ├── package.json └── package.json.meta /.gitignore: -------------------------------------------------------------------------------- 1 | /LICENSE 2 | -------------------------------------------------------------------------------- /Atomic.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Atomic" 3 | } 4 | -------------------------------------------------------------------------------- /Atomic.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 076be2e9ccf4c47d9b6057df5e4fe44b 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Elements.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9123f91218ceb426b850a0c25e1c8dca 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Elements/Implementations.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8bd0ceb130ead4892a65ef86c9c06bae 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | #if ODIN_INSPECTOR 4 | using Sirenix.OdinInspector; 5 | #endif 6 | 7 | namespace Atomic.Elements 8 | { 9 | /// Represents a method object 10 | 11 | #if ODIN_INSPECTOR 12 | [InlineProperty] 13 | #endif 14 | 15 | [Serializable] 16 | public class AtomicAction : IAtomicAction 17 | { 18 | private Action action; 19 | 20 | public AtomicAction() 21 | { 22 | } 23 | 24 | public AtomicAction(Action action) 25 | { 26 | this.action = action; 27 | } 28 | 29 | //Setups action delegate; 30 | public void Compose(Action action) 31 | { 32 | this.action = action; 33 | } 34 | 35 | #if ODIN_INSPECTOR 36 | [Button] 37 | #endif 38 | public void Invoke() 39 | { 40 | this.action?.Invoke(); 41 | } 42 | } 43 | 44 | 45 | #if ODIN_INSPECTOR 46 | [InlineProperty] 47 | #endif 48 | 49 | [Serializable] 50 | public class AtomicAction : IAtomicAction 51 | { 52 | private Action action; 53 | 54 | public AtomicAction() 55 | { 56 | } 57 | 58 | public AtomicAction(Action action) 59 | { 60 | this.action = action; 61 | } 62 | 63 | public void Compose(Action action) 64 | { 65 | this.action = action; 66 | } 67 | 68 | #if ODIN_INSPECTOR 69 | [Button] 70 | #endif 71 | public void Invoke(T arg) 72 | { 73 | this.action?.Invoke(arg); 74 | } 75 | } 76 | 77 | 78 | #if ODIN_INSPECTOR 79 | [InlineProperty] 80 | #endif 81 | 82 | [Serializable] 83 | public class AtomicAction : IAtomicAction 84 | { 85 | private Action action; 86 | 87 | public AtomicAction() 88 | { 89 | } 90 | 91 | public AtomicAction(Action action) 92 | { 93 | this.action = action; 94 | } 95 | 96 | public void Compose(Action action) 97 | { 98 | this.action = action; 99 | } 100 | 101 | #if ODIN_INSPECTOR 102 | [Button] 103 | #endif 104 | public void Invoke(T1 args1, T2 args2) 105 | { 106 | this.action?.Invoke(args1, args2); 107 | } 108 | } 109 | 110 | 111 | #if ODIN_INSPECTOR 112 | [InlineProperty] 113 | #endif 114 | 115 | [Serializable] 116 | public class AtomicAction : IAtomicAction 117 | { 118 | private Action action; 119 | 120 | public AtomicAction() 121 | { 122 | } 123 | 124 | public AtomicAction(Action action) 125 | { 126 | this.action = action; 127 | } 128 | 129 | public void Compose(Action action) 130 | { 131 | this.action = action; 132 | } 133 | 134 | #if ODIN_INSPECTOR 135 | [Button] 136 | #endif 137 | public void Invoke(T1 args1, T2 args2, T3 args3) 138 | { 139 | this.action?.Invoke(args1, args2, args3); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicAction.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 83bde63145cde4d2892b226ab776793d 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | #if ODIN_INSPECTOR 4 | using Sirenix.OdinInspector; 5 | #endif 6 | 7 | namespace Atomic.Elements 8 | { 9 | /// Represents an event object 10 | 11 | #if ODIN_INSPECTOR 12 | [InlineProperty] 13 | #endif 14 | 15 | [Serializable] 16 | public class AtomicEvent : IAtomicEvent, IDisposable 17 | { 18 | private event Action onEvent; 19 | 20 | public void Subscribe(Action action) 21 | { 22 | this.onEvent += action; 23 | } 24 | 25 | public void Unsubscribe(Action action) 26 | { 27 | this.onEvent -= action; 28 | } 29 | 30 | #if ODIN_INSPECTOR 31 | [Button] 32 | #endif 33 | public void Invoke() 34 | { 35 | this.onEvent?.Invoke(); 36 | } 37 | 38 | public void Dispose() 39 | { 40 | DelegateUtils.Dispose(ref this.onEvent); 41 | } 42 | } 43 | 44 | #if ODIN_INSPECTOR 45 | [InlineProperty] 46 | #endif 47 | 48 | [Serializable] 49 | public class AtomicEvent : IAtomicEvent, IDisposable 50 | { 51 | private event Action onEvent; 52 | 53 | public void Subscribe(Action action) 54 | { 55 | this.onEvent += action; 56 | } 57 | 58 | public void Unsubscribe(Action action) 59 | { 60 | this.onEvent -= action; 61 | } 62 | 63 | #if ODIN_INSPECTOR 64 | [Button] 65 | #endif 66 | public void Invoke(T arg) 67 | { 68 | this.onEvent?.Invoke(arg); 69 | } 70 | 71 | public void Dispose() 72 | { 73 | DelegateUtils.Dispose(ref this.onEvent); 74 | } 75 | } 76 | 77 | #if ODIN_INSPECTOR 78 | [InlineProperty] 79 | #endif 80 | 81 | [Serializable] 82 | public class AtomicEvent : IAtomicEvent, IDisposable 83 | { 84 | private event Action onEvent; 85 | 86 | public void Subscribe(Action action) 87 | { 88 | this.onEvent += action; 89 | } 90 | 91 | public void Unsubscribe(Action action) 92 | { 93 | this.onEvent -= action; 94 | } 95 | 96 | #if ODIN_INSPECTOR 97 | [Button] 98 | #endif 99 | public void Invoke(T1 args1, T2 args2) 100 | { 101 | this.onEvent?.Invoke(args1, args2); 102 | } 103 | 104 | public void Dispose() 105 | { 106 | DelegateUtils.Dispose(ref this.onEvent); 107 | } 108 | } 109 | 110 | #if ODIN_INSPECTOR 111 | [InlineProperty] 112 | #endif 113 | 114 | [Serializable] 115 | public class AtomicEvent : IAtomicEvent, IDisposable 116 | { 117 | private event Action onEvent; 118 | 119 | public void Subscribe(Action action) 120 | { 121 | this.onEvent += action; 122 | } 123 | 124 | public void Unsubscribe(Action action) 125 | { 126 | this.onEvent -= action; 127 | } 128 | 129 | #if ODIN_INSPECTOR 130 | [Button] 131 | #endif 132 | public void Invoke(T1 args1, T2 args2, T3 args3) 133 | { 134 | this.onEvent?.Invoke(args1, args2, args3); 135 | } 136 | 137 | public void Dispose() 138 | { 139 | DelegateUtils.Dispose(ref this.onEvent); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicEvent.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7de5fbdb9cd0344618aca6fc4a9b8a8c 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Atomic.Elements; 4 | #if ODIN_INSPECTOR 5 | using Sirenix.OdinInspector; 6 | #endif 7 | 8 | // ReSharper disable ParameterTypeCanBeEnumerable.Global 9 | // ReSharper disable PublicConstructorInAbstractClass 10 | 11 | namespace GameEngine 12 | { 13 | [Serializable] 14 | #if ODIN_INSPECTOR 15 | [InlineProperty] 16 | #endif 17 | public abstract class AtomicExpression : IAtomicExpression 18 | { 19 | private readonly List> members; 20 | 21 | public AtomicExpression() 22 | { 23 | this.members = new List>(); 24 | } 25 | 26 | public AtomicExpression(params IAtomicValue[] members) 27 | { 28 | this.members = new List>(members); 29 | } 30 | 31 | public AtomicExpression(IEnumerable> members) 32 | { 33 | this.members = new List>(members); 34 | } 35 | 36 | public void Append(IAtomicValue member) 37 | { 38 | if (member != null) 39 | { 40 | this.members.Add(member); 41 | } 42 | } 43 | 44 | public void Remove(IAtomicValue member) 45 | { 46 | if (member != null) 47 | { 48 | this.members.Remove(member); 49 | } 50 | } 51 | 52 | #if ODIN_INSPECTOR 53 | [Button] 54 | #endif 55 | public T Invoke() 56 | { 57 | return this.Invoke(this.members); 58 | } 59 | 60 | protected abstract T Invoke(IReadOnlyList> members); 61 | } 62 | 63 | [Serializable] 64 | #if ODIN_INSPECTOR 65 | [InlineProperty] 66 | #endif 67 | public abstract class AtomicExpression : IAtomicExpression 68 | { 69 | private readonly List> members = new(); 70 | 71 | public AtomicExpression() 72 | { 73 | this.members = new List>(); 74 | } 75 | 76 | public AtomicExpression(params IAtomicFunction[] members) 77 | { 78 | this.members = new List>(members); 79 | } 80 | 81 | public AtomicExpression(IEnumerable> members) 82 | { 83 | this.members = new List>(members); 84 | } 85 | 86 | public void Append(IAtomicFunction member) 87 | { 88 | this.members.Add(member); 89 | } 90 | 91 | public void Remove(IAtomicFunction member) 92 | { 93 | this.members.Remove(member); 94 | } 95 | 96 | #if ODIN_INSPECTOR 97 | [Button] 98 | #endif 99 | public R Invoke(T args) 100 | { 101 | return this.Invoke(this.members, args); 102 | } 103 | 104 | protected abstract R Invoke(IReadOnlyList> members, T args); 105 | } 106 | } -------------------------------------------------------------------------------- /Elements/Implementations/AtomicExpression.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4f781185f9a134757aa14ec8f4230014 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | #if ODIN_INSPECTOR 4 | using Sirenix.OdinInspector; 5 | #endif 6 | 7 | namespace Atomic.Elements 8 | { 9 | /// Represents a function object. 10 | 11 | [Serializable] 12 | public class AtomicFunction : IAtomicFunction 13 | { 14 | private Func func; 15 | 16 | #if ODIN_INSPECTOR 17 | [ShowInInspector, ReadOnly] 18 | #endif 19 | public T Value 20 | { 21 | get { return this.func != null ? this.func.Invoke() : default; } 22 | } 23 | 24 | public AtomicFunction() 25 | { 26 | } 27 | 28 | public AtomicFunction(Func func) 29 | { 30 | this.func = func; 31 | } 32 | 33 | public void Compose(Func func) 34 | { 35 | this.func = func; 36 | } 37 | 38 | public T Invoke() 39 | { 40 | return this.func != null ? this.func.Invoke() : default; 41 | } 42 | } 43 | 44 | [Serializable] 45 | public sealed class AtomicFunction : IAtomicFunction 46 | { 47 | private Func func; 48 | 49 | public AtomicFunction() 50 | { 51 | } 52 | 53 | public AtomicFunction(Func func) 54 | { 55 | this.func = func; 56 | } 57 | 58 | public void Compose(Func func) 59 | { 60 | this.func = func; 61 | } 62 | 63 | #if ODIN_INSPECTOR 64 | [Button] 65 | #endif 66 | public R Invoke(T args) 67 | { 68 | return this.func.Invoke(args); 69 | } 70 | } 71 | 72 | [Serializable] 73 | public sealed class AtomicFunction : IAtomicFunction 74 | { 75 | private Func func; 76 | 77 | public AtomicFunction() 78 | { 79 | } 80 | 81 | public AtomicFunction(Func func) 82 | { 83 | this.func = func; 84 | } 85 | 86 | public void Compose(Func func) 87 | { 88 | this.func = func; 89 | } 90 | 91 | #if ODIN_INSPECTOR 92 | [Button] 93 | #endif 94 | public R Invoke(T1 arg1, T2 arg2) 95 | { 96 | return this.func.Invoke(arg1, arg2); 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicFunction.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8c446c51503ed4dff80e78acffbfe881 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicObservable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Atomic.Elements 4 | { 5 | /// Provides observable interface to a specified source. 6 | 7 | [Serializable] 8 | public class AtomicObservable : IAtomicObservable 9 | { 10 | private Action subscribe; 11 | private Action unsubscribe; 12 | 13 | public AtomicObservable() 14 | { 15 | } 16 | 17 | public AtomicObservable(Action subscribe, Action unsubscribe) 18 | { 19 | this.subscribe = subscribe; 20 | this.unsubscribe = unsubscribe; 21 | } 22 | 23 | public void Compose(Action subscribe, Action unsubscribe) 24 | { 25 | this.subscribe = subscribe; 26 | this.unsubscribe = unsubscribe; 27 | } 28 | 29 | public void Subscribe(Action action) 30 | { 31 | this.subscribe?.Invoke(action); 32 | } 33 | 34 | public void Unsubscribe(Action action) 35 | { 36 | this.unsubscribe?.Invoke(action); 37 | } 38 | } 39 | 40 | [Serializable] 41 | public sealed class AtomicObservable : IAtomicObservable 42 | { 43 | private Action> subscribe; 44 | private Action> unsubscribe; 45 | 46 | public AtomicObservable() 47 | { 48 | } 49 | 50 | public AtomicObservable(Action> subscribe, Action> unsubscribe) 51 | { 52 | this.subscribe = subscribe; 53 | this.unsubscribe = unsubscribe; 54 | } 55 | 56 | public void Compose(Action> subscribe, Action> unsubscribe) 57 | { 58 | this.subscribe = subscribe; 59 | this.unsubscribe = unsubscribe; 60 | } 61 | 62 | public void Subscribe(Action action) 63 | { 64 | this.subscribe.Invoke(action); 65 | } 66 | 67 | public void Unsubscribe(Action action) 68 | { 69 | this.unsubscribe.Invoke(action); 70 | } 71 | } 72 | 73 | [Serializable] 74 | public sealed class AtomicObservable : IAtomicObservable 75 | { 76 | private Action> subscribe; 77 | private Action> unsubscribe; 78 | 79 | public AtomicObservable() 80 | { 81 | } 82 | 83 | public AtomicObservable(Action> subscribe, Action> unsubscribe) 84 | { 85 | this.subscribe = subscribe; 86 | this.unsubscribe = unsubscribe; 87 | } 88 | 89 | public void Compose(Action> subscribe, Action> unsubscribe) 90 | { 91 | this.subscribe = subscribe; 92 | this.unsubscribe = unsubscribe; 93 | } 94 | 95 | public void Subscribe(Action action) 96 | { 97 | this.subscribe.Invoke(action); 98 | } 99 | 100 | public void Unsubscribe(Action action) 101 | { 102 | this.unsubscribe.Invoke(action); 103 | } 104 | } 105 | 106 | [Serializable] 107 | public sealed class AtomicObservable : IAtomicObservable 108 | { 109 | private Action> subscribe; 110 | private Action> unsubscribe; 111 | 112 | public AtomicObservable() 113 | { 114 | } 115 | 116 | public AtomicObservable(Action> subscribe, Action> unsubscribe) 117 | { 118 | this.subscribe = subscribe; 119 | this.unsubscribe = unsubscribe; 120 | } 121 | 122 | public void Compose(Action> subscribe, Action> unsubscribe) 123 | { 124 | this.subscribe = subscribe; 125 | this.unsubscribe = unsubscribe; 126 | } 127 | 128 | public void Subscribe(Action action) 129 | { 130 | this.subscribe.Invoke(action); 131 | } 132 | 133 | public void Unsubscribe(Action action) 134 | { 135 | this.unsubscribe.Invoke(action); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicObservable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d91597d57c6c64ac1aebaaf92e1057a0 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | #if ODIN_INSPECTOR 4 | using Sirenix.OdinInspector; 5 | #endif 6 | 7 | namespace Atomic.Elements 8 | { 9 | ///Provides read-write interface to a specified source 10 | 11 | [Serializable] 12 | public sealed class AtomicProperty : IAtomicVariable 13 | { 14 | 15 | #if ODIN_INSPECTOR 16 | [ShowInInspector, ReadOnly] 17 | #endif 18 | public T Value 19 | { 20 | get { return this.getter != null ? this.getter.Invoke() : default; } 21 | set { this.setter?.Invoke(value); } 22 | } 23 | 24 | private Func getter; 25 | private Action setter; 26 | 27 | public AtomicProperty() 28 | { 29 | } 30 | 31 | public AtomicProperty(Func getter, Action setter) 32 | { 33 | this.getter = getter; 34 | this.setter = setter; 35 | } 36 | 37 | public void Compose(Func getter, Action setter) 38 | { 39 | this.getter = getter; 40 | this.setter = setter; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicProperty.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: caad2017f0ba94386b8f48c4bf4af41a 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicPropertyObservable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Sirenix.OdinInspector; 3 | 4 | namespace Atomic.Elements 5 | { 6 | ///Combines AtomicProperty & AtomicObservable 7 | 8 | public sealed class AtomicPropertyObservable : IAtomicVariable, IAtomicObservable 9 | { 10 | private Func getter; 11 | private Action setter; 12 | 13 | private Action> subscribe; 14 | private Action> unsubscribe; 15 | 16 | #if ODIN_INSPECTOR 17 | [ShowInInspector, ReadOnly] 18 | #endif 19 | public T Value 20 | { 21 | get { return this.getter != null ? this.getter.Invoke() : default; } 22 | set { this.setter?.Invoke(value); } 23 | } 24 | 25 | public void Subscribe(Action action) 26 | { 27 | this.subscribe.Invoke(action); 28 | } 29 | 30 | public void Unsubscribe(Action action) 31 | { 32 | this.unsubscribe.Invoke(action); 33 | } 34 | 35 | public AtomicPropertyObservable() 36 | { 37 | } 38 | 39 | public AtomicPropertyObservable( 40 | Func getter, 41 | Action setter, 42 | Action> subscribe, 43 | Action> unsubscribe 44 | ) 45 | { 46 | this.getter = getter; 47 | this.setter = setter; 48 | this.subscribe = subscribe; 49 | this.unsubscribe = unsubscribe; 50 | } 51 | 52 | public void Compose( 53 | Func getter, 54 | Action setter, 55 | Action> subscribe, 56 | Action> unsubscribe 57 | ) 58 | { 59 | this.getter = getter; 60 | this.setter = setter; 61 | this.subscribe = subscribe; 62 | this.unsubscribe = unsubscribe; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicPropertyObservable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 36ca5370a6794c5f9ee23338e5873c2e 3 | timeCreated: 1713804601 4 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicSetter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | #if ODIN_INSPECTOR 3 | using Sirenix.OdinInspector; 4 | #endif 5 | 6 | namespace Atomic.Elements 7 | { 8 | ///Provides setter interface to a specified source. 9 | 10 | [Serializable] 11 | public class AtomicSetter : IAtomicSetter 12 | { 13 | public T Value 14 | { 15 | set => this.action?.Invoke(value); 16 | } 17 | 18 | private Action action; 19 | 20 | public AtomicSetter() 21 | { 22 | } 23 | 24 | public AtomicSetter(Action action) 25 | { 26 | this.action = action; 27 | } 28 | 29 | public void Compose(Action action) 30 | { 31 | this.action = action; 32 | } 33 | 34 | #if UNITY_EDITOR 35 | #if ODIN_INSPECTOR 36 | [Button("Set Value")] 37 | #endif 38 | private void SetValueEditor(T value) => this.action?.Invoke(value); 39 | #endif 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicSetter.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a06c69f9bf1544062826151bf05832d3 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | #if ODIN_INSPECTOR 5 | using Sirenix.OdinInspector; 6 | #endif 7 | 8 | namespace Atomic.Elements 9 | { 10 | /// Represents a serialized read-only property. 11 | 12 | #if ODIN_INSPECTOR 13 | [InlineProperty] 14 | #endif 15 | 16 | [Serializable] 17 | public sealed class AtomicValue : IAtomicValue 18 | { 19 | public T Value 20 | { 21 | get { return this.value; } 22 | } 23 | 24 | #if ODIN_INSPECTOR 25 | [HideLabel] 26 | #endif 27 | [SerializeField] 28 | private T value; 29 | 30 | public AtomicValue(T value) 31 | { 32 | this.value = value; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicValue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3f8538a1f92624921a36171cd8961459 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicVariable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | #if ODIN_INSPECTOR 6 | using Sirenix.OdinInspector; 7 | #endif 8 | 9 | namespace Atomic.Elements 10 | { 11 | /// Represents a serialized read-write reactive property. 12 | 13 | #if ODIN_INSPECTOR 14 | [InlineProperty] 15 | #endif 16 | 17 | [Serializable] 18 | public class AtomicVariable : IAtomicVariableObservable, IDisposable 19 | { 20 | private static readonly IEqualityComparer equalityComparer = EqualityComparer.GetDefault(); 21 | 22 | private Action onChanged; 23 | 24 | [SerializeField] 25 | private T value; 26 | 27 | public T Value 28 | { 29 | get { return this.value; } 30 | set 31 | { 32 | if (!equalityComparer.Equals(this.value, value)) 33 | { 34 | this.value = value; 35 | this.onChanged?.Invoke(value); 36 | } 37 | } 38 | } 39 | 40 | public AtomicVariable() 41 | { 42 | this.value = default; 43 | } 44 | 45 | public AtomicVariable(T value) 46 | { 47 | this.value = value; 48 | } 49 | 50 | public void Subscribe(Action listener) 51 | { 52 | this.onChanged += listener; 53 | } 54 | 55 | public void Unsubscribe(Action listener) 56 | { 57 | this.onChanged -= listener; 58 | } 59 | 60 | #if ODIN_INSPECTOR 61 | [HideLabel, OnValueChanged(nameof(OnValueChanged))] 62 | #endif 63 | private void OnValueChanged(T value) 64 | { 65 | this.onChanged?.Invoke(value); 66 | } 67 | 68 | public void Dispose() 69 | { 70 | DelegateUtils.Dispose(ref this.onChanged); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Elements/Implementations/AtomicVariable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d198613e189464eb88d52a45c07732c7 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d173986a8d45443e48209513611a9d0e 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicAction.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicAction 4 | { 5 | void Invoke(); 6 | } 7 | 8 | public interface IAtomicAction 9 | { 10 | void Invoke(T arg); 11 | } 12 | 13 | public interface IAtomicAction 14 | { 15 | void Invoke(T1 args1, T2 args2); 16 | } 17 | 18 | public interface IAtomicAction 19 | { 20 | void Invoke(T1 args1, T2 args2, T3 args3); 21 | } 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicAction.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2f660e960cbda4ceeb096aad359fb871 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicEvent.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicEvent : IAtomicObservable, IAtomicAction 4 | { 5 | } 6 | 7 | public interface IAtomicEvent : IAtomicObservable, IAtomicAction 8 | { 9 | } 10 | 11 | public interface IAtomicEvent : IAtomicObservable, IAtomicAction 12 | { 13 | } 14 | 15 | public interface IAtomicEvent : IAtomicObservable, IAtomicAction 16 | { 17 | } 18 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicEvent.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 51d3647e946fe4a0abc6cc881cb4ec10 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicExpression.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicExpression : IAtomicFunction 4 | { 5 | void Append(IAtomicValue member); 6 | void Remove(IAtomicValue member); 7 | } 8 | 9 | public interface IAtomicExpression : IAtomicFunction 10 | { 11 | void Append(IAtomicFunction member); 12 | void Remove(IAtomicFunction member); 13 | } 14 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicExpression.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f667a633d2c4943908eb9e42751c196b 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicFunction.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicFunction : IAtomicValue 4 | { 5 | R Invoke(); 6 | 7 | R IAtomicValue.Value => this.Invoke(); 8 | } 9 | 10 | public interface IAtomicFunction 11 | { 12 | R Invoke(T args); 13 | } 14 | 15 | public interface IAtomicFunction 16 | { 17 | R Invoke(T1 args1, T2 args2); 18 | } 19 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicFunction.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 630e1268289ea4123bff9a9b1452b303 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicObservable.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicObservable 4 | { 5 | void Subscribe(System.Action action); 6 | void Unsubscribe(System.Action action); 7 | } 8 | 9 | public interface IAtomicObservable 10 | { 11 | void Subscribe(System.Action action); 12 | void Unsubscribe(System.Action action); 13 | } 14 | 15 | public interface IAtomicObservable 16 | { 17 | void Subscribe(System.Action action); 18 | void Unsubscribe(System.Action action); 19 | } 20 | 21 | public interface IAtomicObservable 22 | { 23 | void Subscribe(System.Action action); 24 | void Unsubscribe(System.Action action); 25 | } 26 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicObservable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 49ee18a37d28741f19627cb9b920ba36 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicSetter.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicSetter 4 | { 5 | T Value { set; } 6 | } 7 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicSetter.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b2c266567813149209027eff74a2ad8f 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicValue.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicValue 4 | { 5 | T Value { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicValue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 066a1130f1fd04a6cae8d75b470657da 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicValueObservable.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicValueObservable : IAtomicValue, IAtomicObservable 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicValueObservable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d8679be927a9440c8a0a1832952f8427 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicVariable.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicVariable : IAtomicValue, IAtomicSetter 4 | { 5 | new T Value { get; set; } 6 | 7 | T IAtomicValue.Value 8 | { 9 | get { return this.Value; } 10 | } 11 | 12 | T IAtomicSetter.Value 13 | { 14 | set => this.Value = value; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicVariable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f053c110427d24c669e8b072b608d11f 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicVariableObservable.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Elements 2 | { 3 | public interface IAtomicVariableObservable : IAtomicVariable, IAtomicValueObservable 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /Elements/Interfaces/IAtomicVariableObservable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 35396071105cb4a74b14c778f6c440a3 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Utils.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 84f83333a0d4d49eb970ba7a26babba5 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Elements/Utils/DelegateUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace Atomic.Elements 5 | { 6 | internal static class DelegateUtils 7 | { 8 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 9 | internal static void Dispose(ref Action action) 10 | { 11 | if (action == null) 12 | { 13 | return; 14 | } 15 | 16 | Delegate[] delegates = action.GetInvocationList(); 17 | for (int i = 0, count = delegates.Length; i < count; i++) 18 | { 19 | action -= (Action) delegates[i]; 20 | } 21 | 22 | action = null; 23 | } 24 | 25 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 26 | internal static void Dispose(ref T del) where T : Delegate 27 | { 28 | if (del == null) 29 | { 30 | return; 31 | } 32 | 33 | Delegate[] delegates = del.GetInvocationList(); 34 | foreach (Delegate value in delegates) 35 | { 36 | del = (T) Delegate.Remove(del, value); 37 | } 38 | 39 | del = null; 40 | } 41 | 42 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 43 | internal static void Dispose(ref Action action) 44 | { 45 | if (action == null) 46 | { 47 | return; 48 | } 49 | 50 | Delegate[] delegates = action.GetInvocationList(); 51 | foreach (var del in delegates) 52 | { 53 | action -= (Action) del; 54 | } 55 | 56 | action = null; 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /Elements/Utils/DelegateUtils.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 63098d7205db94fd49a942adbd7cbb3b 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Elements/Utils/EqualityComparer.cs: -------------------------------------------------------------------------------- 1 | #if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6)) 2 | #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member 3 | #endif 4 | 5 | using System; 6 | using System.Collections.Generic; 7 | using UnityEngine; 8 | 9 | namespace Atomic.Elements 10 | { 11 | //Украл из UniRx! ;) 12 | internal static class EqualityComparer 13 | { 14 | public static readonly IEqualityComparer Vector2 = new Vector2EqualityComparer(); 15 | public static readonly IEqualityComparer Vector3 = new Vector3EqualityComparer(); 16 | public static readonly IEqualityComparer Vector4 = new Vector4EqualityComparer(); 17 | public static readonly IEqualityComparer Color = new ColorEqualityComparer(); 18 | public static readonly IEqualityComparer Color32 = new Color32EqualityComparer(); 19 | public static readonly IEqualityComparer Rect = new RectEqualityComparer(); 20 | public static readonly IEqualityComparer Bounds = new BoundsEqualityComparer(); 21 | public static readonly IEqualityComparer Quaternion = new QuaternionEqualityComparer(); 22 | 23 | static readonly RuntimeTypeHandle vector2Type = typeof(Vector2).TypeHandle; 24 | static readonly RuntimeTypeHandle vector3Type = typeof(Vector3).TypeHandle; 25 | static readonly RuntimeTypeHandle vector4Type = typeof(Vector4).TypeHandle; 26 | static readonly RuntimeTypeHandle colorType = typeof(Color).TypeHandle; 27 | static readonly RuntimeTypeHandle color32Type = typeof(Color32).TypeHandle; 28 | static readonly RuntimeTypeHandle rectType = typeof(Rect).TypeHandle; 29 | static readonly RuntimeTypeHandle boundsType = typeof(Bounds).TypeHandle; 30 | static readonly RuntimeTypeHandle quaternionType = typeof(Quaternion).TypeHandle; 31 | 32 | #if UNITY_2017_2_OR_NEWER 33 | 34 | public static readonly IEqualityComparer Vector2Int = new Vector2IntEqualityComparer(); 35 | public static readonly IEqualityComparer Vector3Int = new Vector3IntEqualityComparer(); 36 | public static readonly IEqualityComparer RangeInt = new RangeIntEqualityComparer(); 37 | public static readonly IEqualityComparer RectInt = new RectIntEqualityComparer(); 38 | public static readonly IEqualityComparer BoundsInt = new BoundsIntEqualityComparer(); 39 | 40 | static readonly RuntimeTypeHandle vector2IntType = typeof(Vector2Int).TypeHandle; 41 | static readonly RuntimeTypeHandle vector3IntType = typeof(Vector3Int).TypeHandle; 42 | static readonly RuntimeTypeHandle rangeIntType = typeof(RangeInt).TypeHandle; 43 | static readonly RuntimeTypeHandle rectIntType = typeof(RectInt).TypeHandle; 44 | static readonly RuntimeTypeHandle boundsIntType = typeof(BoundsInt).TypeHandle; 45 | 46 | #endif 47 | 48 | static class Cache 49 | { 50 | public static readonly IEqualityComparer Comparer; 51 | 52 | static Cache() 53 | { 54 | var comparer = GetDefaultHelper(typeof(T)); 55 | if (comparer == null) 56 | { 57 | Comparer = EqualityComparer.Default; 58 | } 59 | else 60 | { 61 | Comparer = (IEqualityComparer)comparer; 62 | } 63 | } 64 | } 65 | 66 | public static IEqualityComparer GetDefault() 67 | { 68 | return Cache.Comparer; 69 | } 70 | 71 | static object GetDefaultHelper(Type type) 72 | { 73 | var t = type.TypeHandle; 74 | 75 | if (t.Equals(vector2Type)) return (object)EqualityComparer.Vector2; 76 | if (t.Equals(vector3Type)) return (object)EqualityComparer.Vector3; 77 | if (t.Equals(vector4Type)) return (object)EqualityComparer.Vector4; 78 | if (t.Equals(colorType)) return (object)EqualityComparer.Color; 79 | if (t.Equals(color32Type)) return (object)EqualityComparer.Color32; 80 | if (t.Equals(rectType)) return (object)EqualityComparer.Rect; 81 | if (t.Equals(boundsType)) return (object)EqualityComparer.Bounds; 82 | if (t.Equals(quaternionType)) return (object)EqualityComparer.Quaternion; 83 | 84 | #if UNITY_2017_2_OR_NEWER 85 | 86 | if (t.Equals(vector2IntType)) return (object)EqualityComparer.Vector2Int; 87 | if (t.Equals(vector3IntType)) return (object)EqualityComparer.Vector3Int; 88 | if (t.Equals(rangeIntType)) return (object)EqualityComparer.RangeInt; 89 | if (t.Equals(rectIntType)) return (object)EqualityComparer.RectInt; 90 | if (t.Equals(boundsIntType)) return (object)EqualityComparer.BoundsInt; 91 | #endif 92 | 93 | return null; 94 | } 95 | 96 | sealed class Vector2EqualityComparer : IEqualityComparer 97 | { 98 | public bool Equals(Vector2 self, Vector2 vector) 99 | { 100 | return self.x.Equals(vector.x) && self.y.Equals(vector.y); 101 | } 102 | 103 | public int GetHashCode(Vector2 obj) 104 | { 105 | return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2; 106 | } 107 | } 108 | 109 | sealed class Vector3EqualityComparer : IEqualityComparer 110 | { 111 | public bool Equals(Vector3 self, Vector3 vector) 112 | { 113 | return self.x.Equals(vector.x) && self.y.Equals(vector.y) && self.z.Equals(vector.z); 114 | } 115 | 116 | public int GetHashCode(Vector3 obj) 117 | { 118 | return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2 ^ obj.z.GetHashCode() >> 2; 119 | } 120 | } 121 | 122 | sealed class Vector4EqualityComparer : IEqualityComparer 123 | { 124 | public bool Equals(Vector4 self, Vector4 vector) 125 | { 126 | return self.x.Equals(vector.x) && self.y.Equals(vector.y) && self.z.Equals(vector.z) && self.w.Equals(vector.w); 127 | } 128 | 129 | public int GetHashCode(Vector4 obj) 130 | { 131 | return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2 ^ obj.z.GetHashCode() >> 2 ^ obj.w.GetHashCode() >> 1; 132 | } 133 | } 134 | 135 | sealed class ColorEqualityComparer : IEqualityComparer 136 | { 137 | public bool Equals(Color self, Color other) 138 | { 139 | return self.r.Equals(other.r) && self.g.Equals(other.g) && self.b.Equals(other.b) && self.a.Equals(other.a); 140 | } 141 | 142 | public int GetHashCode(Color obj) 143 | { 144 | return obj.r.GetHashCode() ^ obj.g.GetHashCode() << 2 ^ obj.b.GetHashCode() >> 2 ^ obj.a.GetHashCode() >> 1; 145 | } 146 | } 147 | 148 | sealed class RectEqualityComparer : IEqualityComparer 149 | { 150 | public bool Equals(Rect self, Rect other) 151 | { 152 | return self.x.Equals(other.x) && self.width.Equals(other.width) && self.y.Equals(other.y) && self.height.Equals(other.height); 153 | } 154 | 155 | public int GetHashCode(Rect obj) 156 | { 157 | return obj.x.GetHashCode() ^ obj.width.GetHashCode() << 2 ^ obj.y.GetHashCode() >> 2 ^ obj.height.GetHashCode() >> 1; 158 | } 159 | } 160 | 161 | sealed class BoundsEqualityComparer : IEqualityComparer 162 | { 163 | public bool Equals(Bounds self, Bounds vector) 164 | { 165 | return self.center.Equals(vector.center) && self.extents.Equals(vector.extents); 166 | } 167 | 168 | public int GetHashCode(Bounds obj) 169 | { 170 | return obj.center.GetHashCode() ^ obj.extents.GetHashCode() << 2; 171 | } 172 | } 173 | 174 | sealed class QuaternionEqualityComparer : IEqualityComparer 175 | { 176 | public bool Equals(Quaternion self, Quaternion vector) 177 | { 178 | return self.x.Equals(vector.x) && self.y.Equals(vector.y) && self.z.Equals(vector.z) && self.w.Equals(vector.w); 179 | } 180 | 181 | public int GetHashCode(Quaternion obj) 182 | { 183 | return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2 ^ obj.z.GetHashCode() >> 2 ^ obj.w.GetHashCode() >> 1; 184 | } 185 | } 186 | 187 | sealed class Color32EqualityComparer : IEqualityComparer 188 | { 189 | public bool Equals(Color32 self, Color32 vector) 190 | { 191 | return self.a.Equals(vector.a) && self.r.Equals(vector.r) && self.g.Equals(vector.g) && self.b.Equals(vector.b); 192 | } 193 | 194 | public int GetHashCode(Color32 obj) 195 | { 196 | return obj.a.GetHashCode() ^ obj.r.GetHashCode() << 2 ^ obj.g.GetHashCode() >> 2 ^ obj.b.GetHashCode() >> 1; 197 | } 198 | } 199 | 200 | #if UNITY_2017_2_OR_NEWER 201 | 202 | sealed class Vector2IntEqualityComparer : IEqualityComparer 203 | { 204 | public bool Equals(Vector2Int self, Vector2Int vector) 205 | { 206 | return self.x.Equals(vector.x) && self.y.Equals(vector.y); 207 | } 208 | 209 | public int GetHashCode(Vector2Int obj) 210 | { 211 | return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2; 212 | } 213 | } 214 | 215 | sealed class Vector3IntEqualityComparer : IEqualityComparer 216 | { 217 | public static readonly Vector3IntEqualityComparer Default = new Vector3IntEqualityComparer(); 218 | 219 | public bool Equals(Vector3Int self, Vector3Int vector) 220 | { 221 | return self.x.Equals(vector.x) && self.y.Equals(vector.y) && self.z.Equals(vector.z); 222 | } 223 | 224 | public int GetHashCode(Vector3Int obj) 225 | { 226 | return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2 ^ obj.z.GetHashCode() >> 2; 227 | } 228 | } 229 | 230 | sealed class RangeIntEqualityComparer : IEqualityComparer 231 | { 232 | public bool Equals(RangeInt self, RangeInt vector) 233 | { 234 | return self.start.Equals(vector.start) && self.length.Equals(vector.length); 235 | } 236 | 237 | public int GetHashCode(RangeInt obj) 238 | { 239 | return obj.start.GetHashCode() ^ obj.length.GetHashCode() << 2; 240 | } 241 | } 242 | 243 | sealed class RectIntEqualityComparer : IEqualityComparer 244 | { 245 | public bool Equals(RectInt self, RectInt other) 246 | { 247 | return self.x.Equals(other.x) && self.width.Equals(other.width) && self.y.Equals(other.y) && self.height.Equals(other.height); 248 | } 249 | 250 | public int GetHashCode(RectInt obj) 251 | { 252 | return obj.x.GetHashCode() ^ obj.width.GetHashCode() << 2 ^ obj.y.GetHashCode() >> 2 ^ obj.height.GetHashCode() >> 1; 253 | } 254 | } 255 | 256 | sealed class BoundsIntEqualityComparer : IEqualityComparer 257 | { 258 | public bool Equals(BoundsInt self, BoundsInt vector) 259 | { 260 | return Vector3IntEqualityComparer.Default.Equals(self.position, vector.position) 261 | && Vector3IntEqualityComparer.Default.Equals(self.size, vector.size); 262 | } 263 | 264 | public int GetHashCode(BoundsInt obj) 265 | { 266 | return Vector3IntEqualityComparer.Default.GetHashCode(obj.position) ^ Vector3IntEqualityComparer.Default.GetHashCode(obj.size) << 2; 267 | } 268 | } 269 | 270 | #endif 271 | } 272 | } 273 | -------------------------------------------------------------------------------- /Elements/Utils/EqualityComparer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 230d9de905b434a43b85614e3773d424 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7ca6fc46b529f4d60ab192594533d549 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Extensions/ContractAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | // ReSharper disable UnusedParameter.Local 3 | 4 | namespace Atomic.Extensions 5 | { 6 | ///Experimental! 7 | ///Only for inspector! 8 | 9 | //TODO: реализовать статический анализатор для Rider 10 | [AttributeUsage(AttributeTargets.Field)] 11 | public sealed class ContractAttribute : Attribute 12 | { 13 | public ContractAttribute(params Type[] type) 14 | { 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Extensions/ContractAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c911f11a27a834ebdadf4312db1a6d45 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions/ElementExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.CompilerServices; 3 | using Atomic.Elements; 4 | 5 | namespace Atomic.Extensions 6 | { 7 | public static class ElementExtensions 8 | { 9 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 10 | public static T Let(this T it, Action let) 11 | { 12 | let.Invoke(it); 13 | return it; 14 | } 15 | 16 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 17 | public static AtomicValue AsValue(this T it) 18 | { 19 | return new AtomicValue(it); 20 | } 21 | 22 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 23 | public static AtomicVariable AsVariable(this T it) 24 | { 25 | return new AtomicVariable(it); 26 | } 27 | 28 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 29 | public static AtomicFunction AsFunction(Func func) 30 | { 31 | return new AtomicFunction(func); 32 | } 33 | 34 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 35 | public static AtomicFunction AsFunction(this T it, Func func) 36 | { 37 | return new AtomicFunction(() => func.Invoke(it)); 38 | } 39 | 40 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 41 | public static AtomicFunction AsNot(this IAtomicValue it) 42 | { 43 | return new AtomicFunction(() => !it.Value); 44 | } 45 | 46 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 47 | public static AtomicProperty AsProperty(this T it, Func getter, Action setter) 48 | { 49 | return new AtomicProperty(() => getter.Invoke(it), value => setter.Invoke(it, value)); 50 | } 51 | 52 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 53 | public static void Subscribe(this IAtomicObservable it, IAtomicAction action) 54 | { 55 | it.Subscribe(action.Invoke); 56 | } 57 | 58 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 59 | public static void Unsubscribe(this IAtomicObservable it, IAtomicAction action) 60 | { 61 | it.Unsubscribe(action.Invoke); 62 | } 63 | 64 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 65 | public static void Subscribe(this IAtomicObservable it, IAtomicAction action) 66 | { 67 | it.Subscribe(action.Invoke); 68 | } 69 | 70 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 71 | public static void Unsubscribe(this IAtomicObservable it, IAtomicAction action) 72 | { 73 | it.Unsubscribe(action.Invoke); 74 | } 75 | 76 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 77 | public static void Subscribe(this IAtomicObservable it, IAtomicAction action) 78 | { 79 | it.Subscribe(action.Invoke); 80 | } 81 | 82 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 83 | public static void Unsubscribe(this IAtomicObservable it, IAtomicAction action) 84 | { 85 | it.Unsubscribe(action.Invoke); 86 | } 87 | 88 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 89 | public static void Subscribe(this IAtomicObservable it, IAtomicAction action) 90 | { 91 | it.Subscribe(action.Invoke); 92 | } 93 | 94 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 95 | public static void Unsubscribe(this IAtomicObservable it, IAtomicAction action) 96 | { 97 | it.Unsubscribe(action.Invoke); 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /Extensions/ElementExtensions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7f166b3aeb84248b8925949c7d990ef1 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions/Expressions.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 541db417fa30c4daabe4e96d0e2e4562 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicAnd.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using GameEngine; 4 | 5 | namespace Atomic.Elements 6 | { 7 | [Serializable] 8 | public sealed class AtomicAnd : AtomicExpression 9 | { 10 | public AtomicAnd() 11 | { 12 | } 13 | 14 | public AtomicAnd(IEnumerable> members) : base(members) 15 | { 16 | } 17 | 18 | protected override bool Invoke(IReadOnlyList> members) 19 | { 20 | int count = members.Count; 21 | 22 | if (count == 0) 23 | { 24 | return true; 25 | } 26 | 27 | for (int i = 0; i < count; i++) 28 | { 29 | if (!members[i].Value) 30 | { 31 | return false; 32 | } 33 | } 34 | 35 | return true; 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicAnd.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9518dcb44625f4e1698893adc07ea9c0 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicFloatProduct.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using GameEngine; 4 | 5 | namespace Atomic.Elements 6 | { 7 | ///Represents a product of float members 8 | 9 | [Serializable] 10 | public sealed class AtomicFloatProduct : AtomicExpression 11 | { 12 | public AtomicFloatProduct() 13 | { 14 | } 15 | 16 | public AtomicFloatProduct(IEnumerable> members) : base(members) 17 | { 18 | } 19 | 20 | protected override float Invoke(IReadOnlyList> members) 21 | { 22 | int count = members.Count; 23 | if (count == 0) 24 | { 25 | return 0; 26 | } 27 | 28 | float result = 1; 29 | 30 | for (int i = 0; i < count; i++) 31 | { 32 | IAtomicValue member = members[i]; 33 | result *= member.Value; 34 | } 35 | 36 | return result; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicFloatProduct.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1291bde409f6e4c6387d822e00e9b813 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicFloatSum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using GameEngine; 4 | 5 | namespace Atomic.Elements 6 | { 7 | [Serializable] 8 | public sealed class AtomicFloatSum : AtomicExpression 9 | { 10 | public AtomicFloatSum() 11 | { 12 | } 13 | 14 | public AtomicFloatSum(IEnumerable> members) : base(members) 15 | { 16 | } 17 | 18 | protected override float Invoke(IReadOnlyList> members) 19 | { 20 | int count = members.Count; 21 | if (count == 0) 22 | { 23 | return 0; 24 | } 25 | 26 | float result = 0; 27 | 28 | for (int i = 0; i < count; i++) 29 | { 30 | IAtomicValue member = members[i]; 31 | result += member.Value; 32 | } 33 | 34 | return result; 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicFloatSum.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1d4dbad1d51e04fa78ca0a4c05b662be 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicIntProduct.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using GameEngine; 4 | 5 | namespace Atomic.Elements 6 | { 7 | ///Represents a product of float members 8 | 9 | [Serializable] 10 | public sealed class AtomicIntProduct : AtomicExpression 11 | { 12 | public AtomicIntProduct() 13 | { 14 | } 15 | 16 | public AtomicIntProduct(IEnumerable> members) : base(members) 17 | { 18 | } 19 | 20 | protected override int Invoke(IReadOnlyList> members) 21 | { 22 | int count = members.Count; 23 | if (count == 0) 24 | { 25 | return 0; 26 | } 27 | 28 | int result = 1; 29 | 30 | for (int i = 0; i < count; i++) 31 | { 32 | IAtomicValue member = members[i]; 33 | result *= member.Value; 34 | } 35 | 36 | return result; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicIntProduct.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c856457a0db024e7292e9036f7834d08 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicIntSum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Atomic.Elements; 4 | using GameEngine; 5 | 6 | namespace Atomic.Elements 7 | { 8 | [Serializable] 9 | public sealed class AtomicIntSum : AtomicExpression 10 | { 11 | public AtomicIntSum() 12 | { 13 | } 14 | 15 | public AtomicIntSum(IEnumerable> members) : base(members) 16 | { 17 | } 18 | 19 | protected override int Invoke(IReadOnlyList> members) 20 | { 21 | int count = members.Count; 22 | if (count == 0) 23 | { 24 | return 0; 25 | } 26 | 27 | int result = 0; 28 | 29 | for (int i = 0; i < count; i++) 30 | { 31 | IAtomicValue member = members[i]; 32 | result += member.Value; 33 | } 34 | 35 | return result; 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicIntSum.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1b92d93bbaa794decb90e1bb4f7c062f 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicOr.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using GameEngine; 4 | 5 | namespace Atomic.Elements 6 | { 7 | [Serializable] 8 | public sealed class AtomicOr : AtomicExpression 9 | { 10 | public AtomicOr() 11 | { 12 | } 13 | 14 | public AtomicOr(IEnumerable> members) : base(members) 15 | { 16 | } 17 | 18 | protected override bool Invoke(IReadOnlyList> members) 19 | { 20 | int count = members.Count; 21 | if (count == 0) 22 | { 23 | return false; 24 | } 25 | 26 | for (int i = 0; i < count; i++) 27 | { 28 | if (members[i].Value) 29 | { 30 | return true; 31 | } 32 | 33 | } 34 | 35 | return false; 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /Extensions/Expressions/AtomicOr.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ba195c24d2f2740a1b19d2e25516bed0 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Extensions/ObjectExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.CompilerServices; 4 | using Atomic.Elements; 5 | using Atomic.Objects; 6 | 7 | namespace Atomic.Extensions 8 | { 9 | public static class ObjectExtensions 10 | { 11 | private static readonly List cache = new(); 12 | 13 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 14 | public static IAtomicValue GetValue(this IAtomicEntity it, string name) 15 | { 16 | return it.Get>(name); 17 | } 18 | 19 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 20 | public static bool TryGetValue(this IAtomicEntity it, string name, out IAtomicValue result) 21 | { 22 | return it.TryGet(name, out result) && result != null; 23 | } 24 | 25 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 26 | public static IAtomicVariable GetVariable(this IAtomicEntity it, string name) 27 | { 28 | return it.Get>(name); 29 | } 30 | 31 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 32 | public static bool TryGetVariable(this IAtomicEntity it, string name, out IAtomicVariable result) 33 | { 34 | return it.TryGet(name, out result) && result != null; 35 | } 36 | 37 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 38 | public static IAtomicFunction GetFunction(this IAtomicEntity it, string name) 39 | { 40 | return it.Get>(name); 41 | } 42 | 43 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 44 | public static bool TryGetFunction(this IAtomicEntity it, string name, out IAtomicFunction result) 45 | { 46 | return it.TryGet(name, out result) && result != null; 47 | } 48 | 49 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 50 | public static IAtomicFunction GetFunction(this IAtomicEntity it, string name) 51 | { 52 | return it.Get>(name); 53 | } 54 | 55 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 56 | public static bool TryGetFunction( 57 | this IAtomicEntity it, 58 | string name, 59 | out IAtomicFunction result 60 | ) 61 | { 62 | return it.TryGet(name, out result) && result != null; 63 | } 64 | 65 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 66 | public static IAtomicFunction GetFunction(this IAtomicEntity it, string name) 67 | { 68 | return it.Get>(name); 69 | } 70 | 71 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 72 | public static bool TryGetFunction( 73 | this IAtomicEntity it, 74 | string name, 75 | out IAtomicFunction result 76 | ) 77 | { 78 | return it.TryGet(name, out result) && result != null; 79 | } 80 | 81 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 82 | public static IAtomicAction GetAction(this IAtomicEntity it, string name) 83 | { 84 | return it.Get(name); 85 | } 86 | 87 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 88 | public static bool TryGetAction(this IAtomicEntity it, string name, out IAtomicAction result) 89 | { 90 | return it.TryGet(name, out result) && result != null; 91 | } 92 | 93 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 94 | public static IAtomicAction GetAction(this IAtomicEntity it, string name) 95 | { 96 | return it.Get>(name); 97 | } 98 | 99 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 100 | public static bool TryGetAction(this IAtomicEntity it, string name, out IAtomicAction result) 101 | { 102 | return it.TryGet(name, out result) && result != null; 103 | } 104 | 105 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 106 | public static IAtomicAction GetAction(this IAtomicEntity it, string name) 107 | { 108 | return it.Get>(name); 109 | } 110 | 111 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 112 | public static bool TryGetAction(this IAtomicEntity it, string name, out IAtomicAction result) 113 | { 114 | return it.TryGet(name, out result) && result != null; 115 | } 116 | 117 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 118 | public static void InvokeAction(this IAtomicEntity it, string name) 119 | { 120 | it.GetAction(name)?.Invoke(); 121 | } 122 | 123 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 124 | public static void InvokeAction(this IAtomicEntity it, string name, T args) 125 | { 126 | it.GetAction(name)?.Invoke(args); 127 | } 128 | 129 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 130 | public static T InvokeFunction(this IAtomicEntity it, string name) 131 | { 132 | IAtomicFunction function = it.GetFunction(name); 133 | if (function != null) 134 | { 135 | return function.Invoke(); 136 | } 137 | 138 | return default; 139 | } 140 | 141 | public static IAtomicSetter GetSetter(this IAtomicEntity it, string name) 142 | { 143 | return it.Get>(name); 144 | } 145 | 146 | public static void SetVariable(this IAtomicEntity it, string name, T value) 147 | { 148 | if (it.TryGet(name, out IAtomicSetter setter)) 149 | { 150 | setter.Value = value; 151 | } 152 | } 153 | 154 | public static IAtomicExpression GetExpression(this IAtomicEntity it, string name) 155 | { 156 | return it.Get>(name); 157 | } 158 | 159 | public static bool TryGetExpression(this IAtomicEntity it, string name, out IAtomicExpression result) 160 | { 161 | return it.TryGet(name, out result) && result != null; 162 | } 163 | 164 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 165 | public static IAtomicObservable GetObservable(this IAtomicEntity it, string name) 166 | { 167 | return it.Get(name); 168 | } 169 | 170 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 171 | public static bool TryGetObservable(this IAtomicEntity it, string name, out IAtomicObservable result) 172 | { 173 | return it.TryGet(name, out result) && result != null; 174 | } 175 | 176 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 177 | public static IAtomicObservable GetObservable(this IAtomicEntity it, string name) 178 | { 179 | return it.Get>(name); 180 | } 181 | 182 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 183 | public static bool TryGetObservable(this IAtomicEntity it, string name, out IAtomicObservable result) 184 | { 185 | return it.TryGet(name, out result) && result != null; 186 | } 187 | 188 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 189 | public static bool SubscribeOnEvent(this IAtomicEntity it, string name, Action listener) 190 | { 191 | if (it.TryGetObservable(name, out IAtomicObservable observable)) 192 | { 193 | observable.Subscribe(listener); 194 | return true; 195 | } 196 | 197 | return false; 198 | } 199 | 200 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 201 | public static bool SubscribeOnEvent(this IAtomicEntity it, string name, IAtomicAction listener) 202 | { 203 | if (it.TryGetObservable(name, out IAtomicObservable observable)) 204 | { 205 | observable.Subscribe(listener); 206 | return true; 207 | } 208 | 209 | return false; 210 | } 211 | 212 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 213 | public static bool UnsubscribeFromEvent(this IAtomicEntity it, string name, Action listener) 214 | { 215 | if (it.TryGetObservable(name, out IAtomicObservable observable)) 216 | { 217 | observable.Unsubscribe(listener); 218 | return true; 219 | } 220 | 221 | return false; 222 | } 223 | 224 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 225 | public static bool UnsubscribeFromEvent(this IAtomicEntity it, string name, IAtomicAction listener) 226 | { 227 | if (it.TryGetObservable(name, out IAtomicObservable observable)) 228 | { 229 | observable.Unsubscribe(listener); 230 | return true; 231 | } 232 | 233 | return false; 234 | } 235 | 236 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 237 | public static bool SubscribeOnEvent(this IAtomicEntity it, string name, Action listener) 238 | { 239 | if (it.TryGetObservable(name, out IAtomicObservable observable)) 240 | { 241 | observable.Subscribe(listener); 242 | return true; 243 | } 244 | 245 | return false; 246 | } 247 | 248 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 249 | public static bool SubscribeOnEvent(this IAtomicEntity it, string name, IAtomicAction listener) 250 | { 251 | if (it.TryGetObservable(name, out IAtomicObservable observable)) 252 | { 253 | observable.Subscribe(listener); 254 | return true; 255 | } 256 | 257 | return false; 258 | } 259 | 260 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 261 | public static bool UnsubscribeFromEvent(this IAtomicEntity it, string name, Action listener) 262 | { 263 | if (it.TryGetObservable(name, out IAtomicObservable observable)) 264 | { 265 | observable.Unsubscribe(listener); 266 | return true; 267 | } 268 | 269 | return false; 270 | } 271 | 272 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 273 | public static bool UnsubscribeFromEvent(this IAtomicEntity it, string name, IAtomicAction listener) 274 | { 275 | if (it.TryGetObservable(name, out IAtomicObservable observable)) 276 | { 277 | observable.Unsubscribe(listener); 278 | return true; 279 | } 280 | 281 | return false; 282 | } 283 | 284 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 285 | public static void AddLogicRange(this IMutalbleAtomicBehaviour it, IEnumerable targets) 286 | { 287 | foreach (var target in targets) 288 | { 289 | it.AddLogic(target); 290 | } 291 | } 292 | 293 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 294 | public static void AddLogicRange(this IMutalbleAtomicBehaviour it, params IAtomicLogic[] logics) 295 | { 296 | for (int i = 0, count = logics.Length; i < count; i++) 297 | { 298 | it.AddLogic(logics[i]); 299 | } 300 | } 301 | 302 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 303 | public static void RemoveAllLogic(this IMutalbleAtomicBehaviour it) where T : IAtomicLogic 304 | { 305 | cache.Clear(); 306 | cache.AddRange(it.AllLogicUnsafe()); 307 | 308 | for (int i = 0; i < cache.Count; i++) 309 | { 310 | if (cache[i] is T logic) 311 | { 312 | it.RemoveLogic(logic); 313 | } 314 | } 315 | } 316 | 317 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 318 | public static bool RemoveLogic(this IMutalbleAtomicBehaviour it) where T : IAtomicLogic 319 | { 320 | IList elements = it.AllLogicUnsafe(); 321 | 322 | for (int i = 0, count = elements.Count; i < count; i++) 323 | { 324 | if (elements[i] is T element) 325 | { 326 | it.RemoveLogic(element); 327 | return true; 328 | } 329 | } 330 | 331 | return false; 332 | } 333 | 334 | public static bool FindLogic(this IAtomicBehaviour it, out T result) where T : IAtomicLogic 335 | { 336 | IList elements = it.AllLogicUnsafe(); 337 | 338 | for (int i = 0, count = elements.Count; i < count; i++) 339 | { 340 | if (elements[i] is T tElement) 341 | { 342 | result = tElement; 343 | return true; 344 | } 345 | } 346 | 347 | result = default; 348 | return false; 349 | } 350 | 351 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 352 | public static bool AddObject(this IMutableAtomicObject it, string name, IAtomicLogic value) 353 | { 354 | if (it.AddData(name, value)) 355 | { 356 | it.AddLogic(value); 357 | return true; 358 | } 359 | 360 | return false; 361 | } 362 | 363 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 364 | public static void RemoveObject(this IMutableAtomicObject it, string name) 365 | { 366 | if (it.RemoveData(name, out var value) && value is IAtomicLogic logic) 367 | { 368 | it.RemoveLogic(logic); 369 | } 370 | } 371 | } 372 | } 373 | -------------------------------------------------------------------------------- /Extensions/ObjectExtensions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0a6ee1e10c3f946748361734c5e8bb5b 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Igor Gulkin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSE.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ae522ab11ed9425db7265f7adffc77e9 3 | timeCreated: 1718032419 4 | -------------------------------------------------------------------------------- /Objects.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7024598f6e4be4e6a941fd61f214c96b 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Objects/AtomicCompiler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Atomic.Objects 5 | { 6 | public static class AtomicCompiler 7 | { 8 | private static readonly Dictionary compiledEntities = new(); 9 | 10 | //Call it in background thread! 11 | public static void PrecompileEntity(Type objectType) 12 | { 13 | CompileEntity(objectType); 14 | } 15 | 16 | internal static AtomicEntityInfo CompileEntity(Type objectType) 17 | { 18 | if (compiledEntities.TryGetValue(objectType, out AtomicEntityInfo objectInfo)) 19 | { 20 | return objectInfo; 21 | } 22 | 23 | objectInfo = CompileEntityInternal(objectType); 24 | compiledEntities.Add(objectType, objectInfo); 25 | return objectInfo; 26 | } 27 | 28 | private static AtomicEntityInfo CompileEntityInternal(Type objectType) 29 | { 30 | var types = new HashSet(); 31 | var references = new List(); 32 | 33 | foreach (Type @interface in objectType.GetInterfaces()) 34 | { 35 | types.UnionWith(AtomicScanner.ScanTypes(@interface)); 36 | references.AddRange(AtomicScanner.ScanValues(@interface)); 37 | } 38 | 39 | while (objectType != typeof(AtomicObject) && objectType != typeof(AtomicEntity) && objectType != null) 40 | { 41 | types.UnionWith(AtomicScanner.ScanTypes(objectType)); 42 | references.AddRange(AtomicScanner.ScanValues(objectType)); 43 | objectType = objectType!.BaseType; 44 | } 45 | 46 | return new AtomicEntityInfo(types, references); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Objects/AtomicCompiler.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c38ded74e1dcd4f12947a1ffca1cf71b 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/AtomicEntity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | #if ODIN_INSPECTOR 5 | using Sirenix.OdinInspector; 6 | #endif 7 | using UnityEngine; 8 | using UnityEngine.Profiling; 9 | 10 | namespace Atomic.Objects 11 | { 12 | public class AtomicEntity : MonoBehaviour, IMutableAtomicEntity, ISerializationCallbackReceiver 13 | { 14 | #if ODIN_INSPECTOR 15 | [Title("Data"), PropertySpace] 16 | [ShowInInspector, HideInEditorMode, PropertyOrder(100)] 17 | #endif 18 | private ISet typeSet; 19 | 20 | #if ODIN_INSPECTOR 21 | [ShowInInspector, HideInEditorMode, PropertyOrder(101)] 22 | #endif 23 | private IDictionary dataMap; 24 | 25 | public bool Is(string type) 26 | { 27 | return this.typeSet.Contains(type); 28 | } 29 | 30 | public bool Is(params string[] types) 31 | { 32 | for (int i = 0, count = types.Length; i < count; i++) 33 | { 34 | string type = types[i]; 35 | 36 | if (!this.typeSet.Contains(type)) 37 | { 38 | return false; 39 | } 40 | } 41 | 42 | return true; 43 | } 44 | 45 | public T Get(string key) where T : class 46 | { 47 | if (this.dataMap.TryGetValue(key, out var value)) 48 | { 49 | return value as T; 50 | } 51 | 52 | return default; 53 | } 54 | 55 | public bool TryGet(string key, out T result) where T : class 56 | { 57 | if (this.dataMap.TryGetValue(key, out var value)) 58 | { 59 | result = value as T; 60 | return true; 61 | } 62 | 63 | result = default; 64 | return false; 65 | } 66 | 67 | public object Get(string key) 68 | { 69 | if (this.dataMap.TryGetValue(key, out var value)) 70 | { 71 | return value; 72 | } 73 | 74 | return default; 75 | } 76 | 77 | public bool TryGet(string key, out object result) 78 | { 79 | return this.dataMap.TryGetValue(key, out result); 80 | } 81 | 82 | public string[] AllTypes() 83 | { 84 | return this.typeSet.ToArray(); 85 | } 86 | 87 | public KeyValuePair[] AllData() 88 | { 89 | return this.dataMap.ToArray(); 90 | } 91 | 92 | public bool AddData(string key, object value) 93 | { 94 | return this.dataMap.TryAdd(key, value); 95 | } 96 | 97 | public void SetData(string key, object value) 98 | { 99 | this.dataMap[key] = value; 100 | } 101 | 102 | public bool RemoveData(string key) 103 | { 104 | return this.dataMap.Remove(key); 105 | } 106 | 107 | public bool RemoveData(string key, out object value) 108 | { 109 | return this.dataMap.Remove(key, out value); 110 | } 111 | 112 | public void OverrideData(string key, object value, out object prevValue) 113 | { 114 | this.dataMap.TryGetValue(key, out prevValue); 115 | this.dataMap[key] = value; 116 | } 117 | 118 | public bool AddType(string type) 119 | { 120 | return this.typeSet.Add(type); 121 | } 122 | 123 | public void AddTypes(IEnumerable types) 124 | { 125 | this.typeSet.UnionWith(types); 126 | } 127 | 128 | public bool RemoveType(string type) 129 | { 130 | return this.typeSet.Remove(type); 131 | } 132 | 133 | public void RemoveTypes(IEnumerable types) 134 | { 135 | foreach (var type in types) 136 | { 137 | this.typeSet.Remove(type); 138 | } 139 | } 140 | 141 | public ISet AllTypesUnsafe() 142 | { 143 | return this.typeSet; 144 | } 145 | 146 | public IDictionary AllDataUnsafe() 147 | { 148 | return this.dataMap; 149 | } 150 | 151 | public int AllTypesNonAlloc(string[] results) 152 | { 153 | int i = 0; 154 | 155 | foreach (var type in this.typeSet) 156 | { 157 | results[i++] = type; 158 | } 159 | 160 | return i; 161 | } 162 | 163 | public int AllDataNonAlloc(KeyValuePair[] results) 164 | { 165 | int i = 0; 166 | 167 | foreach (var property in this.dataMap) 168 | { 169 | results[i++] = property; 170 | } 171 | 172 | return i; 173 | } 174 | 175 | void ISerializationCallbackReceiver.OnAfterDeserialize() 176 | { 177 | this.OnAfterDeserialize(); 178 | this.Compile(); 179 | } 180 | 181 | void ISerializationCallbackReceiver.OnBeforeSerialize() 182 | { 183 | this.OnBeforeSerialize(); 184 | } 185 | 186 | private void Compile() 187 | { 188 | this.typeSet = new HashSet(); 189 | this.dataMap = new Dictionary(); 190 | 191 | Type myType = this.GetType(); 192 | 193 | //Use auto scan of child class: 194 | if (myType != null && myType != typeof(AtomicEntity) && myType != typeof(AtomicObject)) 195 | { 196 | #if UNITY_EDITOR 197 | Profiler.BeginSample("AtomicCompiler", this); 198 | #endif 199 | AtomicEntityInfo entityInfo = AtomicCompiler.CompileEntity(myType); 200 | 201 | //Register types: 202 | this.typeSet.UnionWith(entityInfo.types); 203 | 204 | //Register values: 205 | foreach (ValueInfo valueInfo in entityInfo.values) 206 | { 207 | string id = valueInfo.id; 208 | object value = valueInfo.value(this); 209 | this.dataMap.Add(id, value); 210 | } 211 | 212 | #if UNITY_EDITOR 213 | Profiler.EndSample(); 214 | #endif 215 | } 216 | } 217 | 218 | protected virtual void OnAfterDeserialize() 219 | { 220 | } 221 | 222 | protected virtual void OnBeforeSerialize() 223 | { 224 | } 225 | } 226 | } -------------------------------------------------------------------------------- /Objects/AtomicEntity.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ce813408feca54db9b9f6f465d738d20 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/AtomicObject.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | #if ODIN_INSPECTOR 3 | using Sirenix.OdinInspector; 4 | #endif 5 | 6 | namespace Atomic.Objects 7 | { 8 | public class AtomicObject : AtomicEntity, IMutableAtomicObject, 9 | IAtomicEnable, 10 | IAtomicDisable, 11 | IAtomicUpdate, 12 | IAtomicFixedUpdate, 13 | IAtomicLateUpdate 14 | { 15 | public bool IsEnable => _enabled; 16 | 17 | #if ODIN_INSPECTOR 18 | [Title("Logic"), PropertySpace, PropertyOrder(150)] 19 | [ShowInInspector, HideInEditorMode] 20 | #endif 21 | private readonly List allLogics = new(); 22 | 23 | private readonly List enables = new(); 24 | private readonly List disables = new(); 25 | private readonly List updates = new(); 26 | private readonly List fixedUpdates = new(); 27 | private readonly List lateUpdates = new(); 28 | 29 | private readonly List _enableCache = new(); 30 | private readonly List _disableCache = new(); 31 | private readonly List _updateCache = new(); 32 | private readonly List _fixedUpdateCache = new(); 33 | private readonly List _lateUpdateCache = new(); 34 | 35 | private bool _enabled; 36 | 37 | public void AddLogic(IAtomicLogic target) 38 | { 39 | if (target == null) 40 | { 41 | return; 42 | } 43 | 44 | this.allLogics.Add(target); 45 | 46 | if (target is IAtomicEnable enable) 47 | { 48 | this.enables.Add(enable); 49 | 50 | if (_enabled) 51 | { 52 | enable.Enable(); 53 | } 54 | } 55 | 56 | if (target is IAtomicDisable disable) 57 | { 58 | this.disables.Add(disable); 59 | } 60 | 61 | if (target is IAtomicUpdate update) 62 | { 63 | this.updates.Add(update); 64 | } 65 | 66 | if (target is IAtomicFixedUpdate fixedUpdate) 67 | { 68 | this.fixedUpdates.Add(fixedUpdate); 69 | } 70 | 71 | if (target is IAtomicLateUpdate lateUpdate) 72 | { 73 | this.lateUpdates.Add(lateUpdate); 74 | } 75 | } 76 | 77 | public void RemoveLogic(IAtomicLogic target) 78 | { 79 | if (target == null) 80 | { 81 | return; 82 | } 83 | 84 | if (!this.allLogics.Remove(target)) 85 | { 86 | return; 87 | } 88 | 89 | if (target is IAtomicEnable enable) 90 | { 91 | this.enables.Remove(enable); 92 | } 93 | 94 | if (target is IAtomicUpdate tickable) 95 | { 96 | this.updates.Remove(tickable); 97 | } 98 | 99 | if (target is IAtomicFixedUpdate fixedTickable) 100 | { 101 | this.fixedUpdates.Remove(fixedTickable); 102 | } 103 | 104 | if (target is IAtomicLateUpdate lateTickable) 105 | { 106 | this.lateUpdates.Remove(lateTickable); 107 | } 108 | 109 | if (target is IAtomicDisable disable) 110 | { 111 | if (_enabled) 112 | { 113 | disable.Disable(); 114 | } 115 | } 116 | } 117 | 118 | public IAtomicLogic[] AllLogic() 119 | { 120 | return this.allLogics.ToArray(); 121 | } 122 | 123 | public int AllLogicNonAlloc(IAtomicLogic[] results) 124 | { 125 | int i = 0; 126 | 127 | foreach (var element in this.allLogics) 128 | { 129 | results[i++] = element; 130 | } 131 | 132 | return i; 133 | } 134 | 135 | public IList AllLogicUnsafe() 136 | { 137 | return this.allLogics; 138 | } 139 | 140 | public void Enable() 141 | { 142 | _enabled = true; 143 | 144 | if (this.enables.Count == 0) 145 | { 146 | return; 147 | } 148 | 149 | _enableCache.Clear(); 150 | _enableCache.AddRange(this.enables); 151 | 152 | for (int i = 0, count = _enableCache.Count; i < count; i++) 153 | { 154 | IAtomicEnable enable = _enableCache[i]; 155 | enable.Enable(); 156 | } 157 | } 158 | 159 | public void Disable() 160 | { 161 | if (this.disables.Count == 0) 162 | { 163 | return; 164 | } 165 | 166 | _disableCache.Clear(); 167 | _disableCache.AddRange(this.disables); 168 | 169 | for (int i = 0, count = _disableCache.Count; i < count; i++) 170 | { 171 | IAtomicDisable disable = _disableCache[i]; 172 | disable.Disable(); 173 | } 174 | 175 | _enabled = false; 176 | } 177 | 178 | public void OnUpdate(float deltaTime) 179 | { 180 | if (this.updates.Count == 0) 181 | { 182 | return; 183 | } 184 | 185 | _updateCache.Clear(); 186 | _updateCache.AddRange(this.updates); 187 | 188 | for (int i = 0, count = _updateCache.Count; i < count; i++) 189 | { 190 | IAtomicUpdate update = _updateCache[i]; 191 | update.OnUpdate(deltaTime); 192 | } 193 | } 194 | 195 | public void OnFixedUpdate(float deltaTime) 196 | { 197 | if (this.fixedUpdates.Count == 0) 198 | { 199 | return; 200 | } 201 | 202 | _fixedUpdateCache.Clear(); 203 | _fixedUpdateCache.AddRange(this.fixedUpdates); 204 | 205 | for (int i = 0, count = _fixedUpdateCache.Count; i < count; i++) 206 | { 207 | IAtomicFixedUpdate fixedUpdate = _fixedUpdateCache[i]; 208 | fixedUpdate.OnFixedUpdate(deltaTime); 209 | } 210 | } 211 | 212 | public void OnLateUpdate(float deltaTime) 213 | { 214 | if (this.lateUpdates.Count == 0) 215 | { 216 | return; 217 | } 218 | 219 | _lateUpdateCache.Clear(); 220 | _lateUpdateCache.AddRange(this.lateUpdates); 221 | 222 | for (int i = 0, count = _lateUpdateCache.Count; i < count; i++) 223 | { 224 | IAtomicLateUpdate lateUpdate = _lateUpdateCache[i]; 225 | lateUpdate.OnLateUpdate(deltaTime); 226 | } 227 | } 228 | } 229 | } -------------------------------------------------------------------------------- /Objects/AtomicObject.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 12116c475628c44d7ae161c49931deb6 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Attributes.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 77c180de91b584b238157220f3dbf9df 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Objects/Attributes/GetAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using JetBrains.Annotations; 3 | 4 | namespace Atomic.Objects 5 | { 6 | [MeansImplicitUse] 7 | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] 8 | public sealed class GetAttribute : Attribute 9 | { 10 | internal readonly string Id; 11 | 12 | public GetAttribute(string Id) 13 | { 14 | this.Id = Id; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Objects/Attributes/GetAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8d3c956988fe64c6b994412bdf92bb85 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Attributes/IsAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using JetBrains.Annotations; 3 | 4 | namespace Atomic.Objects 5 | { 6 | [MeansImplicitUse] 7 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] 8 | public sealed class IsAttribute : Attribute 9 | { 10 | internal readonly string[] Types; 11 | 12 | public IsAttribute(params string[] types) 13 | { 14 | this.Types = types; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Objects/Attributes/IsAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1a1f13158d85e4ec5a044a5fcaa36af9 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Interfaces.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d60adc595dd9a4142a080ada18e67d62 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Objects/Interfaces/IAtomicBehaviour.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Atomic.Objects 4 | { 5 | public interface IAtomicBehaviour 6 | { 7 | IAtomicLogic[] AllLogic(); 8 | 9 | int AllLogicNonAlloc(IAtomicLogic[] results); 10 | 11 | IList AllLogicUnsafe(); 12 | } 13 | } -------------------------------------------------------------------------------- /Objects/Interfaces/IAtomicBehaviour.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7d19da0b4a1104395848cb93412ab3e0 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Interfaces/IAtomicEntity.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Atomic.Objects 4 | { 5 | public interface IAtomicEntity 6 | { 7 | T Get(string key) where T : class; 8 | 9 | object Get(string key); 10 | 11 | bool TryGet(string key, out T result) where T : class; 12 | 13 | bool TryGet(string key, out object result); 14 | 15 | KeyValuePair[] AllData(); 16 | 17 | IDictionary AllDataUnsafe(); 18 | 19 | int AllDataNonAlloc(KeyValuePair[] results); 20 | 21 | bool Is(string type); 22 | 23 | bool Is(params string[] types); 24 | 25 | string[] AllTypes(); 26 | 27 | ISet AllTypesUnsafe(); 28 | 29 | int AllTypesNonAlloc(string[] results); 30 | } 31 | } -------------------------------------------------------------------------------- /Objects/Interfaces/IAtomicEntity.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ed1e08920a4874002a6fc1c39ceb1dc7 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Interfaces/IAtomicLogic.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Objects 2 | { 3 | public interface IAtomicLogic 4 | { 5 | } 6 | 7 | public interface IAtomicEnable : IAtomicLogic 8 | { 9 | void Enable(); 10 | } 11 | 12 | public interface IAtomicDisable : IAtomicLogic 13 | { 14 | void Disable(); 15 | } 16 | 17 | public interface IAtomicUpdate : IAtomicLogic 18 | { 19 | void OnUpdate(float deltaTime); 20 | } 21 | 22 | public interface IAtomicFixedUpdate : IAtomicLogic 23 | { 24 | void OnFixedUpdate(float deltaTime); 25 | } 26 | 27 | public interface IAtomicLateUpdate : IAtomicLogic 28 | { 29 | void OnLateUpdate(float deltaTime); 30 | } 31 | } -------------------------------------------------------------------------------- /Objects/Interfaces/IAtomicLogic.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1fc96fae4a89046dfa1d4de8e59b327f 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Interfaces/IAtomicObject.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Objects 2 | { 3 | public interface IAtomicObject : IAtomicBehaviour, IAtomicEntity 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /Objects/Interfaces/IAtomicObject.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: eb7d810f1b20e4c8e862913658b06ba1 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Interfaces/IMutableAtomicEntity.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Atomic.Objects 4 | { 5 | public interface IMutableAtomicEntity : IAtomicEntity 6 | { 7 | bool AddData(string key, object value); 8 | 9 | void SetData(string key, object value); 10 | 11 | bool RemoveData(string key); 12 | 13 | bool RemoveData(string key, out object value); 14 | 15 | void OverrideData(string key, object value, out object prevValue); 16 | 17 | bool AddType(string type); 18 | 19 | void AddTypes(IEnumerable types); 20 | 21 | bool RemoveType(string type); 22 | 23 | void RemoveTypes(IEnumerable types); 24 | } 25 | } -------------------------------------------------------------------------------- /Objects/Interfaces/IMutableAtomicEntity.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e9f1d9fa297bc41fa8e6c9e88b44580f 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Interfaces/IMutableAtomicObject.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Objects 2 | { 3 | public interface IMutableAtomicObject : IAtomicObject, IMutableAtomicEntity, IMutalbleAtomicBehaviour 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /Objects/Interfaces/IMutableAtomicObject.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 617e0c477ec68428397a521c34c7e996 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Interfaces/IMutalbleAtomicBehaviour.cs: -------------------------------------------------------------------------------- 1 | namespace Atomic.Objects 2 | { 3 | public interface IMutalbleAtomicBehaviour : IAtomicBehaviour 4 | { 5 | void AddLogic(IAtomicLogic target); 6 | 7 | void RemoveLogic(IAtomicLogic target); 8 | } 9 | } -------------------------------------------------------------------------------- /Objects/Interfaces/IMutalbleAtomicBehaviour.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0e6f715872ba04571b9f9b918789a3b2 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Internal.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2b29b8387f9144a69adffdbd6453030d 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Objects/Internal/AtomicEntityInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Atomic.Objects 4 | { 5 | internal sealed class AtomicEntityInfo 6 | { 7 | internal readonly IEnumerable types; 8 | internal readonly IEnumerable values; 9 | 10 | internal AtomicEntityInfo(IEnumerable types, IEnumerable values) 11 | { 12 | this.types = types; 13 | this.values = values; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Objects/Internal/AtomicEntityInfo.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 37cd34b94170c4de49614ab0caaed816 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Internal/AtomicScanner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using UnityEngine; 5 | 6 | namespace Atomic.Objects 7 | { 8 | internal static class AtomicScanner 9 | { 10 | private static readonly Dictionary> scannedTypes = new(); 11 | private static readonly Dictionary> scannedValues = new(); 12 | 13 | internal static IEnumerable ScanTypes(Type target) 14 | { 15 | if (scannedTypes.TryGetValue(target, out IEnumerable types)) 16 | { 17 | return types; 18 | } 19 | 20 | types = ScanTypesInternal(target); 21 | scannedTypes.Add(target, types); 22 | return types; 23 | } 24 | 25 | internal static IEnumerable ScanValues(Type target) 26 | { 27 | if (scannedValues.TryGetValue(target, out IEnumerable references)) 28 | { 29 | return references; 30 | } 31 | 32 | references = ScanValuesInternal(target); 33 | scannedValues.Add(target, references); 34 | return references; 35 | } 36 | 37 | private static IEnumerable ScanTypesInternal(Type target) 38 | { 39 | IsAttribute attribute = target.GetCustomAttribute(); 40 | 41 | if (attribute != null) 42 | { 43 | return attribute.Types; 44 | } 45 | 46 | return Array.Empty(); 47 | } 48 | 49 | private static IEnumerable ScanValuesInternal(Type target) 50 | { 51 | var result = new List(); 52 | 53 | FieldInfo[] fields = ReflectionUtils.GetFields(target); 54 | 55 | for (int i = 0, count = fields.Length; i < count; i++) 56 | { 57 | FieldInfo field = fields[i]; 58 | GetAttribute attribute = field.GetCustomAttribute(); 59 | 60 | if (attribute == null) 61 | { 62 | continue; 63 | } 64 | 65 | var reference = new ValueInfo(attribute.Id, field.GetValue); 66 | result.Add(reference); 67 | } 68 | 69 | PropertyInfo[] properties = ReflectionUtils.GetProperties(target); 70 | for (int i = 0, count = properties.Length; i < count; i++) 71 | { 72 | PropertyInfo property = properties[i]; 73 | GetAttribute attribute = property.GetCustomAttribute(); 74 | 75 | if (attribute == null) 76 | { 77 | continue; 78 | } 79 | 80 | var reference = new ValueInfo(attribute.Id, property.GetValue); 81 | result.Add(reference); 82 | } 83 | 84 | return result; 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /Objects/Internal/AtomicScanner.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7b53f5436e9f94a8cb00576bff76aea9 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Internal/ReflectionUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | 5 | namespace Atomic.Objects 6 | { 7 | public static class ReflectionUtils 8 | { 9 | private static readonly Dictionary fieldsMap = new(); 10 | private static readonly Dictionary propertiesMap = new(); 11 | private static readonly Dictionary methodsMap = new(); 12 | 13 | internal static FieldInfo[] GetFields(Type targetType) 14 | { 15 | if (fieldsMap.TryGetValue(targetType, out var fields)) 16 | { 17 | return fields; 18 | } 19 | 20 | fields = targetType.GetFields( 21 | BindingFlags.Instance | 22 | BindingFlags.Public | 23 | BindingFlags.NonPublic | 24 | BindingFlags.DeclaredOnly 25 | ); 26 | 27 | fieldsMap.Add(targetType, fields); 28 | return fields; 29 | } 30 | 31 | internal static System.Reflection.PropertyInfo[] GetProperties(Type targetType) 32 | { 33 | if (propertiesMap.TryGetValue(targetType, out var properties)) 34 | { 35 | return properties; 36 | } 37 | 38 | properties = targetType.GetProperties( 39 | BindingFlags.Instance | 40 | BindingFlags.Public | 41 | BindingFlags.NonPublic | 42 | BindingFlags.DeclaredOnly 43 | ); 44 | 45 | propertiesMap.Add(targetType, properties); 46 | return properties; 47 | } 48 | 49 | internal static MethodInfo[] GetMethods(Type targetType) 50 | { 51 | if (methodsMap.TryGetValue(targetType, out var methods)) 52 | { 53 | return methods; 54 | } 55 | 56 | methods = targetType.GetMethods( 57 | BindingFlags.Instance | 58 | BindingFlags.Public | 59 | BindingFlags.NonPublic | 60 | BindingFlags.DeclaredOnly 61 | ); 62 | 63 | methodsMap.Add(targetType, methods); 64 | return methods; 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /Objects/Internal/ReflectionUtils.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8cdb5b51e923a49198ee8a936927707e 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Internal/ValueInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Atomic.Objects 4 | { 5 | internal sealed class ValueInfo 6 | { 7 | internal readonly string id; 8 | internal readonly Func value; 9 | 10 | internal ValueInfo(string id, Func value) 11 | { 12 | this.id = id; 13 | this.value = value; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Objects/Internal/ValueInfo.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b187e2113b0344d2e83ccd5a924d91ac 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Proxy.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 128c1357aa8a24ad288aa2df2477db54 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Objects/Proxy/AtomicEntityProxy.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | 4 | namespace Atomic.Objects 5 | { 6 | public class AtomicEntityProxy : MonoBehaviour, IMutableAtomicEntity 7 | { 8 | public AtomicEntity Source 9 | { 10 | get => this.source; 11 | set => this.source = value; 12 | } 13 | 14 | [SerializeField] 15 | private AtomicEntity source; 16 | 17 | public T Get(string key) where T : class 18 | { 19 | return this.source.Get(key); 20 | } 21 | 22 | public object Get(string key) 23 | { 24 | return this.source.Get(key); 25 | } 26 | 27 | public bool TryGet(string key, out T result) where T : class 28 | { 29 | return this.source.TryGet(key, out result); 30 | } 31 | 32 | public bool TryGet(string key, out object result) 33 | { 34 | return this.source.TryGet(key, out result); 35 | } 36 | 37 | public bool Is(string type) 38 | { 39 | return this.source.Is(type); 40 | } 41 | 42 | public bool Is(params string[] types) 43 | { 44 | return this.source.Is(types); 45 | } 46 | 47 | public string[] AllTypes() 48 | { 49 | return this.source.AllTypes(); 50 | } 51 | 52 | public KeyValuePair[] AllData() 53 | { 54 | return this.source.AllData(); 55 | } 56 | 57 | public ISet AllTypesUnsafe() 58 | { 59 | return this.source.AllTypesUnsafe(); 60 | } 61 | 62 | public IDictionary AllDataUnsafe() 63 | { 64 | return this.source.AllDataUnsafe(); 65 | } 66 | 67 | public int AllTypesNonAlloc(string[] results) 68 | { 69 | return this.source.AllTypesNonAlloc(results); 70 | } 71 | 72 | public int AllDataNonAlloc(KeyValuePair[] results) 73 | { 74 | return this.source.AllDataNonAlloc(results); 75 | } 76 | 77 | public bool AddData(string key, object value) 78 | { 79 | return this.source.AddData(key, value); 80 | } 81 | 82 | public void SetData(string key, object value) 83 | { 84 | this.source.SetData(key, value); 85 | } 86 | 87 | public bool RemoveData(string key) 88 | { 89 | return this.source.RemoveData(key); 90 | } 91 | 92 | public bool RemoveData(string key, out object value) 93 | { 94 | return this.source.RemoveData(key, out value); 95 | } 96 | 97 | public void OverrideData(string key, object value, out object prevValue) 98 | { 99 | this.source.OverrideData(key, value, out prevValue); 100 | } 101 | 102 | public bool AddType(string type) 103 | { 104 | return this.source.AddType(type); 105 | } 106 | 107 | public void AddTypes(IEnumerable types) 108 | { 109 | this.source.AddTypes(types); 110 | } 111 | 112 | public bool RemoveType(string type) 113 | { 114 | return this.source.RemoveType(type); 115 | } 116 | 117 | public void RemoveTypes(IEnumerable types) 118 | { 119 | this.source.RemoveTypes(types); 120 | } 121 | } 122 | } -------------------------------------------------------------------------------- /Objects/Proxy/AtomicEntityProxy.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e6679ec8ab2f344298ebb586a936d050 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Objects/Proxy/AtomicObjectProxy.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | // ReSharper disable UnusedMember.Global 4 | 5 | namespace Atomic.Objects 6 | { 7 | public class AtomicObjectProxy : MonoBehaviour, IMutableAtomicObject 8 | { 9 | public AtomicObject Source 10 | { 11 | get => this.source; 12 | set => this.source = value; 13 | } 14 | 15 | [SerializeField] 16 | private AtomicObject source; 17 | 18 | public T Get(string key) where T : class 19 | { 20 | return this.source.Get(key); 21 | } 22 | 23 | public bool TryGet(string key, out T result) where T : class 24 | { 25 | return this.source.TryGet(key, out result); 26 | } 27 | 28 | public object Get(string key) 29 | { 30 | return this.source.Get(key); 31 | } 32 | 33 | public bool TryGet(string key, out object result) 34 | { 35 | return this.source.TryGet(key, out result); 36 | } 37 | 38 | public KeyValuePair[] AllData() 39 | { 40 | return this.source.AllData(); 41 | } 42 | 43 | public ISet AllTypesUnsafe() 44 | { 45 | return source.AllTypesUnsafe(); 46 | } 47 | 48 | public IDictionary AllDataUnsafe() 49 | { 50 | return source.AllDataUnsafe(); 51 | } 52 | 53 | public int AllTypesNonAlloc(string[] results) 54 | { 55 | return source.AllTypesNonAlloc(results); 56 | } 57 | 58 | public int AllDataNonAlloc(KeyValuePair[] results) 59 | { 60 | return source.AllDataNonAlloc(results); 61 | } 62 | 63 | public bool Is(string type) 64 | { 65 | return this.source.Is(type); 66 | } 67 | 68 | public bool Is(params string[] types) 69 | { 70 | return this.source.Is(types); 71 | } 72 | 73 | public string[] AllTypes() 74 | { 75 | return this.source.AllTypes(); 76 | } 77 | 78 | public void AddLogic(IAtomicLogic target) 79 | { 80 | this.source.AddLogic(target); 81 | } 82 | 83 | public void RemoveLogic(IAtomicLogic target) 84 | { 85 | this.source.RemoveLogic(target); 86 | } 87 | 88 | public IAtomicLogic[] AllLogic() 89 | { 90 | return this.source.AllLogic(); 91 | } 92 | 93 | public int AllLogicNonAlloc(IAtomicLogic[] results) 94 | { 95 | return this.source.AllLogicNonAlloc(results); 96 | } 97 | 98 | public IList AllLogicUnsafe() 99 | { 100 | return this.source.AllLogicUnsafe(); 101 | } 102 | 103 | public bool AddData(string key, object value) 104 | { 105 | return this.source.AddData(key, value); 106 | } 107 | 108 | public void SetData(string key, object value) 109 | { 110 | this.source.SetData(key, value); 111 | } 112 | 113 | public bool RemoveData(string key) 114 | { 115 | return this.source.RemoveData(key); 116 | } 117 | 118 | public bool RemoveData(string key, out object value) 119 | { 120 | return this.source.RemoveData(key, out value); 121 | } 122 | 123 | public void OverrideData(string key, object value, out object prevValue) 124 | { 125 | this.source.OverrideData(key, value, out prevValue); 126 | } 127 | 128 | public bool AddType(string type) 129 | { 130 | return this.source.AddType(type); 131 | } 132 | 133 | public void AddTypes(IEnumerable types) 134 | { 135 | this.source.AddTypes(types); 136 | } 137 | 138 | public bool RemoveType(string type) 139 | { 140 | return this.source.RemoveType(type); 141 | } 142 | 143 | public void RemoveTypes(IEnumerable types) 144 | { 145 | this.source.RemoveTypes(types); 146 | } 147 | } 148 | } -------------------------------------------------------------------------------- /Objects/Proxy/AtomicObjectProxy.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 05c0c1bded8fd4efea4d122573a99ab3 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Official](https://img.shields.io/badge/official-%20) 2 | [![GitHub release (latest by date)](https://img.shields.io/github/v/release/starkre22/unity-atomic?color=orange)](https://github.com/starkre22/unity-atomic/releases) 3 | 4 | 5 | > [!IMPORTANT] 6 | > I have started distributing an evolved version of unity-atomic in [StarKRE22/Atomic](https://github.com/StarKRE22/Atomic), please use it instead of unity-atomic. 7 | 8 | Unity Atomic - Atomic Extensions for Unity 9 | === 10 | Created by Igor Gulkin (@StarKRE22) 11 | 12 | What is Unity Atomic? 13 | --- 14 | The atomic approach is an object-oriented approach for game object development in Unity Engine, representing a game object as a composition of atomic data and functions. The atomic object provides a common interface for interacting with it's properties and functions of various entities, and also has the ability to change the structure of an object at runtime. 15 | 16 | Release Notes, see [unity-atomic/releases](https://github.com/StarKRE22/unity-atomic/releases) 17 | 18 | ## Table of Contents 19 | - [Installation](#installation) 20 | - [Quick Start](#quick-start) 21 | - [Documentation](#documentation) 22 | - [Structures](#structures) 23 | - [AtomicValue](#atomic-value) 24 | - [AtomicVariable](#atomic-variable) 25 | - [AtomicAction](#atomic-action) 26 | - [AtomicEvent](#atomic-event) 27 | - [AtomicFunction](#atomic-function) 28 | - [AtomicProperty](#atomic-property) 29 | - [AtomicObservable](#atomic-observable) 30 | - [AtomicPropertyObservable](#atomic-property-observable) 31 | - [AtomicSetter](#atomic-setter) 32 | - [AtomicExpressions](#atomic-expressions) 33 | - [Objects](#objects) 34 | - [Atomic Entity](#atomic-entity) 35 | - [Atomic Object](#atomic-object) 36 | - [Contracts](#contracts) 37 | - [Extensions](#extensions) 38 | - [Structures](extensions-for-structures) 39 | - [Objects](extensions-for-objects) 40 | - [Good Practices](#good-practices) 41 | - [Reusing of object structure](#reusing-of-object-structure) 42 | - [Division into sections](#division-into-sections) 43 | - [Reflection Baking](#reflection-baking) 44 | 45 | ## Installation 46 | 47 | **There are 3 ways of installation:** 48 | 49 | _1. Download code from repository_ 50 | 51 | _2. Download last [Atomic.unitypackage](https://github.com/StarKRE22/unity-atomic/releases/download/ver-2.0/Atomic.unitypackage) from [release notes](https://github.com/StarKRE22/unity-atomic/releases)_ 52 | 53 | _3. Install via Unity Package Manager_ 54 | 55 | ``` 56 | "com.starkre.unity-atomic": "https://github.com/StarKRE22/unity-atomic.git" 57 | ``` 58 | 59 | ## Quick Start 60 | In this section you will see the basic functionality of how you can create a character class and interact with it. 61 | 62 | _Let's create a Character class with health mechanics:_ 63 | ```csharp 64 | [Is("Damagable")] 65 | public sealed class Character : AtomicEntity 66 | { 67 | [Get("Health")] 68 | [SerializeField] 69 | private AtomicVariable health; 70 | 71 | [Get("TakeDamage")] 72 | [SerializeField] 73 | private AtomicAction takeDamageAction; 74 | 75 | [Get("DeathEvent")] 76 | [SerializeField] 77 | private AtomicEvent deathEvent; 78 | 79 | private void Awake() 80 | { 81 | //Declare damage action 82 | this.takeDamageAction.Compose(damage => 83 | { 84 | this.health.Value -= damage; 85 | }); 86 | 87 | //Declare death mechanics 88 | this.health.Subscribe(value => 89 | { 90 | if (value <= 0) this.deathEvent.Invoke(); 91 | }); 92 | } 93 | } 94 | ``` 95 | 96 | _Then you can see examples below of how you can interact with character properties:_ 97 | ```csharp 98 | 99 | IAtomicEntity character = gameObject.GetComponent(); 100 | 101 | //Check if damagable object 102 | bool isDamagable = character.Is("Damagable"); 103 | 104 | //Get health 105 | int health = character.GetValue("Health").Value; 106 | 107 | //Set health 108 | character.SetVariable("Health", 5); 109 | 110 | //Deal damage 111 | character.InvokeAction("TakeDamage", 2); 112 | 113 | //Subscribe on death 114 | character.SubscribeOnEvent("DeathEvent", () => Debug.Log("I'm dead!")); 115 | ``` 116 | 117 | These were simple examples demonstrating the basic capabilities of the atomic approach. 118 | For more detail information look in the documentation section. 119 | 120 | Documentation 121 | === 122 | In this section you will see full capabilities of the atomic approach with theory. 123 | 124 | ## Structures 125 | 126 | There are several different atomic structures in the library that may be required to develop game objects 127 | - [AtomicValue](#atomic-value) 128 | - [AtomicVariable](#atomic-variable) 129 | - [AtomicAction](#atomic-action) 130 | - [AtomicEvent](#atomic-event) 131 | - [AtomicFunction](#atomic-function) 132 | - [AtomicProperty](#atomic-property) 133 | - [AtomicObservable](#atomic-observable) 134 | - [AtomicPropertyObservable](#atomic-property-observable) 135 | - [AtomicSetter](#atomic-setter) 136 | - [AtomicExpressions](#atomic-expressions) 137 | 138 | ### Atomic Value 139 | Represents a read-only property (See class: [AtomicValue](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicValue.cs)) 140 | 141 | ```csharp 142 | var damage = new AtomicValue(5.0f); 143 | float damageValue = damage.Value; //5.0f 144 | ``` 145 | 146 | Example of using as speed property 147 | 148 | ```csharp 149 | public sealed class Character : MonoBehaviour 150 | { 151 | public IAtomicValue Speed => this.speed; 152 | 153 | [SerializeField] 154 | private AtomicValue speed = new(3.0f); 155 | } 156 | ``` 157 | 158 | ### Atomic Variable 159 | Represents a read-write reactive property (See class: [AtomicVariable](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicVariable.cs)) 160 | 161 | ```csharp 162 | var health = new AtomicVariable(5); 163 | int healthValue = health.Value; 164 | 165 | health.Value = 3; 166 | health.Subscribe(value => Debug.Log($"On health changed {value}")); 167 | health.Dispose(); //Unsubscribe all listeners 168 | ``` 169 | 170 | Example of using as health property 171 | 172 | ```csharp 173 | public sealed class Character : MonoBehaviour 174 | { 175 | public IAtomicVariableObservable Health => this.health; 176 | 177 | [SerializeField] 178 | private AtomicVariable health = new(5); 179 | } 180 | ``` 181 | 182 | ### Atomic Action 183 | Represents a method object (See class: [AtomicAction](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicAction.cs)) 184 | 185 | ```csharp 186 | var takeDamageAction = new AtomicAction(damage => Debug.Log($"Take Damage {damage}")) 187 | takeDamageAction.Invoke(5); 188 | ``` 189 | 190 | Example of using action 191 | 192 | ```csharp 193 | public sealed class Character : MonoBehaviour 194 | { 195 | public IAtomicAction TakeDamageAction => this.takeDamageAction; 196 | 197 | [SerializeField] 198 | private AtomicAction takeDamageAction; 199 | 200 | [SerializeField] 201 | private AtomicVariable health = new(100); 202 | 203 | private void Awake() 204 | { 205 | //Init take damage action 206 | this.takeDamageAction.Compose(damage => this.health.Value -= damage); 207 | } 208 | } 209 | ``` 210 | 211 | ### Atomic Event 212 | Represents an event object (See class: [AtomicEvent](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicEvent.cs)) 213 | 214 | ```csharp 215 | var deathEvent = new AtomicEvent(); 216 | deathEvent.Subscribe(() => Debug.Log("I'm dead!")) 217 | deathEvent.Invoke(); 218 | deathEvent.Dispose(); //Unsubscribe all listeners 219 | ``` 220 | 221 | Example of using event 222 | 223 | ```csharp 224 | public sealed class Character : MonoBehaviour 225 | { 226 | public IAtomicObservable DamageTakenEvent => this.damageTakenEvent; 227 | 228 | [SerializeField] 229 | private AtomicAction takeDamageAction; 230 | 231 | [SerializeField] 232 | private AtomicVariable health = new(100); 233 | 234 | [SerializeField] 235 | private AtomicEvent damageTakenEvent; 236 | 237 | private void Awake() 238 | { 239 | //Init take damage action 240 | this.takeDamageAction.Compose(damage => { 241 | this.health.Value -= damage; 242 | this.damageTakenEvent.Invoke(damage); 243 | }); 244 | } 245 | } 246 | ``` 247 | 248 | ### Atomic Function 249 | Represents a function object (See class: [AtomicFunction](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicFunction.cs)) 250 | 251 | ```csharp 252 | var positionFunc = new AtomicFunction(() => gameObject.transform.position); 253 | Vector3 position = positionFunc.Invoke(); 254 | ``` 255 | 256 | Example of using function 257 | 258 | ```csharp 259 | public sealed class Character : MonoBehaviour 260 | { 261 | public IAtomicValue Position => this.positionFunc; 262 | 263 | [SerializeField] 264 | private AtomicFunction positionFunc; 265 | 266 | private void Awake() 267 | { 268 | //Init function 269 | this.positionFunc.Compose(() => this.transform.position); 270 | } 271 | } 272 | ``` 273 | 274 | ### Atomic Property 275 | 276 | Provides read-write interface to a specified source (See class: [AtomicProperty](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicProperty.cs)) 277 | 278 | ```csharp 279 | var positionProperty = new AtomicProperty( 280 | () => transform.position, 281 | value => transform.position = value 282 | ); 283 | 284 | Vector3 position = positionProperty.Value; 285 | positionProperty.Value = new Vector3(10, 10, 10); 286 | ``` 287 | 288 | Example of using property 289 | 290 | ```csharp 291 | public sealed class Character : MonoBehaviour 292 | { 293 | public IAtomicVariable Position => this.positionProperty; 294 | 295 | [SerializeField] 296 | private AtomicFunction positionProperty; 297 | 298 | private void Awake() 299 | { 300 | //Init property 301 | this.positionFunc.Compose( 302 | () => this.transform.position, 303 | value => this.transform.position = value 304 | ); 305 | } 306 | } 307 | ``` 308 | 309 | ### Atomic Observable 310 | 311 | Provides observable interface to a specified source (See 312 | class: [AtomicObservable](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicObservable.cs)) 313 | 314 | Example of using observable 315 | ```csharp 316 | public sealed class Character : MonoBehaviour 317 | { 318 | public IAtomicObservable WeaponChangedEvent => this.weaponChangedEvent; 319 | 320 | [SerializeField] 321 | private AtomicObservable weaponChangedEvent; 322 | 323 | [Inject] 324 | private IWeaponManager weaponManager; 325 | 326 | private void Awake() 327 | { 328 | this.weaponChangedEvent.Compose( 329 | weapon => this.weaponManager.OnChanged += weapon, 330 | weapon => this.weaponManager.OnChanged -= weapon 331 | ); 332 | } 333 | } 334 | 335 | public interface IWeaponManager 336 | { 337 | event Action OnChanged; 338 | } 339 | ``` 340 | 341 | ### Atomic Property Observable 342 | 343 | Combines Atomic Property & Atomic Observable (See 344 | class: [AtomicPropertyObservable](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicPropertyObservable.cs)) 345 | 346 | ### Atomic Setter 347 | Provides setter interface to a specified source. 348 | (See class: [AtomicSetter](https://github.com/StarKRE22/unity-atomic/blob/master/Elements/Implementations/AtomicSetter.cs)) 349 | 350 | ### Atomic Expressions 351 | 352 | Atomic expressions are one of the most powerful extensions of the atomic approach. Provide the ability to make composites of data in the form of sum, product, logical and/or. 353 | 354 | For example, let's assume that we want to make the character move forward at a speed that can be changed by different multipliers. Let there be a trigger effector as an object that will change the character's speed 355 | 356 | To do this, create an atomic expression of speed in the character class 357 | 358 | ```csharp 359 | public sealed class Character : MonoBehaviour 360 | { 361 | public IAtomicExpression MoveSpeed => this.moveSpeedExpression; 362 | 363 | [SerializeField] 364 | private AtomicFloatProduct moveSpeedExpression; 365 | 366 | [SerializeField] 367 | private AtomicValue moveSpeedBase = new(3); 368 | 369 | private void Awake() 370 | { 371 | //Add base speed to move expression: 372 | this.moveSpeedExpression.Append(this.moveSpeedBase); 373 | } 374 | 375 | private void FixedUpdate() 376 | { 377 | //Move character forward: 378 | Vector3 moveStep = this.moveSpeedExpression.Invoke() * Vector3.forward * Time.fixedDeltaTime; 379 | this.transform.position += moveStep; 380 | } 381 | } 382 | ``` 383 | 384 | ```csharp 385 | //Increases character speed for 2 times: 386 | public sealed class SpeedAreaEffector : MonoBehaviour 387 | { 388 | [SerializeField] 389 | private AtomicValue speedMultiplier = new(2); 390 | 391 | private void OnTriggerEnter(Collider other) 392 | { 393 | if (other.TryGetComponent(out Character character)) 394 | { 395 | character.MoveSpeed.Append(this.speedMultiplier); 396 | } 397 | } 398 | 399 | private void OnTriggerExit(Collider other) 400 | { 401 | if (other.TryGetComponent(out Character character)) 402 | { 403 | character.MoveSpeed.Remove(this.speedMultiplier); 404 | } 405 | } 406 | } 407 | ``` 408 | 409 | Thus, every time the character enters the trigger, his speed will double. 410 | 411 | There are other expressions that you can check out in the library: 412 | - [AtomicAnd](https://github.com/StarKRE22/unity-atomic/blob/master/Extensions/Expressions/AtomicAnd.cs) 413 | - [AtomicOr](https://github.com/StarKRE22/unity-atomic/blob/master/Extensions/Expressions/AtomicOr.cs) 414 | - [AtomicIntSum](https://github.com/StarKRE22/unity-atomic/blob/master/Extensions/Expressions/AtomicIntSum.cs) 415 | - [AtomicIntProduct](https://github.com/StarKRE22/unity-atomic/blob/master/Extensions/Expressions/AtomicIntProduct.cs) 416 | - [AtomicFloatSum](https://github.com/StarKRE22/unity-atomic/blob/master/Extensions/Expressions/AtomicFloatSum.cs) 417 | - [AtomicFloatProduct](https://github.com/StarKRE22/unity-atomic/blob/master/Extensions/Expressions/AtomicFloatProduct.cs) 418 | 419 | 420 | Objects 421 | ---- 422 | Let's now see how we can work with our game objects using the atomic approach. 423 | There are two objects in the library that represent the game object: [AtomicEntity](atomic-entity) & [AtomicObject](atomic-object) 424 | 425 | ### Atomic Entity 426 | //TODO 427 | 428 | ### Atomic Object 429 | //TODO 430 | 431 | 432 | Contracts 433 | --- 434 | To make easier development for the team with identifiers and types, it is better to put them into separate constants and add the [Contract] attribute, which will explicitly indicate which type is expected for a specific key. 435 | 436 | ```csharp 437 | //File for keeping object ids 438 | public static class ObjectAPI 439 | { 440 | //Specify expecting type 441 | [Contract(typeof(IAtomicVariableObservable))] 442 | public const string Health = nameof(Health); 443 | 444 | [Contract(typeof(IAtomicAction))] 445 | public const string TakeDamageAction = nameof(TakeDamageAction); 446 | 447 | [Contract(typeof(IAtomicObservable))] 448 | public const string DeathEvent = nameof(DeathEvent); 449 | } 450 | 451 | //File for keeping object types 452 | public static class ObjectType 453 | { 454 | public const string Damagable = nameof(Damagable); 455 | public const string Moveable = nameof(Moveable); 456 | public const string Jumpable = nameof(Jumpable); 457 | } 458 | ``` 459 | 460 | ```csharp 461 | 462 | //Interact with object properties by contract ids 463 | bool isDamagable = character.Is(ObjectType.Damagable); 464 | 465 | int health = character.GetValue(ObjectAPI.Health).Value; 466 | 467 | character.InvokeAction(ObjectAPI.TakeDamageAction, 2); 468 | 469 | character.SubscribeOnEvent(ObjectAPI.DeathEvent,()=> Debug.Log("I'm dead!")); 470 | ``` 471 | 472 | Extensions 473 | --- 474 | ### Extensions for structures 475 | ### Extensions for objects 476 | 477 | //TODO: 478 | 479 | Good Practices 480 | === 481 | In this section, I would like to share good practices on how to develop game objects using the atomic approach as sustainably as possible. 482 | One of the key thoughts is that you can always put any block of code that you repeat into a separate class and assign responsibility to it. 483 | 484 | Reusing of object structure 485 | --- 486 | **If you need reuse game mechanics between different objects then you can create common component** 487 | 488 | For example you need to reuse heath mechanics between Character and Tower. 489 | To do this, you can make HealthComponent that will contain health data, damage action, and death event: 490 | 491 | ```csharp 492 | //Create common health component 493 | [Serializable] 494 | public sealed class HealthComponent 495 | { 496 | public IAtomicVariableObservable Health => this.health; 497 | public IAtomicAction TakeDamageAction => this.takeDamageAction; 498 | public IAtomicObservable DeathEvent => this.deathEvent; 499 | 500 | [SerializeField] 501 | private AtomicVariable health; 502 | 503 | [SerializeField] 504 | private AtomicAction takeDamageAction; 505 | 506 | [SerializeField] 507 | private AtomicEvent deathEvent; 508 | 509 | public void Compose() 510 | { 511 | //Declare damage action 512 | this.takeDamageAction.Compose(damage => { this.health.Value -= damage; }); 513 | 514 | //Declare death mechanics 515 | this.health.Subscribe(value => 516 | { 517 | if (value <= 0) this.deathEvent.Invoke(); 518 | }); 519 | } 520 | } 521 | ``` 522 | 523 | **Reuse health component between Tower and Character** 524 | 525 | ```csharp 526 | [Is(ObjectType.Damagable)] 527 | public sealed class Tower : AtomicEntity 528 | { 529 | #region INTERFACE 530 | 531 | [Get(ObjectAPI.TakeDamageAction)] 532 | public IAtomicAction TakeDamageAction => this.healthComponent.TakeDamageAction; 533 | 534 | #endregion 535 | 536 | #region CORE 537 | 538 | [SerializeField] 539 | private HealthComponent healthComponent; 540 | 541 | private void Awake() 542 | { 543 | this.healthComponent.Compose(); 544 | } 545 | 546 | #endregion 547 | } 548 | 549 | [Is(ObjectType.Damagable)] 550 | public sealed class Character : AtomicEntity 551 | { 552 | #region INTERFACE 553 | 554 | ///Health 555 | [Get(ObjectAPI.Health)] 556 | public IAtomicVariableObservable Health => this.healthComponent.Health; 557 | 558 | [Get(ObjectAPI.TakeDamageAction)] 559 | public IAtomicAction TakeDamageAction => this.healthComponent.TakeDamageAction; 560 | 561 | [Get(ObjectAPI.DeathEvent)] 562 | public IAtomicObservable DeathEvent => this.healthComponent.DeathEvent; 563 | 564 | ///Movement 565 | [Get(ObjectAPI.MoveAction)] 566 | public IAtomicAction MoveAction => this.moveAction; 567 | 568 | #endregion 569 | 570 | #region CORE 571 | 572 | [SerializeField] 573 | private HealthComponent healthComponent; 574 | 575 | [SerializeField] 576 | private AtomicAction moveAction; 577 | 578 | private void Awake() 579 | { 580 | this.healthComponent.Compose(); 581 | this.moveAction.Compose(offset => this.transform.Translate(offset)); 582 | } 583 | 584 | #endregion 585 | } 586 | ``` 587 | Division into sections 588 | --- 589 | //TODO 590 | 591 | Reflection Baking 592 | --- 593 | //TODO 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | Add properties at runtime 604 | --- 605 | 606 | ```csharp 607 | IMutableAtomicEntity character = gameObject.GetComponent(); 608 | 609 | //Make character invisible 610 | character.AddType("Invisible"); 611 | 612 | //Add resource bag to the character 613 | character.AddData("ResourceBag", new AtomicVariable()); 614 | 615 | //Remove jump ability 616 | character.RemoveData("JumpAction"); 617 | ``` 618 | 619 | Add mechanics at runtime 620 | --- 621 | For example you wanna add movement mechanics towards direction for your character. 622 | 623 | First of all, extend Character from AtomicObject. 624 | 625 | ```csharp 626 | public sealed class Character : AtomicObject //derived from MonoBehaviour 627 | { 628 | [Get("Transform")] 629 | public Transform mainTrainsform; 630 | 631 | //Define when Fixed Update required for AtomicObject 632 | private void FixedUpdate() 633 | { 634 | base.OnFixedUpdate(Time.fixedDeltaTime); 635 | } 636 | } 637 | ``` 638 | 639 | Create MovementMechanics class and implement IAtomicFixedUpdate interface which support FixedUpdate of AtomicObject 640 | 641 | ```csharp 642 | public sealed class MovementMechanics : IAtomicFixedUpdate 643 | { 644 | private readonly IAtomicEntity entity; 645 | 646 | public MovementMechanics(IAtomicEntity entity) 647 | { 648 | this.entity = entity; 649 | } 650 | 651 | public void OnFixedUpdate(float deltaTime) 652 | { 653 | if (!entity.TryGet("Transform", out Transform transform) || 654 | !entity.TryGet("MoveSpeed", out IAtomicValue moveSpeed) || 655 | !entity.TryGet("MoveDirection", out IAtomicValue moveDirection)) 656 | { 657 | return; 658 | } 659 | 660 | Vector3 offset = moveDirection.Value * (moveSpeed.Value * deltaTime); 661 | transform.Translate(offset); 662 | } 663 | } 664 | ``` 665 | 666 | Add movement mechanics to your Character 667 | 668 | ```csharp 669 | IMutableAtomicObject character = gameObject.GetComponent(); 670 | 671 | //Add movement mechanics at runtime 672 | character.AddData("MoveSpeed", new AtomicVariable(3)); 673 | character.AddData("MoveDirection", new AtomicVariable(Vector3.forward)); 674 | character.AddLogic(new MovementMechanics(character)); 675 | 676 | //Remove movement mechanics at runtime 677 | character.RemoveLogic(); 678 | ``` 679 | 680 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 350c14109bc7349f88fceadd90b5c2d1 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.starkre.unity-atomic", 3 | "author": "Igor Gulkin", 4 | "displayName": "Unity Atomic", 5 | "unity": "2020.3", 6 | "version": "2.0.0", 7 | "keywords": [ 8 | "atomic" 9 | ], 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/StarKRE22/unity-atomic.git" 13 | }, 14 | 15 | "description": "The atomic approach is an object-oriented approach for game object development, representing a game object as a composition of atomic data and functions. The atomic object provides a common interface for interacting with it's properties and functions of various entities, and also has the ability to change the structure of an object at runtime." 16 | } -------------------------------------------------------------------------------- /package.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 727bc09298ab24cc0a211bf0f50be0e5 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | --------------------------------------------------------------------------------