#include "common.hpp" namespace game { class unit_t : public world::entity_t { world::world_t *world; v2f_t x; void compute_bounds() { bounds[0] = x + size[0]; bounds[1] = x + size[1]; render_bounds[0] = x + render_size[0]; render_bounds[1] = x + render_size[1]; } public: rectf_t size, render_size; struct { bool moving; v2f_t dst; std::list path; } move; void place(world::world_t *world_, v2f_t x_) { world = world_; x = x_; move.moving = false; unlink(); compute_bounds(); 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(); } } unlink(); compute_bounds(); link(world); } bool start_moving(v2f_t dst_) { if (!world) { printf("unit_t::start_moving: entity is not linked\n"); return false; } move.dst = dst_; move.path.clear(); if (world->find_path(x, move.dst, size, &move.path)) move.moving = true; else move.moving = false; return move.moving; } }; 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); if (move.moving) render->debug_path(&move.path); } }; static human_t human; void state_t::start(void) { human.size[0] = v2f_t(-0.4f, -0.4f); human.size[1] = v2f_t(+0.4f, +0.4f); human.render_size[0] = v2f_t(-0.5f, -1.0f); human.render_size[1] = v2f_t(+0.5f, +0.5f); human.place(&world, v2f_t(0.5f, 0.5f)); } void state_t::debug_click(v2f_t x) { human.start_moving(x); } void state_t::tick(void) { human.keep_moving(); } } //namespace game