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

143 lines
4.4 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 "entity/commands.hpp"
  20. #include "entity/components/model.hpp"
  21. #include "entity/components/transform.hpp"
  22. #include "entity/components/copy-transform.hpp"
  23. #include "entity/components/snap.hpp"
  24. #include "entity/components/parent.hpp"
  25. #include <limits>
  26. namespace entity {
  27. namespace command {
  28. void translate(entity::registry& registry, entity::id entity_id, const float3& translation)
  29. {
  30. if (registry.has<component::transform>(entity_id))
  31. {
  32. component::transform& transform = registry.get<component::transform>(entity_id);
  33. transform.local.translation += translation;
  34. }
  35. }
  36. void move_to(entity::registry& registry, entity::id entity_id, const float3& position)
  37. {
  38. if (registry.has<component::transform>(entity_id))
  39. {
  40. component::transform& transform = registry.get<component::transform>(entity_id);
  41. transform.local.translation = position;
  42. }
  43. }
  44. void warp_to(entity::registry& registry, entity::id entity_id, const float3& position)
  45. {
  46. if (registry.has<component::transform>(entity_id))
  47. {
  48. component::transform& transform = registry.get<component::transform>(entity_id);
  49. transform.local.translation = position;
  50. transform.warp = true;
  51. }
  52. }
  53. void set_scale(entity::registry& registry, entity::id entity_id, const float3& scale)
  54. {
  55. if (registry.has<component::transform>(entity_id))
  56. {
  57. component::transform& transform = registry.get<component::transform>(entity_id);
  58. transform.local.scale = scale;
  59. }
  60. }
  61. void set_transform(entity::registry& registry, entity::id entity_id, const math::transform<float>& transform, bool warp)
  62. {
  63. if (registry.has<component::transform>(entity_id))
  64. {
  65. component::transform& component = registry.get<component::transform>(entity_id);
  66. component.local = transform;
  67. component.warp = warp;
  68. }
  69. }
  70. void place(entity::registry& registry, entity::id entity_id, const float2& translation)
  71. {
  72. component::snap component;
  73. component.warp = true;
  74. component.relative = false;
  75. component.autoremove = true;
  76. component.ray.origin = {translation[0], 10000.0f, translation[1]};
  77. component.ray.direction = {0.0f, -1.0f, 0.0f};
  78. registry.assign_or_replace<component::snap>(entity_id, component);
  79. }
  80. void assign_render_layers(entity::registry& registry, entity::id entity_id, unsigned int layers)
  81. {
  82. if (registry.has<component::model>(entity_id))
  83. {
  84. component::model model = registry.get<component::model>(entity_id);
  85. model.layers = layers;
  86. registry.replace<component::model>(entity_id, model);
  87. // Apply to child layers
  88. registry.view<component::parent>().each(
  89. [&](entity::id entity_id, auto& component)
  90. {
  91. if (component.parent == entity_id)
  92. assign_render_layers(registry, entity_id, layers);
  93. });
  94. }
  95. }
  96. void bind_transform(entity::registry& registry, entity::id source, entity::id target)
  97. {
  98. component::copy_transform copy_transform = {target};
  99. registry.assign_or_replace<component::copy_transform>(source, copy_transform);
  100. }
  101. math::transform<float> get_local_transform(entity::registry& registry, entity::id entity_id)
  102. {
  103. if (registry.has<component::transform>(entity_id))
  104. {
  105. const component::transform& component = registry.get<component::transform>(entity_id);
  106. return component.local;
  107. }
  108. return math::identity_transform<float>;
  109. }
  110. math::transform<float> get_world_transform(entity::registry& registry, entity::id entity_id)
  111. {
  112. if (registry.has<component::transform>(entity_id))
  113. {
  114. const component::transform& component = registry.get<component::transform>(entity_id);
  115. return component.world;
  116. }
  117. return math::identity_transform<float>;
  118. }
  119. void parent(entity::registry& registry, entity::id child, entity::id parent)
  120. {
  121. component::parent component;
  122. component.parent = parent;
  123. registry.assign_or_replace<component::parent>(child, component);
  124. }
  125. } // namespace command
  126. } // namespace entity