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

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