123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810 |
- // SuperTux
- // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
- // 2023 Vankata453
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- #include "object/camera.hpp"
- #include <math.h>
- #include "math/random.hpp"
- #include "math/util.hpp"
- #include "object/player.hpp"
- #include "supertux/level.hpp"
- #include "supertux/sector.hpp"
- #include "util/reader_mapping.hpp"
- #include "video/drawing_context.hpp"
- #include "video/video_system.hpp"
- #include "video/viewport.hpp"
- /* Very small value, used in Camera checks. */
- static const float CAMERA_EPSILON = .00001f;
- /* This is the fractional distance toward the peek
- position to move each frame; lower is slower,
- 0 is never get there, 1 is instant. */
- static const float PEEK_ARRIVE_RATIO = 0.03f;
- /**
- * For the multiplayer camera, the camera will ensure all players are visible.
- * These variables allow establishing a minimum zone around them that will also
- * be always visible.
- */
- static const float HORIZONTAL_MARGIN = 196.f; // 6 tiles.
- static const float VERTICAL_MARGIN = 196.f; // 6 tiles.
- static const float PEEK_ADD_HORIZONTAL_MARGIN = 320.f; // 10 tiles.
- static const float PEEK_ADD_VERTICAL_MARGIN = 320.f; // 10 tiles.
- /* 0 = no movement, 1 = no smooth adaptation. */
- static const float MULTIPLAYER_CAM_WEIGHT = 0.1f;
- Camera::Camera(const std::string& name) :
- GameObject(name),
- ExposedObject<Camera, scripting::Camera>(this),
- m_mode(Mode::NORMAL),
- m_defaultmode(Mode::NORMAL),
- m_screen_size(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)),
- m_translation(0.0f, 0.0f),
- m_lookahead_pos(0.0f, 0.0f),
- m_peek_pos(0.0f, 0.0f),
- m_cached_translation(0.0f, 0.0f),
- m_shaketimer(),
- m_shakespeed(),
- m_shakedepth_x(),
- m_shakedepth_y(),
- m_earthquake(false),
- m_earthquake_strength(),
- m_earthquake_delay(),
- m_earthquake_last_offset(0.f),
- m_earthquake_delay_timer(),
- m_scroll_from(0.0f, 0.0f),
- m_scroll_goal(0.0f, 0.0f),
- m_scroll_to_pos(),
- m_scrollspeed(),
- m_scale(1.f),
- m_scale_origin(1.f),
- m_scale_target(1.f),
- m_scale_time_total(0.f),
- m_scale_time_remaining(0.f),
- m_scale_origin_translation(),
- m_scale_target_translation(),
- m_scale_easing(),
- m_scale_anchor(),
- m_minimum_scale(1.f),
- m_enfore_minimum_scale(false)
- {
- }
- Camera::Camera(const ReaderMapping& reader) :
- GameObject(reader),
- ExposedObject<Camera, scripting::Camera>(this),
- m_mode(Mode::NORMAL),
- m_defaultmode(Mode::NORMAL),
- m_screen_size(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)),
- m_translation(0.0f, 0.0f),
- m_lookahead_pos(0.0f, 0.0f),
- m_peek_pos(0.0f, 0.0f),
- m_cached_translation(0.0f, 0.0f),
- m_shaketimer(),
- m_shakespeed(),
- m_shakedepth_x(),
- m_shakedepth_y(),
- m_earthquake(false),
- m_earthquake_strength(),
- m_earthquake_delay(),
- m_earthquake_last_offset(0.f),
- m_earthquake_delay_timer(),
- m_scroll_from(0.0f, 0.0f),
- m_scroll_goal(0.0f, 0.0f),
- m_scroll_to_pos(),
- m_scrollspeed(),
- m_scale(1.f),
- m_scale_origin(1.f),
- m_scale_target(1.f),
- m_scale_time_total(0.f),
- m_scale_time_remaining(0.f),
- m_scale_origin_translation(),
- m_scale_target_translation(),
- m_scale_easing(),
- m_scale_anchor(),
- m_minimum_scale(1.f),
- m_enfore_minimum_scale(false)
- {
- std::string modename;
- reader.get("mode", modename);
- if (modename == "normal")
- {
- m_mode = Mode::NORMAL;
- }
- else if (modename == "autoscroll")
- {
- m_mode = Mode::AUTOSCROLL;
- init_path(reader, true);
- }
- else if (modename == "manual")
- {
- m_mode = Mode::MANUAL;
- }
- else
- {
- m_mode = Mode::NORMAL;
- log_warning << "invalid camera mode '" << modename << "'found in worldfile." << std::endl;
- }
- m_defaultmode = m_mode;
- if (m_name.empty()) {
- m_name = "Camera";
- }
- }
- Camera::~Camera()
- {
- }
- ObjectSettings
- Camera::get_settings()
- {
- ObjectSettings result = GameObject::get_settings();
- result.add_enum(_("Mode"), reinterpret_cast<int*>(&m_defaultmode),
- {_("normal"), _("manual"), _("autoscroll")},
- {"normal", "manual", "autoscroll"},
- {}, "mode");
- result.add_path_ref(_("Path"), *this, get_path_ref(), "path-ref");
- if (get_walker() && get_path() && get_path()->is_valid()) {
- result.add_walk_mode(_("Path Mode"), &get_path()->m_mode, {}, {});
- result.add_bool(_("Adapt Speed"), &get_path()->m_adapt_speed, {}, {});
- result.add_path_handle(_("Handle"), m_path_handle, "handle");
- }
- return result;
- }
- void
- Camera::after_editor_set()
- {
- if (get_walker() && get_path() && get_path()->is_valid()) {
- if (m_defaultmode != Mode::AUTOSCROLL) {
- get_path()->m_nodes.clear();
- auto path_obj = get_path_gameobject();
- if(path_obj != nullptr)
- {
- path_obj->editor_delete();
- }
- }
- } else {
- if (m_defaultmode == Mode::AUTOSCROLL) {
- init_path_pos(Vector(0,0));
- }
- }
- }
- void
- Camera::save_state()
- {
- GameObject::save_state();
- PathObject::save_state();
- }
- void
- Camera::check_state()
- {
- GameObject::check_state();
- PathObject::check_state();
- }
- const Vector
- Camera::get_translation() const
- {
- Vector screen_size = m_screen_size.as_vector();
- return m_translation + ((screen_size * (get_current_scale() - 1.f)) / 2.f);
- }
- void
- Camera::set_translation_centered(const Vector& translation)
- {
- m_translation = translation - m_screen_size.as_vector() / 2;
- }
- Rectf
- Camera::get_rect() const
- {
- return Rectf::from_center(get_center(), m_screen_size);
- }
- void
- Camera::reset(const Vector& tuxpos)
- {
- m_translation.x = tuxpos.x - m_screen_size.width / 2.0f;
- m_translation.y = tuxpos.y - m_screen_size.height / 2.0f;
- m_shakespeed = 0;
- m_shaketimer.stop();
- keep_in_bounds(m_translation);
- m_cached_translation = m_translation;
- }
- void
- Camera::shake(float duration, float x, float y)
- {
- m_shaketimer.start(duration*100.f);
- m_shakedepth_x = x;
- m_shakedepth_y = y;
- m_shakespeed = math::PI_2 / duration;
- }
- void
- Camera::start_earthquake(float strength, float delay)
- {
- if (strength <= 0.f)
- {
- log_warning << "Invalid earthquake strength value provided. Setting to 3." << std::endl;
- strength = 3.f;
- }
- if (delay <= 0.f)
- {
- log_warning << "Invalid earthquake delay value provided. Setting to 0.05." << std::endl;
- delay = 0.05f;
- }
- m_earthquake = true;
- m_earthquake_strength = strength;
- m_earthquake_delay = delay;
- }
- void
- Camera::stop_earthquake()
- {
- m_translation.y -= m_earthquake_last_offset;
- m_cached_translation.y -= m_earthquake_last_offset;
- m_earthquake = false;
- m_earthquake_last_offset = 0.f;
- m_earthquake_delay_timer.stop();
- }
- void
- Camera::scroll_to(const Vector& goal, float scrolltime)
- {
- if(scrolltime == 0.0f)
- {
- m_translation.x = goal.x;
- m_translation.y = goal.y;
- m_mode = Mode::MANUAL;
- return;
- }
- m_scroll_from = m_translation;
- m_scroll_goal = goal;
- keep_in_bounds(m_scroll_goal);
- m_scroll_to_pos = 0;
- m_scrollspeed = 1.f / scrolltime;
- m_mode = Mode::SCROLLTO;
- }
- void
- Camera::draw(DrawingContext& context)
- {
- context.push_transform();
- context.transform().scale = get_current_scale();
- m_screen_size = Sizef(context.get_width(),
- context.get_height());
- context.pop_transform();
- }
- void
- Camera::update(float dt_sec)
- {
- // Minimum scale should be set during the update sequence; else, reset it.
- m_enfore_minimum_scale = false;
- switch (m_mode) {
- case Mode::NORMAL:
- if (Sector::current() && Sector::current()->get_object_count<Player>() > 1)
- {
- update_scroll_normal_multiplayer(dt_sec);
- }
- else
- {
- update_scroll_normal(dt_sec);
- }
- break;
- case Mode::MANUAL:
- keep_in_bounds(m_translation);
- break;
- case Mode::AUTOSCROLL:
- update_scroll_autoscroll(dt_sec);
- break;
- case Mode::SCROLLTO:
- update_scroll_to(dt_sec);
- break;
- default:
- break;
- }
- update_scale(dt_sec);
- update_shake();
- update_earthquake();
- }
- void
- Camera::keep_in_bounds(const Rectf& bounds)
- {
- // Determines the difference between normal and scaled translation.
- const Vector scale_factor = (m_screen_size.as_vector() * (get_current_scale() - 1.f)) / 2.f;
- // Keep the translation's scaled position in provided bounds.
- m_translation.x = (bounds.get_width() > m_screen_size.width ?
- math::clamp(m_translation.x + scale_factor.x, bounds.get_left(), bounds.get_right() - m_screen_size.width) :
- bounds.get_left());
- m_translation.y = (bounds.get_height() > m_screen_size.height ?
- math::clamp(m_translation.y + scale_factor.y, bounds.get_top(), bounds.get_bottom() - m_screen_size.height) :
- bounds.get_top());
- // Remove any scale factor we may have added in the checks above.
- m_translation -= scale_factor;
- }
- void
- Camera::keep_in_bounds(Vector& translation_)
- {
- float width = d_sector->get_width();
- float height = d_sector->get_height();
- // Remove any earthquake offset from the translation.
- translation_.y -= m_earthquake_last_offset;
- if (m_mode == Mode::MANUAL)
- {
- // Determines the difference between normal and scaled translation.
- const Vector scale_factor = (m_screen_size.as_vector() * (get_current_scale() - 1.f)) / 2.f;
- // Keep the translation's scaled position in sector bounds.
- translation_.x = math::clamp(translation_.x + scale_factor.x, 0.0f, width - m_screen_size.width);
- translation_.y = math::clamp(translation_.y + scale_factor.y, 0.0f, height - m_screen_size.height);
- // Remove any scale factor we may have added in the checks above.
- translation_ -= scale_factor;
- }
- else
- {
- // Don't scroll before the start or after the sector's end.
- translation_.x = math::clamp(translation_.x, 0.0f, width - m_screen_size.width);
- translation_.y = math::clamp(translation_.y, 0.0f, height - m_screen_size.height);
- }
- // Add any earthquake offset we may have removed earlier.
- translation_.y += m_earthquake_last_offset;
- if (height < m_screen_size.height)
- translation_.y = height / 2.0f - m_screen_size.height / 2.0f;
- if (width < m_screen_size.width)
- translation_.x = width / 2.0f - m_screen_size.width / 2.0f;
- }
- void
- Camera::update_shake()
- {
- if (m_shaketimer.started()) {
- // Old method:
- // m_translation.x -= sinf(m_shaketimer.get_timegone() * m_shakespeed) * m_shakedepth_x;
- // m_translation.y -= sinf(m_shaketimer.get_timegone() * m_shakespeed) * m_shakedepth_y;
- // Elastic easing:
- m_translation.x -= m_shakedepth_x * ((std::pow(2.f, -0.8f * (m_shakespeed * m_shaketimer.get_timegone()))) *
- std::sin(((0.8f * m_shakespeed * m_shaketimer.get_timegone()) - 0.75f) * (2.f * math::PI) / 3.f));
- m_translation.y -= m_shakedepth_y * ((std::pow(2.f, -0.8f * (m_shakespeed * m_shaketimer.get_timegone()))) *
- std::sin(((0.8f * m_shakespeed * m_shaketimer.get_timegone()) - 0.75f) * (2.f * math::PI) / 3.f));
- }
- }
- void
- Camera::update_earthquake()
- {
- if (!m_earthquake)
- return;
- if (m_earthquake_delay_timer.check())
- {
- if (m_earthquake_last_offset == 0.f)
- {
- m_earthquake_last_offset = m_earthquake_strength * graphicsRandom.randf(-2, 2);
- m_translation.y += m_earthquake_last_offset;
- m_cached_translation.y += m_earthquake_last_offset;
- }
- else
- {
- m_translation.y -= m_earthquake_last_offset;
- m_cached_translation.y -= m_earthquake_last_offset;
- m_earthquake_last_offset = 0.f;
- }
- m_earthquake_delay_timer.start(m_earthquake_delay + static_cast<float>(graphicsRandom.rand(0, 1)));
- }
- else if (!m_earthquake_delay_timer.started())
- {
- m_earthquake_delay_timer.start(m_earthquake_delay + static_cast<float>(graphicsRandom.rand(0, 1)));
- }
- }
- void
- Camera::update_scroll_normal(float dt_sec)
- {
- Player& player = *d_sector->get_players()[0];
- // TODO: Co-op mode needs a good camera.
- const Vector player_pos(player.get_bbox().get_left(),
- player.get_bbox().get_bottom());
- static Vector last_player_pos = player_pos;
- const Vector player_delta = player_pos - last_player_pos;
- last_player_pos = player_pos;
- // Check that we don't have division by zero later.
- if (dt_sec < CAMERA_EPSILON)
- return;
- /****** Vertical Scrolling part. ******/
- if (!player.is_dying())
- {
- m_cached_translation.y = math::clamp(m_cached_translation.y,
- player_pos.y - m_screen_size.height * 0.67f,
- player_pos.y - m_screen_size.height * 0.33f);
- }
- m_translation.y = m_cached_translation.y;
- if (!player.is_dying())
- {
- const float top_edge = m_screen_size.height * 0.3f;
- const float bottom_edge = m_screen_size.height * 0.7f;
- const float translation_compensation_y = player_pos.y - m_translation.y;
- float peek_to_y = 0;
- if (player.peeking_direction_y() == Direction::UP)
- peek_to_y = bottom_edge - translation_compensation_y;
- else if (player.peeking_direction_y() == Direction::DOWN)
- peek_to_y = top_edge - translation_compensation_y;
- float peek_move_y = (peek_to_y - m_peek_pos.y) * PEEK_ARRIVE_RATIO;
- if (fabsf(peek_move_y) < 1.0f)
- peek_move_y = 0.0;
- m_peek_pos.y += peek_move_y;
- m_translation.y -= m_peek_pos.y;
- m_translation.y = math::clamp(m_translation.y,
- player_pos.y - m_screen_size.height * 0.7f,
- player_pos.y - m_screen_size.height * 0.3f);
- m_cached_translation.y = math::clamp(m_cached_translation.y,
- player_pos.y - m_screen_size.height * 0.7f,
- player_pos.y - m_screen_size.height * 0.3f);
- }
- /****** Horizontal scrolling part *******/
- if (!player.is_dying())
- {
- const float left_end = m_screen_size.width * 0.4f;
- const float right_end = m_screen_size.width * 0.6f;
- if (player_delta.x < -CAMERA_EPSILON)
- {
- // Walking left.
- m_lookahead_pos.x -= player_delta.x * 0.8f;
- if (m_lookahead_pos.x > right_end)
- m_lookahead_pos.x = right_end;
- }
- else if (player_delta.x > CAMERA_EPSILON)
- {
- // Walking right.
- m_lookahead_pos.x -= player_delta.x * 0.8f;
- if (m_lookahead_pos.x < left_end)
- m_lookahead_pos.x = left_end;
- }
- else
- {
- m_lookahead_pos.x = math::clamp(m_lookahead_pos.x, left_end, right_end);
- }
- // Adjust for level ends.
- if (player_pos.x < left_end)
- m_lookahead_pos.x = left_end;
- if (player_pos.x > d_sector->get_width() - left_end)
- m_lookahead_pos.x = right_end;
- m_cached_translation.x = player_pos.x - m_lookahead_pos.x;
- }
- m_translation.x = m_cached_translation.x;
- if (!player.is_dying())
- {
- const float left_edge = m_screen_size.width * 0.1666f;
- const float right_edge = m_screen_size.width * 0.8334f;
- const float translation_compensation_x = player_pos.x - m_translation.x;
- float peek_to_x = 0;
- if (player.peeking_direction_x() == Direction::LEFT)
- peek_to_x = right_edge - translation_compensation_x;
- else if (player.peeking_direction_x() == Direction::RIGHT)
- peek_to_x = left_edge - translation_compensation_x;
- float peek_move_x = (peek_to_x - m_peek_pos.x) * PEEK_ARRIVE_RATIO;
- if (fabsf(peek_move_x) < 1.0f)
- peek_move_x = 0.0f;
- m_peek_pos.x += peek_move_x;
- m_translation.x -= m_peek_pos.x;
- m_translation.x = math::clamp(m_translation.x,
- player_pos.x - m_screen_size.width * 0.8334f,
- player_pos.x - m_screen_size.width * 0.1666f);
- m_cached_translation.x = math::clamp(m_cached_translation.x,
- player_pos.x - m_screen_size.width * 0.8334f,
- player_pos.x - m_screen_size.width * 0.1666f);
- }
- keep_in_bounds(m_translation);
- keep_in_bounds(m_cached_translation);
- }
- void
- Camera::update_scroll_normal_multiplayer(float dt_sec)
- {
- m_enfore_minimum_scale = true;
- float x1 = Sector::get().get_width();
- float y1 = Sector::get().get_height();
- float x2 = 0.f;
- float y2 = 0.f;
- for (const auto* p : Sector::get().get_players())
- {
- if (p->is_dead() || p->is_dying())
- continue;
- float lft = p->get_bbox().get_left() - HORIZONTAL_MARGIN;
- float rgt = p->get_bbox().get_right() + HORIZONTAL_MARGIN;
- float top = p->get_bbox().get_top() - VERTICAL_MARGIN;
- float btm = p->get_bbox().get_bottom() + VERTICAL_MARGIN;
- if (p->peeking_direction_x() == Direction::LEFT)
- lft -= PEEK_ADD_HORIZONTAL_MARGIN;
- else if (p->peeking_direction_x() == Direction::RIGHT)
- rgt += PEEK_ADD_HORIZONTAL_MARGIN;
- if (p->peeking_direction_y() == Direction::UP)
- top -= PEEK_ADD_VERTICAL_MARGIN;
- else if (p->peeking_direction_y() == Direction::DOWN)
- btm += PEEK_ADD_VERTICAL_MARGIN;
- x1 = std::min(x1, lft);
- x2 = std::max(x2, rgt);
- y1 = std::min(y1, top);
- y2 = std::max(y2, btm);
- }
- // Might happen if all players are dead.
- if (x2 < x1 || y2 < y1)
- return;
- Rectf cover(std::max(0.f, x1),
- std::max(0.f, y1),
- std::min(Sector::get().get_width(), x2),
- std::min(Sector::get().get_height(), y2));
- float scale = std::min(static_cast<float>(SCREEN_WIDTH) / static_cast<float>(cover.get_width()),
- static_cast<float>(SCREEN_HEIGHT) / static_cast<float>(cover.get_height()));
- float max_scale = std::max(static_cast<float>(SCREEN_WIDTH) / Sector::get().get_width(),
- static_cast<float>(SCREEN_HEIGHT) / Sector::get().get_height());
- // Capping at `m_scale` allows fixing a minor bug where the camera would
- // sometimes be slightly off-sector if scaling goes faster than moving.
- scale = math::clamp(scale, max_scale, m_scale);
- // Can't use m_screen_size because it varies depending on the scale
- auto rect = Rectf::from_center(Vector((cover.get_left() + cover.get_right()) / 2.f, (cover.get_top() + cover.get_bottom()) / 2.f),
- Sizef(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)));
- auto true_rect = Rectf::from_center(Vector((cover.get_left() + cover.get_right()) / 2.f, (cover.get_top() + cover.get_bottom()) / 2.f),
- Sizef(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)) / scale);
- if (true_rect.get_left() < 0.f)
- rect.move(Vector(-true_rect.get_left(), 0.f));
- if (true_rect.get_top() < 0.f)
- rect.move(Vector(0.f, -true_rect.get_top()));
- if (true_rect.get_right() > Sector::get().get_width())
- rect.move(Vector(Sector::get().get_width() - true_rect.get_right(), 0.f));
- if (true_rect.get_bottom() > Sector::get().get_height())
- rect.move(Vector(0.f, Sector::get().get_height() - true_rect.get_bottom()));
- m_translation = m_translation * (1.f - MULTIPLAYER_CAM_WEIGHT) + rect.p1() * MULTIPLAYER_CAM_WEIGHT;
- m_minimum_scale = m_minimum_scale * (1.f - MULTIPLAYER_CAM_WEIGHT) + scale * MULTIPLAYER_CAM_WEIGHT;
- }
- void
- Camera::update_scroll_autoscroll(float dt_sec)
- {
- if (!d_sector->get_object_count<Player>([](const Player& p) { return !p.is_dead() && !p.is_dying(); }))
- return;
- get_walker()->update(dt_sec);
- // TODO: Get the camera size?
- m_translation = get_walker()->get_pos(Sizef(), m_path_handle);
- keep_in_bounds(m_translation);
- }
- void
- Camera::update_scroll_to(float dt_sec)
- {
- m_scroll_to_pos += dt_sec * m_scrollspeed;
- if (m_scroll_to_pos >= 1.0f)
- {
- m_mode = Mode::MANUAL;
- m_translation = m_scroll_goal;
- // If a scale is active and wouldn't finish this frame, reload it with the remaining time,
- // setting the initial scale values from the scroll destination.
- if (m_scale_time_remaining - dt_sec > 0.f)
- {
- m_scale_time_total -= m_scale_time_total - m_scale_time_remaining;
- reload_scale();
- }
- else
- {
- // In case a scale finishes this frame, set its target translation to the scroll destination.
- m_scale_target_translation = m_translation;
- }
- return;
- }
- m_translation = m_scroll_from + (m_scroll_goal - m_scroll_from) * m_scroll_to_pos;
- }
- void
- Camera::update_scale(float dt_sec)
- {
- if (m_scale_time_remaining > 0.f)
- {
- m_scale_time_remaining -= dt_sec;
- if (m_scale_time_remaining <= 0.f)
- {
- m_scale = m_scale_target;
- if (m_mode == Mode::MANUAL)
- m_translation = m_scale_target_translation;
- m_scale_time_remaining = 0.f;
- m_scale_time_total = 0.f;
- }
- else
- {
- float time_progress = (m_scale_time_total - m_scale_time_remaining) / m_scale_time_total;
- float progress = static_cast<float>(m_scale_easing(static_cast<double>(time_progress)));
- m_scale = m_scale_origin + (m_scale_target - m_scale_origin) * progress;
- /** MANUAL mode scale management */
- if (m_mode == Mode::MANUAL)
- {
- // Move camera to the target translation, when zooming in manual mode.
- m_translation = m_scale_origin_translation + (m_scale_target_translation - m_scale_origin_translation) * progress;
- keep_in_bounds(m_translation);
- return;
- }
- }
- // Re-center camera, when zooming in normal mode.
- m_lookahead_pos /= 1.01f;
- }
- // In MANUAL mode, the translation is managed only when a scale is active.
- // In SCROLLTO mode, the translation is managed in update_scroll_to().
- if (m_mode == Mode::MANUAL || m_mode == Mode::SCROLLTO)
- return;
- // FIXME: Poor design: This shouldn't pose a problem to multiplayer.
- if (m_mode == Mode::NORMAL && Sector::current()->get_object_count<Player>() > 1)
- return;
- Vector screen_size = m_screen_size.as_vector();
- m_translation += screen_size * (1.f - get_current_scale()) / 2.f;
- }
- /** Get target scale position from the anchor point (m_scale_anchor). */
- Vector
- Camera::get_scale_anchor_target() const
- {
- // Get target center position from the anchor, and afterwards, top-left position from the center position.
- return get_anchor_center_pos(Rectf(m_translation,
- Sizef(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT))),
- m_scale_anchor) - Vector(static_cast<float>(SCREEN_WIDTH) / 2, static_cast<float>(SCREEN_HEIGHT) / 2);
- }
- /** Reload easing scale from the current position. */
- void
- Camera::reload_scale()
- {
- m_scale_origin = m_scale;
- m_scale_origin_translation = m_translation;
- m_scale_target_translation = get_scale_anchor_target();
- m_scale_time_remaining = m_scale_time_total;
- }
- void
- Camera::ease_scale(float scale, float time, easing ease, AnchorPoint anchor)
- {
- m_scale_anchor = anchor;
- if (time <= 0.f)
- {
- m_scale = scale;
- if (m_mode == Mode::MANUAL)
- m_translation = get_scale_anchor_target();
- }
- else
- {
- m_scale_target = scale;
- m_scale_time_total = time;
- m_scale_easing = ease;
- reload_scale();
- }
- }
- Vector
- Camera::get_center() const
- {
- return m_translation + Vector(m_screen_size.width / 2.0f,
- m_screen_size.height / 2.0f);
- }
- const Vector&
- Camera::get_position() const
- {
- return m_translation;
- }
- const Sizef&
- Camera::get_screen_size() const
- {
- return m_screen_size;
- }
- void
- Camera::move(const Vector& offset)
- {
- scroll_to(m_translation + offset, 0.0f);
- }
- bool
- Camera::is_saveable() const
- {
- return !(Level::current() &&
- Level::current()->is_worldmap());
- }
- /* EOF */
|