💿🐜 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 "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/menu.hpp"
  25. #include "game/ecoregion.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 "math/projection.hpp"
  43. #include <limits>
  44. #include "game/strings.hpp"
  45. #include "utility/hash/fnv1a.hpp"
  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.app->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.app->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 controls and menu callbacks
  106. game::menu::clear_controls(ctx);
  107. game::menu::clear_callbacks(ctx);
  108. // Create change state function
  109. auto change_state_nuptial_flight = [&ctx]()
  110. {
  111. // Queue change to nuptial state
  112. ctx.function_queue.push
  113. (
  114. [&ctx]()
  115. {
  116. ctx.state_machine.pop();
  117. ctx.state_machine.emplace(new game::state::nuptial_flight(ctx));
  118. }
  119. );
  120. };
  121. // Fade out title
  122. this->fade_out_title();
  123. // Fade out menu
  124. game::menu::fade_out(ctx, nullptr);
  125. // Start fade out to white
  126. ctx.fade_transition_color->set_value({1, 1, 1});
  127. ctx.fade_transition->transition(config::new_colony_fade_out_duration, false, ease<float>::out_cubic, false, change_state_nuptial_flight);
  128. };
  129. auto select_options_callback = [this, &ctx]()
  130. {
  131. game::menu::clear_controls(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 controls
  155. game::menu::clear_controls(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 controls
  179. game::menu::clear_controls(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, std::bind(&application::close, ctx.app));
  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. // Queue menu control setup
  205. ctx.function_queue.push(std::bind(game::menu::setup_controls, std::ref(ctx)));
  206. if (fade_in)
  207. {
  208. // Fade in from black
  209. ctx.fade_transition->transition(config::title_fade_in_duration, true, ease<float>::out_cubic);
  210. }
  211. else
  212. {
  213. // Fade in text
  214. fade_in_title();
  215. game::menu::fade_in(ctx, nullptr);
  216. }
  217. if (ctx.entities.find("earth") == ctx.entities.end())
  218. {
  219. game::world::cosmogenesis(ctx);
  220. game::world::create_observer(ctx);
  221. //game::world::enter_ecoregion(ctx, *ctx.resource_manager->load<game::ecoregion>("seedy-scrub.eco"));
  222. }
  223. // Set world time
  224. game::world::set_time(ctx, 2022, 6, 21, 12, 0, 0.0);
  225. // Set world time scale
  226. game::world::set_time_scale(ctx, 0.0);
  227. ctx.surface_camera->set_active(true);
  228. const float ev100_sunny16 = physics::light::ev::from_settings(16.0f, 1.0f / 100.0f, 100.0f);
  229. ctx.surface_camera->set_exposure(ev100_sunny16);
  230. const auto& viewport_size = ctx.app->get_viewport_size();
  231. const float aspect_ratio = static_cast<float>(viewport_size[0]) / static_cast<float>(viewport_size[1]);
  232. float fov = math::vertical_fov(math::radians(100.0f), aspect_ratio);
  233. ctx.surface_camera->look_at({0, 2.0f, 0}, {0, 0, 0}, {0, 0, 1});
  234. ctx.surface_camera->set_perspective(fov, ctx.surface_camera->get_aspect_ratio(), ctx.surface_camera->get_clip_near(), ctx.surface_camera->get_clip_far());
  235. ctx.surface_camera->update_tweens();
  236. // Setup and enable sky and ground passes
  237. ctx.sky_pass->set_enabled(true);
  238. ctx.ground_pass->set_enabled(true);
  239. // Disable UI color clear
  240. ctx.ui_clear_pass->set_cleared_buffers(false, true, false);
  241. //if (!ctx.menu_bg_billboard->is_active())
  242. // game::menu::fade_in_bg(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::menu::clear_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