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

203 lines
5.3 KiB

  1. /*
  2. * Copyright (C) 2023 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 "game/component/model.hpp"
  21. #include "game/component/transform.hpp"
  22. #include "game/component/parent.hpp"
  23. #include "game/component/celestial-body.hpp"
  24. #include "game/component/terrain.hpp"
  25. #include "math/quaternion.hpp"
  26. #include <limits>
  27. namespace entity {
  28. namespace command {
  29. void translate(entity::registry& registry, entity::id eid, const float3& translation)
  30. {
  31. const game::component::transform* transform = registry.try_get<game::component::transform>(eid);
  32. if (transform)
  33. {
  34. registry.patch<game::component::transform>
  35. (
  36. eid,
  37. [&translation](auto& transform)
  38. {
  39. transform.local.translation += translation;
  40. }
  41. );
  42. }
  43. }
  44. void rotate(entity::registry& registry, entity::id eid, float angle, const float3& axis)
  45. {
  46. const game::component::transform* transform = registry.try_get<game::component::transform>(eid);
  47. if (transform)
  48. {
  49. registry.patch<game::component::transform>
  50. (
  51. eid,
  52. [angle, &axis](auto& transform)
  53. {
  54. transform.local.rotation = math::normalize(math::angle_axis(angle, axis) * transform.local.rotation);
  55. }
  56. );
  57. }
  58. }
  59. void move_to(entity::registry& registry, entity::id eid, const float3& position)
  60. {
  61. const game::component::transform* transform = registry.try_get<game::component::transform>(eid);
  62. if (transform)
  63. {
  64. registry.patch<game::component::transform>
  65. (
  66. eid,
  67. [&position](auto& transform)
  68. {
  69. transform.local.translation = position;
  70. }
  71. );
  72. }
  73. }
  74. void warp_to(entity::registry& registry, entity::id eid, const float3& position)
  75. {
  76. const game::component::transform* transform = registry.try_get<game::component::transform>(eid);
  77. if (transform)
  78. {
  79. registry.patch<game::component::transform>
  80. (
  81. eid,
  82. [&position](auto& transform)
  83. {
  84. transform.local.translation = position;
  85. transform.warp = true;
  86. }
  87. );
  88. }
  89. }
  90. void set_scale(entity::registry& registry, entity::id eid, const float3& scale)
  91. {
  92. const game::component::transform* transform = registry.try_get<game::component::transform>(eid);
  93. if (transform)
  94. {
  95. registry.patch<game::component::transform>
  96. (
  97. eid,
  98. [&scale](auto& transform)
  99. {
  100. transform.local.scale = scale;
  101. }
  102. );
  103. }
  104. }
  105. void set_transform(entity::registry& registry, entity::id eid, const math::transform<float>& transform, bool warp)
  106. {
  107. const game::component::transform* transform_component = registry.try_get<game::component::transform>(eid);
  108. if (transform_component)
  109. {
  110. registry.patch<game::component::transform>
  111. (
  112. eid,
  113. [&other_transform = transform, warp](auto& transform)
  114. {
  115. transform.local = other_transform;
  116. transform.warp = warp;
  117. }
  118. );
  119. }
  120. }
  121. void place(entity::registry& registry, entity::id eid, entity::id celestial_body_id, double altitude, double latitude, double longitude)
  122. {
  123. /*
  124. if (registry.has<game::component::transform>(eid))
  125. {
  126. double x = 0.0;
  127. double y = altitude;
  128. double z = 0.0;
  129. if (registry.has<game::component::celestial_body>(celestial_body_id))
  130. {
  131. const game::component::celestial_body& celestial_body = registry.get<game::component::celestial_body>(celestial_body_id);
  132. x = longitude * math::two_pi<double> * celestial_body.radius;
  133. z = -latitude * math::two_pi<double> * celestial_body.radius;
  134. if (registry.has<game::component::terrain>(celestial_body_id))
  135. {
  136. const game::component::terrain& terrain = registry.get<game::component::terrain>(celestial_body_id);
  137. if (terrain.elevation != nullptr)
  138. {
  139. y += terrain.elevation(latitude, longitude);
  140. }
  141. }
  142. }
  143. game::component::transform& transform = registry.get<game::component::transform>(eid);
  144. transform.local.translation = math::vector<float, 3>(double3{x, y, z});
  145. transform.warp = true;
  146. }
  147. */
  148. }
  149. void assign_render_layers(entity::registry& registry, entity::id eid, unsigned int layers)
  150. {
  151. const game::component::model* model = registry.try_get<game::component::model>(eid);
  152. if (model)
  153. {
  154. registry.patch<game::component::model>
  155. (
  156. eid,
  157. [layers](auto& model)
  158. {
  159. model.layers = layers;
  160. }
  161. );
  162. }
  163. }
  164. math::transform<float> get_local_transform(entity::registry& registry, entity::id eid)
  165. {
  166. const game::component::transform* transform = registry.try_get<game::component::transform>(eid);
  167. if (transform)
  168. {
  169. return transform->local;
  170. }
  171. return math::transform<float>::identity;
  172. }
  173. math::transform<float> get_world_transform(entity::registry& registry, entity::id eid)
  174. {
  175. const game::component::transform* transform = registry.try_get<game::component::transform>(eid);
  176. if (transform)
  177. {
  178. return transform->world;
  179. }
  180. return math::transform<float>::identity;
  181. }
  182. } // namespace command
  183. } // namespace entity