MrSmith33 / datadriven

Entity component system (ECS)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This is entity component system (ECS) type library.

Features

  • Automatic code gen for queries (basically inner join)
  • Dynamic registration of component types
  • Convenient entity manager

Example

@Component("component.Transform") struct Transform { float x, y, z; }
@Component("component.Velocity") struct Velocity  { float x, y, z; }
@Component("component.FlagComponent") struct FlagComponent {}

EntityManager eman;

eman.registerComponent!Transform(); // stored in HashMap
eman.registerComponent!Velocity(); // stored in HashMap
eman.registerComponent!FlagComponent(); // stored in HashSet

eman.set(0, Transform(0, 0, 0), Velocity(10, 10, 10), FlagComponent());
eman.set(1, Transform(1, 1, 1), Velocity(10, 10, 10), FlagComponent());
eman.set(2, Transform(2, 2, 2), Velocity(10, 10, 10), FlagComponent());
eman.set(3, Transform(3, 3, 3), Velocity(10, 10, 10));

// will get all entities with given component types
auto query = eman.query!(Transform, Velocity, FlagComponent);

// Will not print entity 3
foreach(row; query) {
	writefln("%s %s %s", row.id, *row.transform_0, *row.velocity_1);
}

eman.remove(0); // Remove all components of entity

assert(!eman.has!Transform(0)); // Can check if has component
assert(!eman.has!Velocity(0));
assert(!eman.has!FlagComponent(0));

Look at main.d, benchmark.d for more examples.

About

Entity component system (ECS)

License:Boost Software License 1.0


Languages

Language:D 100.0%