From def701e42a40a1d95c3195be0c07d9b7b71cdd1d Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Tue, 19 Dec 2017 13:41:52 +0100 Subject: Automatic camera following. --- src/common.hpp | 11 ++++------- src/game/game.cpp | 1 + src/game/interface.cpp | 20 ++++++++++++++++++++ src/game/units.cpp | 2 ++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/common.hpp b/src/common.hpp index 3131b69..13b843d 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -211,18 +211,14 @@ namespace game { }; class state_t { - protected: - friend entity_t; - friend interface::state_t; - - std::unordered_set awake_entities; - std::unordered_set selected_units; public: world::world_t world; interface::state_t *interface; double now = 0.0, dt; bool paused = false; procgen::prng_t dice_prng; + std::unordered_set awake_entities; + std::unordered_set selected_units; void start(void); void stop(void); @@ -244,11 +240,12 @@ namespace interface { float em; struct { - sf::Vector2f center; + v2f_t center; int target_zoom = 3; float zoom = 3.0f; bool panning = false; sf::Vector2f pan_ref; + bool following = true; } camera; struct { diff --git a/src/game/game.cpp b/src/game/game.cpp index fedc403..22b883e 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -74,6 +74,7 @@ void state_t::start(void) soldier->place(&world, v2f_t(1.5, 0.5)); soldier = new unit_soldier_t(this); soldier->place(&world, v2f_t(2.5, 0.5)); + select(rectf_t(v2f_t(0.5, 0.5), v2f_t(2.5, 0.5))); } void state_t::stop(void) diff --git a/src/game/interface.cpp b/src/game/interface.cpp index 67f5482..6512576 100644 --- a/src/game/interface.cpp +++ b/src/game/interface.cpp @@ -27,6 +27,20 @@ void state_t::tick(double dt) size = window->getSize(); + if (!game->selected_units.size()) + camera.following = false; + + if (camera.following) { + v2f_t goal(0, 0); + + for (entity_t *ent : game->selected_units) + goal += ent->render_bounds.center(); + goal /= game->selected_units.size(); + + camera.center[0] = expfade(camera.center[0], goal[0], 20, dt); + camera.center[1] = expfade(camera.center[1], goal[1], 20, dt); + } + camera.zoom = expfade(camera.zoom, camera.target_zoom, 15, dt); { @@ -67,6 +81,7 @@ void state_t::tick(double dt) break; case sf::Mouse::Button::Middle: + camera.following = false; camera.panning = true; camera.pan_ref = wmouse; break; @@ -110,6 +125,11 @@ void state_t::tick(double dt) else print(text::get(text::UNPAUSED)); break; + + case sf::Keyboard::Key::F: + camera.following = true; + break; + case sf::Keyboard::Key::F1: debug_draw_cmodels ^= 1; print("debug_draw_cmodels = " + std::to_string(debug_draw_cmodels)); diff --git a/src/game/units.cpp b/src/game/units.cpp index 4e2b2aa..da6b1a3 100644 --- a/src/game/units.cpp +++ b/src/game/units.cpp @@ -431,6 +431,8 @@ void unit_soldier_t::on_think(void) void unit_soldier_t::on_death(void) { + game->selected_units.erase(this); + render_size[0] = v2f_t(-0.75f, -0.5f); render_size[1] = v2f_t(+0.75f, +0.5f); render_layer = -1; -- cgit