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

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