From b9c27cd6baf8759adad85d494fe6180efe231448 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 10 Oct 2024 23:20:00 -0400 Subject: [PATCH] Now calling this DinkyECS and will use it in the game to move the enemies and such. --- dinkyecs.hpp | 92 ++++++++++++++++++++++++++++++++++++++++ scratchpad/myecstest.cpp | 88 ++------------------------------------ 2 files changed, 95 insertions(+), 85 deletions(-) create mode 100644 dinkyecs.hpp diff --git a/dinkyecs.hpp b/dinkyecs.hpp new file mode 100644 index 0000000..3f7441d --- /dev/null +++ b/dinkyecs.hpp @@ -0,0 +1,92 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace DinkyECS { + + typedef unsigned long Entity; + + typedef std::unordered_map EntityMap; + + struct World { + unsigned long entity_count = 0; + std::unordered_map $components; + std::unordered_map $facts; + + Entity entity() { + return ++entity_count; + } + + template + EntityMap& entity_map_for() { + return $components[std::type_index(typeid(Comp))]; + } + + template + void set(Comp val) { + $facts[std::type_index(typeid(Comp))] = val; + } + + template + 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(res); + } + + template + void assign(Entity ent, Comp val) { + EntityMap &map = entity_map_for(); + map[ent] = val; + } + + template + Comp &component(Entity ent) { + EntityMap &map = entity_map_for(); + // use .at for bounds checking + std::any &res = map.at(ent); + return std::any_cast(res); + } + + template + void system(std::function cb) { + EntityMap &map = entity_map_for(); + for(auto& [entity, any_comp] : map) { + Comp &res = std::any_cast(any_comp); + cb(entity, res); + } + } + + template + void remove(Entity ent) { + EntityMap &map = entity_map_for(); + map.erase(ent); + } + + template + void system(std::function cb) { + EntityMap &map_a = entity_map_for(); + EntityMap &map_b = entity_map_for(); + + for(auto& [entity, any_a] : map_a) { + if(map_b.contains(entity)) { + CompA &res_a = std::any_cast(any_a); + CompB &res_b = component(entity); + cb(entity, res_a, res_b); + } + } + } + + template + std::function runner(std::function cb) { + return [&]{ + system(cb); + }; + } + }; + +} diff --git a/scratchpad/myecstest.cpp b/scratchpad/myecstest.cpp index 6a0701a..78b4059 100644 --- a/scratchpad/myecstest.cpp +++ b/scratchpad/myecstest.cpp @@ -1,92 +1,10 @@ +#include "dinkyecs.hpp" + #include -#include -#include -#include -#include #include -#include using namespace fmt; -typedef unsigned long Entity; - -typedef std::unordered_map EntityMap; - -struct World { - unsigned long entity_count = 0; - std::unordered_map $components; - std::unordered_map $facts; - - Entity entity() { - return ++entity_count; - } - - template - EntityMap& entity_map_for() { - return $components[std::type_index(typeid(Comp))]; - } - - template - void set(Comp val) { - $facts[std::type_index(typeid(Comp))] = val; - } - - template - 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(res); - } - - template - void assign(Entity ent, Comp val) { - EntityMap &map = entity_map_for(); - map[ent] = val; - } - - template - Comp &component(Entity ent) { - EntityMap &map = entity_map_for(); - // use .at for bounds checking - std::any &res = map.at(ent); - return std::any_cast(res); - } - - template - void system(std::function cb) { - EntityMap &map = entity_map_for(); - for(auto& [entity, any_comp] : map) { - Comp &res = std::any_cast(any_comp); - cb(entity, res); - } - } - - template - void remove(Entity ent) { - EntityMap &map = entity_map_for(); - map.erase(ent); - } - - template - void system(std::function cb) { - EntityMap &map_a = entity_map_for(); - EntityMap &map_b = entity_map_for(); - - for(auto& [entity, any_a] : map_a) { - if(map_b.contains(entity)) { - CompA &res_a = std::any_cast(any_a); - CompB &res_b = component(entity); - cb(entity, res_a, res_b); - } - } - } - - template - std::function runner(std::function cb) { - return [&]{ - system(cb); - }; - } -}; +using DinkyECS::Entity, DinkyECS::World; struct Position { double x, y;