diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-11-07 12:06:19 +0100 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-11-07 12:06:19 +0100 |
commit | 7471347839f30a85e985891b945a721967fcfb86 (patch) | |
tree | 762a37e4792672b0af7a0878b41b828508c91e2a /src/game.cpp | |
parent | f951af3cfdef3d23fd5875078a29c15ebf9120e9 (diff) |
Avoid walking into walls.
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/game.cpp b/src/game.cpp index a2f4bbc..e8c179a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -5,10 +5,19 @@ namespace game { class unit_t : public world::entity_t { world::world_t *world; + world::cmodel_t make_cmodel(v2f_t at) + { + world::cmodel_t cmodel; + + cmodel.bounds[0] = at + size[0]; + cmodel.bounds[1] = at + size[1]; + cmodel.cflags = cflags; + + return cmodel; + } + void compute_bounds() { - cmodel.bounds[0] = x + size[0]; - cmodel.bounds[1] = x + size[1]; render_bounds[0] = x + render_size[0]; render_bounds[1] = x + render_size[1]; } @@ -16,6 +25,7 @@ class unit_t : public world::entity_t { public: v2f_t x; rectf_t size, render_size; + world::cflags_t cflags; struct { bool moving = false; @@ -30,9 +40,10 @@ public: world = world_; x = x_; move.moving = false; - cmodel.cflags = 1; + cflags = 1; unlink(); + cmodel = make_cmodel(x); compute_bounds(); link(world); } @@ -47,7 +58,8 @@ public: time = 0.15; while (time > 0.0f) { - v2f_t delta, next; + v2f_t delta, next, x_new; + world::cmodel_t cmodel_next; if (!move.path.size()) { move.moving = false; @@ -59,15 +71,25 @@ public: move.angle = delta.angle(); if (delta.len() >= time) { - x += delta * time / delta.len(); - break; + x_new = x + delta * time / delta.len(); + time -= delta.len(); } else { - x = next; + x_new = next; time -= delta.len(); move.path.pop_front(); } + + cmodel_next = make_cmodel(x_new); + if (world->test_rect(&cmodel_next, this)) { + move.moving = false; + break; + } + + x = x_new; + cmodel = cmodel_next; } + unlink(); compute_bounds(); link(world); |