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

357 lines
11 KiB

  1. /*
  2. * Copyright (C) 2020 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 "control-system.hpp"
  20. #include "input/control.hpp"
  21. #include "orbit-cam.hpp"
  22. #include "scene/camera.hpp"
  23. #include "geometry/intersection.hpp"
  24. #include "animation/ease.hpp"
  25. #include "nest.hpp"
  26. #include "math/math.hpp"
  27. #include "game/entity-commands.hpp"
  28. #include "game/systems/camera-system.hpp"
  29. control_system::control_system(entt::registry& registry):
  30. entity_system(registry),
  31. timestep(0.0f),
  32. zoom(0.0f),
  33. tool(nullptr),
  34. flashlight_eid(entt::null),
  35. underworld_camera(nullptr)
  36. {
  37. control_set.add_control(&move_forward_control);
  38. control_set.add_control(&move_back_control);
  39. control_set.add_control(&move_right_control);
  40. control_set.add_control(&move_left_control);
  41. control_set.add_control(&rotate_ccw_control);
  42. control_set.add_control(&rotate_cw_control);
  43. control_set.add_control(&tilt_up_control);
  44. control_set.add_control(&tilt_down_control);
  45. control_set.add_control(&zoom_in_control);
  46. control_set.add_control(&zoom_out_control);
  47. control_set.add_control(&adjust_camera_control);
  48. control_set.add_control(&ascend_control);
  49. control_set.add_control(&descend_control);
  50. control_set.add_control(&toggle_view_control);
  51. control_set.add_control(&tool_menu_control);
  52. // Set deadzone at 15% for all controls
  53. const std::list<control*>* controls = control_set.get_controls();
  54. for (control* control: *controls)
  55. {
  56. control->set_deadzone(0.15f);
  57. }
  58. zoom_speed = 5.0f; //1
  59. min_elevation = math::radians(-85.0f);
  60. max_elevation = math::radians(85.0f);
  61. near_focal_distance = 2.0f;
  62. far_focal_distance = 200.0f;
  63. near_movement_speed = 10.0f;
  64. far_movement_speed = 80.0f;
  65. near_fov = math::radians(80.0f);
  66. far_fov = math::radians(35.0f);
  67. near_clip_near = 0.1f;
  68. far_clip_near = 5.0f;
  69. near_clip_far = 100.0f;
  70. far_clip_far = 2000.0f;
  71. nest = nullptr;
  72. orbit_cam = nullptr;
  73. mouse_angle = 0.0f;
  74. old_mouse_angle = mouse_angle;
  75. flashlight_turns = 0.0f;
  76. flashlight_turns_i = 0;
  77. flashlight_turns_f = 0.0f;
  78. }
  79. void control_system::update(double t, double dt)
  80. {
  81. // Zoom camera
  82. if (zoom_in_control.is_active())
  83. camera_system->zoom(zoom_speed * dt);
  84. if (zoom_out_control.is_active())
  85. camera_system->zoom(-zoom_speed * dt);
  86. // Move camera
  87. float3 movement{0.0f, 0.0f, 0.0f};
  88. if (move_right_control.is_active())
  89. movement.x += move_right_control.get_current_value();
  90. if (move_left_control.is_active())
  91. movement.x -= move_left_control.get_current_value();
  92. if (move_forward_control.is_active())
  93. movement.z -= move_forward_control.get_current_value();
  94. if (move_back_control.is_active())
  95. movement.z += move_back_control.get_current_value();
  96. if (math::length_squared(movement) != 0.0f)
  97. {
  98. float max_speed = 100.0f * dt;
  99. float speed = std::min<float>(max_speed, math::length(movement * max_speed));
  100. movement = math::normalize(movement) * speed;
  101. float azimuth = camera_system->get_azimuth_spring().x0;
  102. math::quaternion<float> azimuth_rotation = math::angle_axis(azimuth, float3{0.0f, 1.0f, 0.0f});
  103. movement = azimuth_rotation * movement;
  104. ec::translate(registry, camera_subject_eid, movement);
  105. }
  106. // Turn flashlight
  107. float2 viewport_center = {(viewport[0] + viewport[2]) * 0.5f, (viewport[1] + viewport[3]) * 0.5f};
  108. float2 mouse_direction = math::normalize(mouse_position - viewport_center);
  109. old_mouse_angle = mouse_angle;
  110. mouse_angle = std::atan2(-mouse_direction.y, mouse_direction.x);
  111. if (mouse_angle - old_mouse_angle != 0.0f)
  112. {
  113. if (mouse_angle - old_mouse_angle <= -math::pi<float>)
  114. flashlight_turns_i -= 1;
  115. else if (mouse_angle - old_mouse_angle >= math::pi<float>)
  116. flashlight_turns_i += 1;
  117. flashlight_turns_f = (mouse_angle) / math::two_pi<float>;
  118. flashlight_turns = flashlight_turns_i - flashlight_turns_f;
  119. if (flashlight_eid != entt::null && nest)
  120. {
  121. math::transform<float> flashlight_transform = math::identity_transform<float>;
  122. float flashlight_depth = nest->get_shaft_depth(*nest->get_central_shaft(), flashlight_turns);
  123. flashlight_transform.translation = {0.0f, -flashlight_depth, 0.0f};
  124. flashlight_transform.rotation = math::angle_axis(-flashlight_turns * math::two_pi<float> + math::half_pi<float>, {0, 1, 0});
  125. ec::set_transform(registry, flashlight_eid, flashlight_transform, false);
  126. if (underworld_camera)
  127. {
  128. underworld_camera->look_at({0, -flashlight_depth + 50.0f, 0}, {0, -flashlight_depth, 0}, {0, 0, -1});
  129. }
  130. }
  131. }
  132. }
  133. /*
  134. void control_system::update(double t, double dt)
  135. {
  136. this->timestep = dt;
  137. if (adjust_camera_control.is_active() && !adjust_camera_control.was_active())
  138. {
  139. }
  140. // Determine zoom
  141. if (zoom_in_control.is_active())
  142. zoom += zoom_speed * dt * zoom_in_control.get_current_value();
  143. if (zoom_out_control.is_active())
  144. zoom -= zoom_speed * dt * zoom_out_control.get_current_value();
  145. zoom = std::max<float>(0.0f, std::min<float>(1.0f, zoom));
  146. float focal_distance = math::log_lerp<float>(near_focal_distance, far_focal_distance, 1.0f - zoom);
  147. float fov = math::log_lerp<float>(near_fov, far_fov, 1.0f - zoom);
  148. //float elevation_factor = (orbit_cam->get_target_elevation() - min_elevation) / max_elevation;
  149. //fov = math::log_lerp<float>(near_fov, far_fov, elevation_factor);
  150. float clip_near = math::log_lerp<float>(near_clip_near, far_clip_near, 1.0f - zoom);
  151. float clip_far = math::log_lerp<float>(near_clip_far, far_clip_far, 1.0f - zoom);
  152. float movement_speed = math::log_lerp<float>(near_movement_speed * dt, far_movement_speed * dt, 1.0f - zoom);
  153. orbit_cam->set_target_focal_distance(focal_distance);
  154. orbit_cam->get_camera()->set_perspective(fov, orbit_cam->get_camera()->get_aspect_ratio(), clip_near, clip_far);
  155. const float rotation_speed = 2.0f * dt;
  156. float rotation = 0.0f;
  157. if (rotate_ccw_control.is_active())
  158. rotation += rotate_ccw_control.get_current_value() * rotation_speed;
  159. if (rotate_cw_control.is_active())
  160. rotation -= rotate_cw_control.get_current_value() * rotation_speed;
  161. if (rotation)
  162. {
  163. orbit_cam->rotate(rotation);
  164. }
  165. const float tilt_speed = 2.0f * dt;
  166. float tilt = 0.0f;
  167. if (tilt_up_control.is_active())
  168. tilt -= tilt_up_control.get_current_value() * tilt_speed;
  169. if (tilt_down_control.is_active())
  170. tilt += tilt_down_control.get_current_value() * tilt_speed;
  171. if (tilt)
  172. {
  173. orbit_cam->tilt(tilt);
  174. float elevation = orbit_cam->get_target_elevation();
  175. elevation = std::min<float>(max_elevation, std::max<float>(min_elevation, elevation));
  176. orbit_cam->set_target_elevation(elevation);
  177. }
  178. float2 movement{0.0f, 0.0f};
  179. if (move_right_control.is_active())
  180. movement[0] += move_right_control.get_current_value();
  181. if (move_left_control.is_active())
  182. movement[0] -= move_left_control.get_current_value();
  183. if (move_forward_control.is_active())
  184. movement[1] -= move_forward_control.get_current_value();
  185. if (move_back_control.is_active())
  186. movement[1] += move_back_control.get_current_value();
  187. const float deadzone = 0.01f;
  188. float magnitude_squared = math::length_squared(movement);
  189. if (magnitude_squared > deadzone)
  190. {
  191. if (magnitude_squared > 1.0f)
  192. {
  193. movement = math::normalize(movement);
  194. }
  195. orbit_cam->move(movement * movement_speed);
  196. }
  197. float ascention = 0.0f;
  198. if (ascend_control.is_active())
  199. ascention += ascend_control.get_current_value();
  200. if (descend_control.is_active())
  201. ascention -= descend_control.get_current_value();
  202. if (ascention)
  203. {
  204. float old_depth = -orbit_cam->get_target_focal_point()[1];
  205. orbit_cam->set_target_focal_point(orbit_cam->get_target_focal_point() + float3{0.0f, ascention * movement_speed, 0.0f});
  206. if (nest)
  207. {
  208. float3 focal_point = orbit_cam->get_target_focal_point();
  209. float depth = -focal_point[1];
  210. float delta_shaft_angle = nest->get_shaft_angle(*nest->get_central_shaft(), depth) - nest->get_shaft_angle(*nest->get_central_shaft(), old_depth);
  211. //orbit_cam->set_target_azimuth(orbit_cam->get_target_azimuth() - delta_shaft_angle);
  212. orbit_cam->set_target_focal_point(nest->get_shaft_position(*nest->get_central_shaft(), depth));
  213. }
  214. }
  215. orbit_cam->update(dt);
  216. camera* camera = orbit_cam->get_camera();
  217. float3 pick_near = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 0.0f}, viewport);
  218. float3 pick_far = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 1.0f}, viewport);
  219. float3 pick_direction = math::normalize(pick_far - pick_near);
  220. ray<float> picking_ray = {pick_near, pick_direction};
  221. plane<float> ground_plane = {float3{0, 1, 0}, 0.0f};
  222. auto intersection_result = ray_plane_intersection(picking_ray, ground_plane);
  223. if (std::get<0>(intersection_result))
  224. {
  225. float3 pick = picking_ray.extrapolate(std::get<1>(intersection_result));
  226. if (tool)
  227. tool->set_translation(pick);
  228. }
  229. if (toggle_view_control.is_active() && !toggle_view_control.was_active())
  230. {
  231. }
  232. }
  233. */
  234. void control_system::set_orbit_cam(::orbit_cam* orbit_cam)
  235. {
  236. this->orbit_cam = orbit_cam;
  237. }
  238. void control_system::set_camera_system(::camera_system* camera_system)
  239. {
  240. this->camera_system = camera_system;
  241. }
  242. void control_system::set_nest(::nest* nest)
  243. {
  244. this->nest = nest;
  245. }
  246. void control_system::set_tool(model_instance* tool)
  247. {
  248. this->tool = tool;
  249. }
  250. void control_system::set_flashlight(entt::entity eid)
  251. {
  252. flashlight_eid = eid;
  253. }
  254. void control_system::set_camera_subject(entt::entity eid)
  255. {
  256. camera_subject_eid = eid;
  257. }
  258. void control_system::set_viewport(const float4& viewport)
  259. {
  260. this->viewport = viewport;
  261. }
  262. void control_system::set_underworld_camera(::camera* camera)
  263. {
  264. this->underworld_camera = camera;
  265. }
  266. void control_system::handle_event(const mouse_moved_event& event)
  267. {
  268. if (adjust_camera_control.is_active())
  269. {
  270. bool invert_x = true;
  271. bool invert_y = false;
  272. float rotation_factor = event.dx;
  273. float elevation_factor = event.dy;
  274. if (invert_x)
  275. {
  276. rotation_factor *= -1.0f;
  277. }
  278. if (invert_y)
  279. {
  280. elevation_factor *= -1.0f;
  281. }
  282. float rotation = math::radians(22.5f) * rotation_factor * timestep;
  283. float elevation = orbit_cam->get_target_elevation() + elevation_factor * 0.25f * timestep;
  284. elevation = std::min<float>(max_elevation, std::max<float>(min_elevation, elevation));
  285. orbit_cam->rotate(rotation);
  286. orbit_cam->set_target_elevation(elevation);
  287. }
  288. else if (!adjust_camera_control.was_active())
  289. {
  290. mouse_position[0] = event.x;
  291. mouse_position[1] = event.y;
  292. }
  293. }
  294. void control_system::handle_event(const window_resized_event& event)
  295. {
  296. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  297. }