From 29d124b93125d7c9de762c45876f69df3acd549d Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Wed, 18 Oct 2017 19:53:51 +0200 Subject: Render bounds are now working. Start working on unit movement. --- src/game.cpp | 92 ++++++++++++++++++++++++++++++++-------------------------- src/math.hpp | 12 ++++++++ src/render.cpp | 2 +- src/world.cpp | 16 ++++------ 4 files changed, 70 insertions(+), 52 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 725d6f0..d805c83 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -3,63 +3,73 @@ namespace game { class unit_t : public world::entity_t { -}; + world::world_t *world; -class human_t : public unit_t { + void compute_bounds(v2f_t x) + { + 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: - float angle = 0.0f; - bool walking = false; - float stalin = 0.0f; + rectf_t size, render_size; + + struct { + world::tile_index_t pos; + + bool moving; + std::list path; + world::tile_index_t dest; + } move; + + void place(world::world_t *world, world::tile_index_t pos) + { + move.pos = pos; + move.moving = false; + unlink(); + compute_bounds((v2f_t)pos + v2f_t(0.5f, 0.5f)); + link(world); + } + + void keep_moving(void) + { + if (!move.moving) + return; + } + + bool start_moving(world::tile_index_t dest) + { + return false; + } +}; +class human_t : public unit_t { +public: void render_to(render::state_t *render) { - render->render((walking ? &assets::human.legs_walking : - &assets::human.legs_idle), bounds, angle); - render->render(&assets::human.body_idle, bounds, angle); - render->render(&assets::human.head_idle, bounds, angle); + 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); } }; -static std::list humans; +static human_t human; void state_t::start(void) { - for (size_t i = 0; i < 5; i++) { - humans.emplace(humans.end()); - - human_t &human = *(--humans.end()); - - human.bounds.v[0] = v2f_t(0.33f, 0); - human.bounds.v[1] = human.bounds.v[0] + v2f_t(0.66f, 1.0f); - human.render_bounds = human.bounds; - human.link(&world); - } + 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, world::tile_index_t(0, 0)); } void state_t::tick(void) { - for (human_t &human : humans) { - human.stalin -= ((float)rand() / RAND_MAX) * 0.1f; - - human.walking = false; - - if (human.stalin < 0) { - v2f_t delta; - - human.walking = true; - human.angle += (rand() & 2 ? 0.2f : -0.2f); - human.unlink(); - delta = v2f_t(cos(human.angle), sin(human.angle)) * 0.04; - human.bounds[0] += delta; - human.bounds[1] += delta; - human.render_bounds = human.bounds; - human.link(&world); - } - - if (human.stalin < -4.0f) - human.stalin = (float)rand() / RAND_MAX * 3.0f; - } } } //namespace game diff --git a/src/math.hpp b/src/math.hpp index f20cc99..c07b7d0 100644 --- a/src/math.hpp +++ b/src/math.hpp @@ -243,6 +243,18 @@ public: return true; } + friend rect_t operator+(const rect_t &a, const rect_t &b) + { + rect_t r; + + r[0][0] = std::min(a[0][0], b[0][0]); + r[0][1] = std::min(a[0][1], b[0][1]); + r[1][0] = std::max(a[1][0], b[1][0]); + r[1][1] = std::max(a[1][1], b[1][1]); + + return r; + } + friend std::ostream& operator<<(std::ostream& stream, rect_t r) { stream << "(" << r[0] << ", " << r[1] << ")"; diff --git a/src/render.cpp b/src/render.cpp index 5e7d1a4..66eeba8 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -86,7 +86,7 @@ void state_t::render(game::state_t *game) for (world::sector_t *sector : game->world.get_sectors(bbox)) draw_sector(window, sector); - ents = game->world.get_entities(bbox); + ents = game->world.get_render_entities(bbox); ents.sort( [](const world::entity_t *x, const world::entity_t *y) -> bool { diff --git a/src/world.cpp b/src/world.cpp index 511de12..d2fc2bb 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -160,24 +160,20 @@ void entity_t::link_to_sector(sector_t *sector) void entity_t::link(world_t *world) { - //sf::FloatRect total_bounds; + rectf_t total_bounds; float fx, fy; sector_index_t base; float xlip, ylip; size_t xsecs, ysecs; - // TODO - //total.bounds.left = std::min(bounds.left, render_bounds.left); - //total.bounds.top = std::min(bounds.top, render_bounds.top); - //total.bounds.width = std::max(bounds.width, render_bounds.width); - //total.bounds.height = std::max(bounds.height, render_bounds.height); + total_bounds = bounds + render_bounds; - fx = floor(bounds[0][0]); - fy = floor(bounds[0][1]); + fx = floor(total_bounds[0][0]); + fy = floor(total_bounds[0][1]); base = sector_index_at(v2f_t(fx, fy)); - xlip = bounds[1][0] - (base[0] + 1) * SECTOR_SIZE; - ylip = bounds[1][1] - (base[1] + 1) * SECTOR_SIZE; + xlip = total_bounds[1][0] - (base[0] + 1) * SECTOR_SIZE; + ylip = total_bounds[1][1] - (base[1] + 1) * SECTOR_SIZE; if (xlip > 0.0f) xsecs = ceil(xlip / SECTOR_SIZE) + 1; -- cgit