Entitas  0.40.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
Systems.cs
1 using System.Collections.Generic;
2 
3 namespace Entitas {
4 
5  /// Systems provide a convenient way to group systems.
6  /// You can add IInitializeSystem, IExecuteSystem, ICleanupSystem,
7  /// ITearDownSystem, ReactiveS
8  /// ystem and other nested Systems instances.
9  /// All systems will be initialized and executed based on the order
10  /// you added them.
12 
13  protected readonly List<IInitializeSystem> _initializeSystems;
14  protected readonly List<IExecuteSystem> _executeSystems;
15  protected readonly List<ICleanupSystem> _cleanupSystems;
16  protected readonly List<ITearDownSystem> _tearDownSystems;
17 
18  /// Creates a new Systems instance.
19  public Systems() {
20  _initializeSystems = new List<IInitializeSystem>();
21  _executeSystems = new List<IExecuteSystem>();
22  _cleanupSystems = new List<ICleanupSystem>();
23  _tearDownSystems = new List<ITearDownSystem>();
24  }
25 
26  /// Adds the system instance to the systems list.
27  public virtual Systems Add(ISystem system) {
28  var initializeSystem = system as IInitializeSystem;
29  if(initializeSystem != null) {
30  _initializeSystems.Add(initializeSystem);
31  }
32 
33  var executeSystem = system as IExecuteSystem;
34  if(executeSystem != null) {
35  _executeSystems.Add(executeSystem);
36  }
37 
38  var cleanupSystem = system as ICleanupSystem;
39  if(cleanupSystem != null) {
40  _cleanupSystems.Add(cleanupSystem);
41  }
42 
43  var tearDownSystem = system as ITearDownSystem;
44  if(tearDownSystem != null) {
45  _tearDownSystems.Add(tearDownSystem);
46  }
47 
48  return this;
49  }
50 
51  /// Calls Initialize() on all IInitializeSystem and other
52  /// nested Systems instances in the order you added them.
53  public virtual void Initialize() {
54  for (int i = 0; i < _initializeSystems.Count; i++) {
55  _initializeSystems[i].Initialize();
56  }
57  }
58 
59  /// Calls Execute() on all IExecuteSystem and other
60  /// nested Systems instances in the order you added them.
61  public virtual void Execute() {
62  for (int i = 0; i < _executeSystems.Count; i++) {
63  _executeSystems[i].Execute();
64  }
65  }
66 
67  /// Calls Cleanup() on all ICleanupSystem and other
68  /// nested Systems instances in the order you added them.
69  public virtual void Cleanup() {
70  for (int i = 0; i < _cleanupSystems.Count; i++) {
71  _cleanupSystems[i].Cleanup();
72  }
73  }
74 
75  /// Calls TearDown() on all ITearDownSystem and other
76  /// nested Systems instances in the order you added them.
77  public virtual void TearDown() {
78  for (int i = 0; i < _tearDownSystems.Count; i++) {
79  _tearDownSystems[i].TearDown();
80  }
81  }
82 
83  /// Activates all ReactiveSystems in the systems list.
84  public void ActivateReactiveSystems() {
85  for (int i = 0; i < _executeSystems.Count; i++) {
86  var system = _executeSystems[i];
87  var reactiveSystem = system as IReactiveSystem;
88  if(reactiveSystem != null) {
89  reactiveSystem.Activate();
90  }
91 
92  var nestedSystems = system as Systems;
93  if(nestedSystems != null) {
94  nestedSystems.ActivateReactiveSystems();
95  }
96  }
97  }
98 
99  /// Deactivates all ReactiveSystems in the systems list.
100  /// This will also clear all ReactiveSystems.
101  /// This is useful when you want to soft-restart your application and
102  /// want to reuse your existing system instances.
104  for (int i = 0; i < _executeSystems.Count; i++) {
105  var system = _executeSystems[i];
106  var reactiveSystem = system as IReactiveSystem;
107  if(reactiveSystem != null) {
108  reactiveSystem.Deactivate();
109  }
110 
111  var nestedSystems = system as Systems;
112  if(nestedSystems != null) {
113  nestedSystems.DeactivateReactiveSystems();
114  }
115  }
116  }
117 
118  /// Clears all ReactiveSystems in the systems list.
119  public void ClearReactiveSystems() {
120  for (int i = 0; i < _executeSystems.Count; i++) {
121  var system = _executeSystems[i];
122  var reactiveSystem = system as IReactiveSystem;
123  if(reactiveSystem != null) {
124  reactiveSystem.Clear();
125  }
126 
127  var nestedSystems = system as Systems;
128  if(nestedSystems != null) {
129  nestedSystems.ClearReactiveSystems();
130  }
131  }
132  }
133  }
134 }
void ActivateReactiveSystems()
Activates all ReactiveSystems in the systems list.
Definition: Systems.cs:84
void DeactivateReactiveSystems()
Definition: Systems.cs:103
virtual void Execute()
Definition: Systems.cs:61
virtual void TearDown()
Definition: Systems.cs:77
virtual Systems Add(ISystem system)
Adds the system instance to the systems list.
Definition: Systems.cs:27
virtual void Cleanup()
Definition: Systems.cs:69
virtual void Initialize()
Definition: Systems.cs:53
void ClearReactiveSystems()
Clears all ReactiveSystems in the systems list.
Definition: Systems.cs:119
Systems()
Creates a new Systems instance.
Definition: Systems.cs:19