Clean up the DinkyECSso that it's easier to understand and more obvious what a fact vs. component is.
This commit is contained in:
parent
4ed06b10b1
commit
143fe7784c
7 changed files with 82 additions and 105 deletions
|
@ -31,28 +31,28 @@ void configure(DinkyECS::World &world, Entity &test) {
|
|||
println("---Configuring the base system.");
|
||||
Entity test2 = world.entity();
|
||||
|
||||
world.assign<Position>(test, {10,20});
|
||||
world.assign<Velocity>(test, {1,2});
|
||||
world.set<Position>(test, {10,20});
|
||||
world.set<Velocity>(test, {1,2});
|
||||
|
||||
world.assign<Position>(test2, {1,1});
|
||||
world.assign<Velocity>(test2, {10,20});
|
||||
world.set<Position>(test2, {1,1});
|
||||
world.set<Velocity>(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<Player>(player_info);
|
||||
world.set_the<Player>(player_info);
|
||||
|
||||
world.assign<Velocity>(player_eid, {0,0});
|
||||
world.assign<Position>(player_eid, {0,0});
|
||||
world.set<Velocity>(player_eid, {0,0});
|
||||
world.set<Position>(player_eid, {0,0});
|
||||
|
||||
auto enemy = world.entity();
|
||||
world.assign<Velocity>(enemy, {0,0});
|
||||
world.assign<Position>(enemy, {0,0});
|
||||
world.set<Velocity>(enemy, {0,0});
|
||||
world.set<Position>(enemy, {0,0});
|
||||
|
||||
println("--- Creating facts (singletons)");
|
||||
world.set<Gravity>({0.9});
|
||||
world.set_the<Gravity>({0.9});
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
@ -61,31 +61,31 @@ int main() {
|
|||
|
||||
configure(world, test);
|
||||
|
||||
Position &pos = world.component<Position>(test);
|
||||
Position &pos = world.get<Position>(test);
|
||||
println("GOT POS x={}, y={}", pos.x, pos.y);
|
||||
|
||||
Velocity &vel = world.component<Velocity>(test);
|
||||
Velocity &vel = world.get<Velocity>(test);
|
||||
println("GOT VELOCITY x={}, y={}", vel.x, vel.y);
|
||||
|
||||
println("--- Position only system:");
|
||||
world.system<Position>([](const auto &ent, auto &pos) {
|
||||
world.query<Position>([](const auto &ent, auto &pos) {
|
||||
println("entity={}, pos.x={}, pos.y={}", ent, pos.x, pos.y);
|
||||
});
|
||||
|
||||
println("--- Velocity only system:");
|
||||
world.system<Velocity>([](const auto &, auto &vel) {
|
||||
world.query<Velocity>([](const auto &, auto &vel) {
|
||||
println("vel.x={}, vel.y={}", vel.x, vel.y);
|
||||
});
|
||||
|
||||
println("--- Manually get the velocity in position system:");
|
||||
world.system<Position>([&](const auto &ent, auto &pos) {
|
||||
Velocity &vel = world.component<Velocity>(ent);
|
||||
world.query<Position>([&](const auto &ent, auto &pos) {
|
||||
Velocity &vel = world.get<Velocity>(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.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||
Gravity &grav = world.get<Gravity>();
|
||||
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||
Gravity &grav = world.get_the<Gravity>();
|
||||
println("grav={}, entity={}, vel.x, vel.y, pos.x={}, pos.y={}", grav.level, ent, vel.x, vel.y, pos.x, pos.y);
|
||||
});
|
||||
|
||||
|
@ -93,25 +93,15 @@ int main() {
|
|||
world.remove<Velocity>(test);
|
||||
|
||||
println("--- After remove test, should only result in test2:");
|
||||
world.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||
world.query<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 = world.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();
|
||||
|
||||
// how to create an identified entity like the player
|
||||
|
||||
|
||||
// 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<Player>(); // grabbed it
|
||||
world.system<Position>([&](const auto &ent, auto &pos) {
|
||||
auto& player = world.get_the<Player>(); // grabbed it
|
||||
world.query<Position>([&](const auto &ent, auto &pos) {
|
||||
if(player.eid != ent) {
|
||||
println("{} is enemy attacking player {}", ent, player.name);
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue