diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-12-19 19:53:49 +0100 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-12-19 19:53:49 +0100 |
commit | ab725d9770c648bda8cc76425a5b3fcd98dce5f7 (patch) | |
tree | a9ff92c0fa418db28b65598f73bce9313bdfcdd2 /src/game | |
parent | 297524c8ec41b585c4812494791772406653e479 (diff) |
Improve camera following.
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/game.hpp | 2 | ||||
-rw-r--r-- | src/game/interface.cpp | 70 | ||||
-rw-r--r-- | src/game/text.cpp | 12 |
3 files changed, 52 insertions, 32 deletions
diff --git a/src/game/game.hpp b/src/game/game.hpp index d4047a0..de41fc5 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -75,6 +75,8 @@ namespace game { typedef enum { PAUSED, UNPAUSED, + FOLLOWING_ON, + FOLLOWING_OFF, SAY_NO_PATH, SAY_BLOCKED, SAY_READY, diff --git a/src/game/interface.cpp b/src/game/interface.cpp index 1a74685..41e2365 100644 --- a/src/game/interface.cpp +++ b/src/game/interface.cpp @@ -24,40 +24,34 @@ void state_t::tick(double dt) sf::Event event; sf::Vector2f view_size; v2f_t wmouse; // Mouse position in world space; + v2f_t follow_center(0, 0), view_center, pan_delta; + float view_scale; 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); + view_scale = 4.5 * exp(camera.zoom * 0.12); + if (size.x < size.y) { + view_size.y = view_scale; + view_size.x = view_scale * size.x / size.y; + } else { + view_size.x = view_scale; + view_size.y = view_scale * size.y / size.x; } - camera.zoom = expfade(camera.zoom, camera.target_zoom, 15, dt); + view_center = camera.center; - { - float view_scale; + if (camera.following) + view_center += camera.follow_center; - view_scale = 4.5 * exp(camera.zoom * 0.12); - if (size.x < size.y) { - view_size.y = view_scale; - view_size.x = view_scale * size.x / size.y; - } else { - view_size.x = view_scale; - view_size.y = view_scale * size.y / size.x; - } + window->setView(sf::View(view_center, view_size)); - window->setView(sf::View(camera.center, view_size)); + if (camera.panning) { + pan_delta = compute_pan(window, camera.pan_ref); + view_center += compute_pan(window, camera.pan_ref); } + window->setView(sf::View(view_center, view_size)); while (window->pollEvent(event)) { // FIXME: refactor this nested switch clusterfuck @@ -81,7 +75,6 @@ void state_t::tick(double dt) break; case sf::Mouse::Button::Middle: - camera.following = false; camera.panning = true; camera.pan_ref = wmouse; break; @@ -100,7 +93,7 @@ void state_t::tick(double dt) case sf::Mouse::Button::Middle: if (camera.panning) - camera.center += compute_pan(window, camera.pan_ref); + camera.center += pan_delta; camera.panning = false; break; @@ -127,7 +120,15 @@ void state_t::tick(double dt) break; case sf::Keyboard::Key::F: - camera.following = true; + camera.following ^= 1; + + if (!camera.following) { + camera.center = view_center; + print(text::get(text::FOLLOWING_OFF)); + } else { + camera.center = v2f_t(0, 0); + print(text::get(text::FOLLOWING_ON)); + } break; case sf::Keyboard::Key::F1: @@ -155,11 +156,16 @@ void state_t::tick(double dt) } } - if (camera.panning) { - sf::Vector2f delta = compute_pan(window, camera.pan_ref); - window->setView(sf::View(camera.center + delta, view_size)); - } else - window->setView(sf::View(camera.center, view_size)); + if (game->selected_units.size() && camera.following) { + for (entity_t *ent : game->selected_units) + follow_center += ent->render_bounds.center(); + follow_center /= game->selected_units.size(); + + + camera.follow_center = follow_center; + //camera.follow_center[0] = expfade(camera.follow_center[0], follow_center[0], dt, 5); + //camera.follow_center[1] = expfade(camera.follow_center[1], follow_center[1], dt, 5); + } // Compute this _after_ the setView above. wmouse = window->mapPixelToCoords(sf::Mouse::getPosition(*window)); diff --git a/src/game/text.cpp b/src/game/text.cpp index 3da39ff..f8c3f71 100644 --- a/src/game/text.cpp +++ b/src/game/text.cpp @@ -17,6 +17,12 @@ static std::string get_english(index_t index) case UNPAUSED: return "UNPAUSED"; + case FOLLOWING_ON: + return "Following: on."; + + case FOLLOWING_OFF: + return "Following: off."; + case SAY_BLOCKED: return "Something is in my way."; @@ -82,6 +88,12 @@ static std::string get_polish(index_t index) case UNPAUSED: return "WZNOWIONO"; + case FOLLOWING_ON: + return "Podążanie: włączone."; + + case FOLLOWING_OFF: + return "Podążanie: wyłączone."; + case SAY_BLOCKED: return "Coś jest na mojej drodze."; |