felecs 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.byebug_history +20 -0
  3. data/.gitignore +17 -0
  4. data/.inch.yml +11 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +13 -0
  7. data/.ruby-version +1 -0
  8. data/CHANGELOG.mdown +80 -0
  9. data/Gemfile +10 -0
  10. data/Gemfile.lock +87 -0
  11. data/LICENSE +21 -0
  12. data/README.mdown +465 -0
  13. data/Rakefile +66 -0
  14. data/bin/console +15 -0
  15. data/bin/setup +8 -0
  16. data/docs/CNAME +1 -0
  17. data/docs/FelECS/ComponentManager.html +1239 -0
  18. data/docs/FelECS/Components.html +337 -0
  19. data/docs/FelECS/Entities.html +792 -0
  20. data/docs/FelECS/Order.html +251 -0
  21. data/docs/FelECS/Scenes.html +765 -0
  22. data/docs/FelECS/Stage.html +572 -0
  23. data/docs/FelECS/Systems.html +1505 -0
  24. data/docs/FelECS.html +335 -0
  25. data/docs/FelFlame/ComponentManager.html +1239 -0
  26. data/docs/FelFlame/Components.html +333 -0
  27. data/docs/FelFlame/Entities.html +792 -0
  28. data/docs/FelFlame/Helper/ComponentManager.html +1627 -0
  29. data/docs/FelFlame/Helper.html +142 -0
  30. data/docs/FelFlame/Order.html +251 -0
  31. data/docs/FelFlame/Scenes.html +765 -0
  32. data/docs/FelFlame/Stage.html +572 -0
  33. data/docs/FelFlame/Systems.html +1505 -0
  34. data/docs/FelFlame.html +319 -0
  35. data/docs/Felflame_.html +143 -0
  36. data/docs/_index.html +188 -0
  37. data/docs/class_list.html +51 -0
  38. data/docs/css/common.css +1 -0
  39. data/docs/css/full_list.css +58 -0
  40. data/docs/css/style.css +497 -0
  41. data/docs/file.README.html +560 -0
  42. data/docs/file.version.html +74 -0
  43. data/docs/file_list.html +56 -0
  44. data/docs/frames.html +17 -0
  45. data/docs/index.html +560 -0
  46. data/docs/js/app.js +314 -0
  47. data/docs/js/full_list.js +216 -0
  48. data/docs/js/jquery.js +4 -0
  49. data/docs/method_list.html +419 -0
  50. data/docs/top-level-namespace.html +137 -0
  51. data/felecs.gemspec +45 -0
  52. data/lib/felecs/component_manager.rb +279 -0
  53. data/lib/felecs/entity_manager.rb +160 -0
  54. data/lib/felecs/order.rb +24 -0
  55. data/lib/felecs/scene_manager.rb +69 -0
  56. data/lib/felecs/stage_manager.rb +47 -0
  57. data/lib/felecs/system_manager.rb +258 -0
  58. data/lib/felecs/version.rb +9 -0
  59. data/lib/felecs.rb +67 -0
  60. data/mrbgem/mrbgem.rake +4 -0
  61. data/mrbgem/mrblib/felecs.rb +913 -0
  62. metadata +229 -0
@@ -0,0 +1,258 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FelECS
4
+ class Systems
5
+ # How early this System should be executed in a list of Systems
6
+ attr_accessor :priority
7
+
8
+ # The Constant name assigned to this System
9
+
10
+ # Allows overwriting the storage of triggers, such as for clearing.
11
+ # This method should generally only need to be used internally and
12
+ # not by a game developer.
13
+ # @!visibility private
14
+ attr_writer :addition_triggers, :removal_triggers, :attr_triggers
15
+
16
+ # Stores all the scenes this system is a part of.
17
+ attr_writer :scenes
18
+
19
+ def scenes
20
+ @scenes ||= []
21
+ end
22
+
23
+ def priority=(priority)
24
+ @priority = priority
25
+ scenes.each do |scene|
26
+ scene.systems = scene.systems.sort_by(&:priority)
27
+ end
28
+ end
29
+
30
+ # Stores references to components or their managers that trigger
31
+ # this component when a component or component from that manager
32
+ # is added to an entity.
33
+ # Do not edit this hash as it is managed by FelECS automatically.
34
+ # @return [Array<Component>]
35
+ def addition_triggers
36
+ @addition_triggers ||= []
37
+ end
38
+
39
+ # Stores references to components or their managers that trigger
40
+ # this component when a component or component from that manager
41
+ # is removed from an entity.
42
+ # Do not edit this hash as it is managed by FelECS automatically.
43
+ # @return [Array<Component>]
44
+ def removal_triggers
45
+ @removal_triggers ||= []
46
+ end
47
+
48
+ # Stores references to systems that should be triggered when an
49
+ # attribute from this manager is changed
50
+ # Do not edit this hash as it is managed by FelECS automatically.
51
+ # @return [Hash<Symbol, Array<Symbol>>]
52
+ def attr_triggers
53
+ @attr_triggers ||= {}
54
+ end
55
+
56
+ class << self
57
+ # Stores the systems in {FelECS::Components}. This
58
+ # is needed because calling `FelECS::Components.constants`
59
+ # will not let you iterate over the value of the constants
60
+ # but will instead give you an array of symbols. This caches
61
+ # the convertion of those symbols to the actual value of the
62
+ # constants
63
+ def const_cache
64
+ @const_cache || update_const_cache
65
+ end
66
+
67
+ # Updates the array that stores the constants.
68
+ # Used internally by FelECS
69
+ # @!visibility private
70
+ def update_const_cache
71
+ @const_cache = constants.map do |constant|
72
+ const_get constant
73
+ end
74
+ end
75
+
76
+ # Forwards undefined methods to the array of constants
77
+ # if the array can handle the request. Otherwise tells
78
+ # the programmer their code errored
79
+ # @!visibility private
80
+ def respond_to_missing?(method, *)
81
+ if const_cache.respond_to? method
82
+ true
83
+ else
84
+ super
85
+ end
86
+ end
87
+
88
+ # Makes system module behave like arrays with additional
89
+ # methods for managing the array
90
+ # @!visibility private
91
+ def method_missing(method, *args, **kwargs, &block)
92
+ if const_cache.respond_to? method
93
+ const_cache.send(method, *args, **kwargs, &block)
94
+ else
95
+ super
96
+ end
97
+ end
98
+ end
99
+
100
+ # Creates a new System which can be accessed as a constant under the namespace {FelECS::Systems}.
101
+ # The name given is what constant the system is assigned to
102
+ #
103
+ # @example
104
+ # FelECS::Systems.new('PassiveHeal', priority: -2) do
105
+ # FelECS::Components::Health.each do |component|
106
+ # component.hp += 5
107
+ # end
108
+ # end
109
+ # # Give it a low priority so other systems such as a
110
+ # # Poison system would kill the player first
111
+ #
112
+ # @param name [String] The name this system will use. Needs to to be in the Ruby Constant format.
113
+ # @param priority [Integer] Which priority order this system should be executed in relative to other systems. Higher means executed earlier.
114
+ # @param block [Proc] The code you wish to be executed when the system is triggered. Can be defined by using a +do end+ block or using +{ }+ braces.
115
+ def initialize(name, priority: 0, &block)
116
+ FelECS::Systems.const_set(name, self)
117
+ FelECS::Systems.update_const_cache
118
+ @priority = priority
119
+ @block = block
120
+ @scenes = []
121
+ end
122
+
123
+ # Manually execute the system a single time
124
+ def call
125
+ @block.call
126
+ end
127
+
128
+ # Redefine what code is executed by this System when it is called upon.
129
+ # @param block [Proc] The code you wish to be executed when the system is triggered. Can be defined by using a +do end+ block or using +{ }+ braces.
130
+ def redefine(&block)
131
+ @block = block
132
+ end
133
+
134
+ # Removes triggers from this system. This function is fairly flexible so it can accept a few different inputs
135
+ # For addition and removal triggers, you can optionally pass in a component, or a manager to clear specifically
136
+ # the relevant triggers for that one component or manager. If you do not pass a component or manager then it will
137
+ # clear triggers for all components and managers.
138
+ # For attr_triggers
139
+ # @example
140
+ # # To clear all triggers that execute this system when a component is added:
141
+ # FelECS::Systems::ExampleSystem.clear_triggers :addition_triggers
142
+ # # Same as above but for when a component is removed instead
143
+ # FelECS::Systems::ExampleSystem.clear_triggers :removal_triggers
144
+ # # Same as above but for when a component has a certain attribute changed
145
+ # FelECS::Systems::ExampleSystem.clear_triggers :attr_triggers
146
+ #
147
+ # # Clear a trigger from a specific component
148
+ # FelECS::Systems::ExampleSystem.clear_triggers :addition_triggers, FelECS::Component::ExampleComponent[0]
149
+ # # Clear a trigger from a specific component manager
150
+ # FelECS::Systems::ExampleSystem.clear_triggers :addition_triggers, FelECS::Component::ExampleComponent
151
+ #
152
+ # # Clear the trigger that executes a system when the ':example_attr' is changes
153
+ # FelECS::Systems::ExampleSystem.clear_triggers :attr_triggers, :example_attr
154
+ # @param trigger_types [:Symbols] One or more of the following trigger types: +:addition_triggers+, +:removal_triggers+, or +:attr_triggers+. If attr_triggers is used then you may pass attributes you wish to be cleared as symbols in this parameter as well
155
+ # @param component_or_manager [Component or ComponentManager] The object to clear triggers from. Use Nil to clear triggers from all components associated with this system.
156
+ # @return [Boolean] +true+
157
+ def clear_triggers(*trigger_types, component_or_manager: nil)
158
+ trigger_types = %i[addition_triggers removal_triggers attr_triggers] if trigger_types.empty?
159
+
160
+ if trigger_types.include? :attr_triggers
161
+ if (trigger_types - %i[addition_triggers
162
+ removal_triggers
163
+ attr_triggers]).empty?
164
+
165
+ if component_or_manager.nil?
166
+ # remove all attrs
167
+ attr_triggers.each do |cmp_or_mgr, attrs|
168
+ attrs.each do |attr|
169
+ next if cmp_or_mgr.attr_triggers[attr].nil?
170
+
171
+ cmp_or_mgr.attr_triggers[attr].delete self
172
+ end
173
+ self.attr_triggers = {}
174
+ end
175
+ else
176
+ # remove attrs relevant to comp_or_man
177
+ unless attr_triggers[component_or_manager].nil?
178
+ attr_triggers[component_or_manager].each do |attr|
179
+ component_or_manager.attr_triggers[attr].delete self
180
+ end
181
+ attr_triggers[component_or_manager] = []
182
+ end
183
+ end
184
+
185
+ elsif component_or_manager.nil?
186
+
187
+ (trigger_types - %i[addition_triggers removal_triggers attr_triggers]).each do |attr|
188
+ # remove attr
189
+ attr_triggers.each do |cmp_or_mgr, _attrs|
190
+ cmp_or_mgr.attr_triggers[attr].delete self
191
+ end
192
+ end
193
+ attr_triggers.delete(trigger_types - %i[addition_triggers
194
+ removal_triggers
195
+ attr_triggers])
196
+ else
197
+ # remove attr from component_or_manager
198
+ (trigger_types - %i[addition_triggers removal_triggers attr_triggers]).each do |attr|
199
+ next if component_or_manager.attr_triggers[attr].nil?
200
+
201
+ component_or_manager.attr_triggers[attr].delete self
202
+ end
203
+ attr_triggers[component_or_manager] -= trigger_types unless attr_triggers[component_or_manager].nil?
204
+
205
+ end
206
+ end
207
+
208
+ (trigger_types & %i[removal_triggers addition_triggers] - [:attr_triggers]).each do |trigger_type|
209
+ if component_or_manager.nil?
210
+ # remove all removal triggers
211
+ send(trigger_type).each do |trigger|
212
+ trigger.send(trigger_type).delete self
213
+ end
214
+ send("#{trigger_type}=", [])
215
+ else
216
+ # remove removal trigger relevant to comp/man
217
+ send(trigger_type).delete component_or_manager
218
+ component_or_manager.send(trigger_type).delete self
219
+ end
220
+ end
221
+ true
222
+ end
223
+
224
+ # Add a component or component manager so that it triggers this system when the component or a component from the component manager is added to an entity
225
+ # @param component_or_manager [Component or ComponentManager] The component or component manager to trigger this system when added
226
+ # @return [Boolean] +true+
227
+ def trigger_when_added(component_or_manager)
228
+ self.addition_triggers |= [component_or_manager]
229
+ component_or_manager.addition_triggers |= [self]
230
+ true
231
+ end
232
+
233
+ # Add a component or component manager so that it triggers this system when the component or a component from the component manager is removed from an entity
234
+ # @param component_or_manager [Component or ComponentManager] The component or component manager to trigger this system when removed
235
+ # @return [Boolean] +true+
236
+ def trigger_when_removed(component_or_manager)
237
+ self.removal_triggers |= [component_or_manager]
238
+ component_or_manager.removal_triggers |= [self]
239
+ true
240
+ end
241
+
242
+ # Add a component or component manager so that it triggers this system when a component's attribute is changed.
243
+ # @return [Boolean] +true+
244
+ def trigger_when_is_changed(component_or_manager, attr)
245
+ if component_or_manager.attr_triggers[attr].nil?
246
+ component_or_manager.attr_triggers[attr] = [self]
247
+ else
248
+ component_or_manager.attr_triggers[attr] |= [self]
249
+ end
250
+ if attr_triggers[component_or_manager].nil?
251
+ attr_triggers[component_or_manager] = [attr]
252
+ else
253
+ attr_triggers[component_or_manager] |= [attr]
254
+ end
255
+ true
256
+ end
257
+ end
258
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nocov:
4
+ # Keeps the version of the Gem
5
+ module FelECS
6
+ # The version of the Gem
7
+ VERSION = '5.0.0'
8
+ end
9
+ # :nocov:
data/lib/felecs.rb ADDED
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'felecs/entity_manager'
4
+ require_relative 'felecs/component_manager'
5
+ require_relative 'felecs/system_manager'
6
+ require_relative 'felecs/scene_manager'
7
+ require_relative 'felecs/stage_manager'
8
+ require_relative 'felecs/order'
9
+
10
+ require_relative 'felecs/version'
11
+
12
+ # The FelECS namespace where all its functionality resides under.
13
+ module FelECS
14
+ class << self
15
+ # :nocov:
16
+
17
+ # An alias for {FelECS::Stage.call}. It executes a single frame in the game.
18
+ def call
19
+ FelECS::Stage.call
20
+ end
21
+ # :nocov:
22
+ end
23
+
24
+ # Creates and manages Entities. Entities are just collections of Components.
25
+ # You can use array methods directly on this class to access Entities.
26
+ class Entities; end
27
+
28
+ # Creates component managers and allows accessing them them under the {FelECS::Components} namespace as Constants.
29
+ # You can use array methods directly on this class to access Component Managers.
30
+ #
31
+ # To see how component managers are used please look at the {FelECS::ComponentManager} documentation.
32
+ module Components; end
33
+
34
+ # Creates and manages Systems. Systems are the logic of the game and do not contain any data within them. Any systems you create are accessable under the {FelECS::Systems} namespace as Constants.
35
+ # You can use array methods directly on this class to access Systems.
36
+ class Systems; end
37
+
38
+ # Creates and manages Scenes. Scenes are collections of Systems, and execute all the Systems when called upon. Any scenes you create are accessable under the {FelECS::Scenes} namespace as Constants.
39
+ class Scenes; end
40
+
41
+ # Stores Scenes you add to it which you want to execute on each frame. When called upon will execute all Systems in the Scenes in the Stage and will execute them according to their priority order.
42
+ module Stage; end
43
+
44
+ # Sets the priority of a list of Systems or Scenes for you in the order you pass them to this class.
45
+ module Order; end
46
+ end
47
+
48
+ # An alias for {FelECS}
49
+ FECS = FelECS
50
+
51
+ # An alias for {FelECS::Entities}
52
+ FECS::Ent = FelECS::Entities
53
+
54
+ # An alias for {FelECS::Components}
55
+ FECS::Cmp = FelECS::Components
56
+
57
+ # An alias for {FelECS::Systems}
58
+ FECS::Sys = FelECS::Systems
59
+
60
+ # An alias for {FelECS::Scenes}
61
+ FECS::Scn = FelECS::Scenes
62
+
63
+ # An alias for {FelECS::Stage}
64
+ FECS::Stg = FelECS::Stage
65
+
66
+ # An alias for {FelECS::
67
+ FECS::Odr = FelECS::Order
@@ -0,0 +1,4 @@
1
+ MRuby::Gem::Specification.new('mruby-felecs') do |spec|
2
+ spec.license = 'MIT'
3
+ spec.author = '_Tradam'
4
+ end