💿🐜 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.

154 lines
4.7 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/states/credits-state.hpp"
  20. #include "game/states/extras-menu-state.hpp"
  21. #include "game/game.hpp"
  22. #include <engine/animation/ease.hpp>
  23. #include <engine/animation/animation.hpp>
  24. #include <engine/animation/animator.hpp>
  25. #include <engine/scene/text.hpp>
  26. #include <engine/debug/log.hpp>
  27. #include "game/strings.hpp"
  28. #include <engine/utility/hash/fnv1a.hpp>
  29. #include <engine/math/vector.hpp>
  30. credits_state::credits_state(::game& ctx):
  31. game_state(ctx)
  32. {
  33. debug::log_trace("Entering credits state...");
  34. const math::fvec2 viewport_size = math::fvec2(ctx.window->get_viewport_size());
  35. const math::fvec2 viewport_center = viewport_size * 0.5f;
  36. // Construct credits text
  37. credits_text.set_material(ctx.menu_font_material);
  38. credits_text.set_font(&ctx.menu_font);
  39. credits_text.set_color({1.0f, 1.0f, 1.0f, 0.0f});
  40. credits_text.set_content(get_string(ctx, "credits"));
  41. // Align credits text
  42. const auto& credits_aabb = credits_text.get_bounds();
  43. float credits_w = credits_aabb.max.x() - credits_aabb.min.x();
  44. float credits_h = credits_aabb.max.y() - credits_aabb.min.y();
  45. credits_text.set_translation({std::round(viewport_center.x() - credits_w * 0.5f), std::round(viewport_center.y() - credits_h * 0.5f), 0.0f});
  46. // Set up animation timing configuration
  47. const float credits_fade_in_duration = 0.5;
  48. const float credits_scroll_duration = 5.0;
  49. auto set_credits_opacity = [this](int channel, const float& opacity)
  50. {
  51. this->credits_text.set_color({1.0f, 1.0f, 1.0f, opacity});
  52. };
  53. // Build credits fade in animation
  54. credits_fade_in_animation.set_interpolator(ease<float>::in_quad);
  55. animation_channel<float>* credits_fade_in_opacity_channel = credits_fade_in_animation.add_channel(0);
  56. credits_fade_in_opacity_channel->insert_keyframe({0.0f, 0.0f});
  57. credits_fade_in_opacity_channel->insert_keyframe({credits_fade_in_duration, 1.0f});
  58. credits_fade_in_animation.set_frame_callback(set_credits_opacity);
  59. // Add credits animations to animator
  60. ctx.animator->add_animation(&credits_fade_in_animation);
  61. // Start credits fade in animation
  62. credits_fade_in_animation.play();
  63. // Setup window resized callback
  64. window_resized_subscription = ctx.window->get_resized_channel().subscribe
  65. (
  66. [&](const auto& event)
  67. {
  68. const math::fvec2 viewport_size = math::fvec2(event.window->get_viewport_size());
  69. const math::fvec2 viewport_center = viewport_size * 0.5f;
  70. const auto& credits_aabb = credits_text.get_bounds();
  71. float credits_w = credits_aabb.max.x() - credits_aabb.min.x();
  72. float credits_h = credits_aabb.max.y() - credits_aabb.min.y();
  73. credits_text.set_translation({std::round(viewport_center.x() - credits_w * 0.5f), std::round(viewport_center.y() - credits_h * 0.5f), 0.0f});
  74. }
  75. );
  76. // Construct credits skip function
  77. auto skip = [&](const auto& event)
  78. {
  79. ctx.function_queue.emplace
  80. (
  81. [&]()
  82. {
  83. // Change to extras menu state
  84. ctx.state_machine.pop();
  85. ctx.state_machine.emplace(std::make_unique<extras_menu_state>(ctx));
  86. }
  87. );
  88. };
  89. // Set up credits skippers
  90. input_mapped_subscriptions.emplace_back
  91. (
  92. ctx.input_mapper.get_gamepad_button_mapped_channel().subscribe
  93. (
  94. skip
  95. )
  96. );
  97. input_mapped_subscriptions.emplace_back
  98. (
  99. ctx.input_mapper.get_key_mapped_channel().subscribe
  100. (
  101. skip
  102. )
  103. );
  104. input_mapped_subscriptions.emplace_back
  105. (
  106. ctx.input_mapper.get_mouse_button_mapped_channel().subscribe
  107. (
  108. skip
  109. )
  110. );
  111. // Enable credits skippers next frame
  112. ctx.function_queue.push
  113. (
  114. [&]()
  115. {
  116. ctx.input_mapper.connect(ctx.input_manager->get_event_dispatcher());
  117. }
  118. );
  119. ctx.ui_scene->add_object(credits_text);
  120. debug::log_trace("Entered credits state");
  121. }
  122. credits_state::~credits_state()
  123. {
  124. debug::log_trace("Exiting credits state...");
  125. // Disable credits skippers
  126. ctx.input_mapper.disconnect();
  127. input_mapped_subscriptions.clear();
  128. // Destruct credits text
  129. ctx.ui_scene->remove_object(credits_text);
  130. // Destruct credits animations
  131. ctx.animator->remove_animation(&credits_fade_in_animation);
  132. debug::log_trace("Exited credits state");
  133. }