From 5186351345d43801ac45acde8b7e30734eea0349 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Tue, 7 Nov 2017 18:33:03 +0100 Subject: Better unit say. --- src/common.hpp | 14 ++++++++++---- src/game.cpp | 38 +++++++++++++++++++++++++++----------- src/main.cpp | 2 +- src/math.hpp | 11 +++++++++++ src/path_finder.cpp | 2 +- src/render.cpp | 26 ++++++++++++++++++++++++++ src/text.cpp | 19 +++++++++++++++---- 7 files changed, 91 insertions(+), 21 deletions(-) diff --git a/src/common.hpp b/src/common.hpp index 043323a..44493eb 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -175,13 +175,14 @@ namespace game { class unit_t; class state_t { + double now; std::unordered_set selected_units; public: world::world_t world; void start(void); - void tick(double now); + void tick(double now_); // These are called by the interface. void select(rectf_t rect); @@ -250,13 +251,17 @@ namespace render { void load(std::string prefix, size_t xc, size_t yc); }; + typedef enum { + ALIGN_CENTER_BOTTOM + } text_align_t; + class state_t { sf::RenderWindow *window; - double now; void drender_text(rectf_t rect, std::string str); void drender_entity(world::entity_t *ent); public: + double now; state_t(sf::RenderWindow *window_); void begin_frame(double time_); @@ -265,6 +270,7 @@ namespace render { void render(animated_texture_t *anim, rectf_t bounds, bool mirror = false); void render(oriented_sprite_t *sprite, rectf_t bounds, float angle); + void render_text(v2f_t x, float height, const wchar_t *wstr, text_align_t align, sf::Color color); void render_hlrect(rectf_t rect, sf::Color color); void debug_path(std::list *path); @@ -285,8 +291,8 @@ namespace assets { }; namespace text { - extern std::string unit_no_path; - extern std::string unit_blocked; + extern const wchar_t *unit_no_path; + extern const wchar_t *unit_blocked; void load_strings(std::string lang); } diff --git a/src/game.cpp b/src/game.cpp index 48047a4..5dc9af8 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -50,10 +50,14 @@ public: float next_attempt; } move; - void say(std::string str) + const wchar_t *say_text; + double say_time = -INFINITY; + + void say(const wchar_t *wstr, double now) { - //TODO - std::cout << (void*)this << ": " << str << "\n"; + say_text = wstr; + say_time = now; + std::cout << (void*)this << ": " << wstr << "\n"; } void place(world::world_t *world_, v2f_t x_) @@ -116,7 +120,7 @@ public: move.next_attempt = now + 0.5f; } else { if ((x - move.dst).len() > 1.5f) - say(text::unit_blocked); + say(text::unit_blocked, now); move.moving = false; } break; @@ -128,7 +132,7 @@ public: link(world); } - bool start_moving(v2f_t dst_) + bool start_moving(v2f_t dst_, double now) { if (!world) { printf("unit_t::start_moving: entity is not linked\n"); @@ -139,7 +143,7 @@ public: move.path.clear(); if (!world->find_path(x, move.dst, &cmodel, this, &move.path)) { - say(text::unit_no_path); + say(text::unit_no_path, now); move.moving = false; return false; } @@ -174,6 +178,17 @@ public: if (move.moving && debug_draw_paths) render->debug_path(&move.path); + + if (say_time + 5.0 > render->now) { + v2f_t text_pos; + float height; + + text_pos = render_bounds[0] + v2f_t(render_bounds.dim(0) / 2, -render_bounds.dim(1) * 0.1); + height = size.dim_min() * 0.20f; + render->render_text(text_pos, height, say_text, + render::ALIGN_CENTER_BOTTOM, + sf::Color::White); + } } }; @@ -199,7 +214,7 @@ void state_t::select(rectf_t x) selection_cookie++; selected_units.clear(); - for (world::entity_t *ent : world.get_render_entities(x, -1)) { + for (world::entity_t *ent : world.get_render_entities(x)) { unit_t *unit; if (ent->type != ET_UNIT) @@ -214,13 +229,14 @@ void state_t::select(rectf_t x) void state_t::command(v2f_t x) { for (unit_t *unit : selected_units) - unit->start_moving(x); + unit->start_moving(x, now); } -void state_t::tick(double now) +void state_t::tick(double now_) { - human.keep_moving(now); - human2.keep_moving(now); + now = now_; + human.keep_moving(now_); + human2.keep_moving(now_); } } //namespace game diff --git a/src/main.cpp b/src/main.cpp index 8b3eef7..57c923d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ int main() window.setVerticalSyncEnabled(true); - text::load_strings("en"); + text::load_strings("pl"); assets::load(); game.start(); diff --git a/src/math.hpp b/src/math.hpp index 8c93a76..2957d00 100644 --- a/src/math.hpp +++ b/src/math.hpp @@ -275,6 +275,17 @@ public: return v[1][i] - v[0][i]; } + T dim_min(void) const + { + T min = INFINITY; + + for (size_t i = 0; i < N; i++) + if (dim(i) < min) + min = dim(i); + + return min; + } + rect_t norm(void) const { rect_t r; diff --git a/src/path_finder.cpp b/src/path_finder.cpp index 748a72f..15b1614 100644 --- a/src/path_finder.cpp +++ b/src/path_finder.cpp @@ -168,7 +168,7 @@ bool path_finder_t::find(void) dist_simple = (src - dst).len(); - if (dist_simple < 1.0f) + if (dist_simple < 0.5f) return true; start = tile_index_at(src) - base; diff --git a/src/render.cpp b/src/render.cpp index 80ed2b6..259d898 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -181,6 +181,32 @@ void state_t::render(oriented_sprite_t *sprite, rectf_t bounds, float angle) render(sprite->textures + index, bounds, mirror); } +void state_t::render_text(v2f_t x, float height, const wchar_t *wstr, + text_align_t align, sf::Color color) +{ + sf::Text text(wstr, font, 20); + sf::FloatRect rect; + float scale; + v2f_t offset; + + rect = text.getGlobalBounds(); + scale = height / 20.0f; + + switch (align) { + case ALIGN_CENTER_BOTTOM: + offset[0] = -rect.width / 2; + offset[1] = -rect.height; + break; + } + + offset *= scale; + + text.setScale(scale, scale); + text.setPosition(x + offset); + text.setFillColor(color); + window->draw(text); +} + void state_t::render_hlrect(rectf_t rect, sf::Color color) { sf::Color fill; diff --git a/src/text.cpp b/src/text.cpp index baeabcd..b689c99 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -2,13 +2,24 @@ namespace text { -std::string unit_no_path; -std::string unit_blocked; +const wchar_t *unit_no_path; +const wchar_t *unit_blocked; + +static void load_strings_pl(void) +{ + unit_no_path = L"Nie wiem, jak się tam dostać."; + unit_blocked = L"Coś stoi na mojej drodze."; +} void load_strings(std::string lang) { - unit_no_path = "Cannot find a path."; - unit_blocked = "The path is blocked."; + if (lang == "pl") { + load_strings_pl(); + return; + } + + unit_no_path = L"I don't know how to get there."; + unit_blocked = L"Something's in my way."; } } -- cgit