schoffi92 / ztech-ecs

Ztech Engine ECS Module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ztech Engine ECS Module

ECS

  • Entity
  • Component
  • System

Create Entities

  • Create Entity Array with entity_array
  • Register your components
  • One component can registered only once
  • Your component registration should be the first thing you do
  • After adding/releasing entities should not register any other components

Example:

struct car_location_t
{
    double x, y = 0.0f;
};

struct car_movement_t
{
    double vx, vy = 0.0f;
};

ztech::ecs::entity_array entities;

{
    entities.register_component< car_location_t >( );
    entities.register_component< car_movement_t >( );
}

Add entities

  • Locking the entities
  • Use alloc for allocate entities

Example:

ztech::ecs::entity_array entities;

auto id = entities.alloc( ); // allocate only one

// allocate 100 entitities
std::vector< ztech::ecs::entity_id_t > ids;
entities.alloc( ids, 100 );

Free entity

  • Locking the entities
  • Use free for release entities

Example:

ztech::ecs::entity_array entities;

auto id = entities.alloc( ); // allocate only one
entities.free( id );

Iterating through entities

  • Use for_each method what lock the components with shared lock

Example:

ztech::ecs::entity_array entities;

entities.for_each( []( ztech::ecs::entity_id_t id )
{
    // also the invalid entities will be filtered out
    // do something
    auto loc_comp = entities.get_component< car_location_t >( ); // asking for component array
    car_movement_t* mov_comp = entities.get_component_data< car_movement_t >( id ); // asking for the data from the component
    ( *loc_comp )[ id ].x = 10.0f;
    loc_comp->at( id ).y = 10.0f;

    mov_comp->vx = 10.0f;
    mov_comp->vy = 12.0f;
});

// Iterating through entities with two thread
entities.for_each_parallel< 2 >( []( ztech::ecs::entity_id_t id )
{
    // also the invalid entities will be filtered out
    // do something
});

Remove multiple ids

  • Use for_each method to collect removable elements
  • call free with the vector
  • free unique lock the components ( so we cannot call free in for_each )

Example:

std::vector< ztech::ecs::entity_id_t > removed_ids;
entities.for_each( [&removed_ids]( ztech::ecs::entity_id_t id )
{
    // also the invalid entities will be filtered out
    // do something
    if ( id % 2 == 1 ) removed_ids.push_back( id );
});

entities.free( removed_ids );

// Parallel example
std::vector< ztech::ecs::entity_id_t > removed_ids1;
std::vector< ztech::ecs::entity_id_t > removed_ids2;
entities.for_each_parallel< 2 >( [&removed_ids1, &remove_ids2]( ztech::ecs::entity_id_t id, size_t thread_id )
{
    // also the invalid entities will be filtered out
    // do something
    std::vector< ztech::ecs::entity_id_t >* removed_ids = nullptr;
    if ( thread_id == 1 ) removed_ids = &remove_ids2;
    else removed_ids = &remove_ids1;
    if ( id % 2 == 1 ) removed_ids->push_back( id );
});

entities.free( removed_ids1 );
entities.free( removed_ids2 );

About

Ztech Engine ECS Module

License:MIT License


Languages

Language:C++ 95.8%Language:CMake 4.2%