From 8dcacf9e69214f66be09c120ba474b848047cb53 Mon Sep 17 00:00:00 2001 From: realtradam Date: Sat, 12 Jun 2021 00:57:04 -0400 Subject: unit testing and cleanup --- docs/FelFlame.html | 2 +- docs/FelFlame/Components.html | 22 +- docs/FelFlame/Entities.html | 144 ++++++------ docs/FelFlame/Helper.html | 2 +- docs/FelFlame/Helper/ComponentManager.html | 338 ++++++++++++++--------------- docs/FelFlame/Systems.html | 2 +- docs/_index.html | 2 +- docs/file.README.html | 2 +- docs/index.html | 2 +- docs/method_list.html | 12 +- docs/top-level-namespace.html | 2 +- 11 files changed, 271 insertions(+), 259 deletions(-) (limited to 'docs') diff --git a/docs/FelFlame.html b/docs/FelFlame.html index e9ab145..b49faea 100644 --- a/docs/FelFlame.html +++ b/docs/FelFlame.html @@ -193,7 +193,7 @@ diff --git a/docs/FelFlame/Components.html b/docs/FelFlame/Components.html index 633c5fb..9c6c0c7 100644 --- a/docs/FelFlame/Components.html +++ b/docs/FelFlame/Components.html @@ -233,12 +233,12 @@
 
 
-41
-42
-43
+45 +46 +47 -
# File 'component_manager.rb', line 41
+      
# File 'component_manager.rb', line 45
 
 def each(&block)
   constants.each(&block)
@@ -365,12 +365,20 @@
 34
 35
 36
-37
+37 +38 +39 +40 +41
# File 'component_manager.rb', line 23
 
 def new(component_name, *attrs, **attrs_with_defaults)
+  if FelFlame::Components.const_defined?(component_name)
+    raise(NameError.new, "Component Manager '#{component_name}' is already defined")
+  end
+
   const_set(component_name, Class.new(FelFlame::Helper::ComponentManager) {})
   attrs.each do |attr|
     FelFlame::Components.const_get(component_name).attr_accessor attr
@@ -378,7 +386,7 @@
   attrs_with_defaults.each do |attr, _default|
     FelFlame::Components.const_get(component_name).attr_accessor attr
   end
-  FelFlame::Components.const_get(component_name).define_method(:initialize) do
+  FelFlame::Components.const_get(component_name).define_method(:set_defaults) do
     attrs_with_defaults.each do |attr, default|
       instance_variable_set("@#{attr}", default)
     end
@@ -395,7 +403,7 @@
 
 
       
diff --git a/docs/FelFlame/Entities.html b/docs/FelFlame/Entities.html
index 4ba4d6f..38898a8 100644
--- a/docs/FelFlame/Entities.html
+++ b/docs/FelFlame/Entities.html
@@ -250,7 +250,7 @@
         
  • - #add(component) ⇒ Boolean + #add(*components_to_add) ⇒ Boolean @@ -265,7 +265,7 @@
    -

    Returns true when added, or false if it already belongs to the Entity Add a component to the Entity.

    +

    Add any number components to the Entity.

  • @@ -348,7 +348,7 @@
  • - #remove(component) ⇒ Boolean + #remove(*components_to_remove) ⇒ Boolean @@ -481,21 +481,25 @@ 18 19 20 -21
  • +21 +22 +23
    # File 'entity_manager.rb', line 10
     
     def initialize(*components)
       # Assign new unique ID
    -  new_id = self.class.data.find_index { |i| i.nil? }
    +  new_id = self.class.data.find_index(&:nil?)
       new_id = self.class.data.size if new_id.nil?
       self.id = new_id
     
       # Add each component
    -  components.uniq.each do |component|
    -    add component
    -  end
    +  #components.uniq.each do |component|
    +  #  add component
    +  #end
    +  add(*components)
    +
       self.class.data[id] = self
     end
    @@ -639,12 +643,12 @@
     
     
    -103
     104
    -105
    +105 +106 -
    # File 'entity_manager.rb', line 103
    +      
    # File 'entity_manager.rb', line 104
     
     def [](entity_id)
       data[entity_id]
    @@ -693,12 +697,12 @@
           
     
     
    -110
     111
    -112
    +112 +113
    -
    # File 'entity_manager.rb', line 110
    +      
    # File 'entity_manager.rb', line 111
     
     def each(&block)
       data.each(&block)
    @@ -770,10 +774,10 @@
           
     
     
    -118
    +119
    -
    # File 'entity_manager.rb', line 118
    +      
    # File 'entity_manager.rb', line 119
     
     def from_json(json_string, **opts) end
    @@ -790,7 +794,7 @@

    - #add(component) ⇒ Boolean + #add(*components_to_add) ⇒ Boolean @@ -799,7 +803,7 @@

    -

    Returns true when added, or false if it already belongs to the Entity Add a component to the Entity

    +

    Add any number components to the Entity.

    @@ -819,7 +823,7 @@ —
    -

    A component created from any component manager

    +

    Any number of components created from any component manager

    @@ -838,7 +842,7 @@ —
    -

    true if component is added, false if it already is attached

    +

    true if component is added, false if it already is attached or no components given

    @@ -851,6 +855,9 @@
     
     
    +54
    +55
    +56
     57
     58
     59
    @@ -862,24 +869,25 @@
     65
     66
     67
    -68
    -69
    +68
    -
    # File 'entity_manager.rb', line 57
    -
    -def add(component)
    -  if components[component.class.to_s.to_sym].nil?
    -    components[component.class.to_s.to_sym] = [component.id]
    -    component.entities.push id
    -    true
    -  elsif !components[component.class.to_s.to_sym].include? component.id
    -    components[component.class.to_s.to_sym].push component.id
    -    component.entities.push id
    -    true
    -  else
    -    false
    +      
    # File 'entity_manager.rb', line 54
    +
    +def add(*components_to_add)
    +  added = false
    +  components_to_add.each do |component|
    +    if components[component.class].nil?
    +      components[component.class] = [component.id]
    +      component.entities.push id
    +      added =true
    +    elsif !components[component.class].include? component.id
    +      components[component.class].push component.id
    +      component.entities.push id
    +      added = true
    +    end
       end
    +  added
     end
    @@ -925,12 +933,12 @@
     
     
    -25
    -26
    -27
    +27 +28 +29
    -
    # File 'entity_manager.rb', line 25
    +      
    # File 'entity_manager.rb', line 27
     
     def components
       @components ||= {}
    @@ -984,8 +992,6 @@
           
     
     
    -37
    -38
     39
     40
     41
    @@ -996,22 +1002,16 @@
     46
     47
     48
    -49
    -50
    -51
    +49
    -
    # File 'entity_manager.rb', line 37
    +      
    # File 'entity_manager.rb', line 39
     
     def delete
       components.each do |component_manager, component_array|
         component_array.each do |component_id|
    -      FelFlame.const_get(
    -        component_manager.to_s.delete_prefix('FelFlame::')
    -      )[component_id].entities.delete(id)
    -      # The following is neater, but doesnt work for some reason :/
    -      #Object.const_get(component_manager)[component_id].entities.delete(id)
    -    end
    +      component_manager[component_id].entities.delete(id)
    +    end
       end
       FelFlame::Entities.data[id] = nil
       @id = nil
    @@ -1026,7 +1026,7 @@
           

    - #remove(component) ⇒ Boolean + #remove(*components_to_remove) ⇒ Boolean @@ -1046,7 +1046,7 @@
  • - component + component_to_remove (Component) @@ -1074,7 +1074,7 @@ —
    -

    true if component is removed, false if it wasnt attached to component

    +

    true if at least one component is removed, false if none of them were attached to the component

  • @@ -1087,6 +1087,7 @@
     
     
    +73
     74
     75
     76
    @@ -1094,18 +1095,21 @@
     78
     79
     80
    -81
    +81 +82

    -
    # File 'entity_manager.rb', line 74
    -
    -def remove(component)
    -  components[component.class.to_s.to_sym].delete component.id
    -  if component.entities.delete id
    -    true
    -  else
    -    false
    +      
    # File 'entity_manager.rb', line 73
    +
    +def remove(*components_to_remove)
    +  removed = false
    +  components_to_remove.each do |component|
    +    components[component.class].delete component.id
    +    if component.entities.delete id
    +      removed = true
    +    end
       end
    +  removed
     end
    @@ -1151,12 +1155,12 @@
     
     
    -31
    -32
    -33
    +33 +34 +35
    -
    # File 'entity_manager.rb', line 31
    +      
    # File 'entity_manager.rb', line 33
     
     def to_i
       id
    @@ -1210,10 +1214,10 @@
           
     
     
    -86
    +87
    -
    # File 'entity_manager.rb', line 86
    +      
    # File 'entity_manager.rb', line 87
     
     def to_json() end
    @@ -1226,7 +1230,7 @@ diff --git a/docs/FelFlame/Helper.html b/docs/FelFlame/Helper.html index ce27679..2dbcd7a 100644 --- a/docs/FelFlame/Helper.html +++ b/docs/FelFlame/Helper.html @@ -132,7 +132,7 @@ diff --git a/docs/FelFlame/Helper/ComponentManager.html b/docs/FelFlame/Helper/ComponentManager.html index c1d0452..acb1cf8 100644 --- a/docs/FelFlame/Helper/ComponentManager.html +++ b/docs/FelFlame/Helper/ComponentManager.html @@ -184,7 +184,7 @@
  • - .each ⇒ Enumerator + .each(&block) ⇒ Enumerator @@ -205,10 +205,19 @@
  • + + +

    + Instance Method Summary + collapse +

    + +
      +
    • - .new(**attrs) ⇒ Component + #attrs ⇒ Hash @@ -223,25 +232,16 @@
      -

      Creates a new component and sets the values of the attributes given to it.

      +

      A hash, where all the keys are attributes linked to their respective values.

    • -
    - -

    - Instance Method Summary - collapse -

    - -
      -
    • - #attrs ⇒ Hash + #delete ⇒ Boolean @@ -256,7 +256,7 @@
      -

      A hash, where all the keys are attributes linked to their respective values.

      +

      Removes this component from the list and purges all references to this Component from other Entities, as well as its ID and data.

    • @@ -265,7 +265,7 @@
    • - #delete ⇒ Boolean + #entities ⇒ Array<Integer> @@ -280,7 +280,7 @@
      -

      Removes this component from the list and purges all references to this Component from other Entities, as well as its ID and data.

      +

      A list of entity ids that are linked to the component.

    • @@ -289,13 +289,15 @@
    • - #entities ⇒ Array + #initialize(**attrs) ⇒ Component + constructor + @@ -304,7 +306,7 @@
      -

      A list of components that are linked to the component.

      +

      Creates a new component and sets the values of the attributes given to it.

    • @@ -385,6 +387,103 @@
    +
    +

    Constructor Details

    + +
    +

    + + #initialize(**attrs) ⇒ Component + + + + + +

    +
    + +

    Creates a new component and sets the values of the attributes given to it. If an attritbute is not passed then it will remain as the default.

    + + +
    +
    +
    +

    Parameters:

    +
      + +
    • + + attrs + + + (Keyword: Value) + + + + — +
      +

      You can pass any number of Keyword-Value pairs

      +
      + +
    • + +
    + + +
    + + + + +
    +
    +
    +
    +62
    +63
    +64
    +65
    +66
    +67
    +68
    +69
    +70
    +71
    +72
    +73
    +74
    +75
    +76
    +77
    +78
    +79
    +80
    +
    +
    # File 'component_manager.rb', line 62
    +
    +def initialize(**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.size if new_id.nil?
    +  @id = new_id
    +
    +  # Fill params
    +  attrs.each do |key, value|
    +    send "#{key}=", value
    +  end
    +
    +  # Save Component
    +  self.class.data[new_id] = self
    +end
    +
    +
    + +

    Instance Attribute Details

    @@ -430,12 +529,12 @@
     
     
    -53
    -54
    -55
    +57 +58 +59
    -
    # File 'component_manager.rb', line 53
    +      
    # File 'component_manager.rb', line 57
     
     def id
       @id
    @@ -520,12 +619,12 @@
           
     
     
    -69
    -70
    -71
    +96 +97 +98
    -
    # File 'component_manager.rb', line 69
    +      
    # File 'component_manager.rb', line 96
     
     def [](component_id)
       data[component_id]
    @@ -538,7 +637,7 @@
           

    - .eachEnumerator + .each(&block) ⇒ Enumerator @@ -547,7 +646,7 @@

    -

    Iterates over all components within the component manager

    +

    Iterates over all components within the component manager. Special Enumerable methods like map or each_with_index are not implemented

    @@ -574,114 +673,15 @@
     
     
    -95
    -96
    -97
    -98
    -99
    - - -
    # File 'component_manager.rb', line 95
    -
    -def each
    -  data.each do |component|
    -    yield component
    -  end
    -end
    - - - -
    - -
    -

    - - .new(**attrs) ⇒ Component - - - - - -

    -
    - -

    Creates a new component and sets the values of the attributes given to it. If an attritbute is not passed then it will remain as the default.

    - - -
    -
    -
    -

    Parameters:

    -
      - -
    • - - attrs - - - (Keyword: Value) - - - -
    • - -
    - -

    Returns:

    -
      - -
    • - - - (Component) - - - -
    • - -
    - -
    - - @@ -738,14 +738,14 @@
     
     
    -137
    -138
    -139
    -140
    -141
    +143 +144 +145 +146 +147
    -
    -
    -
    -76
    -77
    -78
    -79
    -80
    -81
    -82
    -83
    -84
    -85
    -86
    -87
    -88
    -89
    -90
    -91
    +103 +104 +105
    -
    # File 'component_manager.rb', line 76
    -
    -def new(**attrs)
    -  new_component = super
    +      
    # File 'component_manager.rb', line 103
     
    -  # Generate ID
    -  new_id = self.data.find_index { |i| i.nil? }
    -  new_id = self.data.size if new_id.nil?
    -  new_component.id = new_id
    -
    -  # Fill params
    -  attrs.each do |key, value|
    -    new_component.send "#{key}=", value
    -  end
    -
    -  # Save Component
    -  data[new_id] = new_component
    +def each(&block)
    +  data.compact.each(&block)
     end
    -
    # File 'component_manager.rb', line 137
    +      
    # File 'component_manager.rb', line 143
     
     def attrs
       instance_variables.each_with_object({}) do |key, final|
    @@ -801,20 +801,20 @@
           
     
     
    -124
    -125
    -126
    -127
    -128
    -129
     130
     131
     132
     133
    -134
    +134 +135 +136 +137 +138 +139 +140
    -
    # File 'component_manager.rb', line 124
    +      
    # File 'component_manager.rb', line 130
     
     def delete
       entities.each do |entity_id|
    @@ -835,7 +835,7 @@
           

    - #entitiesArray + #entitiesArray<Integer> @@ -844,7 +844,7 @@

    -

    A list of components that are linked to the component

    +

    A list of entity ids that are linked to the component

    @@ -857,7 +857,7 @@
  • - (Array) + (Array<Integer>) @@ -871,12 +871,12 @@
     
     
    -110
    -111
    -112
    +116 +117 +118
  • -
    # File 'component_manager.rb', line 110
    +      
    # File 'component_manager.rb', line 116
     
     def entities
       @entities ||= []
    @@ -925,12 +925,12 @@
           
     
     
    -104
    -105
    -106
    +110 +111 +112
    -
    # File 'component_manager.rb', line 104
    +      
    # File 'component_manager.rb', line 110
     
     def to_i
       id
    @@ -984,12 +984,12 @@
           
     
     
    -146
    -147
    -148
    +152 +153 +154
    -
    # File 'component_manager.rb', line 146
    +      
    # File 'component_manager.rb', line 152
     
     def to_json
       # should return a json or hash of all data in this component
    @@ -1043,14 +1043,14 @@
           
     
     
    -116
    -117
    -118
    -119
    -120
    +122 +123 +124 +125 +126
    -
    # File 'component_manager.rb', line 116
    +      
    # File 'component_manager.rb', line 122
     
     def update_attrs(**opts)
       opts.each do |key, value|
    @@ -1067,7 +1067,7 @@
     
     
           
    diff --git a/docs/FelFlame/Systems.html b/docs/FelFlame/Systems.html
    index a418bfb..2dd4534 100644
    --- a/docs/FelFlame/Systems.html
    +++ b/docs/FelFlame/Systems.html
    @@ -124,7 +124,7 @@
     
     
           
    diff --git a/docs/_index.html b/docs/_index.html
    index 42c4e44..0ae242d 100644
    --- a/docs/_index.html
    +++ b/docs/_index.html
    @@ -164,7 +164,7 @@
     
     
           
    diff --git a/docs/file.README.html b/docs/file.README.html
    index a62e2fd..c1e498b 100644
    --- a/docs/file.README.html
    +++ b/docs/file.README.html
    @@ -353,7 +353,7 @@ complete I will use a more verbose explanation as below to help users of the fra
     
     
           
    diff --git a/docs/index.html b/docs/index.html
    index 333991c..1d9dc01 100644
    --- a/docs/index.html
    +++ b/docs/index.html
    @@ -353,7 +353,7 @@ complete I will use a more verbose explanation as below to help users of the fra
     
     
           
    diff --git a/docs/method_list.html b/docs/method_list.html
    index 065cba8..cdf6aae 100644
    --- a/docs/method_list.html
    +++ b/docs/method_list.html
    @@ -158,24 +158,24 @@
     
       
  • - #initialize - FelFlame::Entities + #initialize + FelFlame::Helper::ComponentManager
  • - new - FelFlame::Components + #initialize + FelFlame::Entities
  • - new - FelFlame::Helper::ComponentManager + new + FelFlame::Components
  • diff --git a/docs/top-level-namespace.html b/docs/top-level-namespace.html index b2bd7c9..869c856 100644 --- a/docs/top-level-namespace.html +++ b/docs/top-level-namespace.html @@ -127,7 +127,7 @@ -- cgit v1.2.3