diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-10-21 14:26:40 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-10-21 14:26:40 +0200 |
commit | 886a97593c1da1351f978c3dc39d9c1ea2ab57d9 (patch) | |
tree | 4d8c5d456c8c78a7d7639b4cd608435a5f103f01 /src/game.cpp | |
parent | 1c637e1ca5c0b1cf8a91f37c999aa7379fa08a8f (diff) |
First working path finding.
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/game.cpp b/src/game.cpp index ffee87d..2d9b67e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -19,8 +19,9 @@ public: struct { bool moving; - std::list<v2f_t> path; v2f_t dst; + + std::list<v2f_t> path; } move; void place(world::world_t *world_, v2f_t x_) @@ -34,10 +35,47 @@ public: link(world); } + 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; + } + void keep_moving(void) { + float time; + if (!move.moving) return; + + time = 0.15; + + while (time > 0.0f) { + if (!move.path.size()) { + move.moving = false; + break; + } + + if (!move_towards(*move.path.begin(), &time)) { + move.path.pop_front(); + } + } + + compute_bounds(); } bool start_moving(v2f_t dst_) @@ -68,6 +106,9 @@ public: &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); + + if (move.moving) + render->debug_path(&move.path); } }; @@ -89,6 +130,7 @@ void state_t::debug_click(v2f_t x) void state_t::tick(void) { + human.keep_moving(); } } //namespace game |