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