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

399 lines
13 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/brood.hpp"
  20. #include "entity/archetype.hpp"
  21. #include "entity/commands.hpp"
  22. #include "entity/components/observer.hpp"
  23. #include "entity/components/camera.hpp"
  24. #include "entity/components/terrain.hpp"
  25. #include "entity/components/transform.hpp"
  26. #include "entity/components/chamber.hpp"
  27. #include "entity/components/constraints/spring-to.hpp"
  28. #include "entity/components/constraints/three-dof.hpp"
  29. #include "entity/components/constraint-stack.hpp"
  30. #include "animation/screen-transition.hpp"
  31. #include "animation/ease.hpp"
  32. #include "resources/resource-manager.hpp"
  33. #include "render/passes/sky-pass.hpp"
  34. #include "application.hpp"
  35. #include <iostream>
  36. namespace game {
  37. namespace state {
  38. namespace brood {
  39. static void setup_nest(game::context* ctx);
  40. static void setup_ants(game::context* ctx);
  41. static void setup_camera(game::context* ctx);
  42. static void setup_controls(game::context* ctx);
  43. void enter(game::context* ctx)
  44. {
  45. setup_nest(ctx);
  46. setup_ants(ctx);
  47. setup_camera(ctx);
  48. setup_controls(ctx);
  49. ctx->underground_ambient_light->set_intensity(1.0f);
  50. // Create larva
  51. {
  52. entity::archetype* larva_archetype = ctx->resource_manager->load<entity::archetype>("ant-larva.ent");
  53. auto larva_eid = larva_archetype->create(*ctx->entity_registry);
  54. entity::command::warp_to(*ctx->entity_registry, larva_eid, {0.0f, 0.0f, 0.0f});
  55. entity::command::assign_render_layers(*ctx->entity_registry, larva_eid, 0b1);
  56. }
  57. // Create cocoon
  58. {
  59. entity::archetype* cocoon_archetype = ctx->resource_manager->load<entity::archetype>("ant-cocoon.ent");
  60. auto cocoon_eid = cocoon_archetype->create(*ctx->entity_registry);
  61. entity::command::warp_to(*ctx->entity_registry, cocoon_eid, {-50, 0.1935f, 0});
  62. entity::command::assign_render_layers(*ctx->entity_registry, cocoon_eid, 0b1);
  63. }
  64. // Reset tweening
  65. ctx->underground_scene->update_tweens();
  66. // Start fade in
  67. ctx->fade_transition->transition(1.0f, true, ease<float>::in_quad);
  68. }
  69. void exit(game::context* ctx)
  70. {}
  71. void setup_nest(game::context* ctx)
  72. {
  73. // Create nest central shaft entity
  74. if (!ctx->entities.count("shaft"))
  75. {
  76. entity::id shaft_eid = ctx->entity_registry->create();
  77. ctx->entities["shaft"] = shaft_eid;
  78. entity::component::transform transform;
  79. transform.local = math::identity_transform<float>;
  80. transform.world = transform.local;
  81. transform.warp = true;
  82. ctx->entity_registry->assign<entity::component::transform>(shaft_eid, transform);
  83. }
  84. // Create nest lobby chamber entity
  85. if (!ctx->entities.count("lobby"))
  86. {
  87. entity::id lobby_eid = ctx->entity_registry->create();
  88. ctx->entities["lobby"] = lobby_eid;
  89. entity::component::chamber chamber;
  90. chamber.shaft_eid = ctx->entities["shaft"];
  91. chamber.distance = 10.0f;
  92. chamber.previous_chamber_eid = entt::null;
  93. chamber.next_chamber_eid = entt::null;
  94. }
  95. }
  96. void setup_ants(game::context* ctx)
  97. {
  98. // Create queen ant entity
  99. if (!ctx->entities.count("queen"))
  100. {
  101. entity::id queen_eid = ctx->entity_registry->create();
  102. ctx->entities["queen"] = queen_eid;
  103. }
  104. }
  105. void setup_camera(game::context* ctx)
  106. {
  107. // Switch to underground camera
  108. ctx->surface_camera->set_active(false);
  109. ctx->underground_camera->set_active(true);
  110. // Create underground camera entity
  111. if (!ctx->entities.count("underground_cam"))
  112. {
  113. // Create camera target entity
  114. entity::id target_eid = ctx->entity_registry->create();
  115. ctx->entities["underground_cam_target"] = target_eid;
  116. {
  117. // Transform
  118. entity::component::transform target_transform;
  119. target_transform.local = math::identity_transform<float>;
  120. target_transform.world = target_transform.local;
  121. target_transform.warp = true;
  122. ctx->entity_registry->assign<entity::component::transform>(target_eid, target_transform);
  123. }
  124. // Create camera entity
  125. entity::id camera_eid = ctx->entity_registry->create();
  126. ctx->entities["underground_cam"] = camera_eid;
  127. // Create camera transform component
  128. entity::component::transform transform;
  129. transform.local = math::identity_transform<float>;
  130. transform.world = transform.local;
  131. transform.warp = true;
  132. ctx->entity_registry->assign<entity::component::transform>(camera_eid, transform);
  133. // Create camera camera component
  134. entity::component::camera camera;
  135. camera.object = ctx->underground_camera;
  136. ctx->entity_registry->assign<entity::component::camera>(camera_eid, camera);
  137. // Create camera 3DOF constraint entity
  138. entity::id three_dof_constraint_eid = ctx->entity_registry->create();
  139. ctx->entities["underground_cam_3dof"] = three_dof_constraint_eid;
  140. {
  141. // Create 3DOF to constraint
  142. entity::component::constraint::three_dof three_dof;
  143. three_dof.yaw = 0.0f;
  144. three_dof.pitch = 0.0f;
  145. three_dof.roll = 0.0f;
  146. ctx->entity_registry->assign<entity::component::constraint::three_dof>(three_dof_constraint_eid, three_dof);
  147. // Create constraint stack node component
  148. entity::component::constraint_stack_node node;
  149. node.active = true;
  150. node.weight = 1.0f;
  151. node.next = entt::null;
  152. ctx->entity_registry->assign<entity::component::constraint_stack_node>(three_dof_constraint_eid, node);
  153. }
  154. // Create camera spring to constraint entity
  155. entity::id spring_constraint_eid = ctx->entity_registry->create();
  156. {
  157. // Create spring to constraint
  158. entity::component::constraint::spring_to spring;
  159. spring.target = target_eid;
  160. 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>};
  161. spring.translation.w = hz_to_rads(8.0f);
  162. spring.spring_translation = true;
  163. spring.spring_rotation = false;
  164. ctx->entity_registry->assign<entity::component::constraint::spring_to>(spring_constraint_eid, spring);
  165. // Create constraint stack node component
  166. entity::component::constraint_stack_node node;
  167. node.active = true;
  168. node.weight = 1.0f;
  169. node.next = three_dof_constraint_eid;
  170. ctx->entity_registry->assign<entity::component::constraint_stack_node>(spring_constraint_eid, node);
  171. }
  172. // Create camera constraint stack component
  173. entity::component::constraint_stack constraint_stack;
  174. constraint_stack.head = spring_constraint_eid;
  175. ctx->entity_registry->assign<entity::component::constraint_stack>(camera_eid, constraint_stack);
  176. }
  177. ctx->underground_camera->set_exposure(0.0f);
  178. }
  179. void setup_controls(game::context* ctx)
  180. {
  181. // Get underground camera entity
  182. entity::id camera_eid = ctx->entities["underground_cam"];
  183. entity::id target_eid = ctx->entities["underground_cam_target"];
  184. entity::id three_dof_eid = ctx->entities["underground_cam_3dof"];
  185. const float dolly_speed = 20.0f;
  186. const float truck_speed = dolly_speed;
  187. const float pedestal_speed = 30.0f;
  188. const float pan_speed = math::radians(8.0f);
  189. const float tilt_speed = pan_speed;
  190. const input::control* move_slow = ctx->controls["move_slow"];
  191. const input::control* move_fast = ctx->controls["move_fast"];
  192. const input::control* mouse_rotate = ctx->controls["mouse_rotate"];
  193. ctx->controls["dolly_forward"]->set_active_callback
  194. (
  195. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  196. {
  197. if (move_slow->is_active())
  198. value *= 0.5f;
  199. if (move_fast->is_active())
  200. value *= 2.0f;
  201. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  202. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  203. const float3 movement = {0.0f, 0.0f, -truck_speed * value * (1.0f / 60.0f)};
  204. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  205. }
  206. );
  207. // Dolly backward
  208. ctx->controls["dolly_backward"]->set_active_callback
  209. (
  210. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  211. {
  212. if (move_slow->is_active())
  213. value *= 0.5f;
  214. if (move_fast->is_active())
  215. value *= 2.0f;
  216. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  217. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  218. const float3 movement = {0.0f, 0.0f, truck_speed * value * (1.0f / 60.0f)};
  219. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  220. }
  221. );
  222. // Truck right
  223. ctx->controls["truck_right"]->set_active_callback
  224. (
  225. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  226. {
  227. if (move_slow->is_active())
  228. value *= 0.5f;
  229. if (move_fast->is_active())
  230. value *= 2.0f;
  231. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  232. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  233. const float3 movement = {truck_speed * value * (1.0f / 60.0f), 0.0f, 0.0f};
  234. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  235. }
  236. );
  237. // Truck left
  238. ctx->controls["truck_left"]->set_active_callback
  239. (
  240. [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value)
  241. {
  242. if (move_slow->is_active())
  243. value *= 0.5f;
  244. if (move_fast->is_active())
  245. value *= 2.0f;
  246. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  247. const math::quaternion<float> yaw = math::angle_axis(three_dof.yaw, {0.0f, 1.0f, 0.0f});
  248. const float3 movement = {-truck_speed * value * (1.0f / 60.0f), 0.0f, 0.0f};
  249. entity::command::translate(*ctx->entity_registry, target_eid, yaw * movement);
  250. }
  251. );
  252. // Pedestal up
  253. ctx->controls["pedestal_up"]->set_active_callback
  254. (
  255. [ctx, target_eid, pedestal_speed, move_slow, move_fast](float value)
  256. {
  257. if (move_slow->is_active())
  258. value *= 0.5f;
  259. if (move_fast->is_active())
  260. value *= 2.0f;
  261. const float3 movement = {0.0f, pedestal_speed * value * (1.0f / 60.0f), 0.0f};
  262. entity::command::translate(*ctx->entity_registry, target_eid, movement);
  263. }
  264. );
  265. // Pedestal down
  266. ctx->controls["pedestal_down"]->set_active_callback
  267. (
  268. [ctx, target_eid, pedestal_speed, move_slow, move_fast](float value)
  269. {
  270. if (move_slow->is_active())
  271. value *= 0.5f;
  272. if (move_fast->is_active())
  273. value *= 2.0f;
  274. const float3 movement = {0.0f, -pedestal_speed * value * (1.0f / 60.0f), 0.0f};
  275. entity::command::translate(*ctx->entity_registry, target_eid, movement);
  276. }
  277. );
  278. // Mouse rotate
  279. ctx->controls["mouse_rotate"]->set_activated_callback
  280. (
  281. [ctx]()
  282. {
  283. ctx->app->set_relative_mouse_mode(true);
  284. }
  285. );
  286. ctx->controls["mouse_rotate"]->set_deactivated_callback
  287. (
  288. [ctx]()
  289. {
  290. ctx->app->set_relative_mouse_mode(false);
  291. }
  292. );
  293. // Pan left
  294. ctx->controls["pan_left_mouse"]->set_active_callback
  295. (
  296. [ctx, three_dof_eid, pan_speed, mouse_rotate](float value)
  297. {
  298. if (!mouse_rotate->is_active())
  299. return;
  300. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  301. three_dof.yaw += pan_speed * value * (1.0f / 60.0f);
  302. }
  303. );
  304. // Pan right
  305. ctx->controls["pan_right_mouse"]->set_active_callback
  306. (
  307. [ctx, three_dof_eid, pan_speed, mouse_rotate](float value)
  308. {
  309. if (!mouse_rotate->is_active())
  310. return;
  311. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  312. three_dof.yaw -= pan_speed * value * (1.0f / 60.0f);
  313. }
  314. );
  315. // Tilt up
  316. ctx->controls["tilt_up_mouse"]->set_active_callback
  317. (
  318. [ctx, three_dof_eid, tilt_speed, mouse_rotate](float value)
  319. {
  320. if (!mouse_rotate->is_active())
  321. return;
  322. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  323. three_dof.pitch -= tilt_speed * value * (1.0f / 60.0f);
  324. three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch);
  325. }
  326. );
  327. // Tilt down
  328. ctx->controls["tilt_down_mouse"]->set_active_callback
  329. (
  330. [ctx, three_dof_eid, tilt_speed, mouse_rotate](float value)
  331. {
  332. if (!mouse_rotate->is_active())
  333. return;
  334. auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
  335. three_dof.pitch += tilt_speed * value * (1.0f / 60.0f);
  336. three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch);
  337. }
  338. );
  339. }
  340. } // namespace brood
  341. } // namespace state
  342. } // namespace game