From aab35c0f098d695b7cf53c14a8b08a9b4a24550d Mon Sep 17 00:00:00 2001 From: realtradam Date: Sat, 10 Jul 2021 03:09:36 -0400 Subject: entities and components now reference each other with objects --- .yardopts | 13 -- CHANGELOG.mdown | 28 +++ Gemfile.lock | 8 +- README.mdown | 18 ++ docs/_index.html | 81 +------- docs/class_list.html | 2 +- docs/file.README.html | 104 ++++++---- docs/index.html | 104 ++++++---- docs/method_list.html | 424 -------------------------------------- docs/top-level-namespace.html | 39 +--- lib/felflame/component_manager.rb | 5 +- lib/felflame/entity_manager.rb | 21 +- lib/felflame/version.rb | 2 +- spec/entity_manager_spec.rb | 22 +- 14 files changed, 206 insertions(+), 665 deletions(-) delete mode 100644 .yardopts diff --git a/.yardopts b/.yardopts deleted file mode 100644 index 8995a93..0000000 --- a/.yardopts +++ /dev/null @@ -1,13 +0,0 @@ ---readme README.mdown ---markup=markdown|textile|rdoc(default) ---protected ---no-private - -LICENSE -README.mdown -system_manager.rb -component_manager.rb -entity_manager.rb -scene_manager.rb -stage_manager.rb -felflame.rb diff --git a/CHANGELOG.mdown b/CHANGELOG.mdown index 6f0b570..e4e0e9d 100644 --- a/CHANGELOG.mdown +++ b/CHANGELOG.mdown @@ -8,6 +8,34 @@ ![Deprecated](https://img.shields.io/badge/-Deprecated-orange) ![Removed](https://img.shields.io/badge/-Removed-red) +## [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 +# 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 + +# after: +@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 + +# after: +@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 diff --git a/Gemfile.lock b/Gemfile.lock index 6f1c26c..b0da32d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - felflame (1.0.2) + felflame (2.0.0) GEM remote: https://rubygems.org/ @@ -18,10 +18,10 @@ GEM minitest (>= 5.0) ruby-progressbar parallel (1.20.1) - parser (3.0.1.1) + parser (3.0.2.0) ast (~> 2.4.1) rainbow (3.0.0) - rake (13.0.3) + rake (13.0.6) redcarpet (3.5.1) regexp_parser (2.1.1) rexml (3.2.5) @@ -38,7 +38,7 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.10.0) rspec-support (3.10.2) - rubocop (1.17.0) + rubocop (1.18.3) parallel (~> 1.10) parser (>= 3.0.0.0) rainbow (>= 2.2.2, < 4.0) diff --git a/README.mdown b/README.mdown index 9b1e80d..d88a78b 100644 --- a/README.mdown +++ b/README.mdown @@ -26,6 +26,7 @@ - [Accessing](#accessing) - [Get ID](#get-id) - [Adding and Removing Components](#adding-and-removing-components) + - [Accessing Entities' Attached Components](#accessing-entities-attached-components) - [Deletion](#deletion) * [Components](#components-1) - [Creating a Component Manager](#creating-a-component-manager) @@ -34,6 +35,7 @@ - [Accessing Attributes and Changing Them](#accessing-attributes-and-changing-them) - [Deleting Components](#deleting-components) - [Iterating over Components](#iterating-over-components) + - [Accessing Components' attached Entities](#accessing-components-attached-entities) * [Systems](#systems-1) - [Creation](#creation-1) - [Execution](#execution) @@ -142,6 +144,13 @@ We can still add or remove Components from an Entity after it has been created. @entity.remove @component ``` +### Accessing Entities' Attached Components +When Components are added to Entities, they can be accessed from the Entity. By using a Component Manager as a key we can access an array of all components created from that Component Manager that are attached to an entity: + +```ruby +@entity.components[@component_manager] # => [@component1, @component2, component3] +``` + ### Deletion To have all Components from an Entity removed and the Entity deleted we do the following: @@ -210,12 +219,21 @@ Deleting a Component is the same format as deleting an Entity. When a Component ### Iterating over Components When you make Systems you will want to be able to iterate over all Components of the same Component Manager(for example iterating over all sprites to render them). Here is how we do that: + ```ruby FelFlame::Components::Sprites.each do |component| #do something with components end ``` +### Accessing Components' attached Entities +Components also keep track of what Entities are using it. To access this list we do the following: + +```ruby +@component.entities # => [@entity1, @entity2, @entity3] +``` + + ## Systems ### Creation diff --git a/docs/_index.html b/docs/_index.html index a4c9aa2..98534bd 100644 --- a/docs/_index.html +++ b/docs/_index.html @@ -75,85 +75,6 @@ - - - - - - - - - - - - @@ -163,7 +84,7 @@ diff --git a/docs/class_list.html b/docs/class_list.html index fcc39cb..3f4c240 100644 --- a/docs/class_list.html +++ b/docs/class_list.html @@ -43,7 +43,7 @@ diff --git a/docs/file.README.html b/docs/file.README.html index 7066296..67e4cc5 100644 --- a/docs/file.README.html +++ b/docs/file.README.html @@ -65,6 +65,10 @@ MIT License Ko-Fi

+

Check out the comprehensive documentation here!

+ +
+
  • Components @@ -96,6 +101,7 @@
  • Accessing Attributes and Changing Them
  • Deleting Components
  • Iterating over Components
  • +
  • Accessing Components' attached Entities
  • Systems @@ -181,27 +187,31 @@ ECS stands for Entity, Component, and System.

    Usage

    +

    To use FelFlame simply install the gem using gem install felflame or using bundler bundle add felflame and then require it in your project like so: require 'felflame'. Working outside of the gem for rendering engines that do not support the usage of gems is a planned feature in the works.

    +

    Entities

    Creation

    Entities are essentially "objects" in the game world. To create a new Entity we do the following:

    -
    @entity = FelFlame::Entities.new
    +
    @entity = FelFlame::Entities.new
     

    or if we want to add (any number of)components to it:

    -
    @entity = FelFlame::Entites.new(FelFlame::Components::Health.new,
    -                     @component,
    -             FelFlame::Components::Armour[7])
    +
    @entity = FelFlame::Entites.new(
    +  FelFlame::Components::Health.new,
    +  @component,
    +  FelFlame::Components::Armour[7]
    +)
     

    Accessing

    Once components are created we can access them using their ID like so:

    -
    @entity = FelFlame::Entities[2]
    +
    @entity = FelFlame::Entities[2]
     

    Get ID

    @@ -220,6 +230,13 @@ entity where a new entity will claim that ID. To get the ID of an Entity:

    @entity.remove @component
    +

    Accessing Entities' Attached Components

    + +

    When Components are added to Entities, they can be accessed from the Entity. By using a Component Manager as a key we can access an array of all components created from that Component Manager that are attached to an entity:

    + +
    @entity.components[@component_manager] # => [@component1, @component2, component3]
    +
    +

    Deletion

    To have all Components from an Entity removed and the Entity deleted we do the following:

    @@ -234,7 +251,7 @@ entity where a new entity will claim that ID. To get the ID of an Entity:

    Components are where all the data is stored. The data is stored in variables or accessors in each component. These accessors and their defaults are configured when a component manager is created, like so:

    -
    @component_manager = FelFlame::Components.new('Stats', :armour, hp: 100)
    +
    @component_manager = FelFlame::Components.new('Stats', :armour, hp: 100)
     

    In this example we created a component manager called "Stats". @@ -248,12 +265,12 @@ When defining attributes symbols should be used.

    Now that we have a component manager we can make components from it like so:

    -
    @component = FelFlame::Components::Stats.new
    +
    @component = FelFlame::Components::Stats.new
     

    Or we can even change the defaults:

    -
    @component = FelFlame::Components::Stats.new(armour: 'steel')
    +
    @component = FelFlame::Components::Stats.new(armour: 'steel')
     

    Accessing and Getting ID

    @@ -262,7 +279,7 @@ When defining attributes symbols should be used.

    These IDs are only unique within the scope of their respective Component Managers. Here is how we can get the ID, as well as how to access a Component from its Component Manager.

    -
    @component = FelFlame::Components::Stats[2]
    +
    @component = FelFlame::Components::Stats[2]
     @component.id # => 2
     
    @@ -290,25 +307,32 @@ Here are the ways to edit attrubutes, followed by the ways to read them.

    When you make Systems you will want to be able to iterate over all Components of the same Component Manager(for example iterating over all sprites to render them). Here is how we do that:

    -
    FelFlame::Components::Sprites.each do |component|
    +
    FelFlame::Components::Sprites.each do |component|
       #do something with components
     end
     
    +

    Accessing Components' attached Entities

    + +

    Components also keep track of what Entities are using it. To access this list we do the following:

    + +
    @component.entities # => [@entity1, @entity2, @entity3]
    +
    +

    Systems

    Creation

    We can create Systems like so:

    -
    FelFlame::Systems.new(name: 'Render', priority: 2) do
    +
    FelFlame::Systems.new(name: 'Render', priority: 2) do
       # Code and Logic
     end
     

    The name we assign is how we can access the System, like so:

    -
    FelFlame::Systems::Render
    +
    FelFlame::Systems::Render
     

    Priority determines the order Systems should be executed, this is used for Scenes and the Stage. @@ -317,8 +341,8 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    Often we will want to execute some logic on each Component in a given Component Manager so our code might look like this:

    -
    FelFlame::Systems.new(name: 'Render', priority: 2) do
    -  FelFlame::Components::Sprites.each do |component|
    +
    FelFlame::Systems.new(name: 'Render', priority: 2) do
    +  FelFlame::Components::Sprites.each do |component|
         # do something with these components
       end
     end
    @@ -328,7 +352,7 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.  

    After we create a System, it won't do anything on its own until we tell it to. Here is how:

    -
    FelFlame::Systems::Render.call
    +
    FelFlame::Systems::Render.call
     

    Sometimes you might want to manually activate a System, but the more common way to have Systems be triggered is to use Scenes and the Stage or the alternative ways of execution.

    @@ -338,25 +362,25 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    Sometimes you want a System to automatically trigger when a special even happens. FelFlame can keep track of when a Component is added, removed, or when an attribute is changed and then execute Systems linked to these events. Here is how to create these links:

    # When this Component is added to an Entity, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_added(@component)
    +FelFlame::Systems::PassiveRegen.trigger_when_added(@component)
     
     # When this Component is removed from an Entity, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_removed(@component)
    +FelFlame::Systems::PassiveRegen.trigger_when_removed(@component)
     
     # When this Component's health attribute is changed, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_is_set(@component, :health)
    +FelFlame::Systems::PassiveRegen.trigger_when_is_set(@component, :health)
     

    If we want these triggers to happen for all Components that belong to specific Component Manager then we can do that instead:

    # When a Component from this Component Manager is added to an Entity, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_added(@component_manager)
    +FelFlame::Systems::PassiveRegen.trigger_when_added(@component_manager)
     
     # When a Component from this Component Manager is removed from an Entity, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_removed(@component_manager)
    +FelFlame::Systems::PassiveRegen.trigger_when_removed(@component_manager)
     
     # When this Component's health attribute from this Component Manager is changed, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_is_set(@component_manager, :health)
    +FelFlame::Systems::PassiveRegen.trigger_when_is_set(@component_manager, :health)
     

    We can create any number of these links between Systems, Components, and Component Manangers as we like, simply call the method again with our other Components and Component Managers

    @@ -366,41 +390,41 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    If we wish to remove these links that we created, we can do that using the follwing function in any of the following ways:

    # clears ALL triggers with this system
    -FelFlame::Systems::PassiveRegen.clear_triggers
    +FelFlame::Systems::PassiveRegen.clear_triggers
     
     # clears ALL triggers with this Component Manager
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component)
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component)
     
     # clear the 'trigger_when_added' for this Component
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component, :added)
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component, :added)
     
     # clear the 'trigger_when_removed' for this Component
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component, :removed)
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component, :removed)
     
     # clear the 'trigger_when_is_set' for this Component specifically for the health attribute
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component, :is_set, :health)
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component, :is_set, :health)
     

    Likewise we can do the same with Component Managers:

    # clears ALL triggers with this Component
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager) 
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager) 
     
     # clear the 'trigger_when_added' for this Component Manager
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :added) 
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :added) 
     
     # clear the 'trigger_when_removed' for this Component Manager
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :removed) 
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :removed) 
     
     # clear the 'trigger_when_is_set' for this Component Manager specifically for the health attribute
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :is_set, :health) 
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :is_set, :health) 
     

    Redefinition

    If we wanted to change what code or logic a given System executes, we could do that with:

    -
    FelFlame::Systems::PassiveRegen.redefine do
    +
    FelFlame::Systems::PassiveRegen.redefine do
       # Some new logic or code
     end
     
    @@ -411,28 +435,28 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    Once we have all the core parts of ECS, we will want to organize our Systems. To do this we will use Scenes to group up Systems so they can quickly be enabled or disabled. Note that Alternative Executions will occur even if they are not part of a Scene. Here is how we make a new Scene:

    -
    @scene = FelFlame::Scenes.new('ExampleScene')
    +
    @scene = FelFlame::Scenes.new('ExampleScene')
     

    Accessing

    Just like other classes in FelFlame, the name we gave the Scene is how we access it:

    -
    @scene = FelFlame::Scenes::ExampleScene
    +
    @scene = FelFlame::Scenes::ExampleScene
     

    Adding Systems

    Adding Systems is simple. We can add as many as we want. In this example we add 3 different systems:

    -
    FelFlame::Scenes::ExampleScene.add(FelFlame::Systems::Render, @system2, @system3)
    +
    FelFlame::Scenes::ExampleScene.add(FelFlame::Systems::Render, @system2, @system3)
     

    Removing Systems

    Removing Systems works simularly:

    -
    FelFlame::Scenes::ExampleScene.remove(FelFlame::Systems::Render, @system2, @system3)
    +
    FelFlame::Scenes::ExampleScene.remove(FelFlame::Systems::Render, @system2, @system3)
     

    Clearing

    @@ -457,26 +481,26 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    Finally we have the Stage. There is only a single Stage and we do not have to create it as it exists by default. By adding a Scene to the Stage we are saying that the Scene is active. To add a Scene we do the following:

    -
    FelFlame::Stage.add FelFlame::Scene::ExampleScene
    +
    FelFlame::Stage.add FelFlame::Scene::ExampleScene
     

    Removing Scenes

    Likewise we can remove Scenes:

    -
    FelFlame::Stage.remove FelFlame::Scene::ExampleScene
    +
    FelFlame::Stage.remove FelFlame::Scene::ExampleScene
     

    Executing

    On each frame of the game we want to execute the Stage once. When the Stage is executed it is progressing your game 1 frame forward. The Stage will make sure for you that all the Systems from all Scenes added will be executed in the correct order according to their priority. Here is how we do it:

    -
    FelFlame::Stage.call
    +
    FelFlame::Stage.call
     

    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!

    +

    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!

    Contribution

    @@ -488,7 +512,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 0e56dcd..94feb86 100644 --- a/docs/index.html +++ b/docs/index.html @@ -65,6 +65,10 @@ MIT License Ko-Fi

    +

    Check out the comprehensive documentation here!

    + +
    +
  • Components @@ -96,6 +101,7 @@
  • Accessing Attributes and Changing Them
  • Deleting Components
  • Iterating over Components
  • +
  • Accessing Components' attached Entities
  • Systems @@ -181,27 +187,31 @@ ECS stands for Entity, Component, and System.

    Usage

    +

    To use FelFlame simply install the gem using gem install felflame or using bundler bundle add felflame and then require it in your project like so: require 'felflame'. Working outside of the gem for rendering engines that do not support the usage of gems is a planned feature in the works.

    +

    Entities

    Creation

    Entities are essentially "objects" in the game world. To create a new Entity we do the following:

    -
    @entity = FelFlame::Entities.new
    +
    @entity = FelFlame::Entities.new
     

    or if we want to add (any number of)components to it:

    -
    @entity = FelFlame::Entites.new(FelFlame::Components::Health.new,
    -                     @component,
    -             FelFlame::Components::Armour[7])
    +
    @entity = FelFlame::Entites.new(
    +  FelFlame::Components::Health.new,
    +  @component,
    +  FelFlame::Components::Armour[7]
    +)
     

    Accessing

    Once components are created we can access them using their ID like so:

    -
    @entity = FelFlame::Entities[2]
    +
    @entity = FelFlame::Entities[2]
     

    Get ID

    @@ -220,6 +230,13 @@ entity where a new entity will claim that ID. To get the ID of an Entity:

    @entity.remove @component
    +

    Accessing Entities' Attached Components

    + +

    When Components are added to Entities, they can be accessed from the Entity. By using a Component Manager as a key we can access an array of all components created from that Component Manager that are attached to an entity:

    + +
    @entity.components[@component_manager] # => [@component1, @component2, component3]
    +
    +

    Deletion

    To have all Components from an Entity removed and the Entity deleted we do the following:

    @@ -234,7 +251,7 @@ entity where a new entity will claim that ID. To get the ID of an Entity:

    Components are where all the data is stored. The data is stored in variables or accessors in each component. These accessors and their defaults are configured when a component manager is created, like so:

    -
    @component_manager = FelFlame::Components.new('Stats', :armour, hp: 100)
    +
    @component_manager = FelFlame::Components.new('Stats', :armour, hp: 100)
     

    In this example we created a component manager called "Stats". @@ -248,12 +265,12 @@ When defining attributes symbols should be used.

    Now that we have a component manager we can make components from it like so:

    -
    @component = FelFlame::Components::Stats.new
    +
    @component = FelFlame::Components::Stats.new
     

    Or we can even change the defaults:

    -
    @component = FelFlame::Components::Stats.new(armour: 'steel')
    +
    @component = FelFlame::Components::Stats.new(armour: 'steel')
     

    Accessing and Getting ID

    @@ -262,7 +279,7 @@ When defining attributes symbols should be used.

    These IDs are only unique within the scope of their respective Component Managers. Here is how we can get the ID, as well as how to access a Component from its Component Manager.

    -
    @component = FelFlame::Components::Stats[2]
    +
    @component = FelFlame::Components::Stats[2]
     @component.id # => 2
     
    @@ -290,25 +307,32 @@ Here are the ways to edit attrubutes, followed by the ways to read them.

    When you make Systems you will want to be able to iterate over all Components of the same Component Manager(for example iterating over all sprites to render them). Here is how we do that:

    -
    FelFlame::Components::Sprites.each do |component|
    +
    FelFlame::Components::Sprites.each do |component|
       #do something with components
     end
     
    +

    Accessing Components' attached Entities

    + +

    Components also keep track of what Entities are using it. To access this list we do the following:

    + +
    @component.entities # => [@entity1, @entity2, @entity3]
    +
    +

    Systems

    Creation

    We can create Systems like so:

    -
    FelFlame::Systems.new(name: 'Render', priority: 2) do
    +
    FelFlame::Systems.new(name: 'Render', priority: 2) do
       # Code and Logic
     end
     

    The name we assign is how we can access the System, like so:

    -
    FelFlame::Systems::Render
    +
    FelFlame::Systems::Render
     

    Priority determines the order Systems should be executed, this is used for Scenes and the Stage. @@ -317,8 +341,8 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    Often we will want to execute some logic on each Component in a given Component Manager so our code might look like this:

    -
    FelFlame::Systems.new(name: 'Render', priority: 2) do
    -  FelFlame::Components::Sprites.each do |component|
    +
    FelFlame::Systems.new(name: 'Render', priority: 2) do
    +  FelFlame::Components::Sprites.each do |component|
         # do something with these components
       end
     end
    @@ -328,7 +352,7 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.  

    After we create a System, it won't do anything on its own until we tell it to. Here is how:

    -
    FelFlame::Systems::Render.call
    +
    FelFlame::Systems::Render.call
     

    Sometimes you might want to manually activate a System, but the more common way to have Systems be triggered is to use Scenes and the Stage or the alternative ways of execution.

    @@ -338,25 +362,25 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    Sometimes you want a System to automatically trigger when a special even happens. FelFlame can keep track of when a Component is added, removed, or when an attribute is changed and then execute Systems linked to these events. Here is how to create these links:

    # When this Component is added to an Entity, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_added(@component)
    +FelFlame::Systems::PassiveRegen.trigger_when_added(@component)
     
     # When this Component is removed from an Entity, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_removed(@component)
    +FelFlame::Systems::PassiveRegen.trigger_when_removed(@component)
     
     # When this Component's health attribute is changed, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_is_set(@component, :health)
    +FelFlame::Systems::PassiveRegen.trigger_when_is_set(@component, :health)
     

    If we want these triggers to happen for all Components that belong to specific Component Manager then we can do that instead:

    # When a Component from this Component Manager is added to an Entity, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_added(@component_manager)
    +FelFlame::Systems::PassiveRegen.trigger_when_added(@component_manager)
     
     # When a Component from this Component Manager is removed from an Entity, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_removed(@component_manager)
    +FelFlame::Systems::PassiveRegen.trigger_when_removed(@component_manager)
     
     # When this Component's health attribute from this Component Manager is changed, this System will be called
    -FelFlame::Systems::PassiveRegen.trigger_when_is_set(@component_manager, :health)
    +FelFlame::Systems::PassiveRegen.trigger_when_is_set(@component_manager, :health)
     

    We can create any number of these links between Systems, Components, and Component Manangers as we like, simply call the method again with our other Components and Component Managers

    @@ -366,41 +390,41 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    If we wish to remove these links that we created, we can do that using the follwing function in any of the following ways:

    # clears ALL triggers with this system
    -FelFlame::Systems::PassiveRegen.clear_triggers
    +FelFlame::Systems::PassiveRegen.clear_triggers
     
     # clears ALL triggers with this Component Manager
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component)
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component)
     
     # clear the 'trigger_when_added' for this Component
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component, :added)
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component, :added)
     
     # clear the 'trigger_when_removed' for this Component
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component, :removed)
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component, :removed)
     
     # clear the 'trigger_when_is_set' for this Component specifically for the health attribute
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component, :is_set, :health)
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component, :is_set, :health)
     

    Likewise we can do the same with Component Managers:

    # clears ALL triggers with this Component
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager) 
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager) 
     
     # clear the 'trigger_when_added' for this Component Manager
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :added) 
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :added) 
     
     # clear the 'trigger_when_removed' for this Component Manager
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :removed) 
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :removed) 
     
     # clear the 'trigger_when_is_set' for this Component Manager specifically for the health attribute
    -FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :is_set, :health) 
    +FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager, :is_set, :health) 
     

    Redefinition

    If we wanted to change what code or logic a given System executes, we could do that with:

    -
    FelFlame::Systems::PassiveRegen.redefine do
    +
    FelFlame::Systems::PassiveRegen.redefine do
       # Some new logic or code
     end
     
    @@ -411,28 +435,28 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    Once we have all the core parts of ECS, we will want to organize our Systems. To do this we will use Scenes to group up Systems so they can quickly be enabled or disabled. Note that Alternative Executions will occur even if they are not part of a Scene. Here is how we make a new Scene:

    -
    @scene = FelFlame::Scenes.new('ExampleScene')
    +
    @scene = FelFlame::Scenes.new('ExampleScene')
     

    Accessing

    Just like other classes in FelFlame, the name we gave the Scene is how we access it:

    -
    @scene = FelFlame::Scenes::ExampleScene
    +
    @scene = FelFlame::Scenes::ExampleScene
     

    Adding Systems

    Adding Systems is simple. We can add as many as we want. In this example we add 3 different systems:

    -
    FelFlame::Scenes::ExampleScene.add(FelFlame::Systems::Render, @system2, @system3)
    +
    FelFlame::Scenes::ExampleScene.add(FelFlame::Systems::Render, @system2, @system3)
     

    Removing Systems

    Removing Systems works simularly:

    -
    FelFlame::Scenes::ExampleScene.remove(FelFlame::Systems::Render, @system2, @system3)
    +
    FelFlame::Scenes::ExampleScene.remove(FelFlame::Systems::Render, @system2, @system3)
     

    Clearing

    @@ -457,26 +481,26 @@ E.g priority 1 will go first, priority 2 will go second, etcetera.

    Finally we have the Stage. There is only a single Stage and we do not have to create it as it exists by default. By adding a Scene to the Stage we are saying that the Scene is active. To add a Scene we do the following:

    -
    FelFlame::Stage.add FelFlame::Scene::ExampleScene
    +
    FelFlame::Stage.add FelFlame::Scene::ExampleScene
     

    Removing Scenes

    Likewise we can remove Scenes:

    -
    FelFlame::Stage.remove FelFlame::Scene::ExampleScene
    +
    FelFlame::Stage.remove FelFlame::Scene::ExampleScene
     

    Executing

    On each frame of the game we want to execute the Stage once. When the Stage is executed it is progressing your game 1 frame forward. The Stage will make sure for you that all the Systems from all Scenes added will be executed in the correct order according to their priority. Here is how we do it:

    -
    FelFlame::Stage.call
    +
    FelFlame::Stage.call
     

    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!

    +

    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!

    Contribution

    @@ -488,7 +512,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 d8804ef..48eea1f 100644 --- a/docs/method_list.html +++ b/docs/method_list.html @@ -44,430 +44,6 @@ diff --git a/docs/top-level-namespace.html b/docs/top-level-namespace.html index 3443369..6fbfabe 100644 --- a/docs/top-level-namespace.html +++ b/docs/top-level-namespace.html @@ -78,43 +78,6 @@ -

    Defined Under Namespace

    -

    - - - - - Classes: FelFlame - - -

    - - -

    - Constant Summary - collapse -

    - -
    - -
    FF = -
    -
    - -

    An alias for FelFlame

    - - -
    -
    -
    - - -
    -
    -
    FelFlame
    - -
    - @@ -127,7 +90,7 @@ diff --git a/lib/felflame/component_manager.rb b/lib/felflame/component_manager.rb index e6c6d91..43932d2 100644 --- a/lib/felflame/component_manager.rb +++ b/lib/felflame/component_manager.rb @@ -220,8 +220,9 @@ class FelFlame # the remove command is called, breaking the loop if it # wasn't referencing a clone(will get Nil errors) iter = entities.map(&:clone) - iter.each do |entity_id| - FelFlame::Entities[entity_id].remove self #unless FelFlame::Entities[entity_id].nil? + iter.each do |entity| + #FelFlame::Entities[entity_id].remove self #unless FelFlame::Entities[entity_id].nil? + entity.remove self end self.class.data[id] = nil instance_variables.each do |var| diff --git a/lib/felflame/entity_manager.rb b/lib/felflame/entity_manager.rb index 74fc4c3..a05ef93 100644 --- a/lib/felflame/entity_manager.rb +++ b/lib/felflame/entity_manager.rb @@ -41,9 +41,8 @@ class FelFlame # @return [Boolean] +true+ def delete components.each do |component_manager, component_array| - component_array.each do |component_id| - component_manager[component_id].entities.delete(id) - #self.remove FelFlame::Components.const_get(component_manager.name)[component_id] + component_array.each do |component| + component.entities.delete(self) end end FelFlame::Entities.data[id] = nil @@ -58,12 +57,12 @@ class FelFlame def add(*components_to_add) components_to_add.each do |component| if components[component.class].nil? - components[component.class] = [component.id] - component.entities.push id + components[component.class] = [component] + component.entities.push self check_systems component, :addition_triggers - elsif !components[component.class].include? component.id - components[component.class].push component.id - component.entities.push id + elsif !components[component.class].include? component + components[component.class].push component + component.entities.push self check_systems component, :addition_triggers end end @@ -87,9 +86,9 @@ class FelFlame # @return [Boolean] +true+ def remove(*components_to_remove) components_to_remove.each do |component| - check_systems component, :removal_triggers if component.entities.include? id - component.entities.delete id - components[component.class].delete component.id + check_systems component, :removal_triggers if component.entities.include? self + component.entities.delete self + components[component.class].delete component end true end diff --git a/lib/felflame/version.rb b/lib/felflame/version.rb index f69ea19..8483c07 100644 --- a/lib/felflame/version.rb +++ b/lib/felflame/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Felflame - VERSION = "1.0.2" + VERSION = "2.0.0" end diff --git a/spec/entity_manager_spec.rb b/spec/entity_manager_spec.rb index 78300c2..ef638c0 100644 --- a/spec/entity_manager_spec.rb +++ b/spec/entity_manager_spec.rb @@ -46,12 +46,12 @@ describe 'Entities' do it 'can have components attached' do @ent0.add @cmp0 - expect(@ent0.components[@component_manager][0]).to eq(@cmp0.id) + expect(@ent0.components[@component_manager][0]).to eq(@cmp0) @ent1.add @cmp1, @cmp2 expect(@ent1.components[@component_manager].length).to eq(2) - expect(@ent1.components[@component_manager].include?(@cmp1.id)).to be true - expect(@ent1.components[@component_manager].include?(@cmp2.id)).to be true + expect(@ent1.components[@component_manager].include?(@cmp1)).to be true + expect(@ent1.components[@component_manager].include?(@cmp2)).to be true end it 'can get id from to_i' do @@ -71,16 +71,16 @@ describe 'Entities' do @ent0.add @cmp0, @cmp1, @cmp2 @ent1.add @cmp0, @cmp1 @ent2.add @cmp1, @cmp2 - expect(@ent0.components).to eq({@component_manager => [0,1,2]}) - expect(@cmp0.entities).to eq([0,1]) - expect(@cmp1.entities).to eq([0,1,2]) - expect(@cmp2.entities).to eq([0,2]) + expect(@ent0.components).to eq({@component_manager => [@cmp0,@cmp1,@cmp2]}) + expect(@cmp0.entities).to eq([@ent0,@ent1]) + expect(@cmp1.entities).to eq([@ent0,@ent1,@ent2]) + expect(@cmp2.entities).to eq([@ent0,@ent2]) @ent1.delete - expect(@cmp0.entities).to eq([0]) - expect(@cmp1.entities).to eq([0,2]) - expect(@cmp2.entities).to eq([0,2]) + expect(@cmp0.entities).to eq([@ent0]) + expect(@cmp1.entities).to eq([@ent0,@ent2]) + expect(@cmp2.entities).to eq([@ent0,@ent2]) @cmp1.delete - expect(@ent0.components).to eq({@component_manager => [0,2]}) + expect(@ent0.components).to eq({@component_manager => [@cmp0,@cmp2]}) @component_manager.each(&:delete) expect(@component_manager.each.to_a).to eq([]) expect(@ent0.components).to eq({@component_manager => []}) -- cgit v1.2.3