From ed3a6c7e1771100e81c53cc583828712d029b018 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Wed, 13 Dec 2017 20:33:57 +0100 Subject: Add health bars. --- src/game/units.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'src/game/units.cpp') 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) -- cgit