From 866ed5e2ca86d2f780492f01c30b3350037d3f7c Mon Sep 17 00:00:00 2001
From: realtradam
Date: Thu, 3 Feb 2022 04:18:07 -0500
Subject: added new group method for entity manager
---
CHANGELOG.mdown | 74 +-
Gemfile.lock | 2 +-
README.mdown | 27 +
docs/CNAME | 1 -
docs/FelECS.html | 2 +-
docs/FelECS/ComponentManager.html | 2 +-
docs/FelECS/Components.html | 2 +-
docs/FelECS/Entities.html | 145 ++-
docs/FelECS/Order.html | 2 +-
docs/FelECS/Scenes.html | 2 +-
docs/FelECS/Stage.html | 2 +-
docs/FelECS/Systems.html | 2 +-
docs/FelFlame.html | 319 ------
docs/FelFlame/ComponentManager.html | 1239 ---------------------
docs/FelFlame/Components.html | 333 ------
docs/FelFlame/Entities.html | 792 --------------
docs/FelFlame/Helper.html | 142 ---
docs/FelFlame/Helper/ComponentManager.html | 1627 ----------------------------
docs/FelFlame/Order.html | 251 -----
docs/FelFlame/Scenes.html | 765 -------------
docs/FelFlame/Stage.html | 572 ----------
docs/FelFlame/Systems.html | 1505 -------------------------
docs/Felflame_.html | 143 ---
docs/_index.html | 2 +-
docs/file.README.html | 2 +-
docs/file.version.html | 74 --
docs/index.html | 2 +-
docs/method_list.html | 52 +-
docs/top-level-namespace.html | 2 +-
lib/felecs/entity_manager.rb | 33 +
lib/felecs/version.rb | 2 +-
mrbgem/mrblib/felecs.rb | 35 +-
spec/entity_manager_spec.rb | 44 +
33 files changed, 367 insertions(+), 7832 deletions(-)
delete mode 100644 docs/CNAME
delete mode 100644 docs/FelFlame.html
delete mode 100644 docs/FelFlame/ComponentManager.html
delete mode 100644 docs/FelFlame/Components.html
delete mode 100644 docs/FelFlame/Entities.html
delete mode 100644 docs/FelFlame/Helper.html
delete mode 100644 docs/FelFlame/Helper/ComponentManager.html
delete mode 100644 docs/FelFlame/Order.html
delete mode 100644 docs/FelFlame/Scenes.html
delete mode 100644 docs/FelFlame/Stage.html
delete mode 100644 docs/FelFlame/Systems.html
delete mode 100644 docs/Felflame_.html
delete mode 100644 docs/file.version.html
diff --git a/CHANGELOG.mdown b/CHANGELOG.mdown
index 9b4bdba..5e922dd 100644
--- a/CHANGELOG.mdown
+++ b/CHANGELOG.mdown
@@ -8,6 +8,16 @@


+
+## [5.0.1](https://github.com/realtradam/FelFlame/releases/tag/5.0.1) - 2022-02-03
+
+- Added a method to the Entity Manager which allows grouping entities by components and then execute a block for each one.
+```ruby
+FelECS::Entities.group(@component_manager_one, @component_manager_two).do |cmp1, cmp2, ent|
+# do stuff with the components and entity
+end
+```
+
## [5.0.0](https://github.com/realtradam/FelFlame/releases/tag/5.0.0) - 2022-02-02

- Renamed to FelECS
@@ -27,54 +37,54 @@
- Various arrays are automatically compacted

-- Classes and Modules which used a data array now have the entire set of array methods available to their respective Classes and Modules
+ - Classes and Modules which used a data array now have the entire set of array methods available to their respective Classes and Modules
- Convenience methods for accessing a single entity in a component(`@component.entity`) and accessing a single entity in a component(`@entity.component[@component_manager]`)
-- `FelFlame::Order` class which can set the order of your Scenes and Systems
+ - `FelFlame::Order` class which can set the order of your Scenes and Systems
-
-- Replaced all instances of `sort_by!` with `sort_by` for compatibility with mruby
-- Minor optimizations such as less array duplication
+ 
+ - Replaced all instances of `sort_by!` with `sort_by` for compatibility with mruby
+ - Minor optimizations such as less array duplication
## [3.0.0](https://github.com/realtradam/FelFlame/releases/tag/3.0.0) - 2021-07-12
-
-- The Scene alias was changed from ```FelFlame::Sce``` to ```FelFlame::Scn``` as it is more intuitive.
+ 
+ - The Scene alias was changed from ```FelFlame::Sce``` to ```FelFlame::Scn``` as it is more intuitive.
## [2.0.0](https://github.com/realtradam/FelFlame/releases/tag/2.0.0) - 2021-07-10
-
-- Entities and Components now reference each other using the objects themselves rather then their id's
-```ruby
+ 
+ - Entities and Components now reference each other using the objects themselves rather then their id's
+ ```ruby
# before:
-@entity.components[@component_manager].each do |component_id|
- # iterate over id's, usually would need to do a lookup to get the component itself
-end
+ @entity.components[@component_manager].each do |component_id|
+# iterate over id's, usually would need to do a lookup to get the component itself
+ end
# after:
-@entity.components[@component_manager].each do |component|
- # iterate over the components themselves! No need for lookup anymore
-end
-```
-```ruby
+ @entity.components[@component_manager].each do |component|
+# iterate over the components themselves! No need for lookup anymore
+ end
+ ```
+ ```ruby
# same for components referencing entities
# before:
-@component.entities.each do |entity_id|
- #iterate over id's
-end
+ @component.entities.each do |entity_id|
+#iterate over id's
+ end
# after:
-@component.entities.each do |entity|
- # directly iterate over entities themselves!
-end
-```
-
+ @component.entities.each do |entity|
+# directly iterate over entities themselves!
+ end
+ ```
+
## [1.0.2](https://github.com/realtradam/FelFlame/releases/tag/1.0.2) - 2021-07-09
-
-- Stripped superflous files shrinking gem size significantly
+ 
+ - Stripped superflous files shrinking gem size significantly
## [1.0.1](https://github.com/realtradam/FelFlame/releases/tag/1.0.1) - 2021-07-09
-
-- Defining attributes in components are no longer allowed to overwrite methods
+ 
+ - Defining attributes in components are no longer allowed to overwrite methods
## [1.0.0](https://github.com/realtradam/FelFlame/releases/tag/1.0.0) - 2021-07-09
-
-- Initial release
+ 
+ - Initial release
diff --git a/Gemfile.lock b/Gemfile.lock
index 5ce0ad2..5f611bc 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- felecs (5.0.0)
+ felecs (5.0.1)
GEM
remote: https://rubygems.org/
diff --git a/README.mdown b/README.mdown
index 4aea705..dbee907 100644
--- a/README.mdown
+++ b/README.mdown
@@ -27,6 +27,7 @@
* [Accessing](#accessing)
* [Adding and Removing Components](#adding-and-removing-components)
* [Accessing Entities' Attached Components](#accessing-entities-attached-components)
+ * [Iterating Over Grouped Entities](#iterating-over-grouped-entities)
* [Deletion](#deletion)
* [Components](#components-1)
* [Creating a Component Manager](#creating-a-component-manager)
@@ -155,6 +156,32 @@ When Components are added to Entities, they can be accessed from the Entity. By
@entity.components[@component_manager] # => [@component1, @component2, @component3]
```
+### Iterating Over Grouped Entities
+You can execute a block for each entity that has all matching component types attached to it like so:
+
+```ruby
+FelECS::Entities.group(@component_manager_one, @component_manager_two).do |cmp1, cmp2, ent|
+ # do stuff with the components and entity
+end
+
+# or
+
+FelECS::Entities.group(@mgr1, @mgr2, @mgr3, @mgr4).do |cmp1, cmp2, cmp3, cmp4, ent|
+ # do stuff with the components and entity
+end
+
+# or
+
+FelECS::Entities.group(@component_manager_one).do |cmp1, ent|
+ # do stuff with the component and entity
+end
+
+# etc
+```
+
+You can use any number of component managers and it will only iterate over all entities that have at least all these components from these managers attached to them. The arguments in the block(the arguments surrounded by |pipes|) always correspond to each component manager passed as parameters into the group method and then followed by the entity object.
+This means there will be a number of these arguments equal to the number of component managers passed into the group method plus one for the entity.
+
### Deletion
To have all Components from an Entity **removed** and the Entity deleted we do the following:
diff --git a/docs/CNAME b/docs/CNAME
deleted file mode 100644
index 0b0c608..0000000
--- a/docs/CNAME
+++ /dev/null
@@ -1 +0,0 @@
-felflame.tradam.fyi
\ No newline at end of file
diff --git a/docs/FelECS.html b/docs/FelECS.html
index 0e3e334..62efd37 100644
--- a/docs/FelECS.html
+++ b/docs/FelECS.html
@@ -325,7 +325,7 @@
diff --git a/docs/FelECS/ComponentManager.html b/docs/FelECS/ComponentManager.html
index 5179b58..fb78748 100644
--- a/docs/FelECS/ComponentManager.html
+++ b/docs/FelECS/ComponentManager.html
@@ -1229,7 +1229,7 @@
diff --git a/docs/FelECS/Components.html b/docs/FelECS/Components.html
index b1e0185..e58decc 100644
--- a/docs/FelECS/Components.html
+++ b/docs/FelECS/Components.html
@@ -327,7 +327,7 @@
diff --git a/docs/FelECS/Entities.html b/docs/FelECS/Entities.html
index ac43403..6a5045c 100644
--- a/docs/FelECS/Entities.html
+++ b/docs/FelECS/Entities.html
@@ -120,6 +120,39 @@
+
Component Managers are what is used to create individual components which can be attached to entities. When a Component is created from a Component Manager that has accessors given to it, you can set or get the values of those accessors using standard ruby message sending (e.g @component.var = 5), or by using the #to_h and #update_attrs methods instead.
# File 'lib/felflame/component_manager.rb', line 132
-
-definitialize(**attrs)
- # Prepare the object
-# (this is a function created with metaprogramming
-# in FelFlame::Components)
-set_defaults
-
- # Fill params
-attrs.eachdo|key,value|
- send"#{key}=",value
- end
-
- # Save Component
-self.class.pushself
-end
Stores references to systems that should be triggered when this component is added to an enitity. Do not edit this array as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-180
-181
-182
-
-
-
# File 'lib/felflame/component_manager.rb', line 180
-
-defaddition_triggers
- @addition_triggers||=[]
-end
Stores references to systems that should be triggered when an attribute from this component changed. Do not edit this hash as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Hash<Symbol, System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-196
-197
-198
-
-
-
# File 'lib/felflame/component_manager.rb', line 196
-
-defattr_triggers
- @attr_triggers||={}
-end
-
-
-
-
-
-
-
-
-
-
- .removal_triggers ⇒ Array<System>
-
-
-
-
-
-
-
-
-
Stores references to systems that should be triggered when this component is removed from an enitity. Do not edit this array as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-188
-189
-190
-
-
-
# File 'lib/felflame/component_manager.rb', line 188
-
-defremoval_triggers
- @removal_triggers||=[]
-end
Stores references to systems that should be triggered when a component from this manager is added. Do not edit this array as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-109
-110
-111
-
-
-
# File 'lib/felflame/component_manager.rb', line 109
-
-defaddition_triggers
- @addition_triggers||=[]
-end
Stores references to systems that should be triggered when an attribute from this manager is changed. Do not edit this hash as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Hash<Symbol, Array<System>>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-125
-126
-127
-
-
-
# File 'lib/felflame/component_manager.rb', line 125
-
-defattr_triggers
- @attr_triggers||={}
-end
-
-
-
-
-
-
-
-
-
-
- #removal_triggers ⇒ Array<System>
-
-
-
-
-
-
-
-
-
Stores references to systems that should be triggered when a component from this manager is removed. Do not edit this array as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-117
-118
-119
-
-
-
# File 'lib/felflame/component_manager.rb', line 117
-
-defremoval_triggers
- @removal_triggers||=[]
-end
-
-
-
-
-
-
-
-
-
-
Instance Method Details
-
-
-
-
-
- #delete ⇒ Boolean
-
-
-
-
-
-
-
-
-
Removes this component from the list and purges all references to this Component from other Entities, as well as its data.
# File 'lib/felflame/component_manager.rb', line 247
-
-defdelete
- addition_triggers.eachdo|system|
- system.clear_triggerscomponent_or_manager:self
- end
- entities.reverse_eachdo|entity|
- entity.removeself
- end
- self.class._data.deleteself
- instance_variables.eachdo|var|
- instance_variable_set(var,nil)
- end
- true
-end
-
-
-
-
-
-
-
-
- #entities ⇒ Array<Component>
-
-
-
-
-
-
-
-
-
Entities that have this component
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<Component>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-209
-210
-211
-
-
-
# File 'lib/felflame/component_manager.rb', line 209
-
-defentities
- @entities||=[]
-end
-
-
-
-
-
-
-
-
- #entity ⇒ Component
-
-
-
-
-
-
-
-
-
A single entity. Use this if you expect the component to only belong to one entity and you want to access it.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Component)
-
-
-
-
-
-
-
-
-
-
-
-
-
-215
-216
-217
-218
-219
-220
-221
-222
-
-
-
# File 'lib/felflame/component_manager.rb', line 215
-
-defentity
- ifentities.empty?
- Warning.warn("This component belongs to NO entities but you called the method that is intended for components belonging to a single entity.\nYou may have a bug in your logic.")
- elsifentities.length>1
- Warning.warn("This component belongs to MANY entities but you called the method that is intended for components belonging to a single entity.\nYou may have a bug in your logic.")
- end
- entities.first
-end
-
-
-
-
-
-
-
-
- #to_h ⇒ Hash<Symbol, Value>
-
-
-
-
-
-
-
-
-
Returns A hash, where all the keys are attributes storing their respective values.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Hash<Symbol, Value>)
-
-
-
- —
-
-
A hash, where all the keys are attributes storing their respective values.
-
-
-
-
-
-
-
-
-
-
-
-
-262
-263
-264
-265
-266
-267
-268
-
-
-
# File 'lib/felflame/component_manager.rb', line 262
-
-defto_h
- return_hash=instance_variables.each_with_object({})do|key,final|
- final[key.to_s.delete_prefix('@').to_sym]=instance_variable_get(key)
- end
- return_hash.delete(:attr_triggers)
- return_hash
-end
Creates component managers and allows accessing them them under the Components namespace as Constants. You can use array methods directly on this class to access Component Managers.
-
-
To see how component managers are used please look at the ComponentManager documentation.
# Here color is set to default to red
-# while max and current are nil until set.
-# When you make a new component using this component manager
-# these are the values and accessors it will have.
-FelFlame::Component.new('Health',:max,:current,color:'red')
-
-
-
Parameters:
-
-
-
-
- component_name
-
-
- (String)
-
-
-
- —
-
-
Name of your new component manager. Must be stylized in the format of constants in Ruby
-
-
-
-
-
-
- attrs
-
-
- (:Symbols)
-
-
-
- —
-
-
New components made with this manager will include these symbols as accessors, the values of these accessors will default to nil
A hash that uses component manager constant names as keys, and where the values of those keys are arrays that contain the the components attached to this entity.
Can be any number of components, identical duplicates will be automatically purged however different components from the same component manager are allowed.
-
-
-
-
-
-
-
-
-
-
-
-
-
-8
-9
-10
-11
-12
-
-
-
# File 'lib/felflame/entity_manager.rb', line 8
-
-definitialize(*components)
- # Add each component
-add(*components)
- self.class._data.pushself
-end
A single component from a component manager. Use this if you expect the component to only belong to one entity and you want to access it. Access the component using either parameter notation or array notation. Array notation is conventional for better readablility.
# File 'lib/felflame/entity_manager.rb', line 26
-
-defcomponent(manager=nil)
- ifmanager.nil?
- FelFlame::Entities.component_redirect.entity=self
- FelFlame::Entities.component_redirect
- else
- ifcomponents[manager].nil?
- raise"This entity(#{self}) doesnt have any components of this type: #{manager}"
- elsifcomponents[manager].length>1
- Warning.warn("This entity has MANY of this component but you called the method that is intended for having a single of this component type.\nYou may have a bug in your logic.")
- end
-
- components[manager].first
- end
-end
A hash that uses component manager constant names as keys, and where the values of those keys are arrays that contain the the components attached to this entity.
Component Managers are what is used to create individual components which can be attached to entities. When a Component is created from a Component Manager that has accessors given to it, you can set or get the values of those accessors using standard ruby message sending (e.g @component.var = 5), or by using the #attrs and #update_attrs methods instead.
# File 'component_manager.rb', line 104
-
-definitialize(**attrs)
- # Prepare the object
-# (this is a function created with metaprogramming
-# in FelFlame::Components
-set_defaults
-
- # Generate ID
-new_id=self.class.data.find_index{|i|i.nil?}
- new_id=self.class.data.sizeifnew_id.nil?
- @id=new_id
-
- # Fill params
-attrs.eachdo|key,value|
- send"#{key}=",value
- end
-
- # Save Component
-self.class.data[new_id]=self
-end
Stores references to systems that should be triggered when this component is added to an enitity. Do not edit this array as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-136
-137
-138
-
-
-
# File 'component_manager.rb', line 136
-
-defaddition_triggers
- @addition_triggers||=[]
-end
Stores references to systems that should be triggered when an attribute from this component changed. Do not edit this hash as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Hash<Symbol, System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-152
-153
-154
-
-
-
# File 'component_manager.rb', line 152
-
-defattr_triggers
- @attr_triggers||={}
-end
-
-
-
-
-
-
-
-
-
-
- .removal_triggers ⇒ Array<System>
-
-
-
-
-
-
-
-
-
Stores references to systems that should be triggered when this component is removed from an enitity. Do not edit this array as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-144
-145
-146
-
-
-
# File 'component_manager.rb', line 144
-
-defremoval_triggers
- @removal_triggers||=[]
-end
Stores references to systems that should be triggered when a component from this manager is added. Do not edit this array as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-81
-82
-83
-
-
-
# File 'component_manager.rb', line 81
-
-defaddition_triggers
- @addition_triggers||=[]
-end
Stores references to systems that should be triggered when an attribute from this manager is changed. Do not edit this hash as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Hash<Symbol, Array<System>>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-97
-98
-99
-
-
-
# File 'component_manager.rb', line 97
-
-defattr_triggers
- @attr_triggers||={}
-end
-
-
-
-
-
-
-
-
-
-
- #id ⇒ Integer
-
-
-
-
-
-
-
-
-
Holds the unique ID of a component. The ID is only unique within the scope of the component manager it was created from.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Integer)
-
-
-
-
-
-
-
-
-
-
-
-
-
-63
-64
-65
-
-
-
# File 'component_manager.rb', line 63
-
-defid
- @id
-end
-
-
-
-
-
-
-
-
-
-
- #removal_triggers ⇒ Array<System>
-
-
-
-
-
-
-
-
-
Stores references to systems that should be triggered when a component from this manager is removed. Do not edit this array as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<System>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-89
-90
-91
-
-
-
# File 'component_manager.rb', line 89
-
-defremoval_triggers
- @removal_triggers||=[]
-end
-
-
-
-
-
-
-
-
-
-
Class Method Details
-
-
-
-
-
- .[](component_id) ⇒ Component
-
-
-
-
-
-
-
-
-
Gets a Component from the given unique ID. Usage is simular to how an Array lookup works.
-
-
-
-
-
-
-
-
Examples:
-
-
-
# this gets the 'Health' Component with ID 7
-FelFlame::Components::Health[7]
-
-
-
Parameters:
-
-
-
-
- component_id
-
-
- (Integer)
-
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Component)
-
-
-
- —
-
-
Returns the Component that uses the given unique ID, nil if there is no Component associated with the given ID
-
-
-
-
-
-
-
-
-
-
-
-
-169
-170
-171
-
-
-
# File 'component_manager.rb', line 169
-
-def[](component_id)
- data[component_id]
-end
-
-
-
-
-
-
-
-
- .each(&block) ⇒ Enumerator
-
-
-
-
-
-
-
-
-
Iterates over all components within the component manager. Special Enumerable methods like map or each_with_index are not implemented
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Enumerator)
-
-
-
-
-
-
-
-
-
-
-
-
-
-176
-177
-178
-
-
-
# File 'component_manager.rb', line 176
-
-defeach(&block)
- data.compact.each(&block)
-end
# File 'component_manager.rb', line 215
-
-defdelete
- addition_triggers.eachdo|system|
- system.clear_triggerscomponent_or_manager:self
- end
- # This needs to be cloned because indices get deleted as
-# the remove command is called, breaking the loop if it
-# wasn't referencing a clone(will get Nil errors)
-iter=entities.map(&:clone)
- iter.eachdo|entity_id|
- FelFlame::Entities[entity_id].removeself#unless FelFlame::Entities[entity_id].nil?
-end
- self.class.data[id]=nil
- instance_variables.eachdo|var|
- instance_variable_set(var,nil)
- end
- true
-end
-
-
-
-
-
-
-
-
- #entities ⇒ Array<Integer>
-
-
-
-
-
-
-
-
-
A list of entity ids that are linked to the component
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<Integer>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-189
-190
-191
-
-
-
# File 'component_manager.rb', line 189
-
-defentities
- @entities||=[]
-end
Sets the priority of all items passed into this method according to the order they were passed.
-
-
-
-
-
-
-
-
-
-
-
-
Class Method Details
-
-
-
-
-
- .sort(*sortables) ⇒ Boolean
-
-
-
-
-
-
-
-
-
Sets the priority of all items passed into this method according to the order they were passed. If an array is one of the elements then it will give all of those elements in the array the same priority.
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 Scenes namespace as Constants.
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.
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 Systems namespace as Constants. You can use array methods directly on this class to access Systems.
Stores references to systems that should be triggered when an attribute from this manager is changed Do not edit this hash as it is managed by FelFlame automatically.
Stores references to components or their managers that trigger this component when a component or component from that manager is removed from an entity.
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.
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.
Creates a new System which can be accessed as a constant under the namespace FelFlame::Systems. The name given is what constant the system is assigned to
-
-
-
-
-
-
-
-
Examples:
-
-
-
FelFlame::Systems.new('PassiveHeal',priority:-2)do
- FelFlame::Components::Health.eachdo|component|
- component.hp+=5
- end
-end
-# Give it a low priority so other systems such as a
-# Poison system would kill the player first
-
-
-
Parameters:
-
-
-
-
- name
-
-
- (String)
-
-
-
- —
-
-
The name this system will use. Needs to to be in the Ruby Constant format.
Stores references to components or their managers that trigger this component when a component or component from that manager is added to an entity. Do not edit this hash as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<Component>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-35
-36
-37
-
-
-
# File 'lib/felflame/system_manager.rb', line 35
-
-defaddition_triggers
- @addition_triggers||=[]
-end
Stores references to systems that should be triggered when an attribute from this manager is changed Do not edit this hash as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Hash<Symbol, Array<Symbol>>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-52
-53
-54
-
-
-
# File 'lib/felflame/system_manager.rb', line 52
-
-defattr_triggers
- @attr_triggers||={}
-end
-
-
-
-
-
-
-
-
-
-
- #priority ⇒ Object
-
-
-
-
-
-
-
-
-
How early this System should be executed in a list of Systems
-
-
-
-
-
-
-
-
-
-
-
-
-
-6
-7
-8
-
-
-
# File 'lib/felflame/system_manager.rb', line 6
-
-defpriority
- @priority
-end
Stores references to components or their managers that trigger this component when a component or component from that manager is removed from an entity. Do not edit this hash as it is managed by FelFlame automatically.
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
- (Array<Component>)
-
-
-
-
-
-
-
-
-
-
-
-
-
-44
-45
-46
-
-
-
# File 'lib/felflame/system_manager.rb', line 44
-
-defremoval_triggers
- @removal_triggers||=[]
-end
-
-
-
-
-
-
-
-
-
-
- #scenes ⇒ Object
-
-
-
-
-
-
-
-
-
-
-
-19
-20
-21
-
-
-
# File 'lib/felflame/system_manager.rb', line 19
-
-defscenes
- @scenes||=[]
-end
-
-
-
-
-
-
-
-
-
-
Class Method Details
-
-
-
-
-
- .const_cache ⇒ Object
-
-
-
-
-
-
-
-
-
Stores the systems in Components. This is needed because calling `FelFlame::Components.constants` will not let you iterate over the value of the constants but will instead give you an array of symbols. This caches the convertion of those symbols to the actual value of the constants
-
-
-
-
-
-
-
-
-
-
-
-
-
-63
-64
-65
-
-
-
# File 'lib/felflame/system_manager.rb', line 63
-
-defconst_cache
- @const_cache||update_const_cache
-end
-
-
-
-
-
-
-
-
-
Instance Method Details
-
-
-
-
-
- #call ⇒ Object
-
-
-
-
-
-
-
-
-
Manually execute the system a single time
-
-
-
-
-
-
-
-
-
-
-
-
-
-124
-125
-126
-
-
-
# File 'lib/felflame/system_manager.rb', line 124
-
-defcall
- @block.call
-end
Removes triggers from this system. This function is fairly flexible so it can accept a few different inputs For addition and removal triggers, you can optionally pass in a component, or a manager to clear specifically the relevant triggers for that one component or manager. If you do not pass a component or manager then it will clear triggers for all components and managers. For attr_triggers
-
-
-
-
-
-
-
-
Examples:
-
-
-
# To clear all triggers that execute this system when a component is added:
-FelFlame::Systems::ExampleSystem.clear_triggers:addition_triggers
-# Same as above but for when a component is removed instead
-FelFlame::Systems::ExampleSystem.clear_triggers:removal_triggers
-# Same as above but for when a component has a certain attribute changed
-FelFlame::Systems::ExampleSystem.clear_triggers:attr_triggers
-
-# Clear a trigger from a specific component
-FelFlame::Systems::ExampleSystem.clear_triggers:addition_triggers,FelFlame::Component::ExampleComponent[0]
-# Clear a trigger from a specific component manager
-FelFlame::Systems::ExampleSystem.clear_triggers:addition_triggers,FelFlame::Component::ExampleComponent
-
-# Clear the trigger that executes a system when the ':example_attr' is changes
-FelFlame::Systems::ExampleSystem.clear_triggers:attr_triggers,:example_attr
-
-
-
Parameters:
-
-
-
-
- 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
-
-
-
-
-
-
- component_or_manager
-
-
- (Component or ComponentManager)
-
-
- (defaults to: nil)
-
-
- —
-
-
The object to clear triggers from. Use Nil to clear triggers from all components associated with this system.
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
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
diff --git a/lib/felecs/entity_manager.rb b/lib/felecs/entity_manager.rb
index a457ed2..c1f8cd8 100644
--- a/lib/felecs/entity_manager.rb
+++ b/lib/felecs/entity_manager.rb
@@ -100,6 +100,39 @@ module FelECS
# def to_json() end
class << self
+ # Selects every entity that has components of they type passed into
+ # this function and then executes the code block for those components
+ # and entities.
+ # @return [Nil]
+ def group(*component_managers, &block)
+ return nil if component_managers.empty?
+ if component_managers.length == 1
+ component_managers.first.each do |cmp|
+ block.call(cmp, cmp.entity)
+ end
+ else
+ arry = component_managers.first.select do |cmp|
+ ent = cmp.entity
+ keep = true
+ component_managers.drop(1).each do |mgr|
+ next unless ent.components[mgr].nil?
+ keep = false
+ break
+ end
+ keep
+ end
+ arry.each do |cmp|
+ ent = cmp.entity
+ cmp_arry = [cmp]
+ component_managers.drop(1).each do |cmp_mgr|
+ cmp_arry.push ent.component[cmp_mgr]
+ end
+ block.call(*cmp_arry, ent)
+ end
+ end
+ nil
+ end
+
# Makes component managers behave like arrays with additional
# methods for managing the array
# @!visibility private
diff --git a/lib/felecs/version.rb b/lib/felecs/version.rb
index 295b905..507e0f1 100644
--- a/lib/felecs/version.rb
+++ b/lib/felecs/version.rb
@@ -4,6 +4,6 @@
# Keeps the version of the Gem
module FelECS
# The version of the Gem
- VERSION = '5.0.0'
+ VERSION = '5.0.1'
end
# :nocov:
diff --git a/mrbgem/mrblib/felecs.rb b/mrbgem/mrblib/felecs.rb
index 56726b2..77e70cd 100644
--- a/mrbgem/mrblib/felecs.rb
+++ b/mrbgem/mrblib/felecs.rb
@@ -100,6 +100,39 @@ module FelECS
# def to_json() end
class << self
+ # Selects every entity that has components of they type passed into
+ # this function and then executes the code block for those components
+ # and entities.
+ # @return [Nil]
+ def group(*component_managers, &block)
+ return nil if component_managers.empty?
+ if component_managers.length == 1
+ component_managers.first.each do |cmp|
+ block.call(cmp, cmp.entity)
+ end
+ else
+ arry = component_managers.first.select do |cmp|
+ ent = cmp.entity
+ keep = true
+ component_managers.drop(1).each do |mgr|
+ next unless ent.components[mgr].nil?
+ keep = false
+ break
+ end
+ keep
+ end
+ arry.each do |cmp|
+ ent = cmp.entity
+ cmp_arry = [cmp]
+ component_managers.drop(1).each do |cmp_mgr|
+ cmp_arry.push ent.component[cmp_mgr]
+ end
+ block.call(*cmp_arry, ent)
+ end
+ end
+ nil
+ end
+
# Makes component managers behave like arrays with additional
# methods for managing the array
# @!visibility private
@@ -847,7 +880,7 @@ end
# Keeps the version of the Gem
module FelECS
# The version of the Gem
- VERSION = '5.0.0'
+ VERSION = '5.0.1'
end
# :nocov:
diff --git a/spec/entity_manager_spec.rb b/spec/entity_manager_spec.rb
index 25afa45..234c424 100644
--- a/spec/entity_manager_spec.rb
+++ b/spec/entity_manager_spec.rb
@@ -8,6 +8,8 @@ describe 'Entities' do
before :all do
$VERBOSE = nil
@component_manager ||= FelECS::Components.new('TestEntity', :param1, param2: 'def')
+ @component_manager1 ||= FelECS::Components.new('TestEntity1', :param1, param2: 'def')
+ @component_manager2 ||= FelECS::Components.new('TestEntity2', :param1, param2: 'def')
end
before :each do
@@ -25,6 +27,48 @@ describe 'Entities' do
$stderr = @orig_stderr
FelECS::Entities.reverse_each(&:delete)
@component_manager.reverse_each(&:delete)
+ @component_manager1.reverse_each(&:delete)
+ @component_manager2.reverse_each(&:delete)
+ end
+
+ it 'can iterate over component groups' do
+ cmp_1_1 = @component_manager1.new(param1: 1)
+ cmp_1_2 = @component_manager1.new(param1: 1)
+ cmp_1_3 = @component_manager1.new(param1: 1)
+ cmp_remove = @component_manager2.new
+ @ent0.add cmp_1_1, @cmp0, cmp_1_1, @component_manager2.new
+ @ent1.add cmp_1_2, @cmp1, cmp_1_2, cmp_remove
+ @ent2.add cmp_1_3, @component_manager2.new
+ @ent1.remove cmp_remove
+ FelECS::Entities.group(@component_manager1, @component_manager2) do |cmp1, cmp2, ent|
+ cmp1.param1 += 1
+ end
+ cmp_1_1.param1
+ expect(cmp_1_1.param1).to eq(2)
+ expect(cmp_1_2.param1).to eq(1)
+ expect(cmp_1_3.param1).to eq(2)
+ end
+
+ it 'can iterate over a single component in a group' do
+ @ent0.add @cmp0
+ @ent1.add @cmp1
+ @ent2.add @cmp2
+ @cmp0.param1 = @cmp1.param1 = @cmp2.param1 = 1
+ FelECS::Entities.group(@component_manager) do |cmp, ent|
+ cmp.param1 += 1
+ end
+ expect(@cmp0.param1).to eq(2)
+ expect(@cmp1.param1).to eq(2)
+ expect(@cmp2.param1).to eq(2)
+ end
+
+ it 'can iterate over no components in a group' do
+ @cmp0.param1 = 1
+ @ent0.add @cmp0
+ FelECS::Entities.group do
+ @cmp0.param1 = 0
+ end
+ expect(@cmp0.param1).to eq(1)
end
it 'can get a single component' do
--
cgit v1.2.3