|
|
|
#include "dinkyecs.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fmt/core.h>
|
|
|
|
|
|
|
|
using namespace fmt;
|
|
|
|
using DinkyECS::Entity, DinkyECS::World;
|
|
|
|
|
|
|
|
struct Position {
|
|
|
|
double x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Velocity {
|
|
|
|
double x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Gravity {
|
|
|
|
double level;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
World me;
|
|
|
|
|
|
|
|
Entity test = me.entity();
|
|
|
|
Entity test2 = me.entity();
|
|
|
|
|
|
|
|
me.assign<Position>(test, {10,20});
|
|
|
|
me.assign<Velocity>(test, {1,2});
|
|
|
|
|
|
|
|
me.assign<Position>(test2, {1,1});
|
|
|
|
me.assign<Velocity>(test2, {10,20});
|
|
|
|
|
|
|
|
Position &pos = me.component<Position>(test);
|
|
|
|
println("GOT POS x={}, y={}", pos.x, pos.y);
|
|
|
|
|
|
|
|
Velocity &vel = me.component<Velocity>(test);
|
|
|
|
println("GOT VELOCITY x={}, y={}", vel.x, vel.y);
|
|
|
|
|
|
|
|
println("--- Position only system:");
|
|
|
|
me.system<Position>([](const auto &ent, auto &pos) {
|
|
|
|
println("entity={}, pos.x={}, pos.y={}", ent, pos.x, pos.y);
|
|
|
|
});
|
|
|
|
|
|
|
|
println("--- Velocity only system:");
|
|
|
|
me.system<Velocity>([](const auto &, auto &vel) {
|
|
|
|
println("vel.x={}, vel.y={}", vel.x, vel.y);
|
|
|
|
});
|
|
|
|
|
|
|
|
println("--- Manually get the velocity in position system:");
|
|
|
|
me.system<Position>([&](const auto &ent, auto &pos) {
|
|
|
|
Velocity &vel = me.component<Velocity>(ent);
|
|
|
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
|
|
|
});
|
|
|
|
|
|
|
|
println("--- Creating facts (singletons)");
|
|
|
|
me.set<Gravity>({0.9});
|
|
|
|
|
|
|
|
println("--- Query only entities with Position and Velocity:");
|
|
|
|
me.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
|
|
|
Gravity &grav = me.get<Gravity>();
|
|
|
|
println("grav={}, entity={}, vel.x, vel.y, pos.x={}, pos.y={}", grav.level, ent, vel.x, vel.y, pos.x, pos.y);
|
|
|
|
});
|
|
|
|
|
|
|
|
// now remove Velocity
|
|
|
|
me.remove<Velocity>(test);
|
|
|
|
|
|
|
|
println("--- After remove test, should only result in test2:");
|
|
|
|
me.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
|
|
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
|
|
|
});
|
|
|
|
|
|
|
|
println("--- Create a stored system you can save for later.");
|
|
|
|
auto movementSystem = me.runner<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
|
|
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
|
|
|
});
|
|
|
|
|
|
|
|
movementSystem();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|