MarkSkyzoid / lecs

Lightweight Entity Component System

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LECS

LECS (Lightweight Entity Component System) is a simple, lightweight and header only C++ library to add ECS (Entity Component System) to your game or your project.

Features

  • Entities are just lookup IDs
  • EntityIDs have a version so that you don't need to worry about dangling handles
  • Any type can be a component
  • No inheritance for defining components nor systems
  • No RTTI
  • Any function or callable type can be a system
  • Call systems from wherever you want in any order you like!

Also check Sakura (WIP Game Engine using LECS) and Leviathan (Lightweight Event System)!

Installation

You can download the whole repo and copy the contents of the 'lecs' folder into your project's include directories.
Then you can include lecs into your project:

#include <lecs/lecs.hpp>

Usage

Components are simple structs eg.:

struct Transform {
	float position[3];
	float rotation[3];
	float scale[3];
};

You can create new entities from an ECS object:

 lecs::ECS my_ecs;
 lecs::Entity entity = my_ecs.create_entity();

You can assign (or remove) components to entities like this:

 my_ecs.add_component_to_entity<Transform>(entity);

or

 my_ecs.remove_component_from_entity<Transform>(entity);

You can retrieve component data like this:

auto component_data = ecs.get_component<Transform>(entity);
component_data->position[0] = 1.0f;

If there is no component data associated to that entity, the function will return nullptr. You can also check if an entity has a component:

 bool has_transform = ecs.has_component<Transform>(entity));

Systems are just free functions or callable objects, you can choose:

 void velocity_system_update(lecs::ECS& ecs_instance, float delta_time) {
	for (lecs::Entity entity : lecs::EntityIterator<Transform, Velocity>(ecs_instance)) {
		Transform& transform_component = ecs_instance.get_component<Transform>(entity);
		Velocity& velocity_component = ecs_instance.get_component<Velocity>(entity);
		// ... do your things ...
	}
 }

Then you can call system updates from wherever you like, usually your main loop, but this gives you flexibility to how you organize your update:

 velocity_system_update(my_ecs, delta_time);

Of course do not forget to remove any entity you don't need:

 my_ecs.remove_entity(entity);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details

References:

About

Lightweight Entity Component System

License:MIT License


Languages

Language:C++ 100.0%