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

240 lines
7.2 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 "scene/camera.hpp"
  22. #include "geometry/intersection.hpp"
  23. #include "animation/ease.hpp"
  24. #include "nest.hpp"
  25. #include "math/math.hpp"
  26. #include "game/entity-commands.hpp"
  27. #include "game/systems/camera-system.hpp"
  28. #include "animation/orbit-cam.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. control_set.add_control(&equip_lens_control);
  53. control_set.add_control(&equip_brush_control);
  54. control_set.add_control(&equip_forceps_control);
  55. control_set.add_control(&equip_marker_control);
  56. control_set.add_control(&use_tool_control);
  57. // Set deadzone at 15% for all controls
  58. const std::list<control*>* controls = control_set.get_controls();
  59. for (control* control: *controls)
  60. {
  61. control->set_deadzone(0.15f);
  62. }
  63. zoom_speed = 5.0f; //1
  64. min_elevation = math::radians(-85.0f);
  65. max_elevation = math::radians(85.0f);
  66. near_focal_distance = 2.0f;
  67. far_focal_distance = 200.0f;
  68. near_movement_speed = 10.0f;
  69. far_movement_speed = 80.0f;
  70. near_fov = math::radians(80.0f);
  71. far_fov = math::radians(35.0f);
  72. near_clip_near = 0.1f;
  73. far_clip_near = 5.0f;
  74. near_clip_far = 100.0f;
  75. far_clip_far = 2000.0f;
  76. nest = nullptr;
  77. mouse_angle = 0.0f;
  78. old_mouse_angle = mouse_angle;
  79. flashlight_turns = 0.0f;
  80. flashlight_turns_i = 0;
  81. flashlight_turns_f = 0.0f;
  82. }
  83. void control_system::update(double t, double dt)
  84. {
  85. timestep = dt;
  86. // Zoom camera
  87. if (zoom_in_control.is_active())
  88. camera_system->zoom(zoom_speed * dt);
  89. if (zoom_out_control.is_active())
  90. camera_system->zoom(-zoom_speed * dt);
  91. // Rotate camera
  92. const float rotation_speed = math::radians(270.0f);
  93. if (rotate_ccw_control.is_active())
  94. camera_system->pan(rotation_speed * dt * std::min<float>(1.0f, rotate_ccw_control.get_current_value()));
  95. if (rotate_cw_control.is_active())
  96. camera_system->pan(-rotation_speed * dt * std::min<float>(1.0f, rotate_cw_control.get_current_value()));
  97. // Move camera
  98. float3 movement{0.0f, 0.0f, 0.0f};
  99. if (move_right_control.is_active())
  100. movement.x += move_right_control.get_current_value();
  101. if (move_left_control.is_active())
  102. movement.x -= move_left_control.get_current_value();
  103. if (move_forward_control.is_active())
  104. movement.z -= move_forward_control.get_current_value();
  105. if (move_back_control.is_active())
  106. movement.z += move_back_control.get_current_value();
  107. if (math::length_squared(movement) != 0.0f)
  108. {
  109. std::array<float, 2> speed_limits = {15.0f, 100.0f};
  110. float zoom = camera_system->get_orbit_cam()->get_zoom();
  111. float max_speed = math::log_lerp(speed_limits[1], speed_limits[0], zoom) * dt;
  112. float speed = std::min<float>(max_speed, math::length(movement * max_speed));
  113. movement = math::normalize(movement) * speed;
  114. const math::quaternion<float>& azimuth_rotation = camera_system->get_orbit_cam()->get_azimuth_rotation();
  115. movement = azimuth_rotation * movement;
  116. ec::translate(registry, camera_subject_eid, movement);
  117. }
  118. // Turn flashlight
  119. float2 viewport_center = {(viewport[0] + viewport[2]) * 0.5f, (viewport[1] + viewport[3]) * 0.5f};
  120. float2 mouse_direction = math::normalize(mouse_position - viewport_center);
  121. old_mouse_angle = mouse_angle;
  122. mouse_angle = std::atan2(-mouse_direction.y, mouse_direction.x);
  123. if (mouse_angle - old_mouse_angle != 0.0f)
  124. {
  125. if (mouse_angle - old_mouse_angle <= -math::pi<float>)
  126. flashlight_turns_i -= 1;
  127. else if (mouse_angle - old_mouse_angle >= math::pi<float>)
  128. flashlight_turns_i += 1;
  129. flashlight_turns_f = (mouse_angle) / math::two_pi<float>;
  130. flashlight_turns = flashlight_turns_i - flashlight_turns_f;
  131. if (flashlight_eid != entt::null && nest)
  132. {
  133. math::transform<float> flashlight_transform = math::identity_transform<float>;
  134. float flashlight_depth = nest->get_shaft_depth(*nest->get_central_shaft(), flashlight_turns);
  135. flashlight_transform.translation = {0.0f, -flashlight_depth, 0.0f};
  136. flashlight_transform.rotation = math::angle_axis(-flashlight_turns * math::two_pi<float> + math::half_pi<float>, {0, 1, 0});
  137. ec::set_transform(registry, flashlight_eid, flashlight_transform, false);
  138. if (underworld_camera)
  139. {
  140. underworld_camera->look_at({0, -flashlight_depth + 50.0f, 0}, {0, -flashlight_depth, 0}, {0, 0, -1});
  141. }
  142. }
  143. }
  144. }
  145. void control_system::set_camera_system(::camera_system* camera_system)
  146. {
  147. this->camera_system = camera_system;
  148. }
  149. void control_system::set_nest(::nest* nest)
  150. {
  151. this->nest = nest;
  152. }
  153. void control_system::set_tool(model_instance* tool)
  154. {
  155. this->tool = tool;
  156. }
  157. void control_system::set_flashlight(entt::entity eid)
  158. {
  159. flashlight_eid = eid;
  160. }
  161. void control_system::set_camera_subject(entt::entity eid)
  162. {
  163. camera_subject_eid = eid;
  164. }
  165. void control_system::set_viewport(const float4& viewport)
  166. {
  167. this->viewport = viewport;
  168. }
  169. void control_system::set_underworld_camera(::camera* camera)
  170. {
  171. this->underworld_camera = camera;
  172. }
  173. void control_system::handle_event(const mouse_moved_event& event)
  174. {
  175. if (adjust_camera_control.is_active())
  176. {
  177. bool invert_x = true;
  178. bool invert_y = false;
  179. float rotation_factor = event.dx;
  180. float elevation_factor = event.dy;
  181. if (invert_x)
  182. {
  183. rotation_factor *= -1.0f;
  184. }
  185. if (invert_y)
  186. {
  187. elevation_factor *= -1.0f;
  188. }
  189. float rotation = math::radians(15.0f) * rotation_factor * timestep;
  190. float tilt = math::radians(15.0f) * elevation_factor * timestep;
  191. camera_system->pan(rotation);
  192. camera_system->tilt(tilt);
  193. }
  194. else if (!adjust_camera_control.was_active())
  195. {
  196. mouse_position[0] = event.x;
  197. mouse_position[1] = event.y;
  198. }
  199. }
  200. void control_system::handle_event(const window_resized_event& event)
  201. {
  202. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  203. }