From d03ce21592062aed27a571fda60483fcb3fba764 Mon Sep 17 00:00:00 2001 From: realtradam Date: Thu, 30 Dec 2021 07:28:14 -0500 Subject: . --- CHANGELOG.mdown | 2 + docs/FelFlame.html | 2 +- docs/FelFlame/ComponentManager.html | 170 +++++++++++++++++++------- docs/FelFlame/Components.html | 2 +- docs/FelFlame/Entities.html | 229 +++++++++++++++++++++++++++++------- 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 | 28 ++++- docs/top-level-namespace.html | 2 +- lib/felflame/component_manager.rb | 13 +- lib/felflame/entity_manager.rb | 32 ++++- spec/component_manager_spec.rb | 28 +++++ spec/entity_manager_spec.rb | 31 ++++- 18 files changed, 449 insertions(+), 104 deletions(-) diff --git a/CHANGELOG.mdown b/CHANGELOG.mdown index 7f42aac..15f2392 100644 --- a/CHANGELOG.mdown +++ b/CHANGELOG.mdown @@ -15,9 +15,11 @@ ![Changed](https://img.shields.io/badge/-Changed-yellow) - 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 +- Convenience methods for accessing a single entity in a component(`@component.entity`) and accessing a single entity in a component(`@entity.component[@component_manager]`) ![Fixed](https://img.shields.io/badge/-Fixed-blue) - Replaced all instances of `sort_by!` with `sort_by` for compatibility with DragonRuby diff --git a/docs/FelFlame.html b/docs/FelFlame.html index 91b2ecd..8fd0fd6 100644 --- a/docs/FelFlame.html +++ b/docs/FelFlame.html @@ -293,7 +293,7 @@ diff --git a/docs/FelFlame/ComponentManager.html b/docs/FelFlame/ComponentManager.html index 7e92b7b..599b547 100644 --- a/docs/FelFlame/ComponentManager.html +++ b/docs/FelFlame/ComponentManager.html @@ -360,7 +360,7 @@
  • - #entities ⇒ Array<Integer> + #entities ⇒ Array<Component> @@ -378,6 +378,30 @@

    Entities that have this component.

    +
  • + + +
  • + + + #entity ⇒ Component + + + + + + + + + + + + + +
    +

    A single entity.

    +
    +
  • @@ -941,18 +965,18 @@
     
     
    -202
    -203
    -204
    -205
    -206
    -207
    -208
    -209
    -210
    +213 +214 +215 +216 +217 +218 +219 +220 +221 -
    # File 'lib/felflame/component_manager.rb', line 202
    +      
    # File 'lib/felflame/component_manager.rb', line 213
     
     def attr_changed_trigger_systems(attr)
       systems_to_execute = self.class.attr_triggers[attr]
    @@ -1012,22 +1036,22 @@
           
     
     
    -213
    -214
    -215
    -216
    -217
    -218
    -219
    -220
    -221
    -222
    -223
     224
    -225
    +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236
    -
    # File 'lib/felflame/component_manager.rb', line 213
    +      
    # File 'lib/felflame/component_manager.rb', line 224
     
     def delete
       addition_triggers.each do |system|
    @@ -1050,7 +1074,7 @@
           

    - #entitiesArray<Integer> + #entitiesArray<Component> @@ -1072,7 +1096,7 @@
  • - (Array<Integer>) + (Array<Component>) @@ -1099,6 +1123,70 @@ +
  • + +
    +

    + + #entityComponent + + + + + +

    +
    + +

    A single entity. Use this if you expect the component to only belong to one entity and you want to access it.

    + + +
    +
    +
    + +

    Returns:

    +
      + +
    • + + + (Component) + + + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +194
    +195
    +196
    +197
    +198
    +199
    +200
    +201
    +
    +
    # File 'lib/felflame/component_manager.rb', line 194
    +
    +def entity
    +  if entities.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.")
    +  elsif entities.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
    +
    @@ -1145,16 +1233,16 @@
     
     
    -228
    -229
    -230
    -231
    -232
    -233
    -234
    +239 +240 +241 +242 +243 +244 +245
    -
    # File 'lib/felflame/component_manager.rb', line 228
    +      
    # File 'lib/felflame/component_manager.rb', line 239
     
     def to_h
       return_hash = instance_variables.each_with_object({}) do |key, final|
    @@ -1212,14 +1300,14 @@
           
     
     
    -194
    -195
    -196
    -197
    -198
    +205 +206 +207 +208 +209
    -
    # File 'lib/felflame/component_manager.rb', line 194
    +      
    # File 'lib/felflame/component_manager.rb', line 205
     
     def update_attrs(**opts)
       opts.each do |key, value|
    @@ -1236,7 +1324,7 @@
     
     
           
    diff --git a/docs/FelFlame/Components.html b/docs/FelFlame/Components.html
    index 8e866d1..e3b6167 100644
    --- a/docs/FelFlame/Components.html
    +++ b/docs/FelFlame/Components.html
    @@ -321,7 +321,7 @@
     
     
           
    diff --git a/docs/FelFlame/Entities.html b/docs/FelFlame/Entities.html
    index 2dd13c9..aca4cac 100644
    --- a/docs/FelFlame/Entities.html
    +++ b/docs/FelFlame/Entities.html
    @@ -148,6 +148,30 @@
     

    Add any number components to the Entity.

    + + + +
  • + + + #component(manager = nil) ⇒ Component + + + + + + + + + + + + + +
    +

    A single component from a component manager.

    +
    +
  • @@ -306,7 +330,14 @@ 9 10 11 -12
    +12 +13 +14 +15 +16 +17 +18 +19
    # File 'lib/felflame/entity_manager.rb', line 7
    @@ -315,6 +346,13 @@
       # Add each component
       add(*components)
     
    +  # Fancy method redirection for when the `component` method is called
    +  @component_redirect = Object.new
    +  @component_redirect.instance_variable_set(:@entity, self)
    +  @component_redirect.define_singleton_method(:[]) do |component_manager|
    +    instance_variable_get(:@entity).component(component_manager)
    +  end
    +
       self.class._data.push self
     end
    @@ -393,23 +431,23 @@
     
     
    -36
    -37
    -38
    -39
    -40
    -41
    -42
    -43
    -44
    -45
    -46
    -47
    -48
    -49
    +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76
    -
    # File 'lib/felflame/entity_manager.rb', line 36
    +      
    # File 'lib/felflame/entity_manager.rb', line 63
     
     def add(*components_to_add)
       components_to_add.each do |component|
    @@ -428,6 +466,109 @@
         
       
     
    +
    +    
    +      
    +

    + + #component(manager = nil) ⇒ Component + + + + + +

    +
    + +

    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.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    @entity.component[@component_manager] # array notation
    +@entity.component(@component_manager) # method notation
    + +
    +

    Parameters:

    +
      + +
    • + + manager + + + (ComponentManager) + + + (defaults to: nil) + + + — +
      +

      If you pass nil you can then use array notation to access the same value.

      +
      + +
    • + +
    + +

    Returns:

    +
      + +
    • + + + (Component) + + + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +33
    +34
    +35
    +36
    +37
    +38
    +39
    +40
    +41
    +42
    +43
    +44
    +
    +
    # File 'lib/felflame/entity_manager.rb', line 33
    +
    +def component(manager = nil)
    +  if manager.nil?
    +    @component_redirect
    +  else
    +    if components[manager].nil?
    +      raise "This entity(#{self}) doesnt have any components of this type: #{manager}"
    +    elsif components[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
    +
    @@ -469,12 +610,12 @@
     
     
    -16
    -17
    -18
    +23 +24 +25
    -
    # File 'lib/felflame/entity_manager.rb', line 16
    +      
    # File 'lib/felflame/entity_manager.rb', line 23
     
     def components
       @components ||= {}
    @@ -528,19 +669,19 @@
           
     
     
    -22
    -23
    -24
    -25
    -26
    -27
    -28
    -29
    -30
    -31
    +49 +50 +51 +52 +53 +54 +55 +56 +57 +58
    -
    # File 'lib/felflame/entity_manager.rb', line 22
    +      
    # File 'lib/felflame/entity_manager.rb', line 49
     
     def delete
       components.each do |component_manager, component_array|
    @@ -621,23 +762,29 @@
           
     
     
    -66
    -67
    -68
    -69
    -70
    -71
    -72
    -73
    +93 +94 +95 +96 +97 +98 +99 +100 +101 +102 +103
    -
    # File 'lib/felflame/entity_manager.rb', line 66
    +      
    # File 'lib/felflame/entity_manager.rb', line 93
     
     def remove(*components_to_remove)
       components_to_remove.each do |component|
         check_systems component, :removal_triggers if component.entities.include? self
         component.entities.delete self
         components[component.class].delete component
    +    if components[component.class].empty?
    +      components.delete component.class
    +    end
       end
       true
     end
    @@ -651,7 +798,7 @@ diff --git a/docs/FelFlame/Scenes.html b/docs/FelFlame/Scenes.html index ace650f..2c160fe 100644 --- a/docs/FelFlame/Scenes.html +++ b/docs/FelFlame/Scenes.html @@ -753,7 +753,7 @@ diff --git a/docs/FelFlame/Stage.html b/docs/FelFlame/Stage.html index 4003f8f..b2e961b 100644 --- a/docs/FelFlame/Stage.html +++ b/docs/FelFlame/Stage.html @@ -572,7 +572,7 @@ diff --git a/docs/FelFlame/Systems.html b/docs/FelFlame/Systems.html index 2c489e4..e549566 100644 --- a/docs/FelFlame/Systems.html +++ b/docs/FelFlame/Systems.html @@ -1531,7 +1531,7 @@ diff --git a/docs/Felflame_.html b/docs/Felflame_.html index ac5deeb..ea9bbfc 100644 --- a/docs/Felflame_.html +++ b/docs/Felflame_.html @@ -133,7 +133,7 @@ diff --git a/docs/_index.html b/docs/_index.html index 05cc4a4..4b3f2de 100644 --- a/docs/_index.html +++ b/docs/_index.html @@ -168,7 +168,7 @@ diff --git a/docs/file.README.html b/docs/file.README.html index 1b749e7..7deb23f 100644 --- a/docs/file.README.html +++ b/docs/file.README.html @@ -514,7 +514,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 74cd784..a77181a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -514,7 +514,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 f0e11e7..0efe4fe 100644 --- a/docs/method_list.html +++ b/docs/method_list.html @@ -182,13 +182,21 @@
  • - #components + #component FelFlame::Entities
  • +
    + #components + FelFlame::Entities +
    +
  • + + +
  • #const_name FelFlame::Scenes @@ -196,7 +204,7 @@
  • -
  • +
  • #const_name FelFlame::Systems @@ -204,7 +212,7 @@
  • -
  • +
  • #delete FelFlame::Entities @@ -212,7 +220,7 @@
  • -
  • +
  • #delete FelFlame::ComponentManager @@ -220,7 +228,7 @@
  • -
  • +
  • each FelFlame::Systems @@ -228,7 +236,7 @@
  • -
  • +
  • #entities FelFlame::ComponentManager @@ -236,6 +244,14 @@
  • +
  • +
    + #entity + FelFlame::ComponentManager +
    +
  • + +
  • #initialize diff --git a/docs/top-level-namespace.html b/docs/top-level-namespace.html index aa5317b..26c3c67 100644 --- a/docs/top-level-namespace.html +++ b/docs/top-level-namespace.html @@ -127,7 +127,7 @@
    diff --git a/lib/felflame/component_manager.rb b/lib/felflame/component_manager.rb index c513498..ca5be89 100644 --- a/lib/felflame/component_manager.rb +++ b/lib/felflame/component_manager.rb @@ -184,11 +184,22 @@ module FelFlame end # Entities that have this component - # @return [Array] + # @return [Array] def entities @entities ||= [] end + # A single entity. Use this if you expect the component to only belong to one entity and you want to access it. + # @return [Component] + def entity + if entities.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.") + elsif entities.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 + # Update attribute values using a hash or keywords. # @return [Hash] Hash of updated attributes def update_attrs(**opts) diff --git a/lib/felflame/entity_manager.rb b/lib/felflame/entity_manager.rb index 0e40b21..9489f29 100644 --- a/lib/felflame/entity_manager.rb +++ b/lib/felflame/entity_manager.rb @@ -8,6 +8,13 @@ module FelFlame # Add each component add(*components) + # Fancy method redirection for when the `component` method is called + @component_redirect = Object.new + @component_redirect.instance_variable_set(:@entity, self) + @component_redirect.define_singleton_method(:[]) do |component_manager| + instance_variable_get(:@entity).component(component_manager) + end + self.class._data.push self end @@ -17,6 +24,26 @@ module FelFlame @components ||= {} 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. + # @example + # @entity.component[@component_manager] # array notation + # @entity.component(@component_manager) # method notation + # @param manager [ComponentManager] If you pass nil you can then use array notation to access the same value. + # @return [Component] + def component(manager = nil) + if manager.nil? + @component_redirect + else + if components[manager].nil? + raise "This entity(#{self}) doesnt have any components of this type: #{manager}" + elsif components[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 + + # Removes this Entity from the list and purges all references to this Entity from other Components, as well as its data. # @return [Boolean] +true+ def delete @@ -68,6 +95,9 @@ module FelFlame check_systems component, :removal_triggers if component.entities.include? self component.entities.delete self components[component.class].delete component + if components[component.class].empty? + components.delete component.class + end end true end @@ -78,8 +108,6 @@ module FelFlame #def to_json() end class < []}) - expect(@ent2.components).to eq({@component_manager => []}) + expect(@ent0.components).to eq({}) + expect(@ent2.components).to eq({}) FelFlame::Entities.reverse_each(&:delete) expect(FelFlame::Entities.empty?).to be true end -- cgit v1.2.3