DinkyECS is now controlling the game and can handle multiple enemies easily. Next is to clean this up so it's not just one gross pile of code in the gui.
This commit is contained in:
parent
86c98c43c2
commit
33327154ad
9 changed files with 195 additions and 149 deletions
|
@ -4,7 +4,12 @@
|
|||
#include <fmt/core.h>
|
||||
|
||||
using namespace fmt;
|
||||
using DinkyECS::Entity, DinkyECS::World;
|
||||
using DinkyECS::Entity;
|
||||
|
||||
struct Player {
|
||||
std::string name;
|
||||
Entity eid;
|
||||
};
|
||||
|
||||
struct Position {
|
||||
double x, y;
|
||||
|
@ -18,63 +23,104 @@ 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.assign<Position>(test, {10,20});
|
||||
world.assign<Velocity>(test, {1,2});
|
||||
|
||||
world.assign<Position>(test2, {1,1});
|
||||
world.assign<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.assign<Velocity>(player_eid, {0,0});
|
||||
world.assign<Position>(player_eid, {0,0});
|
||||
|
||||
auto enemy = world.entity();
|
||||
world.assign<Velocity>(enemy, {0,0});
|
||||
world.assign<Position>(enemy, {0,0});
|
||||
|
||||
println("--- Creating facts (singletons)");
|
||||
world.set<Gravity>({0.9});
|
||||
}
|
||||
|
||||
int main() {
|
||||
World me;
|
||||
DinkyECS::World world;
|
||||
Entity test = world.entity();
|
||||
|
||||
Entity test = me.entity();
|
||||
Entity test2 = me.entity();
|
||||
configure(world, test);
|
||||
|
||||
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);
|
||||
Position &pos = world.component<Position>(test);
|
||||
println("GOT POS x={}, y={}", pos.x, pos.y);
|
||||
|
||||
Velocity &vel = me.component<Velocity>(test);
|
||||
Velocity &vel = world.component<Velocity>(test);
|
||||
println("GOT VELOCITY x={}, y={}", vel.x, vel.y);
|
||||
|
||||
println("--- Position only system:");
|
||||
me.system<Position>([](const auto &ent, auto &pos) {
|
||||
world.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) {
|
||||
world.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);
|
||||
world.system<Position>([&](const auto &ent, auto &pos) {
|
||||
Velocity &vel = world.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>();
|
||||
world.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||
Gravity &grav = world.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);
|
||||
world.remove<Velocity>(test);
|
||||
|
||||
println("--- After remove test, should only result in test2:");
|
||||
me.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||
world.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) {
|
||||
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) {
|
||||
if(player.eid != ent) {
|
||||
println("{} is enemy attacking player {}", ent, player.name);
|
||||
} else {
|
||||
println("{} is player", player.name);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
playerVsEnemies();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue