diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-12-13 20:33:57 +0100 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-12-13 20:33:57 +0100 |
commit | ed3a6c7e1771100e81c53cc583828712d029b018 (patch) | |
tree | 79a5306a8c622badf2e5c565c1f8be9ce7c5f083 | |
parent | 9be73e14dceb2c4c84ffe7a5a3a635613a27420e (diff) |
Add health bars.
-rw-r--r-- | src/common.hpp | 1 | ||||
-rw-r--r-- | src/game/game.hpp | 2 | ||||
-rw-r--r-- | src/game/units.cpp | 34 | ||||
-rw-r--r-- | src/render.cpp | 8 |
4 files changed, 40 insertions, 5 deletions
diff --git a/src/common.hpp b/src/common.hpp index 4382da6..8a86482 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -287,6 +287,7 @@ namespace render { 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_rect(rectf_t rect, sf::Color color); void render_hlrect(rectf_t rect, sf::Color color); void render_arrow(v2f_t x0, v2f_t x1, sf::Color color); diff --git a/src/game/game.hpp b/src/game/game.hpp index 0ad0614..f6687dc 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -86,7 +86,7 @@ namespace game { bool dead = false; double death_time = -INFINITY; - int health = 1; + int health = 1, max_health = 1; void damage(double now, int points); virtual void die(double now) = 0; diff --git a/src/game/units.cpp b/src/game/units.cpp index c416dd7..b4cb333 100644 --- a/src/game/units.cpp +++ b/src/game/units.cpp @@ -29,6 +29,32 @@ void unit_t::render_to(render::state_t *render) if (selected == selection_cookie) render->render_hlrect(render_bounds, sf::Color::Blue); + if (!dead && health < max_health) + { + rectf_t bar; + float frac; + sf::Color color; + + bar[0][0] = (render_bounds[0][0] + render_bounds[1][0]) / 2; + bar[0][1] = render_bounds[0][1] - 0.05f; + bar[0][0] -= 0.3f; + bar[1][0] = bar[0][0] + 0.6f; + bar[1][1] = bar[0][1] + 0.05f; + render->render_rect(bar, sf::Color::Black); + + frac = (float)health / max_health; + + if (frac < 0.3f) + color = sf::Color::Red; + else if (frac < 0.75f) + color = sf::Color::Yellow; + else + color = sf::Color::Green; + + bar[1][0] = lerp(bar[0][0], bar[1][0], frac); + render->render_rect(bar, color); + } + if (move.moving && debug_draw_paths) render->debug_path(&move.path); } @@ -151,7 +177,7 @@ void unit_t::damage(double now, int points) human_t::human_t() : unit_t(UNIT_HUMAN) { cflags = CF_HUMAN; - health = 20; + health = max_health = 20; size[0] = v2f_t(-0.4f, -0.4f); size[1] = v2f_t(+0.4f, +0.4f); render_size[0] = v2f_t(-0.5f, -1.0f); @@ -212,7 +238,7 @@ void human_t::render_to(render::state_t *render) alien_t::alien_t() : unit_t(UNIT_ALIEN) { cflags = CF_SOLID; - health = 4; + health = max_health = 4; size[0] = v2f_t(-0.2f, -0.2f); size[1] = v2f_t(+0.2f, +0.2f); render_size[0] = v2f_t(-0.3f, -0.3f); @@ -281,9 +307,9 @@ void alien_t::attack(double now, unit_t *target, float range) trace = world->trace(x, target->x, CF_SOLID); if (!trace.hit) - target->damage(now, 12); + target->damage(now, rand() % 6 + rand() % 6 + 2); - next_attack = now + 1.0; + next_attack = now + 0.4; } void alien_t::think(double now, double dt) diff --git a/src/render.cpp b/src/render.cpp index 2c8f7a4..cddb6c9 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -216,6 +216,14 @@ void state_t::render_text(v2f_t x, float height, const wchar_t *wstr, window->draw(text); } +void state_t::render_rect(rectf_t rect, sf::Color color) +{ + wot_rect.setSize(rect.dims()); + wot_rect.setPosition(rect[0]); + wot_rect.setFillColor(color); + window->draw(wot_rect); +} + void state_t::render_hlrect(rectf_t rect, sf::Color color) { sf::Color fill; |