From ab725d9770c648bda8cc76425a5b3fcd98dce5f7 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Tue, 19 Dec 2017 19:53:49 +0100 Subject: Improve camera following. --- src/game/interface.cpp | 70 +++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 32 deletions(-) (limited to 'src/game/interface.cpp') 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)); -- cgit