summaryrefslogtreecommitdiff
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
parent2bae146672c49e8d9c3a514119c6980eef6f9f86 (diff)
Allow humans to shoot.
-rw-r--r--assets/units/alien/dead_0.pngbin0 -> 2219 bytes
-rw-r--r--assets/units/human/body_aiming_ny_0.pngbin0 -> 1415 bytes
-rw-r--r--assets/units/human/body_aiming_ny_1.pngbin0 -> 1202 bytes
-rw-r--r--assets/units/human/body_aiming_x_0.pngbin0 -> 2180 bytes
-rw-r--r--assets/units/human/body_aiming_x_1.pngbin0 -> 2249 bytes
-rw-r--r--assets/units/human/body_aiming_y_0.pngbin0 -> 1933 bytes
-rw-r--r--assets/units/human/body_aiming_y_1.pngbin0 -> 1897 bytes
-rw-r--r--assets/units/human/body_firing_ny_0.pngbin0 -> 1249 bytes
-rw-r--r--assets/units/human/body_firing_ny_1.pngbin0 -> 1362 bytes
-rw-r--r--assets/units/human/body_firing_x_0.pngbin0 -> 2235 bytes
-rw-r--r--assets/units/human/body_firing_x_1.pngbin0 -> 2460 bytes
-rw-r--r--assets/units/human/body_firing_y_0.pngbin0 -> 2605 bytes
-rw-r--r--assets/units/human/body_firing_y_1.pngbin0 -> 2298 bytes
-rw-r--r--src/game/assets.cpp3
-rw-r--r--src/game/game.cpp3
-rw-r--r--src/game/game.hpp7
-rw-r--r--src/game/units.cpp125
17 files changed, 94 insertions, 44 deletions
diff --git a/assets/units/alien/dead_0.png b/assets/units/alien/dead_0.png
new file mode 100644
index 0000000..0c94f17
--- /dev/null
+++ b/assets/units/alien/dead_0.png
Binary files differ
diff --git a/assets/units/human/body_aiming_ny_0.png b/assets/units/human/body_aiming_ny_0.png
new file mode 100644
index 0000000..6fb5791
--- /dev/null
+++ b/assets/units/human/body_aiming_ny_0.png
Binary files differ
diff --git a/assets/units/human/body_aiming_ny_1.png b/assets/units/human/body_aiming_ny_1.png
new file mode 100644
index 0000000..eec667a
--- /dev/null
+++ b/assets/units/human/body_aiming_ny_1.png
Binary files differ
diff --git a/assets/units/human/body_aiming_x_0.png b/assets/units/human/body_aiming_x_0.png
new file mode 100644
index 0000000..ff9fab2
--- /dev/null
+++ b/assets/units/human/body_aiming_x_0.png
Binary files differ
diff --git a/assets/units/human/body_aiming_x_1.png b/assets/units/human/body_aiming_x_1.png
new file mode 100644
index 0000000..dcaba13
--- /dev/null
+++ b/assets/units/human/body_aiming_x_1.png
Binary files differ
diff --git a/assets/units/human/body_aiming_y_0.png b/assets/units/human/body_aiming_y_0.png
new file mode 100644
index 0000000..34d6d50
--- /dev/null
+++ b/assets/units/human/body_aiming_y_0.png
Binary files differ
diff --git a/assets/units/human/body_aiming_y_1.png b/assets/units/human/body_aiming_y_1.png
new file mode 100644
index 0000000..d0689c1
--- /dev/null
+++ b/assets/units/human/body_aiming_y_1.png
Binary files differ
diff --git a/assets/units/human/body_firing_ny_0.png b/assets/units/human/body_firing_ny_0.png
new file mode 100644
index 0000000..214e5c4
--- /dev/null
+++ b/assets/units/human/body_firing_ny_0.png
Binary files differ
diff --git a/assets/units/human/body_firing_ny_1.png b/assets/units/human/body_firing_ny_1.png
new file mode 100644
index 0000000..b0dedae
--- /dev/null
+++ b/assets/units/human/body_firing_ny_1.png
Binary files differ
diff --git a/assets/units/human/body_firing_x_0.png b/assets/units/human/body_firing_x_0.png
new file mode 100644
index 0000000..274bebc
--- /dev/null
+++ b/assets/units/human/body_firing_x_0.png
Binary files differ
diff --git a/assets/units/human/body_firing_x_1.png b/assets/units/human/body_firing_x_1.png
new file mode 100644
index 0000000..640f37d
--- /dev/null
+++ b/assets/units/human/body_firing_x_1.png
Binary files differ
diff --git a/assets/units/human/body_firing_y_0.png b/assets/units/human/body_firing_y_0.png
new file mode 100644
index 0000000..20363b0
--- /dev/null
+++ b/assets/units/human/body_firing_y_0.png
Binary files differ
diff --git a/assets/units/human/body_firing_y_1.png b/assets/units/human/body_firing_y_1.png
new file mode 100644
index 0000000..d2ca534
--- /dev/null
+++ b/assets/units/human/body_firing_y_1.png
Binary files differ
diff --git a/src/game/assets.cpp b/src/game/assets.cpp
index 1034f92..dddd447 100644
--- a/src/game/assets.cpp
+++ b/src/game/assets.cpp
@@ -9,12 +9,15 @@ void load(void)
{
human.head_idle.load("assets/units/human/head_idle", 1, 1, 1);
human.body_idle.load("assets/units/human/body_idle", 2, 2, 2);
+ human.body_aiming.load("assets/units/human/body_aiming", 2, 2, 2);
+ human.body_firing.load("assets/units/human/body_firing", 2, 2, 2);
human.legs_idle.load("assets/units/human/legs_idle", 2, 2);
human.legs_walking.load("assets/units/human/legs_walking", 2, 2);
human.dead.load("assets/units/human/dead_", 1);
alien.idle.load("assets/units/alien/idle", 2, 2, 2);
alien.walking.load("assets/units/alien/walking", 2, 2, 2);
+ alien.dead.load("assets/units/alien/dead_", 1);
world::register_tile(TILE_DIRT, 0);
world::register_tile(TILE_WALL, CF_SOLID);
diff --git a/src/game/game.cpp b/src/game/game.cpp
index 48f852f..5067df7 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -129,7 +129,8 @@ void state_t::tick(double now_, double dt_)
(*i)->sleep();
i = awake_units.erase(i);
} else {
- (*i)->think();
+ if (!(*i)->dead)
+ (*i)->think();
i++;
}
}
diff --git a/src/game/game.hpp b/src/game/game.hpp
index fdb2348..d4aed5d 100644
--- a/src/game/game.hpp
+++ b/src/game/game.hpp
@@ -25,12 +25,14 @@ namespace game {
namespace assets {
typedef struct {
render::oriented_sprite_4M_t head_idle, body_idle;
+ render::oriented_sprite_4M_t body_aiming, body_firing;
render::oriented_sprite_4M2_t legs_idle, legs_walking;
render::animated_texture_t dead;
} human_assets_t;
typedef struct {
render::oriented_sprite_4M_t idle, walking;
+ render::animated_texture_t dead;
} alien_assets_t;
extern human_assets_t human;
@@ -102,6 +104,11 @@ namespace game {
};
class human_t : public unit_t {
+ double next_targetting = -INFINITY;
+ double last_target_time = -INFINITY;
+ v2f_t last_target_x;
+ double last_attack = -INFINITY;
+
public:
human_t(game::state_t *game);
void render_to(render::state_t *render);
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);
}