camera.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // 2023 Vankata453
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "object/camera.hpp"
  18. #include <math.h>
  19. #include "math/random.hpp"
  20. #include "math/util.hpp"
  21. #include "object/player.hpp"
  22. #include "supertux/level.hpp"
  23. #include "supertux/sector.hpp"
  24. #include "util/reader_mapping.hpp"
  25. #include "video/drawing_context.hpp"
  26. #include "video/video_system.hpp"
  27. #include "video/viewport.hpp"
  28. /* Very small value, used in Camera checks. */
  29. static const float CAMERA_EPSILON = .00001f;
  30. /* This is the fractional distance toward the peek
  31. position to move each frame; lower is slower,
  32. 0 is never get there, 1 is instant. */
  33. static const float PEEK_ARRIVE_RATIO = 0.03f;
  34. /**
  35. * For the multiplayer camera, the camera will ensure all players are visible.
  36. * These variables allow establishing a minimum zone around them that will also
  37. * be always visible.
  38. */
  39. static const float HORIZONTAL_MARGIN = 196.f; // 6 tiles.
  40. static const float VERTICAL_MARGIN = 196.f; // 6 tiles.
  41. static const float PEEK_ADD_HORIZONTAL_MARGIN = 320.f; // 10 tiles.
  42. static const float PEEK_ADD_VERTICAL_MARGIN = 320.f; // 10 tiles.
  43. /* 0 = no movement, 1 = no smooth adaptation. */
  44. static const float MULTIPLAYER_CAM_WEIGHT = 0.1f;
  45. Camera::Camera(const std::string& name) :
  46. GameObject(name),
  47. ExposedObject<Camera, scripting::Camera>(this),
  48. m_mode(Mode::NORMAL),
  49. m_defaultmode(Mode::NORMAL),
  50. m_screen_size(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)),
  51. m_translation(0.0f, 0.0f),
  52. m_lookahead_pos(0.0f, 0.0f),
  53. m_peek_pos(0.0f, 0.0f),
  54. m_cached_translation(0.0f, 0.0f),
  55. m_shaketimer(),
  56. m_shakespeed(),
  57. m_shakedepth_x(),
  58. m_shakedepth_y(),
  59. m_earthquake(false),
  60. m_earthquake_strength(),
  61. m_earthquake_delay(),
  62. m_earthquake_last_offset(0.f),
  63. m_earthquake_delay_timer(),
  64. m_scroll_from(0.0f, 0.0f),
  65. m_scroll_goal(0.0f, 0.0f),
  66. m_scroll_to_pos(),
  67. m_scrollspeed(),
  68. m_scale(1.f),
  69. m_scale_origin(1.f),
  70. m_scale_target(1.f),
  71. m_scale_time_total(0.f),
  72. m_scale_time_remaining(0.f),
  73. m_scale_origin_translation(),
  74. m_scale_target_translation(),
  75. m_scale_easing(),
  76. m_scale_anchor(),
  77. m_minimum_scale(1.f),
  78. m_enfore_minimum_scale(false)
  79. {
  80. }
  81. Camera::Camera(const ReaderMapping& reader) :
  82. GameObject(reader),
  83. ExposedObject<Camera, scripting::Camera>(this),
  84. m_mode(Mode::NORMAL),
  85. m_defaultmode(Mode::NORMAL),
  86. m_screen_size(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)),
  87. m_translation(0.0f, 0.0f),
  88. m_lookahead_pos(0.0f, 0.0f),
  89. m_peek_pos(0.0f, 0.0f),
  90. m_cached_translation(0.0f, 0.0f),
  91. m_shaketimer(),
  92. m_shakespeed(),
  93. m_shakedepth_x(),
  94. m_shakedepth_y(),
  95. m_earthquake(false),
  96. m_earthquake_strength(),
  97. m_earthquake_delay(),
  98. m_earthquake_last_offset(0.f),
  99. m_earthquake_delay_timer(),
  100. m_scroll_from(0.0f, 0.0f),
  101. m_scroll_goal(0.0f, 0.0f),
  102. m_scroll_to_pos(),
  103. m_scrollspeed(),
  104. m_scale(1.f),
  105. m_scale_origin(1.f),
  106. m_scale_target(1.f),
  107. m_scale_time_total(0.f),
  108. m_scale_time_remaining(0.f),
  109. m_scale_origin_translation(),
  110. m_scale_target_translation(),
  111. m_scale_easing(),
  112. m_scale_anchor(),
  113. m_minimum_scale(1.f),
  114. m_enfore_minimum_scale(false)
  115. {
  116. std::string modename;
  117. reader.get("mode", modename);
  118. if (modename == "normal")
  119. {
  120. m_mode = Mode::NORMAL;
  121. }
  122. else if (modename == "autoscroll")
  123. {
  124. m_mode = Mode::AUTOSCROLL;
  125. init_path(reader, true);
  126. }
  127. else if (modename == "manual")
  128. {
  129. m_mode = Mode::MANUAL;
  130. }
  131. else
  132. {
  133. m_mode = Mode::NORMAL;
  134. log_warning << "invalid camera mode '" << modename << "'found in worldfile." << std::endl;
  135. }
  136. m_defaultmode = m_mode;
  137. if (m_name.empty()) {
  138. m_name = "Camera";
  139. }
  140. }
  141. Camera::~Camera()
  142. {
  143. }
  144. ObjectSettings
  145. Camera::get_settings()
  146. {
  147. ObjectSettings result = GameObject::get_settings();
  148. result.add_enum(_("Mode"), reinterpret_cast<int*>(&m_defaultmode),
  149. {_("normal"), _("manual"), _("autoscroll")},
  150. {"normal", "manual", "autoscroll"},
  151. {}, "mode");
  152. result.add_path_ref(_("Path"), *this, get_path_ref(), "path-ref");
  153. if (get_walker() && get_path() && get_path()->is_valid()) {
  154. result.add_walk_mode(_("Path Mode"), &get_path()->m_mode, {}, {});
  155. result.add_bool(_("Adapt Speed"), &get_path()->m_adapt_speed, {}, {});
  156. result.add_path_handle(_("Handle"), m_path_handle, "handle");
  157. }
  158. return result;
  159. }
  160. void
  161. Camera::after_editor_set()
  162. {
  163. if (get_walker() && get_path() && get_path()->is_valid()) {
  164. if (m_defaultmode != Mode::AUTOSCROLL) {
  165. get_path()->m_nodes.clear();
  166. auto path_obj = get_path_gameobject();
  167. if(path_obj != nullptr)
  168. {
  169. path_obj->editor_delete();
  170. }
  171. }
  172. } else {
  173. if (m_defaultmode == Mode::AUTOSCROLL) {
  174. init_path_pos(Vector(0,0));
  175. }
  176. }
  177. }
  178. void
  179. Camera::save_state()
  180. {
  181. GameObject::save_state();
  182. PathObject::save_state();
  183. }
  184. void
  185. Camera::check_state()
  186. {
  187. GameObject::check_state();
  188. PathObject::check_state();
  189. }
  190. const Vector
  191. Camera::get_translation() const
  192. {
  193. Vector screen_size = m_screen_size.as_vector();
  194. return m_translation + ((screen_size * (get_current_scale() - 1.f)) / 2.f);
  195. }
  196. void
  197. Camera::set_translation_centered(const Vector& translation)
  198. {
  199. m_translation = translation - m_screen_size.as_vector() / 2;
  200. }
  201. Rectf
  202. Camera::get_rect() const
  203. {
  204. return Rectf::from_center(get_center(), m_screen_size);
  205. }
  206. void
  207. Camera::reset(const Vector& tuxpos)
  208. {
  209. m_translation.x = tuxpos.x - m_screen_size.width / 2.0f;
  210. m_translation.y = tuxpos.y - m_screen_size.height / 2.0f;
  211. m_shakespeed = 0;
  212. m_shaketimer.stop();
  213. keep_in_bounds(m_translation);
  214. m_cached_translation = m_translation;
  215. }
  216. void
  217. Camera::shake(float duration, float x, float y)
  218. {
  219. m_shaketimer.start(duration*100.f);
  220. m_shakedepth_x = x;
  221. m_shakedepth_y = y;
  222. m_shakespeed = math::PI_2 / duration;
  223. }
  224. void
  225. Camera::start_earthquake(float strength, float delay)
  226. {
  227. if (strength <= 0.f)
  228. {
  229. log_warning << "Invalid earthquake strength value provided. Setting to 3." << std::endl;
  230. strength = 3.f;
  231. }
  232. if (delay <= 0.f)
  233. {
  234. log_warning << "Invalid earthquake delay value provided. Setting to 0.05." << std::endl;
  235. delay = 0.05f;
  236. }
  237. m_earthquake = true;
  238. m_earthquake_strength = strength;
  239. m_earthquake_delay = delay;
  240. }
  241. void
  242. Camera::stop_earthquake()
  243. {
  244. m_translation.y -= m_earthquake_last_offset;
  245. m_cached_translation.y -= m_earthquake_last_offset;
  246. m_earthquake = false;
  247. m_earthquake_last_offset = 0.f;
  248. m_earthquake_delay_timer.stop();
  249. }
  250. void
  251. Camera::scroll_to(const Vector& goal, float scrolltime)
  252. {
  253. if(scrolltime == 0.0f)
  254. {
  255. m_translation.x = goal.x;
  256. m_translation.y = goal.y;
  257. m_mode = Mode::MANUAL;
  258. return;
  259. }
  260. m_scroll_from = m_translation;
  261. m_scroll_goal = goal;
  262. keep_in_bounds(m_scroll_goal);
  263. m_scroll_to_pos = 0;
  264. m_scrollspeed = 1.f / scrolltime;
  265. m_mode = Mode::SCROLLTO;
  266. }
  267. void
  268. Camera::draw(DrawingContext& context)
  269. {
  270. context.push_transform();
  271. context.transform().scale = get_current_scale();
  272. m_screen_size = Sizef(context.get_width(),
  273. context.get_height());
  274. context.pop_transform();
  275. }
  276. void
  277. Camera::update(float dt_sec)
  278. {
  279. // Minimum scale should be set during the update sequence; else, reset it.
  280. m_enfore_minimum_scale = false;
  281. switch (m_mode) {
  282. case Mode::NORMAL:
  283. if (Sector::current() && Sector::current()->get_object_count<Player>() > 1)
  284. {
  285. update_scroll_normal_multiplayer(dt_sec);
  286. }
  287. else
  288. {
  289. update_scroll_normal(dt_sec);
  290. }
  291. break;
  292. case Mode::MANUAL:
  293. keep_in_bounds(m_translation);
  294. break;
  295. case Mode::AUTOSCROLL:
  296. update_scroll_autoscroll(dt_sec);
  297. break;
  298. case Mode::SCROLLTO:
  299. update_scroll_to(dt_sec);
  300. break;
  301. default:
  302. break;
  303. }
  304. update_scale(dt_sec);
  305. update_shake();
  306. update_earthquake();
  307. }
  308. void
  309. Camera::keep_in_bounds(const Rectf& bounds)
  310. {
  311. // Determines the difference between normal and scaled translation.
  312. const Vector scale_factor = (m_screen_size.as_vector() * (get_current_scale() - 1.f)) / 2.f;
  313. // Keep the translation's scaled position in provided bounds.
  314. m_translation.x = (bounds.get_width() > m_screen_size.width ?
  315. math::clamp(m_translation.x + scale_factor.x, bounds.get_left(), bounds.get_right() - m_screen_size.width) :
  316. bounds.get_left());
  317. m_translation.y = (bounds.get_height() > m_screen_size.height ?
  318. math::clamp(m_translation.y + scale_factor.y, bounds.get_top(), bounds.get_bottom() - m_screen_size.height) :
  319. bounds.get_top());
  320. // Remove any scale factor we may have added in the checks above.
  321. m_translation -= scale_factor;
  322. }
  323. void
  324. Camera::keep_in_bounds(Vector& translation_)
  325. {
  326. float width = d_sector->get_width();
  327. float height = d_sector->get_height();
  328. // Remove any earthquake offset from the translation.
  329. translation_.y -= m_earthquake_last_offset;
  330. if (m_mode == Mode::MANUAL)
  331. {
  332. // Determines the difference between normal and scaled translation.
  333. const Vector scale_factor = (m_screen_size.as_vector() * (get_current_scale() - 1.f)) / 2.f;
  334. // Keep the translation's scaled position in sector bounds.
  335. translation_.x = math::clamp(translation_.x + scale_factor.x, 0.0f, width - m_screen_size.width);
  336. translation_.y = math::clamp(translation_.y + scale_factor.y, 0.0f, height - m_screen_size.height);
  337. // Remove any scale factor we may have added in the checks above.
  338. translation_ -= scale_factor;
  339. }
  340. else
  341. {
  342. // Don't scroll before the start or after the sector's end.
  343. translation_.x = math::clamp(translation_.x, 0.0f, width - m_screen_size.width);
  344. translation_.y = math::clamp(translation_.y, 0.0f, height - m_screen_size.height);
  345. }
  346. // Add any earthquake offset we may have removed earlier.
  347. translation_.y += m_earthquake_last_offset;
  348. if (height < m_screen_size.height)
  349. translation_.y = height / 2.0f - m_screen_size.height / 2.0f;
  350. if (width < m_screen_size.width)
  351. translation_.x = width / 2.0f - m_screen_size.width / 2.0f;
  352. }
  353. void
  354. Camera::update_shake()
  355. {
  356. if (m_shaketimer.started()) {
  357. // Old method:
  358. // m_translation.x -= sinf(m_shaketimer.get_timegone() * m_shakespeed) * m_shakedepth_x;
  359. // m_translation.y -= sinf(m_shaketimer.get_timegone() * m_shakespeed) * m_shakedepth_y;
  360. // Elastic easing:
  361. m_translation.x -= m_shakedepth_x * ((std::pow(2.f, -0.8f * (m_shakespeed * m_shaketimer.get_timegone()))) *
  362. std::sin(((0.8f * m_shakespeed * m_shaketimer.get_timegone()) - 0.75f) * (2.f * math::PI) / 3.f));
  363. m_translation.y -= m_shakedepth_y * ((std::pow(2.f, -0.8f * (m_shakespeed * m_shaketimer.get_timegone()))) *
  364. std::sin(((0.8f * m_shakespeed * m_shaketimer.get_timegone()) - 0.75f) * (2.f * math::PI) / 3.f));
  365. }
  366. }
  367. void
  368. Camera::update_earthquake()
  369. {
  370. if (!m_earthquake)
  371. return;
  372. if (m_earthquake_delay_timer.check())
  373. {
  374. if (m_earthquake_last_offset == 0.f)
  375. {
  376. m_earthquake_last_offset = m_earthquake_strength * graphicsRandom.randf(-2, 2);
  377. m_translation.y += m_earthquake_last_offset;
  378. m_cached_translation.y += m_earthquake_last_offset;
  379. }
  380. else
  381. {
  382. m_translation.y -= m_earthquake_last_offset;
  383. m_cached_translation.y -= m_earthquake_last_offset;
  384. m_earthquake_last_offset = 0.f;
  385. }
  386. m_earthquake_delay_timer.start(m_earthquake_delay + static_cast<float>(graphicsRandom.rand(0, 1)));
  387. }
  388. else if (!m_earthquake_delay_timer.started())
  389. {
  390. m_earthquake_delay_timer.start(m_earthquake_delay + static_cast<float>(graphicsRandom.rand(0, 1)));
  391. }
  392. }
  393. void
  394. Camera::update_scroll_normal(float dt_sec)
  395. {
  396. Player& player = *d_sector->get_players()[0];
  397. // TODO: Co-op mode needs a good camera.
  398. const Vector player_pos(player.get_bbox().get_left(),
  399. player.get_bbox().get_bottom());
  400. static Vector last_player_pos = player_pos;
  401. const Vector player_delta = player_pos - last_player_pos;
  402. last_player_pos = player_pos;
  403. // Check that we don't have division by zero later.
  404. if (dt_sec < CAMERA_EPSILON)
  405. return;
  406. /****** Vertical Scrolling part. ******/
  407. if (!player.is_dying())
  408. {
  409. m_cached_translation.y = math::clamp(m_cached_translation.y,
  410. player_pos.y - m_screen_size.height * 0.67f,
  411. player_pos.y - m_screen_size.height * 0.33f);
  412. }
  413. m_translation.y = m_cached_translation.y;
  414. if (!player.is_dying())
  415. {
  416. const float top_edge = m_screen_size.height * 0.3f;
  417. const float bottom_edge = m_screen_size.height * 0.7f;
  418. const float translation_compensation_y = player_pos.y - m_translation.y;
  419. float peek_to_y = 0;
  420. if (player.peeking_direction_y() == Direction::UP)
  421. peek_to_y = bottom_edge - translation_compensation_y;
  422. else if (player.peeking_direction_y() == Direction::DOWN)
  423. peek_to_y = top_edge - translation_compensation_y;
  424. float peek_move_y = (peek_to_y - m_peek_pos.y) * PEEK_ARRIVE_RATIO;
  425. if (fabsf(peek_move_y) < 1.0f)
  426. peek_move_y = 0.0;
  427. m_peek_pos.y += peek_move_y;
  428. m_translation.y -= m_peek_pos.y;
  429. m_translation.y = math::clamp(m_translation.y,
  430. player_pos.y - m_screen_size.height * 0.7f,
  431. player_pos.y - m_screen_size.height * 0.3f);
  432. m_cached_translation.y = math::clamp(m_cached_translation.y,
  433. player_pos.y - m_screen_size.height * 0.7f,
  434. player_pos.y - m_screen_size.height * 0.3f);
  435. }
  436. /****** Horizontal scrolling part *******/
  437. if (!player.is_dying())
  438. {
  439. const float left_end = m_screen_size.width * 0.4f;
  440. const float right_end = m_screen_size.width * 0.6f;
  441. if (player_delta.x < -CAMERA_EPSILON)
  442. {
  443. // Walking left.
  444. m_lookahead_pos.x -= player_delta.x * 0.8f;
  445. if (m_lookahead_pos.x > right_end)
  446. m_lookahead_pos.x = right_end;
  447. }
  448. else if (player_delta.x > CAMERA_EPSILON)
  449. {
  450. // Walking right.
  451. m_lookahead_pos.x -= player_delta.x * 0.8f;
  452. if (m_lookahead_pos.x < left_end)
  453. m_lookahead_pos.x = left_end;
  454. }
  455. else
  456. {
  457. m_lookahead_pos.x = math::clamp(m_lookahead_pos.x, left_end, right_end);
  458. }
  459. // Adjust for level ends.
  460. if (player_pos.x < left_end)
  461. m_lookahead_pos.x = left_end;
  462. if (player_pos.x > d_sector->get_width() - left_end)
  463. m_lookahead_pos.x = right_end;
  464. m_cached_translation.x = player_pos.x - m_lookahead_pos.x;
  465. }
  466. m_translation.x = m_cached_translation.x;
  467. if (!player.is_dying())
  468. {
  469. const float left_edge = m_screen_size.width * 0.1666f;
  470. const float right_edge = m_screen_size.width * 0.8334f;
  471. const float translation_compensation_x = player_pos.x - m_translation.x;
  472. float peek_to_x = 0;
  473. if (player.peeking_direction_x() == Direction::LEFT)
  474. peek_to_x = right_edge - translation_compensation_x;
  475. else if (player.peeking_direction_x() == Direction::RIGHT)
  476. peek_to_x = left_edge - translation_compensation_x;
  477. float peek_move_x = (peek_to_x - m_peek_pos.x) * PEEK_ARRIVE_RATIO;
  478. if (fabsf(peek_move_x) < 1.0f)
  479. peek_move_x = 0.0f;
  480. m_peek_pos.x += peek_move_x;
  481. m_translation.x -= m_peek_pos.x;
  482. m_translation.x = math::clamp(m_translation.x,
  483. player_pos.x - m_screen_size.width * 0.8334f,
  484. player_pos.x - m_screen_size.width * 0.1666f);
  485. m_cached_translation.x = math::clamp(m_cached_translation.x,
  486. player_pos.x - m_screen_size.width * 0.8334f,
  487. player_pos.x - m_screen_size.width * 0.1666f);
  488. }
  489. keep_in_bounds(m_translation);
  490. keep_in_bounds(m_cached_translation);
  491. }
  492. void
  493. Camera::update_scroll_normal_multiplayer(float dt_sec)
  494. {
  495. m_enfore_minimum_scale = true;
  496. float x1 = Sector::get().get_width();
  497. float y1 = Sector::get().get_height();
  498. float x2 = 0.f;
  499. float y2 = 0.f;
  500. for (const auto* p : Sector::get().get_players())
  501. {
  502. if (p->is_dead() || p->is_dying())
  503. continue;
  504. float lft = p->get_bbox().get_left() - HORIZONTAL_MARGIN;
  505. float rgt = p->get_bbox().get_right() + HORIZONTAL_MARGIN;
  506. float top = p->get_bbox().get_top() - VERTICAL_MARGIN;
  507. float btm = p->get_bbox().get_bottom() + VERTICAL_MARGIN;
  508. if (p->peeking_direction_x() == Direction::LEFT)
  509. lft -= PEEK_ADD_HORIZONTAL_MARGIN;
  510. else if (p->peeking_direction_x() == Direction::RIGHT)
  511. rgt += PEEK_ADD_HORIZONTAL_MARGIN;
  512. if (p->peeking_direction_y() == Direction::UP)
  513. top -= PEEK_ADD_VERTICAL_MARGIN;
  514. else if (p->peeking_direction_y() == Direction::DOWN)
  515. btm += PEEK_ADD_VERTICAL_MARGIN;
  516. x1 = std::min(x1, lft);
  517. x2 = std::max(x2, rgt);
  518. y1 = std::min(y1, top);
  519. y2 = std::max(y2, btm);
  520. }
  521. // Might happen if all players are dead.
  522. if (x2 < x1 || y2 < y1)
  523. return;
  524. Rectf cover(std::max(0.f, x1),
  525. std::max(0.f, y1),
  526. std::min(Sector::get().get_width(), x2),
  527. std::min(Sector::get().get_height(), y2));
  528. float scale = std::min(static_cast<float>(SCREEN_WIDTH) / static_cast<float>(cover.get_width()),
  529. static_cast<float>(SCREEN_HEIGHT) / static_cast<float>(cover.get_height()));
  530. float max_scale = std::max(static_cast<float>(SCREEN_WIDTH) / Sector::get().get_width(),
  531. static_cast<float>(SCREEN_HEIGHT) / Sector::get().get_height());
  532. // Capping at `m_scale` allows fixing a minor bug where the camera would
  533. // sometimes be slightly off-sector if scaling goes faster than moving.
  534. scale = math::clamp(scale, max_scale, m_scale);
  535. // Can't use m_screen_size because it varies depending on the scale
  536. auto rect = Rectf::from_center(Vector((cover.get_left() + cover.get_right()) / 2.f, (cover.get_top() + cover.get_bottom()) / 2.f),
  537. Sizef(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)));
  538. auto true_rect = Rectf::from_center(Vector((cover.get_left() + cover.get_right()) / 2.f, (cover.get_top() + cover.get_bottom()) / 2.f),
  539. Sizef(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)) / scale);
  540. if (true_rect.get_left() < 0.f)
  541. rect.move(Vector(-true_rect.get_left(), 0.f));
  542. if (true_rect.get_top() < 0.f)
  543. rect.move(Vector(0.f, -true_rect.get_top()));
  544. if (true_rect.get_right() > Sector::get().get_width())
  545. rect.move(Vector(Sector::get().get_width() - true_rect.get_right(), 0.f));
  546. if (true_rect.get_bottom() > Sector::get().get_height())
  547. rect.move(Vector(0.f, Sector::get().get_height() - true_rect.get_bottom()));
  548. m_translation = m_translation * (1.f - MULTIPLAYER_CAM_WEIGHT) + rect.p1() * MULTIPLAYER_CAM_WEIGHT;
  549. m_minimum_scale = m_minimum_scale * (1.f - MULTIPLAYER_CAM_WEIGHT) + scale * MULTIPLAYER_CAM_WEIGHT;
  550. }
  551. void
  552. Camera::update_scroll_autoscroll(float dt_sec)
  553. {
  554. if (!d_sector->get_object_count<Player>([](const Player& p) { return !p.is_dead() && !p.is_dying(); }))
  555. return;
  556. get_walker()->update(dt_sec);
  557. // TODO: Get the camera size?
  558. m_translation = get_walker()->get_pos(Sizef(), m_path_handle);
  559. keep_in_bounds(m_translation);
  560. }
  561. void
  562. Camera::update_scroll_to(float dt_sec)
  563. {
  564. m_scroll_to_pos += dt_sec * m_scrollspeed;
  565. if (m_scroll_to_pos >= 1.0f)
  566. {
  567. m_mode = Mode::MANUAL;
  568. m_translation = m_scroll_goal;
  569. // If a scale is active and wouldn't finish this frame, reload it with the remaining time,
  570. // setting the initial scale values from the scroll destination.
  571. if (m_scale_time_remaining - dt_sec > 0.f)
  572. {
  573. m_scale_time_total -= m_scale_time_total - m_scale_time_remaining;
  574. reload_scale();
  575. }
  576. else
  577. {
  578. // In case a scale finishes this frame, set its target translation to the scroll destination.
  579. m_scale_target_translation = m_translation;
  580. }
  581. return;
  582. }
  583. m_translation = m_scroll_from + (m_scroll_goal - m_scroll_from) * m_scroll_to_pos;
  584. }
  585. void
  586. Camera::update_scale(float dt_sec)
  587. {
  588. if (m_scale_time_remaining > 0.f)
  589. {
  590. m_scale_time_remaining -= dt_sec;
  591. if (m_scale_time_remaining <= 0.f)
  592. {
  593. m_scale = m_scale_target;
  594. if (m_mode == Mode::MANUAL)
  595. m_translation = m_scale_target_translation;
  596. m_scale_time_remaining = 0.f;
  597. m_scale_time_total = 0.f;
  598. }
  599. else
  600. {
  601. float time_progress = (m_scale_time_total - m_scale_time_remaining) / m_scale_time_total;
  602. float progress = static_cast<float>(m_scale_easing(static_cast<double>(time_progress)));
  603. m_scale = m_scale_origin + (m_scale_target - m_scale_origin) * progress;
  604. /** MANUAL mode scale management */
  605. if (m_mode == Mode::MANUAL)
  606. {
  607. // Move camera to the target translation, when zooming in manual mode.
  608. m_translation = m_scale_origin_translation + (m_scale_target_translation - m_scale_origin_translation) * progress;
  609. keep_in_bounds(m_translation);
  610. return;
  611. }
  612. }
  613. // Re-center camera, when zooming in normal mode.
  614. m_lookahead_pos /= 1.01f;
  615. }
  616. // In MANUAL mode, the translation is managed only when a scale is active.
  617. // In SCROLLTO mode, the translation is managed in update_scroll_to().
  618. if (m_mode == Mode::MANUAL || m_mode == Mode::SCROLLTO)
  619. return;
  620. // FIXME: Poor design: This shouldn't pose a problem to multiplayer.
  621. if (m_mode == Mode::NORMAL && Sector::current()->get_object_count<Player>() > 1)
  622. return;
  623. Vector screen_size = m_screen_size.as_vector();
  624. m_translation += screen_size * (1.f - get_current_scale()) / 2.f;
  625. }
  626. /** Get target scale position from the anchor point (m_scale_anchor). */
  627. Vector
  628. Camera::get_scale_anchor_target() const
  629. {
  630. // Get target center position from the anchor, and afterwards, top-left position from the center position.
  631. return get_anchor_center_pos(Rectf(m_translation,
  632. Sizef(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT))),
  633. m_scale_anchor) - Vector(static_cast<float>(SCREEN_WIDTH) / 2, static_cast<float>(SCREEN_HEIGHT) / 2);
  634. }
  635. /** Reload easing scale from the current position. */
  636. void
  637. Camera::reload_scale()
  638. {
  639. m_scale_origin = m_scale;
  640. m_scale_origin_translation = m_translation;
  641. m_scale_target_translation = get_scale_anchor_target();
  642. m_scale_time_remaining = m_scale_time_total;
  643. }
  644. void
  645. Camera::ease_scale(float scale, float time, easing ease, AnchorPoint anchor)
  646. {
  647. m_scale_anchor = anchor;
  648. if (time <= 0.f)
  649. {
  650. m_scale = scale;
  651. if (m_mode == Mode::MANUAL)
  652. m_translation = get_scale_anchor_target();
  653. }
  654. else
  655. {
  656. m_scale_target = scale;
  657. m_scale_time_total = time;
  658. m_scale_easing = ease;
  659. reload_scale();
  660. }
  661. }
  662. Vector
  663. Camera::get_center() const
  664. {
  665. return m_translation + Vector(m_screen_size.width / 2.0f,
  666. m_screen_size.height / 2.0f);
  667. }
  668. const Vector&
  669. Camera::get_position() const
  670. {
  671. return m_translation;
  672. }
  673. const Sizef&
  674. Camera::get_screen_size() const
  675. {
  676. return m_screen_size;
  677. }
  678. void
  679. Camera::move(const Vector& offset)
  680. {
  681. scroll_to(m_translation + offset, 0.0f);
  682. }
  683. bool
  684. Camera::is_saveable() const
  685. {
  686. return !(Level::current() &&
  687. Level::current()->is_worldmap());
  688. }
  689. /* EOF */