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

99 lines
3.3 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 "constraint-system.hpp"
  20. #include "ecs/components/copy-translation-component.hpp"
  21. #include "ecs/components/copy-rotation-component.hpp"
  22. #include "ecs/components/copy-scale-component.hpp"
  23. #include "ecs/components/copy-transform-component.hpp"
  24. #include "ecs/components/transform-component.hpp"
  25. #include "utility/fundamental-types.hpp"
  26. namespace ecs {
  27. constraint_system::constraint_system(ecs::registry& registry):
  28. entity_system(registry)
  29. {}
  30. void constraint_system::update(double t, double dt)
  31. {
  32. auto transforms_view = registry.view<transform_component>();
  33. // Handle copy translation constraints
  34. registry.view<copy_translation_component, transform_component>().each
  35. (
  36. [&](ecs::entity entity, auto& constraint, auto& transform)
  37. {
  38. if (this->registry.has<transform_component>(constraint.target))
  39. {
  40. const float3& target_translation = transforms_view.get(constraint.target).world.translation;
  41. if (constraint.use_x)
  42. transform.world.translation.x = (constraint.invert_x) ? -target_translation.x : target_translation.x;
  43. if (constraint.use_y)
  44. transform.world.translation.y = (constraint.invert_y) ? -target_translation.y : target_translation.y;
  45. if (constraint.use_z)
  46. transform.world.translation.z = (constraint.invert_z) ? -target_translation.z : target_translation.z;
  47. }
  48. }
  49. );
  50. // Handle copy rotation constraints
  51. registry.view<copy_rotation_component, transform_component>().each
  52. (
  53. [&](ecs::entity entity, auto& constraint, auto& transform)
  54. {
  55. if (this->registry.has<transform_component>(constraint.target))
  56. {
  57. transform.world.rotation = transforms_view.get(constraint.target).world.rotation;
  58. }
  59. }
  60. );
  61. // Handle copy scale constraints
  62. registry.view<copy_scale_component, transform_component>().each
  63. (
  64. [&](ecs::entity entity, auto& constraint, auto& transform)
  65. {
  66. if (this->registry.has<transform_component>(constraint.target))
  67. {
  68. const float3& target_scale = transforms_view.get(constraint.target).world.scale;
  69. if (constraint.use_x)
  70. transform.world.scale.x = target_scale.x;
  71. if (constraint.use_y)
  72. transform.world.scale.y = target_scale.y;
  73. if (constraint.use_z)
  74. transform.world.scale.z = target_scale.z;
  75. }
  76. }
  77. );
  78. // Handle copy transform constraints
  79. registry.view<copy_transform_component, transform_component>().each
  80. (
  81. [&](ecs::entity entity, auto& constraint, auto& transform)
  82. {
  83. if (this->registry.has<transform_component>(constraint.target))
  84. {
  85. transform.world = transforms_view.get(constraint.target).world;
  86. }
  87. }
  88. );
  89. }
  90. } // namespace ecs