summaryrefslogtreecommitdiff
path: root/src/game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp110
1 files changed, 76 insertions, 34 deletions
diff --git a/src/game.cpp b/src/game.cpp
index f5341d1..48047a4 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -2,6 +2,12 @@
namespace game {
+static size_t selection_cookie = 1;
+
+enum {
+ ET_UNIT
+};
+
class unit_t : public world::entity_t {
world::world_t *world;
@@ -26,6 +32,11 @@ public:
v2f_t x;
rectf_t size, render_size;
world::cflags_t cflags;
+ size_t selected = 0;
+
+ unit_t() : entity_t(ET_UNIT)
+ {
+ }
struct {
bool moving = false;
@@ -33,8 +44,18 @@ public:
float angle = 0.0f;
std::list<v2f_t> path;
+
+ bool blocked;
+ size_t attempts_left;
+ float next_attempt;
} move;
+ void say(std::string str)
+ {
+ //TODO
+ std::cout << (void*)this << ": " << str << "\n";
+ }
+
void place(world::world_t *world_, v2f_t x_)
{
world = world_;
@@ -48,14 +69,17 @@ public:
link(world);
}
- void keep_moving(void)
+ void keep_moving(double now)
{
float time;
if (!move.moving)
return;
- time = 0.15;
+ if (move.blocked && now < move.next_attempt)
+ return;
+
+ time = 0.15; // FIXME
while (time > 0.0f) {
v2f_t delta, next, x_new;
@@ -80,13 +104,22 @@ public:
}
cmodel_next = make_cmodel(x_new);
- if (world->test_rect(&cmodel_next, this)) {
- move.moving = false;
- break;
+ if (!world->test_rect(&cmodel_next, this)) {
+ x = x_new;
+ cmodel = cmodel_next;
+ continue;
}
- x = x_new;
- cmodel = cmodel_next;
+ if (move.attempts_left) {
+ move.blocked = true;
+ move.attempts_left--;
+ move.next_attempt = now + 0.5f;
+ } else {
+ if ((x - move.dst).len() > 1.5f)
+ say(text::unit_blocked);
+ move.moving = false;
+ }
+ break;
}
@@ -105,12 +138,19 @@ public:
move.dst = dst_;
move.path.clear();
- if (world->find_path(x, move.dst, &cmodel, this, &move.path))
- move.moving = true;
- else
+ if (!world->find_path(x, move.dst, &cmodel, this, &move.path)) {
+ say(text::unit_no_path);
move.moving = false;
+ return false;
+ }
+
+ move.moving = true;
+
+ move.blocked = false;
+ move.attempts_left = 10;
+ move.next_attempt = -INFINITY;
- return move.moving;
+ return true;
}
};
@@ -120,7 +160,14 @@ public:
void render_to(render::state_t *render)
{
- render->render((move.moving ? &assets::human.legs_walking :
+ bool moving;
+
+ if (selected == selection_cookie)
+ render->render_hlrect(render_bounds, sf::Color::Blue);
+
+ moving = move.moving && !move.blocked;
+
+ 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);
render->render(&assets::human.head_idle, render_bounds, move.angle);
@@ -145,40 +192,35 @@ void state_t::start(void)
human2.render_size[0] = v2f_t(-0.5f, -1.0f);
human2.render_size[1] = v2f_t(+0.5f, +0.5f);
human2.place(&world, v2f_t(3.5f, 0.5f));
-
}
void state_t::select(rectf_t x)
{
-
+ selection_cookie++;
+ selected_units.clear();
+
+ for (world::entity_t *ent : world.get_render_entities(x, -1)) {
+ unit_t *unit;
+
+ if (ent->type != ET_UNIT)
+ continue;
+
+ unit = (unit_t*)ent;
+ unit->selected = selection_cookie;
+ selected_units.insert(unit);
+ }
}
void state_t::command(v2f_t x)
{
-
+ for (unit_t *unit : selected_units)
+ unit->start_moving(x);
}
void state_t::tick(double now)
{
- if ((human2.x - human.x).len() > 3) {
- if (now > human2.last_follow + 0.5) {
- for (size_t i = 0; i < 8; i++) {
- float angle;
- v2f_t offset;
-
- angle = (float)i / 8 * 2 * M_PI;
- offset[0] = cos(angle);
- offset[1] = sin(angle);
-
- if (human2.start_moving(human.x + offset))
- break;
- }
- human2.last_follow = now;
- }
- }
-
- human.keep_moving();
- human2.keep_moving();
+ human.keep_moving(now);
+ human2.keep_moving(now);
}
} //namespace game