#include "dinkyecs.hpp" #include #include using namespace fmt; using DinkyECS::Entity; struct Player { std::string name; Entity eid; }; struct Position { double x, y; }; struct Velocity { double x, y; }; struct Gravity { double level; }; /* * Using a function catches instances where I'm not copying * the data into the world. */ void configure(DinkyECS::World &world, Entity &test) { println("---Configuring the base system."); Entity test2 = world.entity(); world.set(test, {10,20}); world.set(test, {1,2}); world.set(test2, {1,1}); world.set(test2, {10,20}); println("---- Setting up the player as a fact in the system."); auto player_eid = world.entity(); Player player_info{"Zed", player_eid}; // just set some player info as a fact with the entity id world.set_the(player_info); world.set(player_eid, {0,0}); world.set(player_eid, {0,0}); auto enemy = world.entity(); world.set(enemy, {0,0}); world.set(enemy, {0,0}); println("--- Creating facts (singletons)"); world.set_the({0.9}); } int main() { DinkyECS::World world; Entity test = world.entity(); configure(world, test); Position &pos = world.get(test); println("GOT POS x={}, y={}", pos.x, pos.y); Velocity &vel = world.get(test); println("GOT VELOCITY x={}, y={}", vel.x, vel.y); println("--- Position only system:"); world.query([](const auto &ent, auto &pos) { println("entity={}, pos.x={}, pos.y={}", ent, pos.x, pos.y); }); println("--- Velocity only system:"); world.query([](const auto &, auto &vel) { println("vel.x={}, vel.y={}", vel.x, vel.y); }); println("--- Manually get the velocity in position system:"); world.query([&](const auto &ent, auto &pos) { Velocity &vel = world.get(ent); println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y); }); println("--- Query only entities with Position and Velocity:"); world.query([&](const auto &ent, auto &pos, auto &vel) { Gravity &grav = world.get_the(); 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 world.remove(test); println("--- After remove test, should only result in test2:"); world.query([&](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); }); // to avoid repeatedly getting the player just make a closure with it // QUESTION: could I just capture it and not have the double function wrapping? auto playerVsEnemies = [&]() { auto& player = world.get_the(); // grabbed it world.query([&](const auto &ent, auto &pos) { if(player.eid != ent) { println("{} is enemy attacking player {}", ent, player.name); } else { println("{} is player", player.name); } }); }; playerVsEnemies(); return 0; }