💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.0 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "game/state/credits.hpp"
  20. #include "game/state/extras-menu.hpp"
  21. #include "game/context.hpp"
  22. #include "animation/ease.hpp"
  23. #include "animation/animation.hpp"
  24. #include "animation/animator.hpp"
  25. #include "application.hpp"
  26. #include "scene/text.hpp"
  27. #include "debug/logger.hpp"
  28. namespace game {
  29. namespace state {
  30. credits::credits(game::context& ctx):
  31. game::state::base(ctx)
  32. {
  33. ctx.logger->push_task("Entering credits state");
  34. // Construct credits text
  35. credits_text.set_material(&ctx.menu_font_material);
  36. credits_text.set_font(&ctx.menu_font);
  37. credits_text.set_color({1.0f, 1.0f, 1.0f, 0.0f});
  38. credits_text.set_content((*ctx.strings)["credits"]);
  39. // Align credits text
  40. const auto& credits_aabb = static_cast<const geom::aabb<float>&>(credits_text.get_local_bounds());
  41. float credits_w = credits_aabb.max_point.x - credits_aabb.min_point.x;
  42. float credits_h = credits_aabb.max_point.y - credits_aabb.min_point.y;
  43. credits_text.set_translation({std::round(-credits_w * 0.5f), std::round(-credits_h * 0.5f), 0.0f});
  44. credits_text.update_tweens();
  45. // Load animation timing configuration
  46. double credits_fade_in_duration = 0.0;
  47. double credits_scroll_duration = 0.0;
  48. if (ctx.config->contains("credits_fade_in_duration"))
  49. credits_fade_in_duration = (*ctx.config)["credits_fade_in_duration"].get<double>();
  50. if (ctx.config->contains("credits_scroll_duration"))
  51. credits_scroll_duration = (*ctx.config)["credits_scroll_duration"].get<double>();
  52. auto set_credits_opacity = [this](int channel, const float& opacity)
  53. {
  54. this->credits_text.set_color({1.0f, 1.0f, 1.0f, opacity});
  55. };
  56. // Build credits fade in animation
  57. credits_fade_in_animation.set_interpolator(ease<float>::in_quad);
  58. animation_channel<float>* credits_fade_in_opacity_channel = credits_fade_in_animation.add_channel(0);
  59. credits_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
  60. credits_fade_in_opacity_channel->insert_keyframe({credits_fade_in_duration, 1.0f});
  61. credits_fade_in_animation.set_frame_callback(set_credits_opacity);
  62. // Add credits animations to animator
  63. ctx.animator->add_animation(&credits_fade_in_animation);
  64. // Start credits fade in animation
  65. credits_fade_in_animation.play();
  66. // Set up credits skipper
  67. ctx.input_listener->set_callback
  68. (
  69. [this, &ctx](const event_base& event)
  70. {
  71. auto id = event.get_event_type_id();
  72. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  73. {
  74. if (this->credits_text.get_color()[3] > 0.0f)
  75. {
  76. ctx.input_listener->set_enabled(false);
  77. // Change state
  78. ctx.state_machine.pop();
  79. ctx.state_machine.emplace(new game::state::extras_menu(ctx));
  80. }
  81. }
  82. }
  83. );
  84. ctx.input_listener->set_enabled(true);
  85. ctx.ui_scene->add_object(&credits_text);
  86. ctx.logger->pop_task(EXIT_SUCCESS);
  87. }
  88. credits::~credits()
  89. {
  90. ctx.logger->push_task("Exiting credits state");
  91. // Disable credits skipper
  92. ctx.input_listener->set_enabled(false);
  93. ctx.input_listener->set_callback(nullptr);
  94. // Destruct credits text
  95. ctx.ui_scene->remove_object(&credits_text);
  96. // Destruct credits animations
  97. ctx.animator->remove_animation(&credits_fade_in_animation);
  98. ctx.logger->pop_task(EXIT_SUCCESS);
  99. }
  100. } // namespace state
  101. } // namespace game