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

330 lines
9.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/nuptial-flight.hpp"
  23. #include "game/world.hpp"
  24. #include "game/load.hpp"
  25. #include "game/menu.hpp"
  26. #include "game/ant/swarm.hpp"
  27. #include "render/passes/clear-pass.hpp"
  28. #include "render/passes/ground-pass.hpp"
  29. #include "render/passes/sky-pass.hpp"
  30. #include "resources/resource-manager.hpp"
  31. #include "render/model.hpp"
  32. #include "animation/animation.hpp"
  33. #include "animation/animator.hpp"
  34. #include "animation/screen-transition.hpp"
  35. #include "animation/ease.hpp"
  36. #include "application.hpp"
  37. #include "config.hpp"
  38. #include "physics/light/exposure.hpp"
  39. #include "game/component/model.hpp"
  40. #include "game/component/steering.hpp"
  41. #include "game/component/transform.hpp"
  42. #include <limits>
  43. namespace game {
  44. namespace state {
  45. main_menu::main_menu(game::context& ctx, bool fade_in):
  46. game::state::base(ctx)
  47. {
  48. ctx.logger->push_task("Entering main menu state");
  49. ctx.ui_clear_pass->set_cleared_buffers(true, true, false);
  50. // Construct title text
  51. title_text.set_material(&ctx.title_font_material);
  52. title_text.set_color({1.0f, 1.0f, 1.0f, (fade_in) ? 1.0f : 0.0f});
  53. title_text.set_font(&ctx.title_font);
  54. title_text.set_content((*ctx.strings)["title_antkeeper"]);
  55. // Align title text
  56. const auto& title_aabb = static_cast<const geom::aabb<float>&>(title_text.get_local_bounds());
  57. float title_w = title_aabb.max_point.x() - title_aabb.min_point.x();
  58. float title_h = title_aabb.max_point.y() - title_aabb.min_point.y();
  59. 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});
  60. title_text.update_tweens();
  61. // Add title text to UI
  62. ctx.ui_scene->add_object(&title_text);
  63. // Construct title fade animation
  64. title_fade_animation.set_interpolator(ease<float>::out_cubic);
  65. animation_channel<float>* opacity_channel = title_fade_animation.add_channel(0);
  66. title_fade_animation.set_frame_callback
  67. (
  68. [this, &ctx](int channel, const float& opacity)
  69. {
  70. float4 color = this->title_text.get_color();
  71. color[3] = opacity;
  72. this->title_text.set_color(color);
  73. }
  74. );
  75. ctx.animator->add_animation(&title_fade_animation);
  76. // Construct menu item texts
  77. scene::text* start_text = new scene::text();
  78. scene::text* options_text = new scene::text();
  79. scene::text* extras_text = new scene::text();
  80. scene::text* quit_text = new scene::text();
  81. // Build list of menu item texts
  82. ctx.menu_item_texts.push_back({start_text, nullptr});
  83. ctx.menu_item_texts.push_back({options_text, nullptr});
  84. ctx.menu_item_texts.push_back({extras_text, nullptr});
  85. ctx.menu_item_texts.push_back({quit_text, nullptr});
  86. // Set content of menu item texts
  87. start_text->set_content((*ctx.strings)["main_menu_start"]);
  88. options_text->set_content((*ctx.strings)["main_menu_options"]);
  89. extras_text->set_content((*ctx.strings)["main_menu_extras"]);
  90. quit_text->set_content((*ctx.strings)["main_menu_quit"]);
  91. // Init menu item index
  92. game::menu::init_menu_item_index(ctx, "main");
  93. game::menu::update_text_color(ctx);
  94. game::menu::update_text_font(ctx);
  95. game::menu::align_text(ctx, true, false, (-ctx.app->get_viewport_dimensions().y() / 3.0f) / 2.0f);
  96. game::menu::update_text_tweens(ctx);
  97. game::menu::add_text_to_ui(ctx);
  98. game::menu::setup_animations(ctx);
  99. auto select_start_callback = [this, &ctx]()
  100. {
  101. // Disable controls and menu callbacks
  102. game::menu::clear_controls(ctx);
  103. game::menu::clear_callbacks(ctx);
  104. // Create change state function
  105. auto change_state_nuptial_flight = [&ctx]()
  106. {
  107. // Queue change to nuptial state
  108. ctx.function_queue.push
  109. (
  110. [&ctx]()
  111. {
  112. ctx.state_machine.pop();
  113. ctx.state_machine.emplace(new game::state::nuptial_flight(ctx));
  114. }
  115. );
  116. };
  117. // Fade out title
  118. this->fade_out_title();
  119. // Fade out menu
  120. game::menu::fade_out(ctx, nullptr);
  121. // Start fade out to white
  122. ctx.fade_transition_color->set_value({1, 1, 1});
  123. ctx.fade_transition->transition(config::new_colony_fade_out_duration, false, ease<float>::out_cubic, false, change_state_nuptial_flight);
  124. };
  125. auto select_options_callback = [this, &ctx]()
  126. {
  127. game::menu::clear_controls(ctx);
  128. // Fade out title
  129. this->fade_out_title();
  130. // Fade out menu
  131. game::menu::fade_out
  132. (
  133. ctx,
  134. [&ctx]()
  135. {
  136. // Queue change to options menu state
  137. ctx.function_queue.push
  138. (
  139. [&ctx]()
  140. {
  141. ctx.state_machine.pop();
  142. ctx.state_machine.emplace(new game::state::options_menu(ctx));
  143. }
  144. );
  145. }
  146. );
  147. };
  148. auto select_extras_callback = [this, &ctx]()
  149. {
  150. // Disable controls
  151. game::menu::clear_controls(ctx);
  152. // Fade out title
  153. this->fade_out_title();
  154. // Fade out menu
  155. game::menu::fade_out
  156. (
  157. ctx,
  158. [&ctx]()
  159. {
  160. // Queue change to extras menu state
  161. ctx.function_queue.push
  162. (
  163. [&ctx]()
  164. {
  165. ctx.state_machine.pop();
  166. ctx.state_machine.emplace(new game::state::extras_menu(ctx));
  167. }
  168. );
  169. }
  170. );
  171. };
  172. auto select_quit_callback = [this, &ctx]()
  173. {
  174. // Disable controls
  175. game::menu::clear_controls(ctx);
  176. // Fade out title
  177. this->fade_out_title();
  178. // Fade out menu
  179. game::menu::fade_out(ctx, nullptr);
  180. // Fade to black then quit
  181. ctx.fade_transition->transition(config::quit_fade_out_duration, false, ease<float>::out_cubic, false, std::bind(&application::close, ctx.app));
  182. };
  183. // Build list of menu select callbacks
  184. ctx.menu_select_callbacks.push_back(select_start_callback);
  185. ctx.menu_select_callbacks.push_back(select_options_callback);
  186. ctx.menu_select_callbacks.push_back(select_extras_callback);
  187. ctx.menu_select_callbacks.push_back(select_quit_callback);
  188. // Build list of menu left callbacks
  189. ctx.menu_left_callbacks.push_back(nullptr);
  190. ctx.menu_left_callbacks.push_back(nullptr);
  191. ctx.menu_left_callbacks.push_back(nullptr);
  192. ctx.menu_left_callbacks.push_back(nullptr);
  193. // Build list of menu right callbacks
  194. ctx.menu_right_callbacks.push_back(nullptr);
  195. ctx.menu_right_callbacks.push_back(nullptr);
  196. ctx.menu_right_callbacks.push_back(nullptr);
  197. ctx.menu_right_callbacks.push_back(nullptr);
  198. // Set menu back callback
  199. ctx.menu_back_callback = select_quit_callback;
  200. // Queue menu control setup
  201. ctx.function_queue.push(std::bind(game::menu::setup_controls, std::ref(ctx)));
  202. if (fade_in)
  203. {
  204. // Fade in from black
  205. ctx.fade_transition->transition(config::title_fade_in_duration, true, ease<float>::out_cubic);
  206. }
  207. else
  208. {
  209. // Fade in text
  210. fade_in_title();
  211. game::menu::fade_in(ctx, nullptr);
  212. }
  213. if (ctx.entities.find("earth") == ctx.entities.end())
  214. {
  215. game::world::cosmogenesis(ctx);
  216. game::world::create_observer(ctx);
  217. game::load::biome(ctx, "desert-scrub.bio");
  218. }
  219. // Set world time
  220. game::world::set_time(ctx, 2022, 6, 21, 12, 0, 0.0);
  221. // Set world time scale
  222. game::world::set_time_scale(ctx, 0.0);
  223. ctx.surface_camera->set_active(true);
  224. const float ev100_sunny16 = physics::light::ev::from_settings(16.0f, 1.0f / 100.0f, 100.0f);
  225. ctx.surface_camera->set_exposure(ev100_sunny16);
  226. const auto& viewport_dimensions = ctx.app->get_viewport_dimensions();
  227. const float aspect_ratio = static_cast<float>(viewport_dimensions[0]) / static_cast<float>(viewport_dimensions[1]);
  228. float fov = math::vertical_fov(math::radians(100.0f), aspect_ratio);
  229. if (ctx.config->contains("near_fov"))
  230. fov = math::vertical_fov(math::radians((*ctx.config)["near_fov"].get<float>()), aspect_ratio);
  231. ctx.surface_camera->look_at({0, 2.0f, 0}, {0, 0, 0}, {0, 0, 1});
  232. ctx.surface_camera->set_perspective(fov, ctx.surface_camera->get_aspect_ratio(), ctx.surface_camera->get_clip_near(), ctx.surface_camera->get_clip_far());
  233. ctx.surface_camera->update_tweens();
  234. // Setup and enable sky and ground passes
  235. ctx.sky_pass->set_enabled(true);
  236. ctx.ground_pass->set_enabled(true);
  237. // Disable UI color clear
  238. ctx.ui_clear_pass->set_cleared_buffers(false, true, false);
  239. //if (!ctx.menu_bg_billboard->is_active())
  240. // game::menu::fade_in_bg(ctx);
  241. ctx.logger->pop_task(EXIT_SUCCESS);
  242. }
  243. main_menu::~main_menu()
  244. {
  245. ctx.logger->push_task("Exiting main menu state");
  246. // Destruct menu
  247. game::menu::clear_controls(ctx);
  248. game::menu::clear_callbacks(ctx);
  249. game::menu::delete_animations(ctx);
  250. game::menu::remove_text_from_ui(ctx);
  251. game::menu::delete_text(ctx);
  252. // Hide menu BG
  253. ctx.menu_bg_billboard->set_active(false);
  254. // Destruct title animation
  255. ctx.animator->remove_animation(&title_fade_animation);
  256. // Destruct title text
  257. ctx.ui_scene->remove_object(&title_text);
  258. ctx.logger->pop_task(EXIT_SUCCESS);
  259. }
  260. void main_menu::fade_in_title()
  261. {
  262. animation_channel<float>* opacity_channel = title_fade_animation.get_channel(0);
  263. opacity_channel->remove_keyframes();
  264. opacity_channel->insert_keyframe({0.0, 0.0f});
  265. opacity_channel->insert_keyframe({config::menu_fade_in_duration, 1.0f});
  266. title_fade_animation.stop();
  267. title_fade_animation.play();
  268. }
  269. void main_menu::fade_out_title()
  270. {
  271. animation_channel<float>* opacity_channel = title_fade_animation.get_channel(0);
  272. opacity_channel->remove_keyframes();
  273. opacity_channel->insert_keyframe({0.0, 1.0f});
  274. opacity_channel->insert_keyframe({config::menu_fade_out_duration, 0.0f});
  275. title_fade_animation.stop();
  276. title_fade_animation.play();
  277. }
  278. } // namespace state
  279. } // namespace game