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

142 lines
4.9 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/states/credits.hpp"
  20. #include "game/states/main-menu.hpp"
  21. #include "animation/ease.hpp"
  22. #include "animation/animation.hpp"
  23. #include "animation/animator.hpp"
  24. #include "application.hpp"
  25. #include "scene/text.hpp"
  26. #include "render/passes/clear-pass.hpp"
  27. namespace game {
  28. namespace state {
  29. namespace credits {
  30. void enter(game::context* ctx)
  31. {
  32. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  33. // Construct credits text
  34. ctx->credits_text = new scene::text();
  35. ctx->credits_text->set_material(&ctx->menu_font_material);
  36. ctx->credits_text->set_font(&ctx->menu_font);
  37. ctx->credits_text->set_color({1.0f, 1.0f, 1.0f, 0.0f});
  38. ctx->credits_text->set_content((*ctx->strings)["credits"]);
  39. // Align credits text
  40. const auto& credits_aabb = static_cast<const geom::aabb<float>&>(ctx->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. ctx->credits_text->set_translation({std::round(-credits_w * 0.5f), std::round(-credits_h * 0.5f), 0.0f});
  44. // Load animation timing configuration
  45. double credits_fade_in_duration = 0.0;
  46. double credits_scroll_duration = 0.0;
  47. if (ctx->config->contains("credits_fade_in_duration"))
  48. credits_fade_in_duration = (*ctx->config)["credits_fade_in_duration"].get<double>();
  49. if (ctx->config->contains("credits_scroll_duration"))
  50. credits_scroll_duration = (*ctx->config)["credits_scroll_duration"].get<double>();
  51. auto set_credits_opacity = [ctx](int channel, const float& opacity)
  52. {
  53. ctx->credits_text->set_color({1.0f, 1.0f, 1.0f, opacity});
  54. };
  55. // Build credits fade in animation
  56. ctx->credits_fade_in_animation = new animation<float>();
  57. animation_channel<float>* credits_fade_in_opacity_channel = ctx->credits_fade_in_animation->add_channel(0);
  58. ctx->credits_fade_in_animation->set_interpolator(ease<float>::in_quad);
  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. ctx->credits_fade_in_animation->set_frame_callback(set_credits_opacity);
  62. // Build credits scroll in animation
  63. ctx->credits_scroll_animation = new animation<float>();
  64. // Trigger credits scroll animation after credits fade in animation ends
  65. ctx->credits_fade_in_animation->set_end_callback
  66. (
  67. [ctx]()
  68. {
  69. ctx->credits_scroll_animation->play();
  70. }
  71. );
  72. // Add credits animations to animator
  73. ctx->animator->add_animation(ctx->credits_fade_in_animation);
  74. ctx->animator->add_animation(ctx->credits_scroll_animation);
  75. // Start credits fade in animation
  76. ctx->credits_fade_in_animation->play();
  77. // Set up credits skipper
  78. ctx->input_listener->set_callback
  79. (
  80. [ctx](const event_base& event)
  81. {
  82. auto id = event.get_event_type_id();
  83. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  84. {
  85. if (ctx->credits_text->get_color()[3] > 0.0f)
  86. {
  87. ctx->input_listener->set_enabled(false);
  88. // Change state
  89. application::state next_state;
  90. next_state.name = "main_menu";
  91. next_state.enter = std::bind(game::state::main_menu::enter, ctx, 2);
  92. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  93. ctx->app->change_state(next_state);
  94. }
  95. }
  96. }
  97. );
  98. ctx->input_listener->set_enabled(true);
  99. ctx->ui_scene->add_object(ctx->credits_text);
  100. ctx->credits_text->update_tweens();
  101. }
  102. void exit(game::context* ctx)
  103. {
  104. // Disable credits skipper
  105. ctx->input_listener->set_enabled(false);
  106. ctx->input_listener->set_callback(nullptr);
  107. // Destruct credits text
  108. ctx->ui_scene->remove_object(ctx->credits_text);
  109. delete ctx->credits_text;
  110. ctx->credits_text = nullptr;
  111. // Destruct credits animations
  112. ctx->animator->remove_animation(ctx->credits_fade_in_animation);
  113. ctx->animator->remove_animation(ctx->credits_scroll_animation);
  114. delete ctx->credits_fade_in_animation;
  115. delete ctx->credits_scroll_animation;
  116. ctx->credits_fade_in_animation = nullptr;
  117. ctx->credits_scroll_animation = nullptr;
  118. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  119. }
  120. } // namespace credits
  121. } // namespace state
  122. } // namespace game