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

143 lines
4.8 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. ctx.credits_text = new scene::text();
  36. ctx.credits_text->set_material(&ctx.menu_font_material);
  37. ctx.credits_text->set_font(&ctx.menu_font);
  38. ctx.credits_text->set_color({1.0f, 1.0f, 1.0f, 0.0f});
  39. ctx.credits_text->set_content((*ctx.strings)["credits"]);
  40. // Align credits text
  41. const auto& credits_aabb = static_cast<const geom::aabb<float>&>(ctx.credits_text->get_local_bounds());
  42. float credits_w = credits_aabb.max_point.x - credits_aabb.min_point.x;
  43. float credits_h = credits_aabb.max_point.y - credits_aabb.min_point.y;
  44. ctx.credits_text->set_translation({std::round(-credits_w * 0.5f), std::round(-credits_h * 0.5f), 0.0f});
  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 = [&ctx](int channel, const float& opacity)
  53. {
  54. ctx.credits_text->set_color({1.0f, 1.0f, 1.0f, opacity});
  55. };
  56. // Build credits fade in animation
  57. ctx.credits_fade_in_animation = new animation<float>();
  58. animation_channel<float>* credits_fade_in_opacity_channel = ctx.credits_fade_in_animation->add_channel(0);
  59. ctx.credits_fade_in_animation->set_interpolator(ease<float>::in_quad);
  60. credits_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
  61. credits_fade_in_opacity_channel->insert_keyframe({credits_fade_in_duration, 1.0f});
  62. ctx.credits_fade_in_animation->set_frame_callback(set_credits_opacity);
  63. // Build credits scroll in animation
  64. ctx.credits_scroll_animation = new animation<float>();
  65. // Trigger credits scroll animation after credits fade in animation ends
  66. ctx.credits_fade_in_animation->set_end_callback
  67. (
  68. [&ctx]()
  69. {
  70. ctx.credits_scroll_animation->play();
  71. }
  72. );
  73. // Add credits animations to animator
  74. ctx.animator->add_animation(ctx.credits_fade_in_animation);
  75. ctx.animator->add_animation(ctx.credits_scroll_animation);
  76. // Start credits fade in animation
  77. ctx.credits_fade_in_animation->play();
  78. // Set up credits skipper
  79. ctx.input_listener->set_callback
  80. (
  81. [&ctx](const event_base& event)
  82. {
  83. auto id = event.get_event_type_id();
  84. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  85. {
  86. if (ctx.credits_text->get_color()[3] > 0.0f)
  87. {
  88. ctx.input_listener->set_enabled(false);
  89. // Change state
  90. ctx.state_machine.pop();
  91. ctx.state_machine.emplace(new game::state::extras_menu(ctx));
  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. ctx.logger->pop_task(EXIT_SUCCESS);
  100. }
  101. credits::~credits()
  102. {
  103. ctx.logger->push_task("Exiting credits state");
  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.logger->pop_task(EXIT_SUCCESS);
  119. }
  120. } // namespace state
  121. } // namespace game