camera.cpp 25 KB

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