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

562 lines
19 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/states/forage.hpp"
  20. #include "entity/archetype.hpp"
  21. #include "entity/commands.hpp"
  22. #include "entity/components/observer.hpp"
  23. #include "entity/components/terrain.hpp"
  24. #include "entity/components/transform.hpp"
  25. #include "entity/components/celestial-body.hpp"
  26. #include "entity/components/tool.hpp"
  27. #include "entity/systems/astronomy.hpp"
  28. #include "animation/screen-transition.hpp"
  29. #include "animation/ease.hpp"
  30. #include "resources/resource-manager.hpp"
  31. #include "entity/components/camera.hpp"
  32. #include "entity/components/constraints/spring-to.hpp"
  33. #include "entity/components/constraints/three-dof.hpp"
  34. #include "entity/components/constraint-stack.hpp"
  35. #include "application.hpp"
  36. namespace game {
  37. namespace state {
  38. namespace forage {
  39. static void setup_camera(game::context* ctx);
  40. static void setup_tools(game::context* ctx);
  41. static void setup_controls(game::context* ctx);
  42. void enter(game::context* ctx)
  43. {
  44. setup_camera(ctx);
  45. setup_tools(ctx);
  46. setup_controls(ctx);
  47. // Find planet EID by name
  48. entity::id planet_eid = ctx->entities["planet"];
  49. // Create biome terrain component
  50. entity::component::terrain biome_terrain;
  51. biome_terrain.max_lod = 18;
  52. biome_terrain.patch_material = ctx->resource_manager->load<material>("desert-terrain.mtl");
  53. biome_terrain.elevation = [](double, double) -> double
  54. {
  55. return 0.0;
  56. };
  57. // Replace planet terrain component with biome terrain component
  58. ctx->entity_registry->replace<entity::component::terrain>(planet_eid, biome_terrain);
  59. // Create observer
  60. entity::id observer_eid = ctx->entity_registry->create();
  61. {
  62. entity::component::observer observer;
  63. observer.reference_body_eid = planet_eid;
  64. observer.elevation = 0.0;
  65. observer.latitude = 0.0;
  66. observer.longitude = 0.0;
  67. observer.camera = ctx->surface_camera;
  68. ctx->entity_registry->assign<entity::component::observer>(observer_eid, observer);
  69. // Set reference location of astronomy system
  70. ctx->astronomy_system->set_reference_body(planet_eid);
  71. ctx->astronomy_system->set_observer_location(double3{observer.elevation, observer.latitude, observer.longitude});
  72. }
  73. // Create larva
  74. {
  75. entity::archetype* larva_archetype = ctx->resource_manager->load<entity::archetype>("ant-larva.ent");
  76. auto larva_eid = larva_archetype->create(*ctx->entity_registry);
  77. entity::command::warp_to(*ctx->entity_registry, larva_eid, {50, 0.1935f, 0});
  78. entity::command::assign_render_layers(*ctx->entity_registry, larva_eid, 0b10);
  79. }
  80. // Create cocoon
  81. {
  82. entity::archetype* cocoon_archetype = ctx->resource_manager->load<entity::archetype>("ant-cocoon.ent");
  83. auto cocoon_eid = cocoon_archetype->create(*ctx->entity_registry);
  84. entity::command::warp_to(*ctx->entity_registry, cocoon_eid, {-50, 0.1935f, 0});
  85. entity::command::assign_render_layers(*ctx->entity_registry, cocoon_eid, 0b10);
  86. }
  87. // Create moon
  88. {
  89. entity::archetype* moon_archetype = ctx->resource_manager->load<entity::archetype>("moon.ent");
  90. auto moon_eid = moon_archetype->create(*ctx->entity_registry);
  91. entity::command::warp_to(*ctx->entity_registry, moon_eid, {50.0f, 50.0f, 50.0f});
  92. entity::command::assign_render_layers(*ctx->entity_registry, moon_eid, 0b10);
  93. entity::command::set_scale(*ctx->entity_registry, moon_eid, float3{1.0f, 1.0f, 1.0f} * 10.0f);
  94. }
  95. ctx->surface_scene->update_tweens();
  96. // Start fade in
  97. ctx->fade_transition->transition(1.0f, true, ease<float>::in_quad);
  98. }
  99. void exit(game::context* ctx)
  100. {}
  101. void setup_camera(game::context* ctx)
  102. {
  103. // Switch to surface camera
  104. ctx->underground_camera->set_active(false);
  105. ctx->surface_camera->set_active(true);
  106. // Create surface camera entity
  107. if (!ctx->entities.count("surface_cam"))
  108. {
  109. // Create camera target entity
  110. entity::id target_eid = ctx->entity_registry->create();
  111. ctx->entities["surface_cam_target"] = target_eid;
  112. {
  113. // Transform
  114. entity::component::transform target_transform;
  115. target_transform.local = math::identity_transform<float>;
  116. target_transform.world = target_transform.local;
  117. target_transform.warp = true;
  118. ctx->entity_registry->assign<entity::component::transform>(target_eid, target_transform);
  119. }
  120. // Create camera entity
  121. entity::id camera_eid = ctx->entity_registry->create();
  122. ctx->entities["surface_cam"] = camera_eid;
  123. // Create camera transform component
  124. entity::component::transform transform;
  125. transform.local = math::identity_transform<float>;
  126. transform.world = transform.local;
  127. transform.warp = true;
  128. ctx->entity_registry->assign<entity::component::transform>(camera_eid, transform);
  129. // Create camera camera component
  130. entity::component::camera camera;
  131. camera.object = ctx->surface_camera;
  132. ctx->entity_registry->assign<entity::component::camera>(camera_eid, camera);
  133. // Create camera 3DOF constraint entity
  134. entity::id three_dof_constraint_eid = ctx->entity_registry->create();
  135. ctx->entities["surface_cam_3dof"] = three_dof_constraint_eid;
  136. {
  137. // Create 3DOF to constraint
  138. entity::component::constraint::three_dof three_dof;
  139. three_dof.yaw = 0.0f;
  140. three_dof.pitch = 0.0f;
  141. three_dof.roll = 0.0f;
  142. ctx->entity_registry->assign<entity::component::constraint::three_dof>(three_dof_constraint_eid, three_dof);
  143. // Create constraint stack node component
  144. entity::component::constraint_stack_node node;
  145. node.active = true;
  146. node.weight = 1.0f;
  147. node.next = entt::null;
  148. ctx->entity_registry->assign<entity::component::constraint_stack_node>(three_dof_constraint_eid, node);
  149. }
  150. // Create camera spring to constraint entity
  151. entity::id spring_constraint_eid = ctx->entity_registry->create();
  152. {
  153. // Create spring to constraint
  154. entity::component::constraint::spring_to spring;
  155. spring.target = target_eid;
  156. spring.translation = {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, 1.0f, math::two_pi<float>};
  157. spring.translation.w = hz_to_rads(8.0f);
  158. spring.spring_translation = true;
  159. spring.spring_rotation = false;
  160. ctx->entity_registry->assign<entity::component::constraint::spring_to>(spring_constraint_eid, spring);
  161. // Create constraint stack node component
  162. entity::component::constraint_stack_node node;
  163. node.active = true;
  164. node.weight = 1.0f;
  165. node.next = three_dof_constraint_eid;
  166. ctx->entity_registry->assign<entity::component::constraint_stack_node>(spring_constraint_eid, node);
  167. }
  168. // Create camera constraint stack component
  169. entity::component::constraint_stack constraint_stack;
  170. constraint_stack.head = spring_constraint_eid;
  171. ctx->entity_registry->assign<entity::component::constraint_stack>(camera_eid, constraint_stack);
  172. }
  173. ctx->surface_camera->set_exposure(-14.5f);
  174. }
  175. void setup_tools(game::context* ctx)
  176. {
  177. if (!ctx->entities.count("move_tool"))
  178. {
  179. entity::id tool_eid = ctx->entity_registry->create();
  180. ctx->entities["move_tool"] = tool_eid;
  181. }
  182. if (!ctx->entities.count("paint_tool"))
  183. {
  184. entity::id tool_eid = ctx->entity_registry->create();
  185. ctx->entities["paint_tool"] = tool_eid;
  186. }
  187. if (!ctx->entities.count("flip_tool"))
  188. {
  189. entity::id tool_eid = ctx->entity_registry->create();
  190. ctx->entities["flip_tool"] = tool_eid;
  191. }
  192. if (!ctx->entities.count("poke_tool"))
  193. {
  194. entity::id tool_eid = ctx->entity_registry->create();
  195. ctx->entities["poke_tool"] = tool_eid;
  196. }
  197. if (!ctx->entities.count("inspect_tool"))
  198. {
  199. entity::id tool_eid = ctx->entity_registry->create();
  200. ctx->entities["inspect_tool"] = tool_eid;
  201. }
  202. if (!ctx->entities.count("label_tool"))
  203. {
  204. entity::id tool_eid = ctx->entity_registry->create();
  205. ctx->entities["label_tool"] = tool_eid;
  206. }
  207. if (!ctx->entities.count("time_tool"))
  208. {
  209. entity::id tool_eid = ctx->entity_registry->create();
  210. ctx->entities["time_tool"] = tool_eid;
  211. entity::component::tool tool;
  212. tool.active = [ctx]()
  213. {
  214. auto [mouse_x, mouse_y] = ctx->app->get_mouse()->get_current_position();
  215. auto [window_w, window_h] = ctx->app->get_viewport_dimensions();
  216. entity::id planet_eid = ctx->entities["planet"];
  217. entity::component::celestial_body& body = ctx->entity_registry->get<entity::component::celestial_body>(planet_eid);
  218. body.axial_tilt = math::radians(360.0f) * ((float)mouse_x / (float)window_w);
  219. };
  220. ctx->entity_registry->assign<entity::component::tool>(tool_eid, tool);
  221. }
  222. // Set active tool
  223. ctx->entities["active_tool"] = ctx->entities["time_tool"];
  224. }
  225. void setup_controls(game::context* ctx)
  226. {
  227. // Get underground camera entity
  228. entity::id camera_eid = ctx->entities["surface_cam"];
  229. entity::id target_eid = ctx->entities["surface_cam_target"];
  230. entity::id three_dof_eid = ctx->entities["surface_cam_3dof"];
  231. const float dolly_speed = 20.0f;
  232. const float truck_speed = dolly_speed;
  233. const float pedestal_speed = 30.0f;
  234. float mouse_look_sensitivity = 1.0f;
  235. bool mouse_invert_tilt = false;
  236. bool mouse_invert_pan = false;
  237. float gamepad_look_sensitivity = 1.0f;
  238. bool gamepad_invert_tilt = false;
  239. bool gamepad_invert_pan = false;
  240. if (ctx->config->contains("mouse_look_sensitivity"))
  241. mouse_look_sensitivity = math::radians((*ctx->config)["mouse_look_sensitivity"].get<float>());
  242. if (ctx->config->contains("mouse_invert_tilt"))
  243. mouse_invert_tilt = math::radians((*ctx->config)["mouse_invert_tilt"].get<bool>());
  244. if (ctx->config->contains("mouse_invert_pan"))
  245. mouse_invert_pan = math::radians((*ctx->config)["mouse_invert_pan"].get<bool>());
  246. if (ctx->config->contains("gamepad_look_sensitivity"))
  247. gamepad_look_sensitivity = math::radians((*ctx->config)["gamepad_look_sensitivity"].get<float>());
  248. if (ctx->config->contains("gamepad_invert_tilt"))
  249. gamepad_invert_tilt = math::radians((*ctx->config)["gamepad_invert_tilt"].get<bool>());
  250. if (ctx->config->contains("gamepad_invert_pan"))
  251. gamepad_invert_pan = math::radians((*ctx->config)["gamepad_invert_pan"].get<bool>());
  252. const input::control* move_slow = ctx->controls["move_slow"];
  253. const input::control* move_fast = ctx->controls["move_fast"];
  254. const input::control* mouse_rotate = ctx->controls["mouse_rotate"];
  255. float mouse_tilt_factor = mouse_look_sensitivity * (mouse_invert_tilt ? -1.0f : 1.0f);
  256. float mouse_pan_factor = mouse_look_sensitivity * (mouse_invert_pan ? -1.0f : 1.0f);
  257. float gamepad_tilt_factor = gamepad_look_sensitivity * (gamepad_invert_tilt ? -1.0f : 1.0f);
  258. float gamepad_pan_factor = gamepad_look_sensitivity * (gamepad_invert_pan ? -1.0f : 1.0f);
  259. ctx->controls["dolly_forward"]->set_active_callback
  260. (
  261. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  262. {
  263. if (move_slow->is_active())
  264. value *= 0.25f;
  265. if (move_fast->is_active())
  266. value *= 4.0f;
  267. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  268. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  269. const float3 movement = {0.0f, 0.0f, -truck_speed * value * (1.0f / 60.0f)};
  270. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  271. }
  272. );
  273. // Dolly backward
  274. ctx->controls["dolly_backward"]->set_active_callback
  275. (
  276. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  277. {
  278. if (move_slow->is_active())
  279. value *= 0.25f;
  280. if (move_fast->is_active())
  281. value *= 4.0f;
  282. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  283. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  284. const float3 movement = {0.0f, 0.0f, truck_speed * value * (1.0f / 60.0f)};
  285. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  286. }
  287. );
  288. // Truck right
  289. ctx->controls["truck_right"]->set_active_callback
  290. (
  291. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  292. {
  293. if (move_slow->is_active())
  294. value *= 0.25f;
  295. if (move_fast->is_active())
  296. value *= 4.0f;
  297. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  298. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  299. const float3 movement = {truck_speed * value * (1.0f / 60.0f), 0.0f, 0.0f};
  300. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  301. }
  302. );
  303. // Truck left
  304. ctx->controls["truck_left"]->set_active_callback
  305. (
  306. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  307. {
  308. if (move_slow->is_active())
  309. value *= 0.25f;
  310. if (move_fast->is_active())
  311. value *= 4.0f;
  312. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  313. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  314. const float3 movement = {-truck_speed * value * (1.0f / 60.0f), 0.0f, 0.0f};
  315. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  316. }
  317. );
  318. // Pedestal up
  319. ctx->controls["pedestal_up"]->set_active_callback
  320. (
  321. [ctx, target_eid, pedestal_speed, move_slow, move_fast](float value)
  322. {
  323. if (move_slow->is_active())
  324. value *= 0.25f;
  325. if (move_fast->is_active())
  326. value *= 4.0f;
  327. const float3 movement = {0.0f, pedestal_speed * value * (1.0f / 60.0f), 0.0f};
  328. entity::command::translate(*ctx->entity_registry, target_eid, movement);
  329. }
  330. );
  331. // Pedestal down
  332. ctx->controls["pedestal_down"]->set_active_callback
  333. (
  334. [ctx, target_eid, pedestal_speed, move_slow, move_fast](float value)
  335. {
  336. if (move_slow->is_active())
  337. value *= 0.25f;
  338. if (move_fast->is_active())
  339. value *= 4.0f;
  340. const float3 movement = {0.0f, -pedestal_speed * value * (1.0f / 60.0f), 0.0f};
  341. entity::command::translate(*ctx->entity_registry, target_eid, movement);
  342. }
  343. );
  344. // Mouse rotate
  345. ctx->controls["mouse_rotate"]->set_activated_callback
  346. (
  347. [ctx]()
  348. {
  349. ctx->app->set_relative_mouse_mode(true);
  350. }
  351. );
  352. ctx->controls["mouse_rotate"]->set_deactivated_callback
  353. (
  354. [ctx]()
  355. {
  356. ctx->app->set_relative_mouse_mode(false);
  357. }
  358. );
  359. // Pan left
  360. ctx->controls["pan_left_gamepad"]->set_active_callback
  361. (
  362. [ctx, three_dof_eid, gamepad_pan_factor](float value)
  363. {
  364. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  365. three_dof.yaw += gamepad_pan_factor * value * (1.0f / 60.0f);
  366. }
  367. );
  368. ctx->controls["pan_left_mouse"]->set_active_callback
  369. (
  370. [ctx, three_dof_eid, mouse_pan_factor, mouse_rotate](float value)
  371. {
  372. if (!mouse_rotate->is_active())
  373. return;
  374. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  375. three_dof.yaw += mouse_pan_factor * value * (1.0f / 60.0f);
  376. }
  377. );
  378. // Pan right
  379. ctx->controls["pan_right_gamepad"]->set_active_callback
  380. (
  381. [ctx, three_dof_eid, gamepad_pan_factor](float value)
  382. {
  383. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  384. three_dof.yaw -= gamepad_pan_factor * value * (1.0f / 60.0f);
  385. }
  386. );
  387. ctx->controls["pan_right_mouse"]->set_active_callback
  388. (
  389. [ctx, three_dof_eid, mouse_pan_factor, mouse_rotate](float value)
  390. {
  391. if (!mouse_rotate->is_active())
  392. return;
  393. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  394. three_dof.yaw -= mouse_pan_factor * value * (1.0f / 60.0f);
  395. }
  396. );
  397. // Tilt up
  398. ctx->controls["tilt_up_gamepad"]->set_active_callback
  399. (
  400. [ctx, three_dof_eid, gamepad_tilt_factor](float value)
  401. {
  402. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  403. three_dof.pitch -= gamepad_tilt_factor * value * (1.0f / 60.0f);
  404. three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch);
  405. }
  406. );
  407. ctx->controls["tilt_up_mouse"]->set_active_callback
  408. (
  409. [ctx, three_dof_eid, mouse_tilt_factor, mouse_rotate](float value)
  410. {
  411. if (!mouse_rotate->is_active())
  412. return;
  413. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  414. three_dof.pitch -= mouse_tilt_factor * value * (1.0f / 60.0f);
  415. three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch);
  416. }
  417. );
  418. // Tilt down
  419. ctx->controls["tilt_down_gamepad"]->set_active_callback
  420. (
  421. [ctx, three_dof_eid, gamepad_tilt_factor](float value)
  422. {
  423. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  424. three_dof.pitch += gamepad_tilt_factor * value * (1.0f / 60.0f);
  425. three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch);
  426. }
  427. );
  428. ctx->controls["tilt_down_mouse"]->set_active_callback
  429. (
  430. [ctx, three_dof_eid, mouse_tilt_factor, mouse_rotate](float value)
  431. {
  432. if (!mouse_rotate->is_active())
  433. return;
  434. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  435. three_dof.pitch += mouse_tilt_factor * value * (1.0f / 60.0f);
  436. three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch);
  437. }
  438. );
  439. // Use tool
  440. ctx->controls["use_tool"]->set_activated_callback
  441. (
  442. [ctx]()
  443. {
  444. if (ctx->entities.count("active_tool"))
  445. {
  446. entity::id tool_eid = ctx->entities["active_tool"];
  447. const auto& tool = ctx->entity_registry->get<entity::component::tool>(tool_eid);
  448. if (tool.activated)
  449. tool.activated();
  450. }
  451. }
  452. );
  453. ctx->controls["use_tool"]->set_deactivated_callback
  454. (
  455. [ctx]()
  456. {
  457. if (ctx->entities.count("active_tool"))
  458. {
  459. entity::id tool_eid = ctx->entities["active_tool"];
  460. const auto& tool = ctx->entity_registry->get<entity::component::tool>(tool_eid);
  461. if (tool.deactivated)
  462. tool.deactivated();
  463. }
  464. }
  465. );
  466. ctx->controls["use_tool"]->set_active_callback
  467. (
  468. [ctx](float value)
  469. {
  470. if (ctx->entities.count("active_tool"))
  471. {
  472. entity::id tool_eid = ctx->entities["active_tool"];
  473. const auto& tool = ctx->entity_registry->get<entity::component::tool>(tool_eid);
  474. if (tool.active)
  475. tool.active();
  476. }
  477. }
  478. );
  479. /*
  480. auto [mouse_x, mouse_y] = ctx->app->get_mouse()->get_current_position();
  481. ctx->logger->log("tool used (" + std::to_string(mouse_x) + ", " + std::to_string(mouse_y) + ")");
  482. entity::id planet_eid = ctx->entities["planet"];
  483. entity::component::celestial_body& body = ctx->entity_registry->get<entity::component::celestial_body>(planet_eid);
  484. body.axial_tilt += math::radians(30.0f) * 1.0f / 60.0f;
  485. */
  486. }
  487. } // namespace forage
  488. } // namespace state
  489. } // namespace game