Finally upgraded the strings to const& since I'm about to pull guecs out and distribute it. Oh well, the joke's finally over.
This commit is contained in:
parent
82ce3cb6be
commit
438bd8ab8a
8 changed files with 53 additions and 74 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -27,4 +27,5 @@ backup
|
|||
*.dll
|
||||
*.world
|
||||
coverage
|
||||
coverage/*
|
||||
.venv
|
||||
|
|
|
@ -61,11 +61,6 @@ Pathing Autowalker::path_to_items() {
|
|||
return compute_paths<components::InventoryItem>(fsm);
|
||||
}
|
||||
|
||||
Pathing Autowalker::path_to_devices() {
|
||||
return compute_paths<components::Device>(fsm);
|
||||
}
|
||||
|
||||
|
||||
void Autowalker::handle_window_events() {
|
||||
fsm.$window.handleEvents(
|
||||
[&](const sf::Event::KeyPressed &) {
|
||||
|
@ -315,6 +310,8 @@ void Autowalker::autowalk() {
|
|||
handle_boss_fight();
|
||||
handle_player_walk(start, goal);
|
||||
|
||||
if(map_opened_once && move_attempts > 20) send_event(gui::Event::MAP_OPEN);
|
||||
|
||||
move_attempts++;
|
||||
} while(move_attempts < 100 && fsm.autowalking);
|
||||
}
|
||||
|
|
|
@ -42,5 +42,4 @@ struct Autowalker {
|
|||
|
||||
Pathing path_to_enemies();
|
||||
Pathing path_to_items();
|
||||
Pathing path_to_devices();
|
||||
};
|
||||
|
|
11
dinkyecs.hpp
11
dinkyecs.hpp
|
@ -203,11 +203,14 @@ namespace DinkyECS
|
|||
* return pointers (assuming optional can handle pointers)
|
||||
*/
|
||||
template <typename Comp>
|
||||
std::optional<Comp> get_if(DinkyECS::Entity entity) {
|
||||
if(has<Comp>(entity)) {
|
||||
return std::make_optional<Comp>(get<Comp>(entity));
|
||||
Comp* get_if(DinkyECS::Entity entity) {
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
auto &storage = component_storage_for<Comp>();
|
||||
if(map.contains(entity)) {
|
||||
auto index = map.at(entity);
|
||||
return &storage.data[index];
|
||||
} else {
|
||||
return std::nullopt;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
34
guecs.cpp
34
guecs.cpp
|
@ -22,12 +22,12 @@ namespace guecs {
|
|||
text->setCharacterSize(size);
|
||||
}
|
||||
|
||||
void Textual::update(std::wstring& new_content) {
|
||||
void Textual::update(const wstring& new_content) {
|
||||
content = new_content;
|
||||
text->setString(content);
|
||||
}
|
||||
|
||||
void Sprite::update(const std::string& new_name) {
|
||||
void Sprite::update(const string& new_name) {
|
||||
if(new_name != name) {
|
||||
name = new_name;
|
||||
auto sprite_texture = textures::get(name);
|
||||
|
@ -142,7 +142,7 @@ namespace guecs {
|
|||
return {float($parser.grid_w), float($parser.grid_h)};
|
||||
}
|
||||
|
||||
void UI::layout(std::string grid) {
|
||||
void UI::layout(const string& grid) {
|
||||
$grid = grid;
|
||||
bool good = $parser.parse($grid);
|
||||
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();
|
||||
// this lets you look up an entity by name
|
||||
$name_ents.insert_or_assign(name, entity);
|
||||
|
@ -162,13 +162,13 @@ namespace guecs {
|
|||
return entity;
|
||||
}
|
||||
|
||||
DinkyECS::Entity UI::entity(std::string name) {
|
||||
DinkyECS::Entity UI::entity(const string& name) {
|
||||
dbc::check($name_ents.contains(name),
|
||||
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", 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));
|
||||
}
|
||||
|
||||
|
@ -293,27 +293,31 @@ namespace guecs {
|
|||
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);
|
||||
// BUG: this should have two branches that just update the sprite
|
||||
set_init<Sprite>(ent, {sprite_name});
|
||||
|
||||
if(auto sprite = get_if<Sprite>(ent)) {
|
||||
sprite->update(sprite_name);
|
||||
} else {
|
||||
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);
|
||||
|
||||
if(auto text = get_if<Textual>(ent)) {
|
||||
text->text->setString(content);
|
||||
if(has<Textual>(ent)) {
|
||||
auto& text = get<Textual>(ent);
|
||||
text.text->setString(content);
|
||||
} else {
|
||||
auto &cell = cell_for(ent);
|
||||
Textual to_set{content, 20};
|
||||
to_set.init(cell, $font);
|
||||
to_set.text->setFillColor(ColorValue::LIGHT_MID);
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
if(auto text = get_if<Label>(ent)) {
|
||||
|
|
44
guecs.hpp
44
guecs.hpp
|
@ -26,7 +26,7 @@ namespace guecs {
|
|||
shared_ptr<sf::Text> text = nullptr;
|
||||
|
||||
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 {
|
||||
|
@ -49,13 +49,13 @@ namespace guecs {
|
|||
};
|
||||
|
||||
struct Sprite {
|
||||
std::string name;
|
||||
string name;
|
||||
int padding = GUECS_PADDING;
|
||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||
std::shared_ptr<sf::Texture> texture = nullptr;
|
||||
|
||||
void init(lel::Cell &cell);
|
||||
void update(const std::string& new_name);
|
||||
void update(const string& new_name);
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
|
@ -80,12 +80,12 @@ namespace guecs {
|
|||
};
|
||||
|
||||
struct CellName {
|
||||
std::string name;
|
||||
string name;
|
||||
};
|
||||
|
||||
struct Effect {
|
||||
float duration = 0.1f;
|
||||
std::string name{"ui_shader"};
|
||||
string name{"ui_shader"};
|
||||
float $u_time_end = 0.0;
|
||||
bool $active = false;
|
||||
std::shared_ptr<sf::Clock> $clock = nullptr;
|
||||
|
@ -100,7 +100,7 @@ namespace guecs {
|
|||
};
|
||||
|
||||
struct Sound {
|
||||
std::string on_click{"ui_click"};
|
||||
string on_click{"ui_click"};
|
||||
void play(bool hover);
|
||||
void stop(bool hover);
|
||||
};
|
||||
|
@ -129,20 +129,20 @@ namespace guecs {
|
|||
class UI {
|
||||
public:
|
||||
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;
|
||||
lel::Parser $parser;
|
||||
std::string $grid = "";
|
||||
string $grid = "";
|
||||
|
||||
UI();
|
||||
|
||||
void position(int x, int y, int width, int height);
|
||||
sf::Vector2f get_position();
|
||||
sf::Vector2f get_size();
|
||||
void layout(std::string grid);
|
||||
DinkyECS::Entity init_entity(std::string name);
|
||||
DinkyECS::Entity entity(std::string name);
|
||||
DinkyECS::Entity entity(std::string name, int id);
|
||||
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);
|
||||
|
||||
inline lel::CellMap& cells() {
|
||||
return $parser.cells;
|
||||
|
@ -155,7 +155,7 @@ namespace guecs {
|
|||
void init();
|
||||
void render(sf::RenderWindow& window);
|
||||
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 debug_layout(sf::RenderWindow& window);
|
||||
|
||||
|
@ -183,7 +183,7 @@ namespace guecs {
|
|||
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);
|
||||
return $world.get<lel::Cell>(ent);
|
||||
}
|
||||
|
@ -194,7 +194,7 @@ namespace guecs {
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
std::optional<Comp> get_if(DinkyECS::Entity entity) {
|
||||
Comp* get_if(DinkyECS::Entity 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) {
|
||||
sf::Shader *shader_ptr = nullptr;
|
||||
|
||||
if($world.has<Effect>(ent)) {
|
||||
auto& shader = $world.get<Effect>(ent);
|
||||
|
||||
if(shader.$active && !is_shape) {
|
||||
auto ptr = shader.checkout_ptr();
|
||||
if(auto shader = $world.get_if<Effect>(ent)) {
|
||||
if(shader->$active && !is_shape) {
|
||||
auto ptr = shader->checkout_ptr();
|
||||
ptr->setUniform("is_shape", is_shape);
|
||||
// NOTE: this is needed because SFML doesn't handle shared_ptr
|
||||
shader_ptr = ptr.get();
|
||||
|
@ -235,9 +233,9 @@ namespace guecs {
|
|||
window.draw(*target, shader_ptr);
|
||||
}
|
||||
|
||||
void show_sprite(string region, string sprite_name);
|
||||
void show_text(string region, wstring content);
|
||||
void show_label(string region, wstring content);
|
||||
void show_sprite(const string& region, const string& sprite_name);
|
||||
void show_text(const string& region, const wstring& content);
|
||||
void show_label(const string& region, const wstring& content);
|
||||
};
|
||||
|
||||
Clickable make_action(DinkyECS::World& target, Events::GUI event);
|
||||
|
|
27
main_ui.cpp
27
main_ui.cpp
|
@ -37,10 +37,6 @@ namespace gui {
|
|||
$overlay_ui.init();
|
||||
}
|
||||
|
||||
void MainUI::show_level() {
|
||||
$show_level = true;
|
||||
}
|
||||
|
||||
void MainUI::render() {
|
||||
auto aimed_at = $camera.aimed_at();
|
||||
if($level.collision->occupied(aimed_at)) {
|
||||
|
@ -49,19 +45,8 @@ namespace gui {
|
|||
$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();
|
||||
$rayview.draw($window);
|
||||
}
|
||||
if($needs_render) $rayview.render();
|
||||
$rayview.draw($window);
|
||||
|
||||
$overlay_ui.render($window);
|
||||
}
|
||||
|
@ -125,12 +110,6 @@ namespace gui {
|
|||
}
|
||||
|
||||
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 {
|
||||
public:
|
||||
int $compass_dir = 0;
|
||||
bool $show_level = false;
|
||||
bool $needs_render = true;
|
||||
sf::Clock $clock;
|
||||
sf::RenderWindow& $window;
|
||||
|
@ -41,7 +40,6 @@ namespace gui {
|
|||
void render();
|
||||
void dirty();
|
||||
|
||||
void show_level();
|
||||
void health_low();
|
||||
void dead_entity(DinkyECS::Entity entity);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue