|
6 | 6 |
|
7 | 7 | # Entity Component System |
8 | 8 |
|
9 | | -[]() |
| 9 | +[]() |
10 | 10 | []() |
11 | 11 | []() |
12 | 12 |
|
@@ -54,205 +54,85 @@ It's recommended to build your entire project around these life cycle methods. |
54 | 54 |
|
55 | 55 | <img src="https://raw.githubusercontent.com/elraccoone/unity-entity-component-system/master/.github/WIKI/lifecycle.png" width="100%"></br> |
56 | 56 |
|
57 | | -## Classes, Methods and Properties |
58 | | - |
59 | | -Use build in file generator to create new instances for any of these types. |
60 | | - |
61 | | -```cs |
62 | | -/// Base class for every controller. |
63 | | -/// NOTE: This class allows the usage of [Injected] systems, services and controller. |
64 | | -/// NOTE: This class allows the usage of [Asset] properties. |
65 | | -abstract class Controller { |
66 | | - |
67 | | - /// A reference to the controller. |
68 | | - static Controller Instance { get; }; |
69 | | - |
70 | | - /// Method invoked when the controller is initializing. |
71 | | - virtual void OnInitialize (); |
72 | | - /// Method invoked when the controller is initialized. |
73 | | - virtual void OnInitialized (); |
74 | | - /// Method invoked when the controller updates, will be called every frame. |
75 | | - virtual void OnUpdate (); |
76 | | - |
77 | | - /// Register your systems and services to the controller. This can only be |
78 | | - /// done during 'OnInitialize' cycle. |
79 | | - void Register (params System.Type[] typesOf); |
80 | | - /// Enables or disabled a system, enabling the systems allows them to invoke |
81 | | - /// their cycle methods such as OnUpdate, OnPhysics, OnDrawGui and others. |
82 | | - void SetSystemEnabled<S> (bool value); |
83 | | - /// Returns whether a system is enabled. |
84 | | - bool IsSystemEnabled<S> (); |
85 | | - /// Gets a system from this controller. |
86 | | - S GetSystem<S> () where S : IEntitySystem, new(); |
87 | | - /// Gets a system from this controller. |
88 | | - System.Object GetSystem (System.Type typeOf); |
89 | | - /// Check whether this controller has a system. |
90 | | - bool HasSystem<S> () where S : IEntitySystem, new(); |
91 | | - /// Gets a service from this controller. |
92 | | - S GetService<S> () where S : IService, new(); |
93 | | - /// Gets a system from this controller. |
94 | | - System.Object GetService (System.Type typeOf); |
95 | | - /// Check whether this controller has a service. |
96 | | - bool HasService<S> () where S : IService, new(); |
97 | | - /// Gets an asset from this controller. |
98 | | - AssetType GetAsset<AssetType> (string name); |
99 | | - /// Gets an asset from this controller. |
100 | | - System.Object GetAsset (string name); |
101 | | - /// Check whether this controller has an asset. |
102 | | - bool HasAsset (string name); |
| 57 | +## What's in the box? |
| 58 | + |
| 59 | +### Controllers |
| 60 | + |
| 61 | +**Introduction:** The controller is the heart of your application, each application should consist of just one, commonly named the MainController. The Controller is the first entry point of the Entity Component System and is the place where all of your [systems](#systems) and [services](#services) are registered. Your controller should be attached to a game object in your scene. |
| 62 | + |
| 63 | +```csharp |
| 64 | +public class MainController : Controller { } |
| 65 | +``` |
| 66 | + |
| 67 | +**Virtual On Initialize:** The controller consists of an OnInitialize virtual method. This method can be overwritten and will be invoked during the very start of your application. During this cycle none [injectables](#Injectables) or [assets](#Assets) are being assigned, it is important to invoke the Register method during this cycle since this is the only time in your application you can register [systems](#Systems) and [services](#Services). |
| 68 | + |
| 69 | +```csharp |
| 70 | +public class MainController : Controller { |
| 71 | + public override void OnInitialize () { |
| 72 | + this.Register ( |
| 73 | + typeof (MovementSystem), |
| 74 | + typeof (AudioService) |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +**Virtual On Initialized:** The controller consists of an OnInitialized virtual method. This method can be overwritten and will be invoked when all [systems](#Systems) and [services](#Services) did initialize and all [injectables](#Injectables) and [assets](#Assets) properties are assigned. |
| 81 | + |
| 82 | +```csharp |
| 83 | +public class MainController : Controller { |
| 84 | + public override void OnInitialized () { } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +**Virtual On Update:** The controller consists of an OnUpdate virtual method. This method can be overwritten and will be invoked during the update cycle. This cycle will run once every frame, the controller's Update is invoked before the [system's](#Systems) and [service's](#Services) update cycles. |
| 89 | + |
| 90 | +```csharp |
| 91 | +public class MainController : Controller { |
| 92 | + public override void OnUpdate () { } |
103 | 93 | } |
104 | 94 | ``` |
105 | 95 |
|
106 | | -```cs |
107 | | -/// Base class for every entity component. |
108 | | -/// NOTE: This class allows the usage of [Referenced] and [Protected] properties. |
109 | | -abstract class EntityComponent<EntityComponentType, EntitySystemType> : UnityEngine.MonoBehaviour, IEntityComponent |
110 | | - where EntityComponentType : EntityComponent<EntityComponentType, EntitySystemType>, new() |
111 | | - where EntitySystemType : EntitySystem<EntitySystemType, EntityComponentType>, new() { |
112 | | - |
113 | | - /// Defines whether this component is enabled. |
114 | | - bool isEnabled { get; }; |
115 | | - |
116 | | - /// Sets the game object of the entity active. |
117 | | - void SetActive (bool value); |
118 | | - /// Destroys the game object of the entity. |
119 | | - void Destroy (); |
120 | | - /// Gets a component on an enity and sets it's reference to a property. |
121 | | - void GetComponentToProperty<UnityComponentType> (ref UnityComponentType entityProperty, bool includeChildren = false, bool includeInactive = false) |
122 | | - /// Adds an asset to the entity. |
123 | | - void AddAsset (UnityEngine.Object asset); |
124 | | - /// Loads a asset from the controller and adds it as an asset to the entity. |
125 | | - void AddAsset (string name); |
126 | | - /// Sets the position of an entity. |
127 | | - void SetPosition (float x, float y, float z = 0); |
128 | | - /// Adds to the position of an entity. |
129 | | - void AddPosition (float x, float y, float z = 0); |
130 | | - /// Sets the local position of an entity. |
131 | | - void SetLocalPosition (float x, float y, float z = 0); |
132 | | - /// Adds to the local position of an entity. |
133 | | - void AddLocalPosition (float x, float y, float z = 0); |
134 | | - /// Sets the EulerAngles of an entity. |
135 | | - void SetEulerAngles (float x, float y, float z); |
136 | | - /// Adds to the EulerAngles of an entity. |
137 | | - void AddEulerAngles (float x, float y, float z); |
138 | | - /// Sets the local EulerAngles of an entity. |
139 | | - void SetLocalEulerAngles (float x, float y, float z); |
140 | | - /// Adds to the local EulerAngles of an entity. |
141 | | - void AddLocalEulerAngles (float x, float y, float z); |
142 | | - /// Sets the local scale of an entity. |
143 | | - void SetLocalScale (float x, float y, float z); |
144 | | - /// Adds to the local Scale of an entity. |
145 | | - void AddLocalScale (float x, float y, float z); |
| 96 | +**Enabling Systems:** To enable or disable [systems](#Systems), the controller contains of a method EnableSystem which allows [systems](#Systems) to stop their life cycle methods such as OnUpdate, OnPhysics, OnDrawGui and others. You can provide the [system's](#System) type using a generic. |
| 97 | + |
| 98 | +```csharp |
| 99 | +public class MainController : Controller { |
| 100 | + public void SomeMethod () { |
| 101 | + this.SetSystemEnabled<MovementSystem> (true); |
| 102 | + this.SetSystemEnabled<InteractableSystem> (false); |
| 103 | + } |
146 | 104 | } |
147 | 105 | ``` |
148 | 106 |
|
149 | | -```cs |
150 | | -/// Base class for every entity system. |
151 | | -/// NOTE: This class allows the usage of [Injected] systems, services and controller. |
152 | | -/// NOTE: This class allows the usage of [Asset] properties. |
153 | | -abstract class EntitySystem<EntitySystemType, EntityComponentType> : IEntitySystem |
154 | | - where EntitySystemType : EntitySystem<EntitySystemType, EntityComponentType>, new() |
155 | | - where EntityComponentType : EntityComponent<EntityComponentType, EntitySystemType>, new() { |
156 | | - |
157 | | - /// An instance reference to the controller. |
158 | | - static EntitySystemType Instance { get; }; |
159 | | - /// A list of the system's instantiated entity components. |
160 | | - System.Collections.Generic.List<EntityComponentType> entities { get; }; |
161 | | - /// The first instantiated entity compoent if this system. |
162 | | - EntityComponentType entity { get; }; |
163 | | - /// Defines the number of instantiated entity components this system has. |
164 | | - int entityCount { get; }; |
165 | | - /// Defines whether the system has instantiated entity components. |
166 | | - bool hasEntities { get; }; |
167 | | - |
168 | | - /// Method invoked when the system will initialize. |
169 | | - virtual void OnInitialize (); |
170 | | - /// Method invoked when the system is initialized. |
171 | | - virtual void OnInitialized (); |
172 | | - /// Method invoked when the physics update, will be called every fixed frame. |
173 | | - /// NOTE: Define the ECS_PHYSICS scripting symbol to use this method. |
174 | | - virtual void OnPhysics (); |
175 | | - /// Method invoked when the system is drawing the gizmos, will be called |
176 | | - /// every gizmos draw call. |
177 | | - virtual void OnUpdate (); |
178 | | - /// Method invoked when the camera renders, will be called every late frame. |
179 | | - /// NOTE: Define the ECS_RENDER scripting symbol to use this method. |
180 | | - virtual void OnRender (); |
181 | | - /// Method invoked when the system is drawing the gizmos, will be called |
182 | | - /// every gizmos draw call. |
183 | | - virtual void OnDrawGizmos (); |
184 | | - /// Method invoked when the system is drawing the gui, will be called every |
185 | | - /// on gui draw call. |
186 | | - virtual void OnDrawGui (); |
187 | | - /// Method invoked when the system becomes enabled. |
188 | | - virtual void OnEnabled (); |
189 | | - /// Method invoked when the system becomes disabled. |
190 | | - virtual void OnDisabled (); |
191 | | - /// Method invoked when an entity of this system is initializing. |
192 | | - virtual void OnEntityInitialize (EntityComponentType entity); |
193 | | - /// Method invoked when an entity of this system is initialized. |
194 | | - virtual void OnEntityInitialized (EntityComponentType entity); |
195 | | - /// Method invoked when an entity of this system becomes enabled. |
196 | | - virtual void OnEntityEnabled (EntityComponentType entity); |
197 | | - /// Method invoked when an entity of this system becomes disabled. |
198 | | - virtual void OnEntityDisabled (EntityComponentType entity); |
199 | | - /// Method invoked when an entity of this system will destroy. |
200 | | - virtual void OnEntityWillDestroy (EntityComponentType entity); |
201 | | - /// Method invoked before the system will update, return whether this system |
202 | | - /// should update. will be called every frame. |
203 | | - virtual bool ShouldUpdate (); |
204 | | - |
205 | | - /// Returns another component on an entity. |
206 | | - void GetComponentOnEntity<C> (EntityComponentType entity, System.Action<C> then); |
207 | | - /// Returns another component on an entity. |
208 | | - C GetComponentOnEntity<C> (EntityComponentType entity); |
209 | | - /// Checks whether an entity has a specific component. |
210 | | - bool HasComponentOnEntity<C> (EntityComponentType entity); |
211 | | - /// Creates a new entity. |
212 | | - EntityComponentType CreateEntity (); |
213 | | - /// Clones an entity. |
214 | | - EntityComponentType CloneEntity (EntityComponentType entity); |
215 | | - /// Clones an entity on a given position in the hierarchy. |
216 | | - EntityComponentType CloneEntity (EntityComponentType entity, UnityEngine.Transform parentTransform); |
217 | | - /// Finds entities using a predicate match. |
218 | | - EntityComponentType[] MatchEntities (System.Predicate<EntityComponentType> match); |
219 | | - /// Finds an entity using a predicate match. |
220 | | - EntityComponentType[] MatchEntity (System.Predicate<EntityComponentType> match); |
221 | | - /// Starts a coroutine on this system. |
222 | | - UnityEngine.Coroutine StartCoroutine (System.Collections.IEnumerator routine); |
223 | | - /// Stops a given coroutine. |
224 | | - void StopCoroutine (System.Collections.IEnumerator routine); |
225 | | - void StopCoroutine (UnityEngine.Coroutine routine); |
226 | | - /// Sets whether the system is enabled or disabled, enabling the system allows |
227 | | - /// it to invoke all of the cycle calls such as OnUpdate and OnDrawGizmos. |
228 | | - void SetEnabled (bool value); |
| 107 | +**Checking Whether Systems Are Enabled:** To check whether [systems](#Systems) are enable or disabled, the controller contains of a method IsSystemEnabled. Invoking the method will return a booling informing if the [system](#Systems) is enabled or not. You can provide the [system's](#System) type using a generic. |
| 108 | + |
| 109 | +```csharp |
| 110 | +public class MainController : Controller { |
| 111 | + public void SomeMethod () { |
| 112 | + if (this.IsSystemEnabled<MovementSystem> ()) { } |
| 113 | + } |
229 | 114 | } |
230 | 115 | ``` |
231 | 116 |
|
232 | | -```cs |
233 | | -/// Base class for every service |
234 | | -/// NOTE: This class allows the usage of [Injected] systems, services and controller. |
235 | | -/// NOTE: This class allows the usage of [Asset] properties. |
236 | | -abstract class Service<ServiceType> : IService |
237 | | - where ServiceType : Service<ServiceType>, new() { |
238 | | - |
239 | | - /// An instance reference to the service. |
240 | | - static ServiceType Instance { get; }; |
241 | | - |
242 | | - /// Method invoked when the service will initialize. |
243 | | - virtual void OnInitialize (); |
244 | | - /// Method invoked when the system is initialized. |
245 | | - virtual void OnInitialized (); |
246 | | - /// Method invoked when the service is drawing the gizmos, will be called |
247 | | - /// every gizmos draw call. |
248 | | - virtual void OnDrawGizmos (); |
249 | | - /// Method invoked when the service is drawing the gui, will be called every |
250 | | - /// on gui draw call. |
251 | | - virtual void OnDrawGui (); |
252 | | - |
253 | | - /// Starts a coroutine on this service. |
254 | | - UnityEngine.Coroutine StartCoroutine (System.Collections.IEnumerator routine); |
255 | | - /// Stops a given coroutine. |
256 | | - void StopCoroutine (System.Collections.IEnumerator routine); |
| 117 | +**Injection:** The controller allows insertion of [injected](#Injectables) [systems](#Systems) and [services](#Services) properties. When being injected, all public methods and properties are accessibly. When adding the attribute to any of these type of fields, they will be assigned during the OnInitialze cycle. All public methods and properties within these properties are accessibly like normal. |
| 118 | + |
| 119 | +```csharp |
| 120 | +public class MainController : Controller { |
| 121 | + [Injected] private MovementSystem movementSystem; |
| 122 | + [Injected] private AudioService audioService; |
257 | 123 | } |
258 | 124 | ``` |
| 125 | + |
| 126 | +**Notes:** While it is recommended to move as much logic into [services](#Services) and [systems](#Systems), it is possible to let your controller house any functionality. If you use the controller for this purpose, try to keep it down to only application wide and core functionality. |
| 127 | + |
| 128 | +### Systems |
| 129 | + |
| 130 | +_This secection of the documentation will be updated very soon!_ |
| 131 | + |
| 132 | +### Components |
| 133 | + |
| 134 | +_This secection of the documentation will be updated very soon!_ |
| 135 | + |
| 136 | +### Services |
| 137 | + |
| 138 | +_This secection of the documentation will be updated very soon!_ |
0 commit comments