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

159 lines
4.9 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 "tracking-system.hpp"
  20. #include "ecs/components/transform-component.hpp"
  21. #include "ecs/components/marker-component.hpp"
  22. #include "event/event-dispatcher.hpp"
  23. #include "resources/resource-manager.hpp"
  24. #include "scene/collection.hpp"
  25. #include "scene/model-instance.hpp"
  26. #include "math/math.hpp"
  27. #include "renderer/material.hpp"
  28. #include "renderer/model.hpp"
  29. #include "utility/fundamental-types.hpp"
  30. #include "ecs/commands.hpp"
  31. namespace ecs {
  32. tracking_system::tracking_system(ecs::registry& registry, ::event_dispatcher* event_dispatcher, ::resource_manager* resource_manager):
  33. entity_system(registry),
  34. event_dispatcher(event_dispatcher),
  35. resource_manager(resource_manager),
  36. scene_collection(nullptr)
  37. {
  38. registry.on_construct<trackable_component>().connect<&tracking_system::on_component_construct>(this);
  39. registry.on_destroy<trackable_component>().connect<&tracking_system::on_component_destroy>(this);
  40. // Load paint ball model
  41. paint_ball_model = resource_manager->load<model>("paint-ball.mdl");
  42. // Load tracker model
  43. tracker_model = resource_manager->load<model>("tracker.mdl");
  44. // Load paint ball materials
  45. paint_ball_materials = new material*[7];
  46. paint_ball_materials[0] = resource_manager->load<material>("paint-ball-yellow.mtl");
  47. paint_ball_materials[1] = resource_manager->load<material>("paint-ball-green.mtl");
  48. paint_ball_materials[2] = resource_manager->load<material>("paint-ball-blue.mtl");
  49. paint_ball_materials[3] = resource_manager->load<material>("paint-ball-purple.mtl");
  50. paint_ball_materials[4] = resource_manager->load<material>("paint-ball-pink.mtl");
  51. paint_ball_materials[5] = resource_manager->load<material>("paint-ball-red.mtl");
  52. paint_ball_materials[6] = resource_manager->load<material>("paint-ball-orange.mtl");
  53. event_dispatcher->subscribe<tool_pressed_event>(this);
  54. event_dispatcher->subscribe<tool_released_event>(this);
  55. }
  56. tracking_system::~tracking_system()
  57. {
  58. event_dispatcher->unsubscribe<tool_pressed_event>(this);
  59. event_dispatcher->unsubscribe<tool_released_event>(this);
  60. for (auto it = trackers.begin(); it != trackers.end(); ++it)
  61. {
  62. delete it->second;
  63. }
  64. delete[] paint_ball_materials;
  65. }
  66. void tracking_system::update(double t, double dt)
  67. {
  68. for (auto it = trackers.begin(); it != trackers.end(); ++it)
  69. {
  70. const transform_component& transform = registry.get<transform_component>(it->first);
  71. // Project world coordinates to screen coordinates
  72. // Update billboard position
  73. it->second->set_translation(transform.world.translation);
  74. if (transform.warp)
  75. {
  76. it->second->update_tweens();
  77. }
  78. }
  79. }
  80. void tracking_system::set_scene(scene::collection* collection)
  81. {
  82. this->scene_collection = collection;
  83. }
  84. void tracking_system::on_component_construct(ecs::registry& registry, ecs::entity entity, trackable_component& component)
  85. {
  86. }
  87. void tracking_system::on_component_destroy(ecs::registry& registry, ecs::entity entity)
  88. {
  89. if (auto it = trackers.find(entity); it != trackers.end())
  90. {
  91. // Remove model instance from all layers
  92. /*
  93. for (std::size_t i = 0; i < layers.size(); ++i)
  94. {
  95. layers[i]->remove_object(it->second);
  96. }
  97. */
  98. delete it->second;
  99. trackers.erase(it);
  100. }
  101. }
  102. void tracking_system::handle_event(const tool_pressed_event& event)
  103. {
  104. if (registry.has<marker_component>(event.entity))
  105. {
  106. math::transform<float> transform = command::get_world_transform(registry, event.entity);
  107. int marker_index = registry.get<marker_component>(event.entity).color;
  108. if (marker_index > 0)
  109. {
  110. const float tracker_scale = 1.0f;
  111. // Create tracker model instance
  112. scene::model_instance* instance = new scene::model_instance();
  113. instance->set_model(tracker_model);
  114. instance->set_translation(transform.translation);
  115. instance->set_scale(float3{tracker_scale, tracker_scale, tracker_scale});
  116. // Set tracker paint ball material
  117. const model_group* paint_ball_model_group = tracker_model->get_group("paint-ball");
  118. if (paint_ball_model_group != nullptr)
  119. {
  120. instance->set_material(paint_ball_model_group->get_index(), paint_ball_materials[marker_index - 1]);
  121. }
  122. instance->update_tweens();
  123. scene_collection->add_object(instance);
  124. }
  125. }
  126. }
  127. void tracking_system::handle_event(const tool_released_event& event)
  128. {
  129. }
  130. } // namespace ecs