From aad51d646812804052ce2ae5e926b667b4e24d6d Mon Sep 17 00:00:00 2001 From: realtradam Date: Mon, 3 Jan 2022 08:19:24 -0500 Subject: . --- CHANGELOG.mdown | 65 ++++++++------- README.mdown | 32 ++++++- docs/FelFlame.html | 24 +++++- docs/FelFlame/ComponentManager.html | 162 ++++++++++++++++++------------------ docs/FelFlame/Components.html | 2 +- docs/FelFlame/Entities.html | 2 +- docs/FelFlame/Order.html | 109 ++++++++++++++++++++---- docs/FelFlame/Scenes.html | 2 +- docs/FelFlame/Stage.html | 2 +- docs/FelFlame/Systems.html | 2 +- docs/Felflame_.html | 2 +- docs/_index.html | 2 +- docs/file.README.html | 2 +- docs/index.html | 2 +- docs/method_list.html | 2 +- docs/top-level-namespace.html | 2 +- lib/felflame.rb | 7 ++ lib/felflame/component_manager.rb | 22 ----- lib/felflame/order.rb | 19 ++++- spec/order_spec.rb | 71 ++++++++++++++++ 20 files changed, 366 insertions(+), 167 deletions(-) create mode 100644 spec/order_spec.rb diff --git a/CHANGELOG.mdown b/CHANGELOG.mdown index e60824f..454c4b8 100644 --- a/CHANGELOG.mdown +++ b/CHANGELOG.mdown @@ -14,59 +14,60 @@ ![Changed](https://img.shields.io/badge/-Changed-yellow) - Scenes now have their own priority attribute - - Stages now sort and execute by Scenes, rather then the net Systems in the Scenes +- Stages now sort and execute by Scenes, rather then the net Systems in the Scenes - Component method `.attrs` was renamed to `.to_h` - Renamed the `data` accessor to `._data` - Various arrays are automatically compacted ![Added](https://img.shields.io/badge/-Added-brightgreen) -- 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 if your Scenes and Systems -![Fixed](https://img.shields.io/badge/-Fixed-blue) -- Replaced all instances of `sort_by!` with `sort_by` for compatibility with mruby -- Minor optimizations such as less array duplication + ![Fixed](https://img.shields.io/badge/-Fixed-blue) + - 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 -![Changed](https://img.shields.io/badge/-Changed-yellow) -- The Scene alias was changed from ```FelFlame::Sce``` to ```FelFlame::Scn``` as it is more intuitive + ![Changed](https://img.shields.io/badge/-Changed-yellow) + - 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 -![Changed](https://img.shields.io/badge/-Changed-yellow) -- Entities and Components now reference each other using the objects themselves rather then their id's -```ruby + ![Changed](https://img.shields.io/badge/-Changed-yellow) + - 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 -![Fixed](https://img.shields.io/badge/-Fixed-blue) -- Stripped superflous files shrinking gem size significantly + ![Fixed](https://img.shields.io/badge/-Fixed-blue) + - Stripped superflous files shrinking gem size significantly ## [1.0.1](https://github.com/realtradam/FelFlame/releases/tag/1.0.1) - 2021-07-09 -![Fixed](https://img.shields.io/badge/-Fixed-blue) -- Defining attributes in components are no longer allowed to overwrite methods + ![Fixed](https://img.shields.io/badge/-Fixed-blue) + - 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 -![Added](https://img.shields.io/badge/-Added-brightgreen) -- Initial release + ![Added](https://img.shields.io/badge/-Added-brightgreen) + - Initial release diff --git a/README.mdown b/README.mdown index e281b7e..acae578 100644 --- a/README.mdown +++ b/README.mdown @@ -20,6 +20,7 @@ * [Systems](#systems) * [Scenes](#scenes) * [Stage](#stage) + * [Order](#order) * [Usage](#usage) * [Entities](#entities-1) * [Creation](#creation) @@ -51,6 +52,8 @@ * [Adding Scenes](#adding-scenes) * [Removing Scenes](#removing-scenes) * [Executing](#executing) + * [Order](#order-1) + * [Setting the order](#setting-the-order) * [Closing Notes](#closing-notes) * [Contribution](#contribution) @@ -83,7 +86,7 @@ Systems are where all the logic or code is kept. There is no data stored in here By using this pattern it allows programmers to easily control what an "object" or entity can do and how much data it needs to have. It avoids the issue of inhertance as no inhertance is ever required in this system. If you need a certain entity to have a certain functionality you just add the relevant component to it, and the systems that automatically go over specific components will give your entitiy the desired functionality. -**"But your framework also has `Scenes` and a `Stage`, what is that about?"** +**"But your framework also has `Scenes`, `Stage`, and `Order`, what is that about?"** --- @@ -93,6 +96,9 @@ Scenes are simply a collection or subset of Systems. This allows for an easy way ### Stage The Stage is Scenes which are activated. This means any Scenes on the Stage are executed each frame, while the rest of the Systems are not. +### Order +Order is a helper class which can set the priority of Scenes and Systems. + --- @@ -423,6 +429,30 @@ On each frame of the game generally we will want to execute the Stage once. When FelFlame::Stage.call ``` +## Order + +### Setting the order + +To set the order you just need to call `FelFlame::Order.sort` and pass Scenes or Systems in the parameters in the order you wish for them to execute + +```ruby +FelFlame::Order.sort( + @system1, + @system2, + @system3 +) +``` + +If you want some Scenes or Systems to have the same priority then just pass them as an array: + +```ruby +FelFlame::Order.sort( + @scene1, + [@scene2_1, @scene2_2], + @scene3 +) +``` + ## Closing Notes There are some methods I haven't gone over in the overview. If you want to see everything and read in more detail check out the [Documentation](https://felflame.tradam.fyi)! diff --git a/docs/FelFlame.html b/docs/FelFlame.html index 1ec7927..0320860 100644 --- a/docs/FelFlame.html +++ b/docs/FelFlame.html @@ -197,6 +197,22 @@
FelFlame::Stage
+
Odr = +
+
+
An alias for {FelFlame +
+ + +
+
+
+ + +
+
+
FelFlame::Order
+ @@ -273,12 +289,12 @@
 
 
-17
 18
-19
+19 +20 -
# File 'lib/felflame.rb', line 17
+      
# File 'lib/felflame.rb', line 18
 
 def call
   FelFlame::Stage.call
@@ -293,7 +309,7 @@
 
 
       
diff --git a/docs/FelFlame/ComponentManager.html b/docs/FelFlame/ComponentManager.html
index 143fd3a..a29ad10 100644
--- a/docs/FelFlame/ComponentManager.html
+++ b/docs/FelFlame/ComponentManager.html
@@ -507,23 +507,23 @@
       
 
 
-154
-155
-156
-157
-158
-159
-160
-161
-162
-163
-164
-165
-166
-167
+132 +133 +134 +135 +136 +137 +138 +139 +140 +141 +142 +143 +144 +145
-
# File 'lib/felflame/component_manager.rb', line 154
+      
# File 'lib/felflame/component_manager.rb', line 132
 
 def initialize(**attrs)
   # Prepare the object
@@ -590,12 +590,12 @@
       
 
 
-202
-203
-204
+180 +181 +182
-
# File 'lib/felflame/component_manager.rb', line 202
+      
# File 'lib/felflame/component_manager.rb', line 180
 
 def addition_triggers
   @addition_triggers ||= []
@@ -646,12 +646,12 @@
       
 
 
-218
-219
-220
+196 +197 +198
-
# File 'lib/felflame/component_manager.rb', line 218
+      
# File 'lib/felflame/component_manager.rb', line 196
 
 def attr_triggers
   @attr_triggers ||= {}
@@ -702,12 +702,12 @@
       
 
 
-210
-211
-212
+188 +189 +190
-
# File 'lib/felflame/component_manager.rb', line 210
+      
# File 'lib/felflame/component_manager.rb', line 188
 
 def removal_triggers
   @removal_triggers ||= []
@@ -763,12 +763,12 @@
       
 
 
-131
-132
-133
+109 +110 +111
-
# File 'lib/felflame/component_manager.rb', line 131
+      
# File 'lib/felflame/component_manager.rb', line 109
 
 def addition_triggers
   @addition_triggers ||= []
@@ -819,12 +819,12 @@
       
 
 
-147
-148
-149
+125 +126 +127
-
# File 'lib/felflame/component_manager.rb', line 147
+      
# File 'lib/felflame/component_manager.rb', line 125
 
 def attr_triggers
   @attr_triggers ||= {}
@@ -875,12 +875,12 @@
       
 
 
-139
-140
-141
+117 +118 +119
-
# File 'lib/felflame/component_manager.rb', line 139
+      
# File 'lib/felflame/component_manager.rb', line 117
 
 def removal_triggers
   @removal_triggers ||= []
@@ -941,22 +941,22 @@
       
 
 
-269
-270
-271
-272
-273
-274
-275
-276
-277
-278
-279
-280
-281
+247 +248 +249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259
-
# File 'lib/felflame/component_manager.rb', line 269
+      
# File 'lib/felflame/component_manager.rb', line 247
 
 def delete
   addition_triggers.each do |system|
@@ -1015,12 +1015,12 @@
       
 
 
-231
-232
-233
+209 +210 +211
-
# File 'lib/felflame/component_manager.rb', line 231
+      
# File 'lib/felflame/component_manager.rb', line 209
 
 def entities
   @entities ||= []
@@ -1069,17 +1069,17 @@
       
 
 
-237
-238
-239
-240
-241
-242
-243
-244
+215 +216 +217 +218 +219 +220 +221 +222
-
# File 'lib/felflame/component_manager.rb', line 237
+      
# File 'lib/felflame/component_manager.rb', line 215
 
 def entity
   if entities.empty?
@@ -1138,16 +1138,16 @@
       
 
 
-284
-285
-286
-287
-288
-289
-290
+262 +263 +264 +265 +266 +267 +268
-
# File 'lib/felflame/component_manager.rb', line 284
+      
# File 'lib/felflame/component_manager.rb', line 262
 
 def to_h
   return_hash = instance_variables.each_with_object({}) do |key, final|
@@ -1205,14 +1205,14 @@
       
 
 
-248
-249
-250
-251
-252
+226 +227 +228 +229 +230
-
# File 'lib/felflame/component_manager.rb', line 248
+      
# File 'lib/felflame/component_manager.rb', line 226
 
 def update_attrs(**opts)
   opts.each do |key, value|
@@ -1229,7 +1229,7 @@
 
 
       
diff --git a/docs/FelFlame/Components.html b/docs/FelFlame/Components.html
index 37c11bf..43a84f0 100644
--- a/docs/FelFlame/Components.html
+++ b/docs/FelFlame/Components.html
@@ -323,7 +323,7 @@
 
 
       
diff --git a/docs/FelFlame/Entities.html b/docs/FelFlame/Entities.html
index 43d61fa..f7ae79a 100644
--- a/docs/FelFlame/Entities.html
+++ b/docs/FelFlame/Entities.html
@@ -782,7 +782,7 @@
 
 
       
diff --git a/docs/FelFlame/Order.html b/docs/FelFlame/Order.html
index b17a786..e55ce58 100644
--- a/docs/FelFlame/Order.html
+++ b/docs/FelFlame/Order.html
@@ -78,12 +78,25 @@
   
   
Defined in:
-
lib/felflame/order.rb
+
lib/felflame.rb,
+ lib/felflame/order.rb
+
+

Overview

+
+ +

Sets the priority of a list of Systems or Scenes for you in the order you pass them to this class.

+ + +
+
+
+ +
@@ -92,7 +105,7 @@

- Instance Method Summary + Class Method Summary collapse

@@ -101,7 +114,7 @@
  • - #sort(*sortables) ⇒ Object + .sort(*sortables) ⇒ Boolean @@ -115,7 +128,9 @@ -
    +
    +

    Sets the priority of all items passed into this method according to the order they were passed.

    +
  • @@ -125,32 +140,96 @@ -
    -

    Instance Method Details

    +
    +

    Class Method Details

    -

    +

    + + .sort(*sortables) ⇒ Boolean - #sort(*sortables) ⇒ Object + + + +

    +
    + +

    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.

    + +
    +
    +
    +

    Parameters:

    + +

    Returns:

    +
      + +
    • + + + (Boolean) + + + + — +
      +

      true.

      +
      + +
    • - + + +
      @@ -162,7 +241,7 @@ diff --git a/docs/FelFlame/Scenes.html b/docs/FelFlame/Scenes.html index 6245fa6..fd224e6 100644 --- a/docs/FelFlame/Scenes.html +++ b/docs/FelFlame/Scenes.html @@ -755,7 +755,7 @@ diff --git a/docs/FelFlame/Stage.html b/docs/FelFlame/Stage.html index e944f44..2acb4cb 100644 --- a/docs/FelFlame/Stage.html +++ b/docs/FelFlame/Stage.html @@ -562,7 +562,7 @@ diff --git a/docs/FelFlame/Systems.html b/docs/FelFlame/Systems.html index be6a374..4fe103e 100644 --- a/docs/FelFlame/Systems.html +++ b/docs/FelFlame/Systems.html @@ -1495,7 +1495,7 @@ diff --git a/docs/Felflame_.html b/docs/Felflame_.html index ac59bac..ee0ed1d 100644 --- a/docs/Felflame_.html +++ b/docs/Felflame_.html @@ -133,7 +133,7 @@ diff --git a/docs/_index.html b/docs/_index.html index d3becf8..e2c0548 100644 --- a/docs/_index.html +++ b/docs/_index.html @@ -183,7 +183,7 @@ diff --git a/docs/file.README.html b/docs/file.README.html index 8565de7..e075115 100644 --- a/docs/file.README.html +++ b/docs/file.README.html @@ -518,7 +518,7 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

      diff --git a/docs/index.html b/docs/index.html index 916cce2..38b9106 100644 --- a/docs/index.html +++ b/docs/index.html @@ -518,7 +518,7 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

      diff --git a/docs/method_list.html b/docs/method_list.html index e0c8da1..5f38f92 100644 --- a/docs/method_list.html +++ b/docs/method_list.html @@ -358,7 +358,7 @@
    • - #sort + sort FelFlame::Order
    • diff --git a/docs/top-level-namespace.html b/docs/top-level-namespace.html index 78ec112..0a054fc 100644 --- a/docs/top-level-namespace.html +++ b/docs/top-level-namespace.html @@ -127,7 +127,7 @@ diff --git a/lib/felflame.rb b/lib/felflame.rb index b7a386a..21a6eaf 100644 --- a/lib/felflame.rb +++ b/lib/felflame.rb @@ -5,6 +5,7 @@ require_relative 'felflame/component_manager' require_relative 'felflame/system_manager' require_relative 'felflame/scene_manager' require_relative 'felflame/stage_manager' +require_relative 'felflame/order' require_relative 'felflame/version' @@ -39,6 +40,9 @@ module FelFlame # 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. module Stage; end + + # Sets the priority of a list of Systems or Scenes for you in the order you pass them to this class. + module Order; end end # An alias for {FelFlame} @@ -58,3 +62,6 @@ FF::Scn = FelFlame::Scenes # An alias for {FelFlame::Stage} FF::Stg = FelFlame::Stage + +# An alias for {FelFlame:: +FF::Odr = FelFlame::Order diff --git a/lib/felflame/component_manager.rb b/lib/felflame/component_manager.rb index 04faccb..6ac7463 100644 --- a/lib/felflame/component_manager.rb +++ b/lib/felflame/component_manager.rb @@ -48,28 +48,6 @@ module FelFlame FelFlame::Components.const_get(component_name) end - # Makes component module behave like an array of component - # managers with additional methods for managing the array - # @!visibility private - # #def respond_to_missing?(method, *) - # if constants.respond_to? method - # true - # else - # super - # end - # end - - ## Makes component module behave like arrays with additional - ## methods for managing the array - ## @!visibility private - # def method_missing(method, *args, **kwargs, &block) - # if constants.respond_to? method - # constants.send(method, *args, **kwargs, &block) - # else - # super - # end - # end - # Stores the components managers in {FelFlame::Components}. This # is needed because calling `FelFlame::Components.constants` # will not let you iterate over the value of the constants diff --git a/lib/felflame/order.rb b/lib/felflame/order.rb index 0bed66b..3202b25 100644 --- a/lib/felflame/order.rb +++ b/lib/felflame/order.rb @@ -1,6 +1,23 @@ module FelFlame module Order - def sort(*sortables) + + # 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. + # @param sortables [(Systems and Array) or (Scenes and Array)] + # @return [Boolean] +true+. + def self.sort(*sortables) + sortables.each_with_index do |sorted, index| + if sorted.respond_to? :priority + sorted.priority = index + else + sorted.each do |item| + item.priority = index + end + end + end + true end end end diff --git a/spec/order_spec.rb b/spec/order_spec.rb new file mode 100644 index 0000000..2dfe7a6 --- /dev/null +++ b/spec/order_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require_relative '../lib/felflame' + +# class EntitiesTest < Minitest::Test + +describe 'Order' do + before :all do + @result = [] + @system0 = FelFlame::Systems.new('System1', priority: 0) do + @result.push 0 + end + @system2 = FelFlame::Systems.new('System3', priority: 2) do + @result.push 2 + end + @system1 = FelFlame::Systems.new('System2', priority: 1) do + @result.push 1 + end + + @scene1 = FelFlame::Scenes.new('Scene0', priority: 1) + @scene0 = FelFlame::Scenes.new('Scene0', priority: 0) + end + + before :each do + end + + after :each do + @result.clear + @scene0.clear + @scene1.clear + @system0.priority = 0 + @system1.priority = 1 + @system2.priority = 2 + @scene0.priority = 0 + @scene1.priority = 1 + end + + it 'can sort Scenes' do + @scene0.add @system0 + @scene1.add @system1 + FelFlame::Order.sort( + @scene1, + @scene0 + ) + expect(@scene1.priority < @scene0.priority).to eq(true) + end + + it 'can sort Systems' do + @scene0.add @system0, @system1, @system2 + FelFlame::Order.sort( + @system2, + @system0, + @system1 + ) + @scene0.call + expect(@result).to eq([2,0,1]) + end + + it 'can handle array' do + FelFlame::Order.sort( + @system0, + [ + @system1, + @system2 + ] + ) + expect(@system0.priority < @system1.priority).to eq(true) + expect(@system0.priority < @system2.priority).to eq(true) + expect(@system1.priority == @system2.priority).to eq(true) + end +end -- cgit v1.2.3
       
       
      -3
      -4
      +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21
      -
      # File 'lib/felflame/order.rb', line 3
      -
      -def sort(*sortables)
      +      
      # File 'lib/felflame/order.rb', line 10
      +
      +def self.sort(*sortables)
      +  sortables.each_with_index do |sorted, index|
      +    if sorted.respond_to? :priority
      +      sorted.priority = index
      +    else
      +      sorted.each do |item|
      +        item.priority = index
      +      end
      +    end
      +  end
      +  true
       end