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

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