Entitas  0.35.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, ReactiveSystem and other nested Systems instances.
8  /// All systems will be initialized and executed based on the order
9  /// 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 reactiveSystem = system as ReactiveSystem;
29 
30  var initializeSystem = reactiveSystem != null
31  ? reactiveSystem.subsystem as IInitializeSystem
32  : system as IInitializeSystem;
33 
34  if(initializeSystem != null) {
35  _initializeSystems.Add(initializeSystem);
36  }
37 
38  var executeSystem = system as IExecuteSystem;
39  if(executeSystem != null) {
40  _executeSystems.Add(executeSystem);
41  }
42 
43  var cleanupSystem = reactiveSystem != null
44  ? reactiveSystem.subsystem as ICleanupSystem
45  : system as ICleanupSystem;
46 
47  if(cleanupSystem != null) {
48  _cleanupSystems.Add(cleanupSystem);
49  }
50 
51  var tearDownSystem = reactiveSystem != null
52  ? reactiveSystem.subsystem as ITearDownSystem
53  : system as ITearDownSystem;
54 
55  if(tearDownSystem != null) {
56  _tearDownSystems.Add(tearDownSystem);
57  }
58 
59  return this;
60  }
61 
62  /// Calls Initialize() on all IInitializeSystem, ReactiveSystem and
63  /// other nested Systems instances in the order you added them.
64  public virtual void Initialize() {
65  for (int i = 0; i < _initializeSystems.Count; i++) {
66  _initializeSystems[i].Initialize();
67  }
68  }
69 
70  /// Calls Execute() on all IExecuteSystem, ReactiveSystem and other
71  /// nested Systems instances in the order you added them.
72  public virtual void Execute() {
73  for (int i = 0; i < _executeSystems.Count; i++) {
74  _executeSystems[i].Execute();
75  }
76  }
77 
78  /// Calls Cleanup() on all ICleanupSystem, ReactiveSystem and other
79  /// nested Systems instances in the order you added them.
80  public virtual void Cleanup() {
81  for (int i = 0; i < _cleanupSystems.Count; i++) {
82  _cleanupSystems[i].Cleanup();
83  }
84  }
85 
86  /// Calls TearDown() on all ITearDownSystem, ReactiveSystem and other
87  /// nested Systems instances in the order you added them.
88  public virtual void TearDown() {
89  for (int i = 0; i < _tearDownSystems.Count; i++) {
90  _tearDownSystems[i].TearDown();
91  }
92  }
93 
94  /// Activates all ReactiveSystems in the systems list.
95  public virtual void ActivateReactiveSystems() {
96  for (int i = 0; i < _executeSystems.Count; i++) {
97  var system = _executeSystems[i];
98  var reactiveSystem = system as ReactiveSystem;
99  if(reactiveSystem != null) {
100  reactiveSystem.Activate();
101  }
102 
103  var nestedSystems = system as Systems;
104  if(nestedSystems != null) {
105  nestedSystems.ActivateReactiveSystems();
106  }
107  }
108  }
109 
110  /// Deactivates all ReactiveSystems in the systems list.
111  /// This will also clear all ReactiveSystems.
112  /// This is useful when you want to soft-restart your application and
113  /// want to reuse your existing system instances.
114  public virtual void DeactivateReactiveSystems() {
115  for (int i = 0; i < _executeSystems.Count; i++) {
116  var system = _executeSystems[i];
117  var reactiveSystem = system as ReactiveSystem;
118  if(reactiveSystem != null) {
119  reactiveSystem.Deactivate();
120  }
121 
122  var nestedSystems = system as Systems;
123  if(nestedSystems != null) {
124  nestedSystems.DeactivateReactiveSystems();
125  }
126  }
127  }
128 
129  /// Clears all ReactiveSystems in the systems list.
130  public virtual void ClearReactiveSystems() {
131  for (int i = 0; i < _executeSystems.Count; i++) {
132  var system = _executeSystems[i];
133  var reactiveSystem = system as ReactiveSystem;
134  if(reactiveSystem != null) {
135  reactiveSystem.Clear();
136  }
137 
138  var nestedSystems = system as Systems;
139  if(nestedSystems != null) {
140  nestedSystems.ClearReactiveSystems();
141  }
142  }
143  }
144  }
145 }
virtual void DeactivateReactiveSystems()
Definition: Systems.cs:114
virtual void Execute()
Definition: Systems.cs:72
virtual void TearDown()
Definition: Systems.cs:88
virtual Systems Add(ISystem system)
Adds the system instance to the systems list.
Definition: Systems.cs:27
virtual void Cleanup()
Definition: Systems.cs:80
virtual void ActivateReactiveSystems()
Activates all ReactiveSystems in the systems list.
Definition: Systems.cs:95
virtual void Initialize()
Definition: Systems.cs:64
IReactiveExecuteSystem subsystem
Systems()
Creates a new Systems instance.
Definition: Systems.cs:19
void Clear()
Clears all accumulated changes.
virtual void ClearReactiveSystems()
Clears all ReactiveSystems in the systems list.
Definition: Systems.cs:130