GUECS refactor part 1.

This commit is contained in:
Zed A. Shaw 2025-05-04 11:06:53 -04:00
parent 4e7f837240
commit 20176cf54a
2 changed files with 108 additions and 60 deletions

110
guecs.hpp
View file

@ -16,6 +16,16 @@
namespace guecs {
using std::shared_ptr, std::make_shared, std::wstring, std::string;
using Entity = DinkyECS::Entity;
using EntityMap = std::unordered_map<Entity, size_t>;
template <typename T>
struct ComponentStorage {
std::vector<T> data;
std::queue<size_t> free_indices;
};
struct Textual {
std::wstring content;
unsigned int size = GUECS_FONT_SIZE;
@ -45,7 +55,7 @@ namespace guecs {
/* This is actually called by UI::mouse and passed the entity ID of the
* button pressed so you can interact with it in the event handler.
*/
std::function<void(DinkyECS::Entity ent, std::any data)> action;
std::function<void(Entity ent, std::any data)> action;
};
struct Sprite {
@ -127,10 +137,16 @@ namespace guecs {
void init();
};
class UI {
public:
DinkyECS::World $world;
std::unordered_map<string, DinkyECS::Entity> $name_ents;
unsigned long entity_count = 0;
std::unordered_map<std::type_index, EntityMap> $components;
std::unordered_map<std::type_index, std::any> $facts;
std::unordered_map<std::type_index, std::any> $component_storages;
std::unordered_map<string, Entity> $name_ents;
shared_ptr<sf::Font> $font = nullptr;
lel::Parser $parser;
string $grid = "";
@ -141,9 +157,9 @@ namespace guecs {
sf::Vector2f get_position();
sf::Vector2f get_size();
void layout(const string& grid);
DinkyECS::Entity init_entity(const string& name);
DinkyECS::Entity entity(const string& name);
DinkyECS::Entity entity(const string& name, int id);
Entity init_entity(const string& name);
Entity entity(const string& name);
Entity entity(const string& name, int id);
inline lel::CellMap& cells() {
return $parser.cells;
@ -157,16 +173,67 @@ namespace guecs {
void render(sf::RenderWindow& window);
bool mouse(float x, float y, bool hover);
void click_on(const string& name, bool required=false);
void click_on(DinkyECS::Entity slot_id);
void click_on(Entity slot_id);
void debug_layout(sf::RenderWindow& window);
Entity entity() { return $world.entity(); }
template <typename Comp>
bool has_the() {
return $world.has_the<Comp>();
}
template <typename Comp>
void set_the(Comp val) {
return $world.set_the<Comp>(val);
}
template <typename Comp>
Comp &get_the() {
return $world.get_the<Comp>();
}
template <typename Comp>
void set(DinkyECS::Entity ent, Comp val) {
void set(Entity ent, Comp val) {
$world.set<Comp>(ent, val);
}
template <typename Comp>
void set_init(DinkyECS::Entity ent, Comp val) {
Comp& get(Entity entity) {
return $world.get<Comp>(entity);
}
template <typename Comp>
Comp* get_if(Entity entity) {
return $world.get_if<Comp>(entity);
}
template <typename Comp>
bool has(Entity entity) {
return $world.has<Comp>(entity);
}
template <typename Comp>
void remove(Entity ent) {
$world.remove<Comp>(ent);
}
template <typename Comp>
void query(std::function<void(Entity, Comp &)> cb) {
$world.query<Comp>(cb);
}
template <typename CompA, typename CompB>
void query(std::function<void(Entity, CompA &, CompB &)> cb) {
$world.query<CompA, CompB>(cb);
}
template <typename Comp>
void set_init(Entity ent, Comp val) {
dbc::check(has<lel::Cell>(ent),"WRONG! slot is missing its cell?!");
auto& cell = get<lel::Cell>(ent);
val.init(cell);
@ -174,40 +241,21 @@ namespace guecs {
}
template <typename Comp>
void do_if(DinkyECS::Entity ent, std::function<void(Comp &)> cb) {
void do_if(Entity ent, std::function<void(Comp &)> cb) {
if($world.has<Comp>(ent)) {
cb($world.get<Comp>(ent));
}
}
lel::Cell& cell_for(DinkyECS::Entity ent) {
lel::Cell& cell_for(Entity ent) {
return $world.get<lel::Cell>(ent);
}
lel::Cell& cell_for(const string& name) {
DinkyECS::Entity ent = entity(name);
Entity ent = entity(name);
return $world.get<lel::Cell>(ent);
}
template <typename Comp>
Comp& get(DinkyECS::Entity entity) {
return $world.get<Comp>(entity);
}
template <typename Comp>
Comp* get_if(DinkyECS::Entity entity) {
return $world.get_if<Comp>(entity);
}
template <typename Comp>
bool has(DinkyECS::Entity entity) {
return $world.has<Comp>(entity);
}
template <typename Comp>
void remove(DinkyECS::Entity ent) {
$world.remove<Comp>(ent);
}
// BUG: close could just be remove with overload
template <typename Comp>
@ -219,7 +267,7 @@ namespace guecs {
}
template<typename T>
void render_helper(sf::RenderWindow& window, DinkyECS::Entity ent, bool is_shape, T& target) {
void render_helper(sf::RenderWindow& window, Entity ent, bool is_shape, T& target) {
sf::Shader *shader_ptr = nullptr;
if(auto shader = $world.get_if<Effect>(ent)) {