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

134 lines
4.0 KiB

  1. /*
  2. * Copyright (C) 2020 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_transform(entt::registry& registry, entt::entity eid, const math::transform<float>& transform, bool warp)
  54. {
  55. if (registry.has<transform_component>(eid))
  56. {
  57. transform_component& component = registry.get<transform_component>(eid);
  58. component.local = transform;
  59. component.warp = warp;
  60. }
  61. }
  62. void place(entt::registry& registry, entt::entity eid, const float2& translation)
  63. {
  64. snap_component component;
  65. component.warp = true;
  66. component.relative = false;
  67. component.autoremove = true;
  68. component.ray.origin = {translation[0], 10000.0f, translation[1]};
  69. component.ray.direction = {0.0f, -1.0f, 0.0f};
  70. registry.assign_or_replace<snap_component>(eid, component);
  71. }
  72. void assign_render_layers(entt::registry& registry, entt::entity eid, unsigned int layers)
  73. {
  74. if (registry.has<model_component>(eid))
  75. {
  76. model_component model = registry.get<model_component>(eid);
  77. model.layers = layers;
  78. registry.replace<model_component>(eid, model);
  79. // Apply to child layers
  80. registry.view<parent_component>().each(
  81. [&](auto entity, auto& component)
  82. {
  83. if (component.parent == eid)
  84. assign_render_layers(registry, entity, layers);
  85. });
  86. }
  87. }
  88. void bind_transform(entt::registry& registry, entt::entity source_eid, entt::entity target_eid)
  89. {
  90. copy_transform_component copy_transform = {target_eid};
  91. registry.assign_or_replace<copy_transform_component>(source_eid, copy_transform);
  92. }
  93. math::transform<float> get_local_transform(entt::registry& registry, entt::entity eid)
  94. {
  95. if (registry.has<transform_component>(eid))
  96. {
  97. const transform_component& component = registry.get<transform_component>(eid);
  98. return component.local;
  99. }
  100. return math::identity_transform<float>;
  101. }
  102. math::transform<float> get_world_transform(entt::registry& registry, entt::entity eid)
  103. {
  104. if (registry.has<transform_component>(eid))
  105. {
  106. const transform_component& component = registry.get<transform_component>(eid);
  107. return component.world;
  108. }
  109. return math::identity_transform<float>;
  110. }
  111. void parent(entt::registry& registry, entt::entity child, entt::entity parent)
  112. {
  113. parent_component component;
  114. component.parent = parent;
  115. registry.assign_or_replace<parent_component>(child, component);
  116. }
  117. } // namespace ec