diff --git a/include/NazaraEditor/Core/Application/Level.hpp b/include/NazaraEditor/Core/Application/Level.hpp index dacf70c..c95f181 100644 --- a/include/NazaraEditor/Core/Application/Level.hpp +++ b/include/NazaraEditor/Core/Application/Level.hpp @@ -3,11 +3,20 @@ #include #include +#include namespace Nz { class EditorBaseApplication; + struct RaycastHit + { + entt::handle entity; + + Nz::Vector3f position; + float distance; + }; + class NAZARAEDITOR_CORE_API Level final { public: @@ -21,6 +30,9 @@ namespace Nz entt::handle CreateEntity(); bool CreateNewLevel(); + // This is slow af + std::vector Raycast(const Nz::Rayf& ray); + protected: EditorBaseApplication* m_application; diff --git a/src/NazaraEditor/Core/Application/Level.cpp b/src/NazaraEditor/Core/Application/Level.cpp index f999f6b..17001f8 100644 --- a/src/NazaraEditor/Core/Application/Level.cpp +++ b/src/NazaraEditor/Core/Application/Level.cpp @@ -20,4 +20,31 @@ namespace Nz m_world = &ecs.AddWorld(); return true; } + + std::vector Level::Raycast(const Nz::Rayf& ray) + { + std::vector entities; + + auto& registry = m_world->GetRegistry(); + for (auto&& entity : registry.storage().each()) + { + Nz::GraphicsComponent* component = registry.try_get(std::get(entity)); + if (component != nullptr) + { + float distance = 0; + if (ray.Intersect(component->GetAABB(), &distance)) + { + entities.push_back({ + .entity = entt::handle(registry, std::get(entity)), + .position = ray.origin + ray.direction * distance, + .distance = distance + }); + } + } + } + + // sort the result by distance first + std::sort(entities.begin(), entities.end(), [](auto&& A, auto&& B) { return A.distance < B.distance; }); + return entities; + } } \ No newline at end of file