FelFlame

Maintainability Test Coverage Inline docs MIT License Ko-Fi

What is FelFlame?

FelFlame is an ECS framework for developing games in the Ruby language. FelFlame has been designed from the ground up with these three ideas in mind:

  1. Engine Agnostic: FelFlame has been designed to be rendering engine agnostic as long as the target rendering engine is written in Ruby. This means that this framework can be dropped into existing rendering engines such as Ruby2D or DRGTK with little modifications.
  2. Easily Extensible: FelFlame has been designed such that extensions to its capabilities can be easily added. Extensions such as rendering engine wrappers, premade systems, premade components, etcetera can be easily coded and then distributed as gems.
  3. Priciple of (My) Least Astonishment: I want to develop games using a language and framework I love and makes sense to me, inspired by the Philosophy of the creator of Ruby.

What is ECS?

ECS is a software architectural pattern that is used in video game development. Traditionally games were programmed using an object oriented method, while ECS instead attempts to program games using a data oriented method instead.
ECS stands for Entity, Component, and System.


Components

This is where the data or information of a given "object" is stored. There is no logic or code here.

Entities

Entities will contain one or more Components, but contains no logic or data otherwise

Systems

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?"


Scenes

Scenes are simply a collection or subset of Systems. This allows for an easy way to activate and deactivate Systems.

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.


If all of this sounds very confusing, don't worry. A video tutorial series is in the works where I will build a game using this framework and explain every step of the way. You can also read some examples and explanations below.

Usage

Entities

Creation

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

@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])

Accessing

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

@entity = FelFlame::Entities[2]

Get ID

Entity ID's are generated starting from 0 and ascending, unless if there is a missing ID because of a deleted entity where a new entity will claim that ID. To get the ID of an Entity:

@entity.id

Adding and Removing Components

We can still add or remove Components from an Entity after it has been created. Here is how:

@entity.add @component
@entity.remove @component

Deletion

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

@entity.delete

Components

Creating a Component Manager

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)

In this example we created a component manager called "Stats". The name given to component managers must follow the same rules for naming constants in ruby for a reason you will shortly see. The parameters following are all creating the attributes we can set. We can set any number of parameters we wish, in this example we define two. The :armour parameter is being created without a default, it will equal to nil when a new component is created, while hp will be equal to 100 when a component is created. When defining attributes symbols should be used.

Creating a Component from a Component Manager

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

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

Or we can even change the defaults:

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

Accessing and Getting ID

Just like Entities, Components have IDs. 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.id # => 2

Accessing Attributes and Changing Them

There are a few different ways we can read or change the attributes of a component depending on what our needs are. Here are the ways to edit attrubutes, followed by the ways to read them.

@component.armour = 'Mythril'
@component.update_attrs(armour: 'Leather', hp: 95)
@component.hp # => 95
@component.attrs # => {armour: 'Leather', hp: 95}

Deleting Components

Deleting a Component is the same format as deleting an Entity. When a Component is deleted referenced to it such as to entities are automatically cleared.

@component.delete

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:

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

Systems

Creation

We can create Systems like so:

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

Priority determines the order Systems should be executed, this is used for Scenes and the Stage. The lower the number, the earlier a given System will be executed. 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|
    # do something with these components
  end
end

Execution

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

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.

Alternative Execution

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)

# When this Component is removed from an Entity, this System will be called
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)

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)

# 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)

# 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)

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

Clearing Alternative Executions

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

# clears ALL triggers with this Component Manager
FelFlame::Systems::PassiveRegen.clear_triggers(@component)

# clear the 'trigger_when_added' for this Component
FelFlame::Systems::PassiveRegen.clear_triggers(@component, :added)

# clear the 'trigger_when_removed' for this Component
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)

Likewise we can do the same with Component Managers:

# clears ALL triggers with this Component
FelFlame::Systems::PassiveRegen.clear_triggers(@component_manager) 

# clear the 'trigger_when_added' for this Component Manager
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) 

# 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) 

Redefinition

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

FelFlame::Systems::PassiveRegen.redefine do
  # Some new logic or code
end

Scenes

Creation

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')

Accessing

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

@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)

Removing Systems

Removing Systems works simularly:

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

Clearing

If you want to remove all Systems from a Scene here is how we do it:

@scene.clear

Execution

To execute all Systems within a scene once we can just do:

@scene.call

The Scene will make sure that the systems are executed in the correct order based on their given priorities

Stage

Adding Scenes

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

Removing Scenes

Likewise we can remove Scenes:

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

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!

Contribution

Contributors are welcome! I am always looking to impove the capabilities of game development in Ruby. Feel free to open an issue to discuss a proposed changed or fix. To code a change or fix first fork the project. Next write your changes or fixes. Make sure all your changes and fixes are properly documented using Yard(I will not merge if it is not 100% documented) and make sure everything has tests written for it with Rspec(I will also not merge if it does not have 100% test coverage). Once you have your changes made then simply make a pull request.

If you need help writing documentation or tests feel free to ask!

If you want to contribute to development with a thanks you can always buy me a coffee ;^)