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

168 lines
5.0 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/snap.hpp"
  23. #include "entity/components/parent.hpp"
  24. #include "entity/components/celestial-body.hpp"
  25. #include "entity/components/terrain.hpp"
  26. #include <limits>
  27. namespace entity {
  28. namespace command {
  29. void translate(entity::registry& registry, entity::id eid, const float3& translation)
  30. {
  31. if (registry.has<component::transform>(eid))
  32. {
  33. component::transform& transform = registry.get<component::transform>(eid);
  34. transform.local.translation += translation;
  35. }
  36. }
  37. void rotate(entity::registry& registry, entity::id eid, float angle, const float3& axis)
  38. {
  39. if (registry.has<component::transform>(eid))
  40. {
  41. component::transform& transform = registry.get<component::transform>(eid);
  42. transform.local.rotation = math::angle_axis(angle, axis) * transform.local.rotation;
  43. }
  44. }
  45. void move_to(entity::registry& registry, entity::id eid, const float3& position)
  46. {
  47. if (registry.has<component::transform>(eid))
  48. {
  49. component::transform& transform = registry.get<component::transform>(eid);
  50. transform.local.translation = position;
  51. }
  52. }
  53. void warp_to(entity::registry& registry, entity::id eid, const float3& position)
  54. {
  55. if (registry.has<component::transform>(eid))
  56. {
  57. component::transform& transform = registry.get<component::transform>(eid);
  58. transform.local.translation = position;
  59. transform.warp = true;
  60. }
  61. }
  62. void set_scale(entity::registry& registry, entity::id eid, const float3& scale)
  63. {
  64. if (registry.has<component::transform>(eid))
  65. {
  66. component::transform& transform = registry.get<component::transform>(eid);
  67. transform.local.scale = scale;
  68. }
  69. }
  70. void set_transform(entity::registry& registry, entity::id eid, const math::transform<float>& transform, bool warp)
  71. {
  72. if (registry.has<component::transform>(eid))
  73. {
  74. component::transform& component = registry.get<component::transform>(eid);
  75. component.local = transform;
  76. component.warp = warp;
  77. }
  78. }
  79. void place(entity::registry& registry, entity::id eid, entity::id celestial_body_id, double altitude, double latitude, double longitude)
  80. {
  81. if (registry.has<component::transform>(eid))
  82. {
  83. double x = 0.0;
  84. double y = altitude;
  85. double z = 0.0;
  86. if (registry.has<component::celestial_body>(celestial_body_id))
  87. {
  88. const component::celestial_body& celestial_body = registry.get<component::celestial_body>(celestial_body_id);
  89. x = longitude * math::two_pi<double> * celestial_body.radius;
  90. z = -latitude * math::two_pi<double> * celestial_body.radius;
  91. if (registry.has<component::terrain>(celestial_body_id))
  92. {
  93. const component::terrain& terrain = registry.get<component::terrain>(celestial_body_id);
  94. if (terrain.elevation != nullptr)
  95. {
  96. y += terrain.elevation(latitude, longitude);
  97. }
  98. }
  99. }
  100. component::transform& transform = registry.get<component::transform>(eid);
  101. transform.local.translation = math::type_cast<float>(double3{x, y, z});
  102. transform.warp = true;
  103. }
  104. }
  105. void assign_render_layers(entity::registry& registry, entity::id eid, unsigned int layers)
  106. {
  107. if (registry.has<component::model>(eid))
  108. {
  109. component::model model = registry.get<component::model>(eid);
  110. model.layers = layers;
  111. registry.replace<component::model>(eid, model);
  112. // Apply to child layers
  113. registry.view<component::parent>().each(
  114. [&](entity::id eid, auto& component)
  115. {
  116. if (component.parent == eid)
  117. assign_render_layers(registry, eid, layers);
  118. });
  119. }
  120. }
  121. math::transform<float> get_local_transform(entity::registry& registry, entity::id eid)
  122. {
  123. if (registry.has<component::transform>(eid))
  124. {
  125. const component::transform& component = registry.get<component::transform>(eid);
  126. return component.local;
  127. }
  128. return math::identity_transform<float>;
  129. }
  130. math::transform<float> get_world_transform(entity::registry& registry, entity::id eid)
  131. {
  132. if (registry.has<component::transform>(eid))
  133. {
  134. const component::transform& component = registry.get<component::transform>(eid);
  135. return component.world;
  136. }
  137. return math::identity_transform<float>;
  138. }
  139. void parent(entity::registry& registry, entity::id child, entity::id parent)
  140. {
  141. component::parent component;
  142. component.parent = parent;
  143. registry.assign_or_replace<component::parent>(child, component);
  144. }
  145. } // namespace command
  146. } // namespace entity