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

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