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

97 lines
3.3 KiB

  1. /*
  2. * Copyright (C) 2023 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/systems/steering-system.hpp"
  20. #include "game/components/steering-component.hpp"
  21. #include "game/components/transform-component.hpp"
  22. #include "game/components/winged-locomotion-component.hpp"
  23. #include "game/components/rigid-body-component.hpp"
  24. #include <engine/entity/id.hpp>
  25. #include <engine/ai/steering/behavior/wander.hpp>
  26. #include <engine/ai/steering/behavior/seek.hpp>
  27. #include <engine/math/quaternion.hpp>
  28. #include <engine/config.hpp>
  29. steering_system::steering_system(entity::registry& registry):
  30. updatable_system(registry)
  31. {}
  32. void steering_system::update(float t, float dt)
  33. {
  34. registry.group<steering_component>(entt::get<transform_component, winged_locomotion_component, rigid_body_component>).each
  35. (
  36. [&](entity::id entity_id, auto& steering, auto& transform, auto& locomotion, const auto& body_component)
  37. {
  38. auto& agent = steering.agent;
  39. auto& body = *body_component.body;
  40. // Update agent parameters
  41. agent.position = transform.local.translation;
  42. agent.orientation = transform.local.rotation;
  43. agent.velocity = body.get_linear_velocity();
  44. // Accumulate steering forces
  45. math::fvec3 force = {0, 0, 0};
  46. if (steering.wander_weight)
  47. {
  48. //force += ai::steering::behavior::wander_2d(agent, steering.wander_noise * dt, steering.wander_distance, steering.wander_radius, steering.wander_angle) * steering.wander_weight;
  49. force += ai::steering::behavior::wander_3d(agent, steering.wander_noise * dt, steering.wander_distance, steering.wander_radius, steering.wander_angle, steering.wander_angle2) * steering.wander_weight;
  50. }
  51. if (steering.seek_weight)
  52. {
  53. force += ai::steering::behavior::seek(agent, steering.seek_target) * steering.seek_weight;
  54. }
  55. // Normalize force
  56. if (steering.sum_weights)
  57. {
  58. force /= steering.sum_weights;
  59. }
  60. // Pass force to winged locomotion component
  61. registry.patch<::winged_locomotion_component>
  62. (
  63. entity_id,
  64. [&](auto& component)
  65. {
  66. component.force = force;
  67. }
  68. );
  69. // Rotate agent
  70. const float speed_squared = math::sqr_length(agent.velocity);
  71. if (speed_squared)
  72. {
  73. agent.orientation = math::look_rotation(agent.velocity / std::sqrt(speed_squared), agent.up);
  74. agent.forward = agent.orientation * config::global_forward;
  75. agent.up = agent.orientation * config::global_up;
  76. }
  77. // Update orientation
  78. registry.patch<::transform_component>
  79. (
  80. entity_id,
  81. [&agent](auto& component)
  82. {
  83. component.local.rotation = agent.orientation;
  84. }
  85. );
  86. }
  87. );
  88. }