Skip to content

Commit deb18e8

Browse files
Refactored and improved the System Enabling logic
1 parent 001bbb7 commit deb18e8

File tree

5 files changed

+139
-138
lines changed

5 files changed

+139
-138
lines changed

README.md

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Entity Component System
88

9-
[![npm](https://img.shields.io/badge/upm-3.5.0-232c37.svg?style=for-the-badge)]()
9+
[![npm](https://img.shields.io/badge/upm-3.6.0-232c37.svg?style=for-the-badge)]()
1010
[![npm](https://img.shields.io/github/stars/elraccoone/unity-entity-component-system.svg?style=for-the-badge)]()
1111
[![npm](https://img.shields.io/badge/build-passing-brightgreen.svg?style=for-the-badge)]()
1212

@@ -61,191 +61,191 @@ Use build in file generator to create new instances for any of these types.
6161
```cs
6262
/// Base class for every controller.
6363
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
64-
public abstract class Controller {
64+
abstract class Controller {
6565

6666
/// A reference to the controller.
67-
public static Controller Instance;
67+
static Controller Instance;
6868

6969
/// Method invoked when the controller is initializing.
70-
public virtual void OnInitialize ();
70+
virtual void OnInitialize ();
7171
/// Method invoked when the controller is initialized.
72-
public virtual void OnInitialized ();
72+
virtual void OnInitialized ();
7373
/// Method invoked when the controller updates, will be called every frame.
74-
public virtual void OnUpdate ();
74+
virtual void OnUpdate ();
7575

7676
/// Register your systems and services to the controller. This can only be
7777
/// done during 'OnInitialize' cycle.
78-
public void Register (params System.Type[] typesOf);
79-
/// Enables systems.
80-
public void EnableSystems (params System.Type[] typesOf);
81-
/// Disables systems.
82-
public void DisableSystems (params System.Type[] typesOf);
78+
void Register (params System.Type[] typesOf);
79+
/// Enables or disabled a system, enabling the systems allows them to invoke
80+
/// their cycle methods such as OnUpdate, OnPhysics, OnDrawGui and others.
81+
void SetSystemEnabled<S> (bool value);
82+
/// Returns whether a system is enabled.
83+
bool IsSystemEnabled<S> ();
8384
/// Gets a system from this controller.
84-
public S GetSystem<S> () where S : IEntitySystem, new();
85+
S GetSystem<S> () where S : IEntitySystem, new();
8586
/// Gets a system from this controller.
86-
public System.Object GetSystem (System.Type typeOf);
87+
System.Object GetSystem (System.Type typeOf);
8788
/// Check whether this controller has a system.
88-
public bool HasSystem<S> () where S : IEntitySystem, new();
89+
bool HasSystem<S> () where S : IEntitySystem, new();
8990
/// Gets a service from this controller.
90-
public S GetService<S> () where S : IService, new();
91+
S GetService<S> () where S : IService, new();
9192
/// Gets a system from this controller.
92-
public System.Object GetService (System.Type typeOf);
93+
System.Object GetService (System.Type typeOf);
9394
/// Check whether this controller has a service.
94-
public bool HasService<S> () where S : IService, new();
95+
bool HasService<S> () where S : IService, new();
9596
/// Gets an asset from this controller.
96-
public AssetType GetAsset<AssetType> (string name);
97+
AssetType GetAsset<AssetType> (string name);
9798
/// Check whether this controller has an asset.
98-
public bool HasAsset (string name);
99+
bool HasAsset (string name);
99100
}
100101
```
101102

102103
```cs
103104
/// Base class for every entity component.
104105
/// NOTE: This class allows the usage of [Referenced] and [Protected] objects and primitives.
105-
public abstract class EntityComponent<EntityComponentType, EntitySystemType> : UnityEngine.MonoBehaviour, IEntityComponent
106+
abstract class EntityComponent<EntityComponentType, EntitySystemType> : UnityEngine.MonoBehaviour, IEntityComponent
106107
where EntityComponentType : EntityComponent<EntityComponentType, EntitySystemType>, new()
107108
where EntitySystemType : EntitySystem<EntitySystemType, EntityComponentType>, new() {
108109

109110
/// Defines whether this component is enabled.
110-
public bool isEnabled { get; };
111+
bool isEnabled { get; };
111112
/// Sets the game object of the entity active.
112-
public void SetActive (bool value);
113+
void SetActive (bool value);
113114
/// Destroys the game object of the entity.
114-
public void Destroy ();
115+
void Destroy ();
115116
/// Adds an asset to the entity.
116-
public void AddAsset (UnityEngine.Object asset);
117+
void AddAsset (UnityEngine.Object asset);
117118
/// Loads a asset from the controller and adds it as an asset to the entity.
118-
public void AddAsset (string name);
119+
void AddAsset (string name);
119120

120121
/// Sets the position of an entity.
121-
public void SetPosition (float x, float y, float z = 0);
122+
void SetPosition (float x, float y, float z = 0);
122123
/// Adds to the position of an entity.
123-
public void AddPosition (float x, float y, float z = 0);
124+
void AddPosition (float x, float y, float z = 0);
124125
/// Sets the local position of an entity.
125-
public void SetLocalPosition (float x, float y, float z = 0);
126+
void SetLocalPosition (float x, float y, float z = 0);
126127
/// Adds to the local position of an entity.
127-
public void AddLocalPosition (float x, float y, float z = 0);
128+
void AddLocalPosition (float x, float y, float z = 0);
128129
/// Sets the EulerAngles of an entity.
129-
public void SetEulerAngles (float x, float y, float z);
130+
void SetEulerAngles (float x, float y, float z);
130131
/// Adds to the EulerAngles of an entity.
131-
public void AddEulerAngles (float x, float y, float z);
132+
void AddEulerAngles (float x, float y, float z);
132133
/// Sets the local EulerAngles of an entity.
133-
public void SetLocalEulerAngles (float x, float y, float z);
134+
void SetLocalEulerAngles (float x, float y, float z);
134135
/// Adds to the local EulerAngles of an entity.
135-
public void AddLocalEulerAngles (float x, float y, float z);
136+
void AddLocalEulerAngles (float x, float y, float z);
136137
/// Sets the local scale of an entity.
137-
public void SetLocalScale (float x, float y, float z);
138+
void SetLocalScale (float x, float y, float z);
138139
/// Adds to the local Scale of an entity.
139-
public void AddLocalScale (float x, float y, float z);
140+
void AddLocalScale (float x, float y, float z);
140141
}
141142
```
142143

143144
```cs
144145
/// Base class for every entity system.
145146
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
146-
public abstract class EntitySystem<EntitySystemType, EntityComponentType> : IEntitySystem
147+
abstract class EntitySystem<EntitySystemType, EntityComponentType> : IEntitySystem
147148
where EntitySystemType : EntitySystem<EntitySystemType, EntityComponentType>, new()
148149
where EntityComponentType : EntityComponent<EntityComponentType, EntitySystemType>, new() {
149150

150151
/// An instance reference to the controller.
151-
public static EntitySystemType Instance;
152+
static EntitySystemType Instance;
152153
/// A list of the system's instantiated entity components.
153-
public System.Collections.Generic.List<EntityComponentType> entities;
154+
System.Collections.Generic.List<EntityComponentType> entities;
154155
/// The first instantiated entity compoent if this system.
155-
public EntityComponentType entity;
156+
EntityComponentType entity;
156157
/// Defines the number of instantiated entity components this system has.
157-
public int entityCount;
158+
int entityCount;
158159
/// Defines whether the system has instantiated entity components.
159-
public bool hasEntities;
160+
bool hasEntities;
160161

161162
/// Method invoked when the system will initialize.
162-
public virtual void OnInitialize ();
163+
virtual void OnInitialize ();
163164
/// Method invoked when the system is initialized.
164-
public virtual void OnInitialized ();
165+
virtual void OnInitialized ();
165166
/// Method invoked when the physics update, will be called every fixed frame.
166167
/// NOTE: Define the ECS_PHYSICS scripting symbol to use this method.
167-
public virtual void OnPhysics ();
168+
virtual void OnPhysics ();
168169
/// Method invoked when the system is drawing the gizmos, will be called
169170
/// every gizmos draw call.
170-
public virtual void OnUpdate ();
171+
virtual void OnUpdate ();
171172
/// Method invoked when the camera renders, will be called every late frame.
172173
/// NOTE: Define the ECS_RENDER scripting symbol to use this method.
173-
public virtual void OnRender ();
174+
virtual void OnRender ();
174175
/// Method invoked when the system is drawing the gizmos, will be called
175176
/// every gizmos draw call.
176-
public virtual void OnDrawGizmos ();
177+
virtual void OnDrawGizmos ();
177178
/// Method invoked when the system is drawing the gui, will be called every
178179
/// on gui draw call.
179-
public virtual void OnDrawGui ();
180+
virtual void OnDrawGui ();
180181
/// Method invoked when the system becomes enabled.
181-
public virtual void OnEnabled ();
182+
virtual void OnEnabled ();
182183
/// Method invoked when the system becomes disabled.
183-
public virtual void OnDisabled ();
184+
virtual void OnDisabled ();
184185
/// Method invoked when an entity of this system is initializing.
185-
public virtual void OnEntityInitialize (EntityComponentType entity);
186+
virtual void OnEntityInitialize (EntityComponentType entity);
186187
/// Method invoked when an entity of this system is initialized.
187-
public virtual void OnEntityInitialized (EntityComponentType entity);
188+
virtual void OnEntityInitialized (EntityComponentType entity);
188189
/// Method invoked when an entity of this system becomes enabled.
189-
public virtual void OnEntityEnabled (EntityComponentType entity);
190+
virtual void OnEntityEnabled (EntityComponentType entity);
190191
/// Method invoked when an entity of this system becomes disabled.
191-
public virtual void OnEntityDisabled (EntityComponentType entity);
192+
virtual void OnEntityDisabled (EntityComponentType entity);
192193
/// Method invoked when an entity of this system will destroy.
193-
public virtual void OnEntityWillDestroy (EntityComponentType entity);
194+
virtual void OnEntityWillDestroy (EntityComponentType entity);
194195
/// Method invoked before the system will update, return whether this system
195196
/// should update. will be called every frame.
196-
public virtual bool ShouldUpdate ();
197+
virtual bool ShouldUpdate ();
197198

198199
/// Returns another component on an entity.
199-
public void GetComponentOnEntity<C> (EntityComponentType entity, System.Action<C> then);
200+
void GetComponentOnEntity<C> (EntityComponentType entity, System.Action<C> then);
200201
/// Returns another component on an entity.
201-
public C GetComponentOnEntity<C> (EntityComponentType entity);
202+
C GetComponentOnEntity<C> (EntityComponentType entity);
202203
/// Checks whether an entity has a specific component.
203-
public bool HasComponentOnEntity<C> (EntityComponentType entity);
204+
bool HasComponentOnEntity<C> (EntityComponentType entity);
204205
/// Creates a new entity.
205-
public EntityComponentType CreateEntity ();
206+
EntityComponentType CreateEntity ();
206207
/// Clones an entity.
207-
public EntityComponentType CloneEntity (EntityComponentType entity);
208+
EntityComponentType CloneEntity (EntityComponentType entity);
208209
/// Clones an entity on a given position in the hierarchy.
209-
public EntityComponentType CloneEntity (EntityComponentType entity, UnityEngine.Transform parentTransform);
210+
EntityComponentType CloneEntity (EntityComponentType entity, UnityEngine.Transform parentTransform);
210211
/// Finds entities using a predicate match.
211-
public EntityComponentType[] MatchEntities (System.Predicate<EntityComponentType> match);
212+
EntityComponentType[] MatchEntities (System.Predicate<EntityComponentType> match);
212213
/// Finds an entity using a predicate match.
213-
public EntityComponentType[] MatchEntity (System.Predicate<EntityComponentType> match);
214+
EntityComponentType[] MatchEntity (System.Predicate<EntityComponentType> match);
214215
/// Starts a coroutine on this system.
215-
public UnityEngine.Coroutine StartCoroutine (System.Collections.IEnumerator routine);
216+
UnityEngine.Coroutine StartCoroutine (System.Collections.IEnumerator routine);
216217
/// Stops a given coroutine.
217-
public void StopCoroutine (System.Collections.IEnumerator routine);
218-
public void StopCoroutine (UnityEngine.Coroutine routine);
219-
/// Enables or disables this system.
220-
public void SetEnabled (bool isEnabled);
221-
/// Gets the enabled status of this system
222-
public bool GetEnabled ();
218+
void StopCoroutine (System.Collections.IEnumerator routine);
219+
void StopCoroutine (UnityEngine.Coroutine routine);
220+
/// Sets whether the system is enabled or disabled, enabling the system allows
221+
/// it to invoke all of the cycle calls such as OnUpdate and OnDrawGizmos.
222+
void SetEnabled (bool value);
223223
}
224224
```
225225

226226
```cs
227227
/// Base class for every service
228228
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
229-
public abstract class Service<ServiceType> : IService
229+
abstract class Service<ServiceType> : IService
230230
where ServiceType : Service<ServiceType>, new() {
231231

232232
/// An instance reference to the service.
233-
public static ServiceType Instance;
233+
static ServiceType Instance;
234234

235235
/// Method invoked when the service will initialize.
236-
public virtual void OnInitialize ();
236+
virtual void OnInitialize ();
237237
/// Method invoked when the system is initialized.
238-
public virtual void OnInitialized ();
238+
virtual void OnInitialized ();
239239
/// Method invoked when the service is drawing the gizmos, will be called
240240
/// every gizmos draw call.
241-
public virtual void OnDrawGizmos ();
241+
virtual void OnDrawGizmos ();
242242
/// Method invoked when the service is drawing the gui, will be called every
243243
/// on gui draw call.
244-
public virtual void OnDrawGui ();
244+
virtual void OnDrawGui ();
245245

246246
/// Starts a coroutine on this service.
247-
public UnityEngine.Coroutine StartCoroutine (System.Collections.IEnumerator routine);
247+
UnityEngine.Coroutine StartCoroutine (System.Collections.IEnumerator routine);
248248
/// Stops a given coroutine.
249-
public void StopCoroutine (System.Collections.IEnumerator routine);
249+
void StopCoroutine (System.Collections.IEnumerator routine);
250250
}
251251
```

0 commit comments

Comments
 (0)