antara::gaming::ecs

antara::gaming::ecs::system_manager

class system_manager

This class allows the manipulation of systems, the addition, deletion, update of systems, deactivation of a system, etc.

Public Types

using system_ptr = std::unique_ptr<base_system>

sugar name for a pointer to base_system

using system_array = std::vector<system_ptr>

sugar name for an array of system_ptr

using system_registry = std::array<system_array, system_type::size>

sugar name for a multidimensional array of system_array (pre_update, logic_update, post_update)

Public Functions

system_manager(entt::registry &registry, bool subscribe_to_internal_events = true)

Constructors.

Parameters
  • registry: The entity_registry is provided to the system when it is created.

  • subscribe_to_internal_events: Choose whether to subscribe to default system_manager events

    Note

    Principal Constructor.

    Example:
    #include <entt/entity/registry.hpp>
    #include <entt/dispatcher/dispatcher.hpp>
    #include <antara/gaming/ecs/system.manager.hpp>
    
    int main()
    {
        entt::registry entity_registry;
        entt::dispatcher& dispatcher{registry.set<entt::dispatcher>()};
        antara::gaming::ecs::system_manager mgr{entity_registry};
    }
    

void receive_add_base_system(const ecs::event::add_base_system &evt)

Callback.

Parameters
  • evt: The event that contains the system to add

void start()

This function tells the system manager that you start your game.

Note

This function, which indicates the game is spinning, allows actions to be done at each end of the frame like delete systems or add them while we are going to iterate on

Example:
#include <entt/entity/registry.hpp>
#include <entt/dispatcher/dispatcher.hpp>
#include <antara/gaming/ecs/system.manager.hpp>

int main()
{
    entt::registry entity_registry;
    entt::dispatcher dispatcher
    antara::gaming::ecs::system_manager system_manager{entity_registry, dispatcher};
    system_manager.start();
    return 0;
}

std::size_t update()

Return

number of systems which are successfully updated

Note

This is the function that update your systems.
Based on the logic of the different kinds of antara systems, this function takes care of updating your systems in the right order.

Warning

If you have not loaded any system into the system_manager the function returns 0.
If you decide to mark a system, it’s automatically deleted at the end of the current loop tick through this function.
If you decide to add a system through an ecs::event::add_base_system event, it’s automatically added at the end of the current loop tick through this function.

Example:
#include <entt/entity/registry.hpp>
#include <entt/dispatcher/dispatcher.hpp>
#include <antara/gaming/ecs/system.manager.hpp>

int main()
{
    entt::registry entity_registry;
    entt::dispatcher dispatcher
    antara::gaming::ecs::system_manager system_manager{entity_registry, dispatcher};
    system_manager.start();
    // ... added 5 differents systems here
    std::size_t nb_systems_updated = system_manager.update();
    if (nb_systems_updated != 5) {
       // Oh no, i expected 5 systems to be executed in this game loop tick
    }
    return 0;
}

std::size_t update_systems(system_type system_type_to_update)

Return

number of systems which are successfully updated

Note

This function is called multiple times by update().
It is useful if you want to program your own update function without going through the one provided by us.

See

update

Parameters
  • system_type_to_update: kind of systems to update (pre_update, logic_update, post_update)

template<typename TSystem>
const TSystem &get_system() const

This function allows you to get a system through a template parameter.

Return

A reference to the system obtained.

Template Parameters
  • TSystem: represents the system to get.

template<typename TSystem>
TSystem &get_system()

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Example:

#include <entt/entity/registry.hpp>
#include <entt/dispatcher/dispatcher.hpp>
#include <antara/gaming/ecs/system.manager.hpp>

int main()
{
    entt::registry entity_registry;
    entt::dispatcher dispatcher
    antara::gaming::ecs::system_manager system_manager{entity_registry, dispatcher};
    system_manager.start();
    // ... added 2 differents systems here (render_system, and a log_system)
    auto& render_system = system_manager.get_system<game::render_system>();

    const auto& log_system = system_manager.get_system<game::log_system>();
    return 0;
}

template<typename ...TSystems>
std::tuple<std::add_lvalue_reference_t<TSystems>...> get_systems()

This function allow you to get multiple system through multiple templates parameters.

Note

This function recursively calls the get_system function Based on the logic of the different kinds of antara systems, this function takes care of updating your systems in the right order.

Return

Tuple of systems obtained.

See

get_system

Template Parameters
  • TSystems: represents a list of systems to get

template<typename ...TSystems>
std::tuple<std::add_lvalue_reference_t<std::add_const_t<TSystems>>...> get_systems() const

const version overload of get_systems

Note

This function is marked as nodiscard.

Example:
#include <entt/entity/registry.hpp>
#include <entt/dispatcher/dispatcher.hpp>
#include <antara/gaming/ecs/system.manager.hpp>

int main()
{
    entt::registry entity_registry;
    entt::dispatcher dispatcher
    antara::gaming::ecs::system_manager system_manager{entity_registry, dispatcher};
    // ... added differents systems here
    // Called from a const context
    auto &&[system_foo, system_bar] = system_manager.get_systems<system_foo, system_bar>();

    // Called from a non const context
    auto&&[system_foo_nc, system_bar_nc] = system_manager.get_systems<system_foo, system_bar>();

    // Get it as a tuple
    auto tuple_systems = system_manager.get_systems<system_foo, system_bar>();
    return 0;
}
See

get_systems

template<typename TSystem>
bool has_system() const

This function allow you to verify if a system is already registered in the system_manager.

Note

This function is marked as nodiscard.

Return

true if the system has been loaded, false otherwise

Template Parameters
  • TSystem: Represents the system that needs to be verified

Example:

#include <entt/entity/registry.hpp>
#include <entt/dispatcher/dispatcher.hpp>
#include <antara/gaming/ecs/system.manager.hpp>

int main()
{
    entt::registry entity_registry;
    entt::dispatcher dispatcher
    antara::gaming::ecs::system_manager system_manager{entity_registry, dispatcher};

    bool result = system_manager.has_system<my_game::render_system>();
    if (!result) {
        // Oh no, i don't have a rendering system.
    }
    return 0;
}

template<typename ...TSystems>
bool has_systems() const

This function allow you to verify if a list of systems is already registered in the system_manager.

Note

This function is marked as nodiscard.
This function recursively calls the has_system function.

Return

true if the list of systems has been loaded, false otherwise

See

has_system

Template Parameters
  • TSystems: represents a list of system that needs to be verified

Example:

#include <entt/entity/registry.hpp>
#include <entt/dispatcher/dispatcher.hpp>
#include <antara/gaming/ecs/system.manager.hpp>

int main()
{
    entt::registry entity_registry;
    entt::dispatcher dispatcher
    antara::gaming::ecs::system_manager system_manager{entity_registry, dispatcher};

    bool result = system_manager.has_systems<my_game::render_system, my_game::input_systems>();
    if (!result) {
        // Oh no, atleast one of the systems is not present
    }
    return 0;
}

template<typename TSystem>
bool mark_system()

Note

This function marks a system that will be destroyed at the next tick of the game loop.

Return

true if the system has been marked, false otherwise

Template Parameters
  • TSystem: Represents the system that needs to be marked

template<typename ...TSystems>
bool mark_systems()

This function recursively calls the mark_system function

Note

This function marks a list of systems, marked systems will be destroyed at the next tick of the game loop.

Return

true if the list of systems has been marked, false otherwise

See

mark_system

Template Parameters
  • TSystems: Represents a list of systems that needs to be marked

template<typename TSystem>
bool enable_system()

Note

This function enable a system

Return

true if the system has been enabled, false otherwise

Template Parameters
  • TSystem: Represents the system that needs to be enabled.

template<typename ...TSystems>
bool enable_systems()

This function recursively calls the enable_system function

Note

This function enable a list of systems

Return

true if the list of systems has been enabled, false otherwise

See

enable_system

Template Parameters
  • TSystems: Represents a list of systems that needs to be enabled

template<typename TSystem>
bool disable_system()

Note

This function disable a system

Return

true if the the system has been disabled, false otherwise

Attention

If you deactivate a system, it will not be destroyed but simply ignore during the game loop

Template Parameters
  • TSystem: Represents the system that needs to be disabled

template<typename ...TSystems>
bool disable_systems()

This function recursively calls the disable_system function

Note

This function disable a list of systems

Return

true if the list of systems has been disabled, false otherwise

Template Parameters
  • TSystems: Represents a list of systems that needs to be disabled

std::size_t nb_systems() const

Return

number of systems

std::size_t nb_systems(system_type sys_type) const

Return

number of systems of a specific type.

Parameters
  • sys_type: represent the type of systems.

template<typename TSystem, typename ...TSystemArgs>
TSystem &create_system(TSystemArgs&&... args)

Note

This function allow you to create a system with the given argument

Note

This function is a factory

Return

Returns a reference to the created system

Template Parameters
  • TSystem: represents the type of system to create

  • TSystemArgs: represents the arguments needed to construct the system to create

template<typename ...TSystems, typename ...TArgs>
auto load_systems(TArgs&&... args)

Return

Tuple of systems loaded

See

create_system

Template Parameters
  • TSystems: represents a list of systems to be loaded

Private Types

using clock = std::chrono::steady_clock

Private data members.

Private Functions

base_system &add_system_(system_ptr &&system, system_type sys_type)

Private member functions.