Finally upgraded the strings to const& since I'm about to pull guecs out and distribute it. Oh well, the joke's finally over.

master
Zed A. Shaw 3 days ago
parent 82ce3cb6be
commit 438bd8ab8a
  1. 1
      .gitignore
  2. 7
      autowalker.cpp
  3. 1
      autowalker.hpp
  4. 11
      dinkyecs.hpp
  5. 32
      guecs.cpp
  6. 44
      guecs.hpp
  7. 21
      main_ui.cpp
  8. 2
      main_ui.hpp

1
.gitignore vendored

@ -27,4 +27,5 @@ backup
*.dll *.dll
*.world *.world
coverage coverage
coverage/*
.venv .venv

@ -61,11 +61,6 @@ Pathing Autowalker::path_to_items() {
return compute_paths<components::InventoryItem>(fsm); return compute_paths<components::InventoryItem>(fsm);
} }
Pathing Autowalker::path_to_devices() {
return compute_paths<components::Device>(fsm);
}
void Autowalker::handle_window_events() { void Autowalker::handle_window_events() {
fsm.$window.handleEvents( fsm.$window.handleEvents(
[&](const sf::Event::KeyPressed &) { [&](const sf::Event::KeyPressed &) {
@ -315,6 +310,8 @@ void Autowalker::autowalk() {
handle_boss_fight(); handle_boss_fight();
handle_player_walk(start, goal); handle_player_walk(start, goal);
if(map_opened_once && move_attempts > 20) send_event(gui::Event::MAP_OPEN);
move_attempts++; move_attempts++;
} while(move_attempts < 100 && fsm.autowalking); } while(move_attempts < 100 && fsm.autowalking);
} }

@ -42,5 +42,4 @@ struct Autowalker {
Pathing path_to_enemies(); Pathing path_to_enemies();
Pathing path_to_items(); Pathing path_to_items();
Pathing path_to_devices();
}; };

@ -203,11 +203,14 @@ namespace DinkyECS
* return pointers (assuming optional can handle pointers) * return pointers (assuming optional can handle pointers)
*/ */
template <typename Comp> template <typename Comp>
std::optional<Comp> get_if(DinkyECS::Entity entity) { Comp* get_if(DinkyECS::Entity entity) {
if(has<Comp>(entity)) { EntityMap &map = entity_map_for<Comp>();
return std::make_optional<Comp>(get<Comp>(entity)); auto &storage = component_storage_for<Comp>();
if(map.contains(entity)) {
auto index = map.at(entity);
return &storage.data[index];
} else { } else {
return std::nullopt; return nullptr;
} }
} }
}; };

@ -22,12 +22,12 @@ namespace guecs {
text->setCharacterSize(size); text->setCharacterSize(size);
} }
void Textual::update(std::wstring& new_content) { void Textual::update(const wstring& new_content) {
content = new_content; content = new_content;
text->setString(content); text->setString(content);
} }
void Sprite::update(const std::string& new_name) { void Sprite::update(const string& new_name) {
if(new_name != name) { if(new_name != name) {
name = new_name; name = new_name;
auto sprite_texture = textures::get(name); auto sprite_texture = textures::get(name);
@ -142,7 +142,7 @@ namespace guecs {
return {float($parser.grid_w), float($parser.grid_h)}; return {float($parser.grid_w), float($parser.grid_h)};
} }
void UI::layout(std::string grid) { void UI::layout(const string& grid) {
$grid = grid; $grid = grid;
bool good = $parser.parse($grid); bool good = $parser.parse($grid);
dbc::check(good, "LEL parsing failed."); dbc::check(good, "LEL parsing failed.");
@ -153,7 +153,7 @@ namespace guecs {
} }
} }
DinkyECS::Entity UI::init_entity(std::string name) { DinkyECS::Entity UI::init_entity(const string& name) {
auto entity = $world.entity(); auto entity = $world.entity();
// this lets you look up an entity by name // this lets you look up an entity by name
$name_ents.insert_or_assign(name, entity); $name_ents.insert_or_assign(name, entity);
@ -162,13 +162,13 @@ namespace guecs {
return entity; return entity;
} }
DinkyECS::Entity UI::entity(std::string name) { DinkyECS::Entity UI::entity(const string& name) {
dbc::check($name_ents.contains(name), dbc::check($name_ents.contains(name),
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name)); fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name));
return $name_ents.at(name); return $name_ents.at(name);
} }
DinkyECS::Entity UI::entity(std::string name, int id) { DinkyECS::Entity UI::entity(const string& name, int id) {
return entity(fmt::format("{}{}", name, id)); return entity(fmt::format("{}{}", name, id));
} }
@ -293,27 +293,31 @@ namespace guecs {
return action_count > 0; return action_count > 0;
} }
void UI::show_sprite(string region, string sprite_name) { void UI::show_sprite(const string& region, const string& sprite_name) {
auto ent = entity(region); auto ent = entity(region);
// BUG: this should have two branches that just update the sprite
if(auto sprite = get_if<Sprite>(ent)) {
sprite->update(sprite_name);
} else {
set_init<Sprite>(ent, {sprite_name}); set_init<Sprite>(ent, {sprite_name});
} }
}
void UI::show_text(string region, wstring content) { void UI::show_text(const string& region, const wstring& content) {
auto ent = entity(region); auto ent = entity(region);
if(auto text = get_if<Textual>(ent)) { if(has<Textual>(ent)) {
text->text->setString(content); auto& text = get<Textual>(ent);
text.text->setString(content);
} else { } else {
auto &cell = cell_for(ent); auto &cell = cell_for(ent);
Textual to_set{content, 20}; Textual to_set{content, 20};
to_set.init(cell, $font); to_set.init(cell, $font);
to_set.text->setFillColor(ColorValue::LIGHT_MID);
set<Textual>(ent, to_set); set<Textual>(ent, to_set);
} }
} }
void UI::click_on(const std::string& name, bool required) { void UI::click_on(const string& name, bool required) {
auto ent = entity(name); auto ent = entity(name);
if(required) { if(required) {
@ -334,7 +338,7 @@ namespace guecs {
} }
} }
void UI::show_label(string region, wstring content) { void UI::show_label(const string& region, const wstring& content) {
auto ent = entity(region); auto ent = entity(region);
if(auto text = get_if<Label>(ent)) { if(auto text = get_if<Label>(ent)) {

@ -26,7 +26,7 @@ namespace guecs {
shared_ptr<sf::Text> text = nullptr; shared_ptr<sf::Text> text = nullptr;
void init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr); void init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr);
void update(std::wstring& new_content); void update(const std::wstring& new_content);
}; };
struct Label : public Textual { struct Label : public Textual {
@ -49,13 +49,13 @@ namespace guecs {
}; };
struct Sprite { struct Sprite {
std::string name; string name;
int padding = GUECS_PADDING; int padding = GUECS_PADDING;
std::shared_ptr<sf::Sprite> sprite = nullptr; std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Texture> texture = nullptr; std::shared_ptr<sf::Texture> texture = nullptr;
void init(lel::Cell &cell); void init(lel::Cell &cell);
void update(const std::string& new_name); void update(const string& new_name);
}; };
struct Rectangle { struct Rectangle {
@ -80,12 +80,12 @@ namespace guecs {
}; };
struct CellName { struct CellName {
std::string name; string name;
}; };
struct Effect { struct Effect {
float duration = 0.1f; float duration = 0.1f;
std::string name{"ui_shader"}; string name{"ui_shader"};
float $u_time_end = 0.0; float $u_time_end = 0.0;
bool $active = false; bool $active = false;
std::shared_ptr<sf::Clock> $clock = nullptr; std::shared_ptr<sf::Clock> $clock = nullptr;
@ -100,7 +100,7 @@ namespace guecs {
}; };
struct Sound { struct Sound {
std::string on_click{"ui_click"}; string on_click{"ui_click"};
void play(bool hover); void play(bool hover);
void stop(bool hover); void stop(bool hover);
}; };
@ -129,20 +129,20 @@ namespace guecs {
class UI { class UI {
public: public:
DinkyECS::World $world; DinkyECS::World $world;
std::unordered_map<std::string, DinkyECS::Entity> $name_ents; std::unordered_map<string, DinkyECS::Entity> $name_ents;
shared_ptr<sf::Font> $font = nullptr; shared_ptr<sf::Font> $font = nullptr;
lel::Parser $parser; lel::Parser $parser;
std::string $grid = ""; string $grid = "";
UI(); UI();
void position(int x, int y, int width, int height); void position(int x, int y, int width, int height);
sf::Vector2f get_position(); sf::Vector2f get_position();
sf::Vector2f get_size(); sf::Vector2f get_size();
void layout(std::string grid); void layout(const string& grid);
DinkyECS::Entity init_entity(std::string name); DinkyECS::Entity init_entity(const string& name);
DinkyECS::Entity entity(std::string name); DinkyECS::Entity entity(const string& name);
DinkyECS::Entity entity(std::string name, int id); DinkyECS::Entity entity(const string& name, int id);
inline lel::CellMap& cells() { inline lel::CellMap& cells() {
return $parser.cells; return $parser.cells;
@ -155,7 +155,7 @@ namespace guecs {
void init(); void init();
void render(sf::RenderWindow& window); void render(sf::RenderWindow& window);
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, bool hover);
void click_on(const std::string& name, bool required=false); void click_on(const string& name, bool required=false);
void click_on(DinkyECS::Entity slot_id); void click_on(DinkyECS::Entity slot_id);
void debug_layout(sf::RenderWindow& window); void debug_layout(sf::RenderWindow& window);
@ -183,7 +183,7 @@ namespace guecs {
return $world.get<lel::Cell>(ent); return $world.get<lel::Cell>(ent);
} }
lel::Cell& cell_for(std::string name) { lel::Cell& cell_for(const string& name) {
DinkyECS::Entity ent = entity(name); DinkyECS::Entity ent = entity(name);
return $world.get<lel::Cell>(ent); return $world.get<lel::Cell>(ent);
} }
@ -194,7 +194,7 @@ namespace guecs {
} }
template <typename Comp> template <typename Comp>
std::optional<Comp> get_if(DinkyECS::Entity entity) { Comp* get_if(DinkyECS::Entity entity) {
return $world.get_if<Comp>(entity); return $world.get_if<Comp>(entity);
} }
@ -221,11 +221,9 @@ namespace guecs {
void render_helper(sf::RenderWindow& window, DinkyECS::Entity ent, bool is_shape, T& target) { void render_helper(sf::RenderWindow& window, DinkyECS::Entity ent, bool is_shape, T& target) {
sf::Shader *shader_ptr = nullptr; sf::Shader *shader_ptr = nullptr;
if($world.has<Effect>(ent)) { if(auto shader = $world.get_if<Effect>(ent)) {
auto& shader = $world.get<Effect>(ent); if(shader->$active && !is_shape) {
auto ptr = shader->checkout_ptr();
if(shader.$active && !is_shape) {
auto ptr = shader.checkout_ptr();
ptr->setUniform("is_shape", is_shape); ptr->setUniform("is_shape", is_shape);
// NOTE: this is needed because SFML doesn't handle shared_ptr // NOTE: this is needed because SFML doesn't handle shared_ptr
shader_ptr = ptr.get(); shader_ptr = ptr.get();
@ -235,9 +233,9 @@ namespace guecs {
window.draw(*target, shader_ptr); window.draw(*target, shader_ptr);
} }
void show_sprite(string region, string sprite_name); void show_sprite(const string& region, const string& sprite_name);
void show_text(string region, wstring content); void show_text(const string& region, const wstring& content);
void show_label(string region, wstring content); void show_label(const string& region, const wstring& content);
}; };
Clickable make_action(DinkyECS::World& target, Events::GUI event); Clickable make_action(DinkyECS::World& target, Events::GUI event);

@ -37,10 +37,6 @@ namespace gui {
$overlay_ui.init(); $overlay_ui.init();
} }
void MainUI::show_level() {
$show_level = true;
}
void MainUI::render() { void MainUI::render() {
auto aimed_at = $camera.aimed_at(); auto aimed_at = $camera.aimed_at();
if($level.collision->occupied(aimed_at)) { if($level.collision->occupied(aimed_at)) {
@ -49,19 +45,8 @@ namespace gui {
$rayview.aiming_at = 0; $rayview.aiming_at = 0;
} }
if($show_level) {
auto time = $clock.getElapsedTime();
auto st = textures::get("down_the_well");
float tick = ease::in_out_back(ease::sine(time.asSeconds()));
float scale = std::lerp(1.0, 1.3, tick);
st.sprite->setScale({scale, scale});
$window.draw(*st.sprite);
$overlay_ui.show_label("middle", L"INTO THE WELL YOU GO...");
} else {
if($needs_render) $rayview.render(); if($needs_render) $rayview.render();
$rayview.draw($window); $rayview.draw($window);
}
$overlay_ui.render($window); $overlay_ui.render($window);
} }
@ -125,12 +110,6 @@ namespace gui {
} }
void MainUI::mouse(int x, int y, bool hover) { void MainUI::mouse(int x, int y, bool hover) {
if($show_level) {
$show_level = false;
$level.world->send<Events::GUI>(Events::GUI::STAIRS_DOWN, $level.player, {});
$overlay_ui.close_label("middle");
} else {
$overlay_ui.$gui.mouse(x, y, hover); $overlay_ui.$gui.mouse(x, y, hover);
} }
} }
}

@ -15,7 +15,6 @@ namespace gui {
class MainUI { class MainUI {
public: public:
int $compass_dir = 0; int $compass_dir = 0;
bool $show_level = false;
bool $needs_render = true; bool $needs_render = true;
sf::Clock $clock; sf::Clock $clock;
sf::RenderWindow& $window; sf::RenderWindow& $window;
@ -41,7 +40,6 @@ namespace gui {
void render(); void render();
void dirty(); void dirty();
void show_level();
void health_low(); void health_low();
void dead_entity(DinkyECS::Entity entity); void dead_entity(DinkyECS::Entity entity);
}; };

Loading…
Cancel
Save