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

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