diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-10-21 14:40:20 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-10-21 14:40:20 +0200 |
commit | f08fb7293382dbaf240860119d128486ada62221 (patch) | |
tree | ce9a85f47b3dacc98f75a9f73f1378d4c00ec915 /src/game.cpp | |
parent | 42fece714e30b899208a90183a571452eb35811e (diff) |
Improve short paths and do some refactoring.
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/src/game.cpp b/src/game.cpp index 97aee59..2dab5c6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -18,8 +18,9 @@ public: rectf_t size, render_size; struct { - bool moving; + bool moving = false; v2f_t dst; + float angle = 0.0f; std::list<v2f_t> path; } move; @@ -38,19 +39,6 @@ public: bool move_towards(v2f_t point, float *time) { bool partial; - v2f_t delta; - - delta = point - x; - - if (delta.len() >= *time) { - partial = true; - x += delta * *time / delta.len(); - *time = 0.0f; - } else { - partial = false; - x = point; - *time -= delta.len(); - } return partial; } @@ -65,12 +53,23 @@ public: time = 0.15; while (time > 0.0f) { + v2f_t delta, next; + if (!move.path.size()) { move.moving = false; break; } - if (!move_towards(*move.path.begin(), &time)) { + next = *move.path.begin(); + delta = next - x; + move.angle = delta.angle(); + + if (delta.len() >= time) { + x += delta * time / delta.len(); + break; + } else { + x = next; + time -= delta.len(); move.path.pop_front(); } } @@ -103,11 +102,10 @@ class human_t : public unit_t { public: void render_to(render::state_t *render) { - float angle = 0; render->render((move.moving ? &assets::human.legs_walking : - &assets::human.legs_idle), render_bounds, angle); - render->render(&assets::human.body_idle, render_bounds, angle); - render->render(&assets::human.head_idle, render_bounds, angle); + &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); if (move.moving) render->debug_path(&move.path); |