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

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