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

273 lines
7.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/state/main-menu.hpp"
  20. #include "game/state/options-menu.hpp"
  21. #include "game/state/extras-menu.hpp"
  22. #include "game/state/forage.hpp"
  23. #include "game/state/nuptial-flight.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. main_menu::main_menu(game::context& ctx, bool fade_in):
  38. game::state::base(ctx)
  39. {
  40. ctx.logger->push_task("Entering main menu state");
  41. ctx.ui_clear_pass->set_cleared_buffers(true, true, false);
  42. // Construct title text
  43. title_text.set_material(&ctx.title_font_material);
  44. title_text.set_color({1.0f, 1.0f, 1.0f, (fade_in) ? 1.0f : 0.0f});
  45. title_text.set_font(&ctx.title_font);
  46. title_text.set_content((*ctx.strings)["title_antkeeper"]);
  47. // Align title text
  48. const auto& title_aabb = static_cast<const geom::aabb<float>&>(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. title_text.set_translation({std::round(-title_w * 0.5f), std::round(-title_h * 0.5f + (ctx.app->get_viewport_dimensions().y / 3.0f) / 2.0f), 0.0f});
  52. title_text.update_tweens();
  53. // Add title text to UI
  54. ctx.ui_scene->add_object(&title_text);
  55. // Construct title fade animation
  56. title_fade_animation.set_interpolator(ease<float>::out_cubic);
  57. animation_channel<float>* opacity_channel = title_fade_animation.add_channel(0);
  58. title_fade_animation.set_frame_callback
  59. (
  60. [this, &ctx](int channel, const float& opacity)
  61. {
  62. float4 color = this->title_text.get_color();
  63. color[3] = opacity;
  64. this->title_text.set_color(color);
  65. }
  66. );
  67. ctx.animator->add_animation(&title_fade_animation);
  68. // Construct menu item texts
  69. scene::text* start_text = new scene::text();
  70. scene::text* options_text = new scene::text();
  71. scene::text* extras_text = new scene::text();
  72. scene::text* quit_text = new scene::text();
  73. // Build list of menu item texts
  74. ctx.menu_item_texts.push_back({start_text, nullptr});
  75. ctx.menu_item_texts.push_back({options_text, nullptr});
  76. ctx.menu_item_texts.push_back({extras_text, nullptr});
  77. ctx.menu_item_texts.push_back({quit_text, nullptr});
  78. // Set content of menu item texts
  79. start_text->set_content((*ctx.strings)["main_menu_start"]);
  80. options_text->set_content((*ctx.strings)["main_menu_options"]);
  81. extras_text->set_content((*ctx.strings)["main_menu_extras"]);
  82. quit_text->set_content((*ctx.strings)["main_menu_quit"]);
  83. // Init menu item index
  84. game::menu::init_menu_item_index(ctx, "main");
  85. game::menu::update_text_color(ctx);
  86. game::menu::update_text_font(ctx);
  87. game::menu::align_text(ctx, true, false, (-ctx.app->get_viewport_dimensions().y / 3.0f) / 2.0f);
  88. game::menu::update_text_tweens(ctx);
  89. game::menu::add_text_to_ui(ctx);
  90. game::menu::setup_animations(ctx);
  91. auto select_start_callback = [&ctx]()
  92. {
  93. // Disable controls and menu callbacks
  94. game::menu::clear_controls(ctx);
  95. game::menu::clear_callbacks(ctx);
  96. // Create change state function
  97. auto change_state_nuptial_flight = [&ctx]()
  98. {
  99. // Queue change to nuptial state
  100. ctx.function_queue.push
  101. (
  102. [&ctx]()
  103. {
  104. ctx.state_machine.pop();
  105. ctx.state_machine.emplace(new game::state::nuptial_flight(ctx));
  106. }
  107. );
  108. };
  109. // Set up timing
  110. const float fade_out_duration = 1.0f;
  111. // Schedule state change
  112. timeline* timeline = ctx.timeline;
  113. float t = timeline->get_position();
  114. timeline->add_sequence({{t + fade_out_duration, change_state_nuptial_flight}});
  115. // Start fade out to white
  116. ctx.fade_transition_color->set_value({1, 1, 1});
  117. ctx.fade_transition->transition(fade_out_duration, false, ease<float>::out_cubic, false);
  118. };
  119. auto select_options_callback = [this, &ctx]()
  120. {
  121. game::menu::clear_controls(ctx);
  122. // Fade out title
  123. this->fade_out_title();
  124. // Fade out menu
  125. game::menu::fade_out
  126. (
  127. ctx,
  128. [&ctx]()
  129. {
  130. // Queue change to options menu state
  131. ctx.function_queue.push
  132. (
  133. [&ctx]()
  134. {
  135. ctx.state_machine.pop();
  136. ctx.state_machine.emplace(new game::state::options_menu(ctx));
  137. }
  138. );
  139. }
  140. );
  141. };
  142. auto select_extras_callback = [this, &ctx]()
  143. {
  144. // Disable controls
  145. game::menu::clear_controls(ctx);
  146. // Fade out title
  147. this->fade_out_title();
  148. // Fade out menu
  149. game::menu::fade_out
  150. (
  151. ctx,
  152. [&ctx]()
  153. {
  154. // Queue change to extras menu state
  155. ctx.function_queue.push
  156. (
  157. [&ctx]()
  158. {
  159. ctx.state_machine.pop();
  160. ctx.state_machine.emplace(new game::state::extras_menu(ctx));
  161. }
  162. );
  163. }
  164. );
  165. };
  166. auto select_quit_callback = [&ctx]()
  167. {
  168. ctx.app->close();
  169. };
  170. // Build list of menu select callbacks
  171. ctx.menu_select_callbacks.push_back(select_start_callback);
  172. ctx.menu_select_callbacks.push_back(select_options_callback);
  173. ctx.menu_select_callbacks.push_back(select_extras_callback);
  174. ctx.menu_select_callbacks.push_back(select_quit_callback);
  175. // Build list of menu left callbacks
  176. ctx.menu_left_callbacks.push_back(nullptr);
  177. ctx.menu_left_callbacks.push_back(nullptr);
  178. ctx.menu_left_callbacks.push_back(nullptr);
  179. ctx.menu_left_callbacks.push_back(nullptr);
  180. // Build list of menu right callbacks
  181. ctx.menu_right_callbacks.push_back(nullptr);
  182. ctx.menu_right_callbacks.push_back(nullptr);
  183. ctx.menu_right_callbacks.push_back(nullptr);
  184. ctx.menu_right_callbacks.push_back(nullptr);
  185. // Set menu back callback
  186. ctx.menu_back_callback = select_quit_callback;
  187. // Queue menu control setup
  188. ctx.function_queue.push(std::bind(game::menu::setup_controls, std::ref(ctx)));
  189. if (fade_in)
  190. {
  191. // Fade in from black
  192. ctx.fade_transition->transition(0.5f, true, ease<float>::out_cubic);
  193. }
  194. else
  195. {
  196. // Fade in text
  197. fade_in_title();
  198. game::menu::fade_in(ctx, nullptr);
  199. }
  200. ctx.logger->pop_task(EXIT_SUCCESS);
  201. }
  202. main_menu::~main_menu()
  203. {
  204. ctx.logger->push_task("Exiting main menu state");
  205. // Destruct menu
  206. game::menu::clear_controls(ctx);
  207. game::menu::clear_callbacks(ctx);
  208. game::menu::delete_animations(ctx);
  209. game::menu::remove_text_from_ui(ctx);
  210. game::menu::delete_text(ctx);
  211. // Destruct title animation
  212. ctx.animator->remove_animation(&title_fade_animation);
  213. // Destruct title text
  214. ctx.ui_scene->remove_object(&title_text);
  215. ctx.logger->pop_task(EXIT_SUCCESS);
  216. }
  217. void main_menu::fade_in_title()
  218. {
  219. animation_channel<float>* opacity_channel = title_fade_animation.get_channel(0);
  220. opacity_channel->remove_keyframes();
  221. opacity_channel->insert_keyframe({0.0, 0.0f});
  222. opacity_channel->insert_keyframe({game::menu::fade_in_duration, 1.0f});
  223. title_fade_animation.stop();
  224. title_fade_animation.play();
  225. }
  226. void main_menu::fade_out_title()
  227. {
  228. animation_channel<float>* opacity_channel = title_fade_animation.get_channel(0);
  229. opacity_channel->remove_keyframes();
  230. opacity_channel->insert_keyframe({0.0, 1.0f});
  231. opacity_channel->insert_keyframe({game::menu::fade_out_duration, 0.0f});
  232. title_fade_animation.stop();
  233. title_fade_animation.play();
  234. }
  235. } // namespace state
  236. } // namespace game