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

183 lines
6.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  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/main-menu.hpp"
  20. #include "game/states/options-menu.hpp"
  21. #include "game/states/forage.hpp"
  22. #include "game/states/nuptial-flight.hpp"
  23. #include "game/states/credits.hpp"
  24. #include "game/menu.hpp"
  25. #include "render/passes/clear-pass.hpp"
  26. #include "resources/resource-manager.hpp"
  27. #include "render/model.hpp"
  28. #include "animation/animation.hpp"
  29. #include "animation/animator.hpp"
  30. #include "animation/screen-transition.hpp"
  31. #include "animation/ease.hpp"
  32. #include "animation/timeline.hpp"
  33. #include "application.hpp"
  34. #include <limits>
  35. namespace game {
  36. namespace state {
  37. namespace main_menu {
  38. void enter(game::context* ctx)
  39. {
  40. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  41. // Construct title text
  42. ctx->title_text = new scene::text();
  43. ctx->title_text->set_material(&ctx->title_font_material);
  44. ctx->title_text->set_font(&ctx->title_font);
  45. ctx->title_text->set_color({1.0f, 1.0f, 1.0f, 1.0f});
  46. ctx->title_text->set_content((*ctx->strings)["title_antkeeper"]);
  47. // Align title text
  48. const auto& title_aabb = static_cast<const geom::aabb<float>&>(ctx->title_text->get_local_bounds());
  49. float title_w = title_aabb.max_point.x - title_aabb.min_point.x;
  50. float title_h = title_aabb.max_point.y - title_aabb.min_point.y;
  51. ctx->title_text->set_translation({std::round(-title_w * 0.5f), std::round(-title_h * 0.5f + (std::get<1>(ctx->app->get_viewport_dimensions()) / 3.0f) / 2.0f), 0.0f});
  52. // Add title text to UI
  53. ctx->ui_scene->add_object(ctx->title_text);
  54. ctx->title_text->update_tweens();
  55. // Construct menu item texts
  56. scene::text* start_text = new scene::text();
  57. scene::text* options_text = new scene::text();
  58. scene::text* credits_text = new scene::text();
  59. scene::text* quit_text = new scene::text();
  60. // Build list of menu item texts
  61. ctx->menu_item_texts.push_back({start_text, nullptr});
  62. ctx->menu_item_texts.push_back({options_text, nullptr});
  63. ctx->menu_item_texts.push_back({credits_text, nullptr});
  64. ctx->menu_item_texts.push_back({quit_text, nullptr});
  65. // Set content of menu item texts
  66. start_text->set_content((*ctx->strings)["main_menu_start"]);
  67. options_text->set_content((*ctx->strings)["main_menu_options"]);
  68. credits_text->set_content((*ctx->strings)["main_menu_credits"]);
  69. quit_text->set_content((*ctx->strings)["main_menu_quit"]);
  70. // Init menu item index
  71. game::menu::init_menu_item_index(ctx, "main");
  72. game::menu::update_text_color(ctx);
  73. game::menu::update_text_font(ctx);
  74. game::menu::align_text(ctx, true, false, (-std::get<1>(ctx->app->get_viewport_dimensions()) / 3.0f) / 2.0f);
  75. game::menu::update_text_tweens(ctx);
  76. game::menu::add_text_to_ui(ctx);
  77. auto select_start_callback = [ctx]()
  78. {
  79. // Disable controls and menu callbacks
  80. game::menu::clear_controls(ctx);
  81. game::menu::clear_callbacks(ctx);
  82. // Create change state function
  83. auto change_state_nuptial_flight = [ctx]()
  84. {
  85. application::state next_state;
  86. next_state.name = "nuptial_flight";
  87. next_state.enter = std::bind(game::state::nuptial_flight::enter, ctx);
  88. next_state.exit = std::bind(game::state::nuptial_flight::exit, ctx);
  89. ctx->app->change_state(next_state);
  90. };
  91. // Set up timing
  92. const float fade_out_duration = 1.0f;
  93. // Schedule state change
  94. timeline* timeline = ctx->timeline;
  95. float t = timeline->get_position();
  96. timeline->add_sequence({{t + fade_out_duration, change_state_nuptial_flight}});
  97. // Start fade out to white
  98. ctx->fade_transition_color->set_value({1, 1, 1});
  99. ctx->fade_transition->transition(fade_out_duration, false, ease<float>::out_quad, false);
  100. };
  101. auto select_options_callback = [ctx]()
  102. {
  103. application::state next_state;
  104. next_state.name = "options_menu";
  105. next_state.enter = std::bind(game::state::options_menu::enter, ctx);
  106. next_state.exit = std::bind(game::state::options_menu::exit, ctx);
  107. ctx->app->change_state(next_state);
  108. };
  109. auto select_credits_callback = [ctx]()
  110. {
  111. application::state next_state;
  112. next_state.name = "credits";
  113. next_state.enter = std::bind(game::state::credits::enter, ctx);
  114. next_state.exit = std::bind(game::state::credits::exit, ctx);
  115. ctx->app->change_state(next_state);
  116. };
  117. auto select_quit_callback = [ctx]()
  118. {
  119. ctx->app->close(EXIT_SUCCESS);
  120. };
  121. // Build list of menu select callbacks
  122. ctx->menu_select_callbacks.push_back(select_start_callback);
  123. ctx->menu_select_callbacks.push_back(select_options_callback);
  124. ctx->menu_select_callbacks.push_back(select_credits_callback);
  125. ctx->menu_select_callbacks.push_back(select_quit_callback);
  126. // Build list of menu left callbacks
  127. ctx->menu_left_callbacks.push_back(nullptr);
  128. ctx->menu_left_callbacks.push_back(nullptr);
  129. ctx->menu_left_callbacks.push_back(nullptr);
  130. ctx->menu_left_callbacks.push_back(nullptr);
  131. // Build list of menu right callbacks
  132. ctx->menu_right_callbacks.push_back(nullptr);
  133. ctx->menu_right_callbacks.push_back(nullptr);
  134. ctx->menu_right_callbacks.push_back(nullptr);
  135. ctx->menu_right_callbacks.push_back(nullptr);
  136. // Set menu back callback
  137. ctx->menu_back_callback = select_quit_callback;
  138. // Schedule menu control setup
  139. timeline* timeline = ctx->timeline;
  140. float t = timeline->get_position();
  141. timeline->add_sequence({{t + game::menu::input_delay, std::bind(game::menu::setup_controls, ctx)}});
  142. }
  143. void exit(game::context* ctx)
  144. {
  145. // Destruct menu
  146. game::menu::clear_controls(ctx);
  147. game::menu::clear_callbacks(ctx);
  148. game::menu::remove_text_from_ui(ctx);
  149. game::menu::delete_text(ctx);
  150. // Destruct title text
  151. ctx->ui_scene->remove_object(ctx->title_text);
  152. delete ctx->title_text;
  153. ctx->title_text = nullptr;
  154. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  155. }
  156. } // namespace main_menu
  157. } // namespace state
  158. } // namespace game