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

361 lines
11 KiB

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