Skip to content

Wase-Engine/wase-ecs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Wase ECS logo banner

Website badge Discord badge Mit License badge Issues badge Lines badge Stars badge

Wase ECS is an open source ECS library made with C++17.

Setup

To set up Wase ECS you will need CMake and C++17.

mkdir build && cd build
cmake ..

To include tests you can add the -Denable_test=1 flag to the cmake command.

Usage

To include Wase ECS to your project you can include the ecs.h file.

Creating a World object

First you need to set up a World object. The World handles everything for the ECS.

wase::ecs::World world;

You can update all the systems that are registered in the world with the update method.

world.update(deltaTime);

Managing entities

With the world object you are able to manage your entities.

world.createEntity();
Entity* entity = world.getEntity(entityId);
world.destroyEntity(entity->getId());

The Entity class has some methods itself too to manage the entity state.

Id id = entity->getId();
world.enableEntity(id);
world.disableEntity(id);
bool enabled = entity.isEnabled();

Components

You can create custom components yourself.

struct TransformComponent : wase::ecs::Component
{
    int x = 0;
    int y = 0;
};

To manage components on entities you can use the methods in the Entity class.

world.addComponent<TransformComponent>(entityId);
TransformComponent& component = world.getComponent<TransformComponent>(entityId);
bool hasComponent = world.hasComponent<TransformComponent>(entityId);
world.removeComponent<TransformComponent>(entityId);

Systems

You can create custom systems yourself.

class PhysicsSystem : public wase::ecs::System 
{
};

You can register the new system to the world object.

world.registerSystem<PhysicsSystem>();

When a system gets registered it adds all the entities in the world to it that match the entity filter.

Entity filter

With the entity filter you can specify which entities should be added to the system depending on their components.

class PhysicsSystem : public wase::ecs::System 
{
public:
    PhysicsSystem()
    {
        getFilter().require<TransformComponent>();
    }
};

This will require the TransformComponent for any entities to be added to the PhysicsSystem. You can also use the exclude filter so that entities with this component will not be added.

class PhysicsSystem : public wase::ecs::System 
{
public:
    PhysicsSystem()
    {
        getFilter().exclude<TransformComponent>();
    }
};

Updating systems

All registered systems get updated when the update method on the world object is being executed.

world.update(deltaTime);

You can add logic to the update method of the system.

class PhysicsSystem : public wase::ecs::System 
{
public:
    PhysicsSystem()
    {
        getFilter().require<TransformComponent>();
    }
    
    void update(const float deltaTime) override
    {
        for(Entity* entity : m_EnabledEntities)
        {
            auto& transform = m_ComponentPool->getComponent<TransformComponent>(entity->getId());
            transform.x++;
            transform.y++;
        }
    }
};

m_EnabledEntities holds all the entities that are matched with the entity filter and are enabled. You could also make use of m_DisabledEntities, this contains all the matching entities that are disabled.

Contributing

If you want to contribute to Wase ECS you can head over to the contributing page for more information.

About

An entity component system written in C++

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors