💿🐜 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
3.9 KiB

  1. /*
  2. * Copyright (C) 2023 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/log.hpp"
  28. #include "game/strings.hpp"
  29. #include "utility/hash/fnv1a.hpp"
  30. using namespace hash::literals;
  31. namespace game {
  32. namespace state {
  33. credits::credits(game::context& ctx):
  34. game::state::base(ctx)
  35. {
  36. debug::log::trace("Entering credits state...");
  37. // Construct credits text
  38. credits_text.set_material(&ctx.menu_font_material);
  39. credits_text.set_font(&ctx.menu_font);
  40. credits_text.set_color({1.0f, 1.0f, 1.0f, 0.0f});
  41. credits_text.set_content(get_string(ctx, "credits"_fnv1a32));
  42. // Align credits text
  43. const auto& credits_aabb = static_cast<const geom::aabb<float>&>(credits_text.get_local_bounds());
  44. float credits_w = credits_aabb.max_point.x() - credits_aabb.min_point.x();
  45. float credits_h = credits_aabb.max_point.y() - credits_aabb.min_point.y();
  46. credits_text.set_translation({std::round(-credits_w * 0.5f), std::round(-credits_h * 0.5f), 0.0f});
  47. credits_text.update_tweens();
  48. // Set up animation timing configuration
  49. const double credits_fade_in_duration = 0.5;
  50. const double credits_scroll_duration = 5.0;
  51. auto set_credits_opacity = [this](int channel, const float& opacity)
  52. {
  53. this->credits_text.set_color({1.0f, 1.0f, 1.0f, opacity});
  54. };
  55. // Build credits fade in animation
  56. credits_fade_in_animation.set_interpolator(ease<float>::in_quad);
  57. animation_channel<float>* credits_fade_in_opacity_channel = credits_fade_in_animation.add_channel(0);
  58. credits_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
  59. credits_fade_in_opacity_channel->insert_keyframe({credits_fade_in_duration, 1.0f});
  60. credits_fade_in_animation.set_frame_callback(set_credits_opacity);
  61. // Add credits animations to animator
  62. ctx.animator->add_animation(&credits_fade_in_animation);
  63. // Start credits fade in animation
  64. credits_fade_in_animation.play();
  65. // Set up credits skipper
  66. input_mapped_subscription = ctx.input_mapper.get_input_mapped_channel().subscribe
  67. (
  68. [this, &ctx](const auto& event)
  69. {
  70. auto mapping_type = event.mapping->get_mapping_type();
  71. if (mapping_type != input::mapping_type::gamepad_axis &&
  72. mapping_type != input::mapping_type::mouse_motion &&
  73. mapping_type != input::mapping_type::mouse_scroll)
  74. {
  75. if (this->credits_text.get_color()[3] > 0.0f)
  76. {
  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_mapper.connect(ctx.app->get_device_manager().get_event_queue());
  85. ctx.ui_scene->add_object(&credits_text);
  86. debug::log::trace("Entered credits state");
  87. }
  88. credits::~credits()
  89. {
  90. debug::log::trace("Exiting credits state...");
  91. // Disable credits skipper
  92. ctx.input_mapper.disconnect();
  93. // Destruct credits text
  94. ctx.ui_scene->remove_object(&credits_text);
  95. // Destruct credits animations
  96. ctx.animator->remove_animation(&credits_fade_in_animation);
  97. debug::log::trace("Exited credits state");
  98. }
  99. } // namespace state
  100. } // namespace game