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

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