summaryrefslogtreecommitdiff
path: root/src/game/units.cpp
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-12-14 18:40:11 +0100
committerPaweł Redman <pawel.redman@gmail.com>2017-12-14 18:40:11 +0100
commitb914d67b4e683c2d3e43c1854d6ef48b1878e4b6 (patch)
treeb91f8e74db15f597b3c86b2f86f16062eeffdf1f /src/game/units.cpp
parent2bae146672c49e8d9c3a514119c6980eef6f9f86 (diff)
Allow humans to shoot.
Diffstat (limited to 'src/game/units.cpp')
-rw-r--r--src/game/units.cpp125
1 files changed, 82 insertions, 43 deletions
diff --git a/src/game/units.cpp b/src/game/units.cpp
index 672e73f..2eb5dbe 100644
--- a/src/game/units.cpp
+++ b/src/game/units.cpp
@@ -211,6 +211,41 @@ void unit_t::try_attack(unit_t *target)
target->damage(dmg_total, this);
}
+static unit_t *find_target(world::world_t *world, v2f_t x, float r,
+ unit_t::type_t type)
+{
+ rectf_t rect;
+ float nearest_dist = INFINITY;
+ unit_t *nearest = NULL;
+
+ rect[0] = x - v2f_t(r, r);
+ rect[1] = x + v2f_t(r, r);
+
+ for (world::entity_t *ent : world->get_entities(rect, -1)) {
+ unit_t *unit;
+ float dist;
+
+ if (ent->type != ET_UNIT)
+ continue;
+
+ unit = (unit_t*)ent;
+
+ if (unit->type != type)
+ continue;
+
+ if (unit->dead)
+ continue;
+
+ dist = (unit->x - x).len();
+ if (dist < nearest_dist) {
+ nearest_dist = dist;
+ nearest = unit;
+ }
+ }
+
+ return nearest;
+}
+
human_t::human_t(game::state_t *game) : unit_t(game, UNIT_HUMAN)
{
cflags = CF_BODY;
@@ -219,7 +254,6 @@ human_t::human_t(game::state_t *game) : unit_t(game, UNIT_HUMAN)
size[1] = v2f_t(+0.4f, +0.4f);
render_size[0] = v2f_t(-0.5f, -1.0f);
render_size[1] = v2f_t(+0.5f, +0.5f);
- render_layer = -1;
name = text::get(text::UNIT_HUMAN);
}
@@ -233,6 +267,25 @@ void human_t::sleep(void)
void human_t::think(void)
{
+ if (game->now > next_targetting) {
+ unit_t *target;
+
+ target = find_target(world, x, 10.0f, UNIT_ALIEN);
+ if (target) {
+ last_target_time = game->now;
+ last_target_x = target->x;
+
+ if (last_attack + 0.5 < game->now) {
+ last_attack = game->now;
+ try_attack(target);
+ }
+
+ //start_moving(target->x, CF_SOLID);
+ }
+
+ next_targetting = game->now + 0.2;
+ }
+
if (!keep_moving(4.0))
say(text::get(text::SAY_BLOCKED));
}
@@ -241,6 +294,7 @@ void human_t::die(void)
{
render_size[0] = v2f_t(-0.75f, -0.5f);
render_size[1] = v2f_t(+0.75f, +0.5f);
+ render_layer = -1;
unlink();
compute_bounds();
link(world);
@@ -249,13 +303,29 @@ void human_t::die(void)
void human_t::render_to(render::state_t *render)
{
if (!dead) {
- bool moving;
+ render::oriented_sprite_t *legs, *body;
+ float body_angle;
+
+ if (move.moving && !move.blocked)
+ legs = &assets::human.legs_walking;
+ else
+ legs = &assets::human.legs_idle;
+
+ if (last_target_time + 3 > game->now) {
+ if (last_attack + 0.1 > game->now)
+ body = &assets::human.body_firing;
+ else
+ body = &assets::human.body_aiming;
- moving = move.moving && !move.blocked;
+ body_angle = (last_target_x - x).angle();
- render->render((moving ? &assets::human.legs_walking :
- &assets::human.legs_idle), render_bounds, move.angle);
- render->render(&assets::human.body_idle, render_bounds, move.angle);
+ } else {
+ body = &assets::human.body_idle;
+ body_angle = move.angle;
+ }
+
+ render->render(legs, render_bounds, move.angle);
+ render->render(body, render_bounds, body_angle);
render->render(&assets::human.head_idle, render_bounds, move.angle);
} else
render->render(&assets::human.dead, render_bounds);
@@ -297,41 +367,7 @@ void alien_t::sleep(void)
void alien_t::die(void)
{
-}
-
-static unit_t *find_target(world::world_t *world, v2f_t x, float r,
- unit_t::type_t type)
-{
- rectf_t rect;
- float nearest_dist = INFINITY;
- unit_t *nearest = NULL;
-
- rect[0] = x - v2f_t(r, r);
- rect[1] = x + v2f_t(r, r);
-
- for (world::entity_t *ent : world->get_entities(rect, -1)) {
- unit_t *unit;
- float dist;
-
- if (ent->type != ET_UNIT)
- continue;
-
- unit = (unit_t*)ent;
-
- if (unit->type != type)
- continue;
-
- if (unit->dead)
- continue;
-
- dist = (unit->x - x).len();
- if (dist < nearest_dist) {
- nearest_dist = dist;
- nearest = unit;
- }
- }
-
- return nearest;
+ render_layer = -1;
}
void alien_t::attack(unit_t *target, float range)
@@ -374,8 +410,11 @@ void alien_t::render_to(render::state_t *render)
moving = move.moving && !move.blocked;
- render->render((moving ? &assets::alien.walking :
- &assets::alien.idle), render_bounds, move.angle);
+ if (!dead)
+ render->render((moving ? &assets::alien.walking :
+ &assets::alien.idle), render_bounds, move.angle);
+ else
+ render->render(&assets::alien.dead, render_bounds);
unit_t::render_to(render);
}