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

568 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_tilt_sensitivity = 1.0f;
  235. float mouse_pan_sensitivity = 1.0f;
  236. bool mouse_invert_tilt = false;
  237. bool mouse_invert_pan = false;
  238. float gamepad_tilt_sensitivity = 1.0f;
  239. float gamepad_pan_sensitivity = 1.0f;
  240. bool gamepad_invert_tilt = false;
  241. bool gamepad_invert_pan = false;
  242. if (ctx->config->contains("mouse_tilt_sensitivity"))
  243. mouse_tilt_sensitivity = math::radians((*ctx->config)["mouse_tilt_sensitivity"].get<float>());
  244. if (ctx->config->contains("mouse_pan_sensitivity"))
  245. mouse_pan_sensitivity = math::radians((*ctx->config)["mouse_pan_sensitivity"].get<float>());
  246. if (ctx->config->contains("mouse_invert_tilt"))
  247. mouse_invert_tilt = math::radians((*ctx->config)["mouse_invert_tilt"].get<bool>());
  248. if (ctx->config->contains("mouse_invert_pan"))
  249. mouse_invert_pan = math::radians((*ctx->config)["mouse_invert_pan"].get<bool>());
  250. if (ctx->config->contains("gamepad_tilt_sensitivity"))
  251. gamepad_tilt_sensitivity = math::radians((*ctx->config)["gamepad_tilt_sensitivity"].get<float>());
  252. if (ctx->config->contains("gamepad_pan_sensitivity"))
  253. gamepad_pan_sensitivity = math::radians((*ctx->config)["gamepad_pan_sensitivity"].get<float>());
  254. if (ctx->config->contains("gamepad_invert_tilt"))
  255. gamepad_invert_tilt = math::radians((*ctx->config)["gamepad_invert_tilt"].get<bool>());
  256. if (ctx->config->contains("gamepad_invert_pan"))
  257. gamepad_invert_pan = math::radians((*ctx->config)["gamepad_invert_pan"].get<bool>());
  258. const input::control* move_slow = ctx->controls["move_slow"];
  259. const input::control* move_fast = ctx->controls["move_fast"];
  260. const input::control* mouse_rotate = ctx->controls["mouse_rotate"];
  261. float mouse_tilt_factor = mouse_tilt_sensitivity * (mouse_invert_tilt ? -1.0f : 1.0f);
  262. float mouse_pan_factor = mouse_pan_sensitivity * (mouse_invert_pan ? -1.0f : 1.0f);
  263. float gamepad_tilt_factor = gamepad_tilt_sensitivity * (gamepad_invert_tilt ? -1.0f : 1.0f);
  264. float gamepad_pan_factor = gamepad_pan_sensitivity * (gamepad_invert_pan ? -1.0f : 1.0f);
  265. ctx->controls["dolly_forward"]->set_active_callback
  266. (
  267. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  268. {
  269. if (move_slow->is_active())
  270. value *= 0.25f;
  271. if (move_fast->is_active())
  272. value *= 4.0f;
  273. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  274. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  275. const float3 movement = {0.0f, 0.0f, -truck_speed * value * (1.0f / 60.0f)};
  276. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  277. }
  278. );
  279. // Dolly backward
  280. ctx->controls["dolly_backward"]->set_active_callback
  281. (
  282. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  283. {
  284. if (move_slow->is_active())
  285. value *= 0.25f;
  286. if (move_fast->is_active())
  287. value *= 4.0f;
  288. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  289. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  290. const float3 movement = {0.0f, 0.0f, truck_speed * value * (1.0f / 60.0f)};
  291. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  292. }
  293. );
  294. // Truck right
  295. ctx->controls["truck_right"]->set_active_callback
  296. (
  297. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  298. {
  299. if (move_slow->is_active())
  300. value *= 0.25f;
  301. if (move_fast->is_active())
  302. value *= 4.0f;
  303. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  304. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  305. const float3 movement = {truck_speed * value * (1.0f / 60.0f), 0.0f, 0.0f};
  306. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  307. }
  308. );
  309. // Truck left
  310. ctx->controls["truck_left"]->set_active_callback
  311. (
  312. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  313. {
  314. if (move_slow->is_active())
  315. value *= 0.25f;
  316. if (move_fast->is_active())
  317. value *= 4.0f;
  318. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  319. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  320. const float3 movement = {-truck_speed * value * (1.0f / 60.0f), 0.0f, 0.0f};
  321. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  322. }
  323. );
  324. // Pedestal up
  325. ctx->controls["pedestal_up"]->set_active_callback
  326. (
  327. [ctx, target_eid, pedestal_speed, move_slow, move_fast](float value)
  328. {
  329. if (move_slow->is_active())
  330. value *= 0.25f;
  331. if (move_fast->is_active())
  332. value *= 4.0f;
  333. const float3 movement = {0.0f, pedestal_speed * value * (1.0f / 60.0f), 0.0f};
  334. entity::command::translate(*ctx->entity_registry, target_eid, movement);
  335. }
  336. );
  337. // Pedestal down
  338. ctx->controls["pedestal_down"]->set_active_callback
  339. (
  340. [ctx, target_eid, pedestal_speed, move_slow, move_fast](float value)
  341. {
  342. if (move_slow->is_active())
  343. value *= 0.25f;
  344. if (move_fast->is_active())
  345. value *= 4.0f;
  346. const float3 movement = {0.0f, -pedestal_speed * value * (1.0f / 60.0f), 0.0f};
  347. entity::command::translate(*ctx->entity_registry, target_eid, movement);
  348. }
  349. );
  350. // Mouse rotate
  351. ctx->controls["mouse_rotate"]->set_activated_callback
  352. (
  353. [ctx]()
  354. {
  355. ctx->app->set_relative_mouse_mode(true);
  356. }
  357. );
  358. ctx->controls["mouse_rotate"]->set_deactivated_callback
  359. (
  360. [ctx]()
  361. {
  362. ctx->app->set_relative_mouse_mode(false);
  363. }
  364. );
  365. // Pan left
  366. ctx->controls["pan_left_gamepad"]->set_active_callback
  367. (
  368. [ctx, three_dof_eid, gamepad_pan_factor](float value)
  369. {
  370. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  371. three_dof.yaw += gamepad_pan_factor * value * (1.0f / 60.0f);
  372. }
  373. );
  374. ctx->controls["pan_left_mouse"]->set_active_callback
  375. (
  376. [ctx, three_dof_eid, mouse_pan_factor, mouse_rotate](float value)
  377. {
  378. if (!mouse_rotate->is_active())
  379. return;
  380. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  381. three_dof.yaw += mouse_pan_factor * value * (1.0f / 60.0f);
  382. }
  383. );
  384. // Pan right
  385. ctx->controls["pan_right_gamepad"]->set_active_callback
  386. (
  387. [ctx, three_dof_eid, gamepad_pan_factor](float value)
  388. {
  389. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  390. three_dof.yaw -= gamepad_pan_factor * value * (1.0f / 60.0f);
  391. }
  392. );
  393. ctx->controls["pan_right_mouse"]->set_active_callback
  394. (
  395. [ctx, three_dof_eid, mouse_pan_factor, mouse_rotate](float value)
  396. {
  397. if (!mouse_rotate->is_active())
  398. return;
  399. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  400. three_dof.yaw -= mouse_pan_factor * value * (1.0f / 60.0f);
  401. }
  402. );
  403. // Tilt up
  404. ctx->controls["tilt_up_gamepad"]->set_active_callback
  405. (
  406. [ctx, three_dof_eid, gamepad_tilt_factor](float value)
  407. {
  408. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  409. three_dof.pitch -= gamepad_tilt_factor * value * (1.0f / 60.0f);
  410. three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch);
  411. }
  412. );
  413. ctx->controls["tilt_up_mouse"]->set_active_callback
  414. (
  415. [ctx, three_dof_eid, mouse_tilt_factor, mouse_rotate](float value)
  416. {
  417. if (!mouse_rotate->is_active())
  418. return;
  419. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  420. three_dof.pitch -= mouse_tilt_factor * value * (1.0f / 60.0f);
  421. three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch);
  422. }
  423. );
  424. // Tilt down
  425. ctx->controls["tilt_down_gamepad"]->set_active_callback
  426. (
  427. [ctx, three_dof_eid, gamepad_tilt_factor](float value)
  428. {
  429. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  430. three_dof.pitch += gamepad_tilt_factor * value * (1.0f / 60.0f);
  431. three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch);
  432. }
  433. );
  434. ctx->controls["tilt_down_mouse"]->set_active_callback
  435. (
  436. [ctx, three_dof_eid, mouse_tilt_factor, mouse_rotate](float value)
  437. {
  438. if (!mouse_rotate->is_active())
  439. return;
  440. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  441. three_dof.pitch += mouse_tilt_factor * value * (1.0f / 60.0f);
  442. three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch);
  443. }
  444. );
  445. // Use tool
  446. ctx->controls["use_tool"]->set_activated_callback
  447. (
  448. [ctx]()
  449. {
  450. if (ctx->entities.count("active_tool"))
  451. {
  452. entity::id tool_eid = ctx->entities["active_tool"];
  453. const auto& tool = ctx->entity_registry->get<entity::component::tool>(tool_eid);
  454. if (tool.activated)
  455. tool.activated();
  456. }
  457. }
  458. );
  459. ctx->controls["use_tool"]->set_deactivated_callback
  460. (
  461. [ctx]()
  462. {
  463. if (ctx->entities.count("active_tool"))
  464. {
  465. entity::id tool_eid = ctx->entities["active_tool"];
  466. const auto& tool = ctx->entity_registry->get<entity::component::tool>(tool_eid);
  467. if (tool.deactivated)
  468. tool.deactivated();
  469. }
  470. }
  471. );
  472. ctx->controls["use_tool"]->set_active_callback
  473. (
  474. [ctx](float value)
  475. {
  476. if (ctx->entities.count("active_tool"))
  477. {
  478. entity::id tool_eid = ctx->entities["active_tool"];
  479. const auto& tool = ctx->entity_registry->get<entity::component::tool>(tool_eid);
  480. if (tool.active)
  481. tool.active();
  482. }
  483. }
  484. );
  485. /*
  486. auto [mouse_x, mouse_y] = ctx->app->get_mouse()->get_current_position();
  487. ctx->logger->log("tool used (" + std::to_string(mouse_x) + ", " + std::to_string(mouse_y) + ")");
  488. entity::id planet_eid = ctx->entities["planet"];
  489. entity::component::celestial_body& body = ctx->entity_registry->get<entity::component::celestial_body>(planet_eid);
  490. body.axial_tilt += math::radians(30.0f) * 1.0f / 60.0f;
  491. */
  492. }
  493. } // namespace forage
  494. } // namespace state
  495. } // namespace game