|
|
|
@ -7,15 +7,6 @@ |
|
|
|
|
#include <any> |
|
|
|
|
|
|
|
|
|
using namespace fmt; |
|
|
|
|
|
|
|
|
|
struct Position { |
|
|
|
|
double x, y; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct Velocity { |
|
|
|
|
double x, y; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
typedef unsigned long Entity; |
|
|
|
|
|
|
|
|
|
typedef std::unordered_map<Entity, std::any> EntityMap; |
|
|
|
@ -23,6 +14,7 @@ typedef std::unordered_map<Entity, std::any> EntityMap; |
|
|
|
|
struct World { |
|
|
|
|
unsigned long entity_count = 0; |
|
|
|
|
std::unordered_map<std::type_index, EntityMap> $components; |
|
|
|
|
std::unordered_map<std::type_index, std::any> $facts; |
|
|
|
|
|
|
|
|
|
Entity entity() { |
|
|
|
|
return ++entity_count; |
|
|
|
@ -33,6 +25,18 @@ struct World { |
|
|
|
|
return $components[std::type_index(typeid(Comp))]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Comp> |
|
|
|
|
void set(Comp val) { |
|
|
|
|
$facts[std::type_index(typeid(Comp))] = val; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Comp> |
|
|
|
|
Comp &get() { |
|
|
|
|
// use .at to get std::out_of_range if fact not set
|
|
|
|
|
std::any &res = $facts.at(std::type_index(typeid(Comp))); |
|
|
|
|
return std::any_cast<Comp&>(res); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Comp> |
|
|
|
|
void assign(Entity ent, Comp val) { |
|
|
|
|
EntityMap &map = entity_map_for<Comp>(); |
|
|
|
@ -42,7 +46,8 @@ struct World { |
|
|
|
|
template <typename Comp> |
|
|
|
|
Comp &component(Entity ent) { |
|
|
|
|
EntityMap &map = entity_map_for<Comp>(); |
|
|
|
|
std::any &res = map[ent]; |
|
|
|
|
// use .at for bounds checking
|
|
|
|
|
std::any &res = map.at(ent); |
|
|
|
|
return std::any_cast<Comp&>(res); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -74,6 +79,25 @@ struct World { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<typename CompA, typename CompB> |
|
|
|
|
std::function<void()> runner(std::function<void(const Entity&, CompA&, CompB&)> cb) { |
|
|
|
|
return [&]{ |
|
|
|
|
system<CompA, CompB>(cb); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct Position { |
|
|
|
|
double x, y; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct Velocity { |
|
|
|
|
double x, y; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct Gravity { |
|
|
|
|
double level; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
int main() { |
|
|
|
@ -110,9 +134,13 @@ int main() { |
|
|
|
|
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) { |
|
|
|
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y); |
|
|
|
|
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
|
|
|
|
@ -123,5 +151,12 @@ int main() { |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|