From a110022ec2d678d77922748b7cc7c7df09b57243 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 3 Aug 2025 19:06:22 +0300 Subject: [PATCH] add 'core:pathfinding' component --- res/scripts/components/pathfinding.lua | 39 ++++++++++++++++++++++++++ src/graphics/render/WorldRenderer.cpp | 8 ++++-- src/voxels/Pathfinding.cpp | 1 + 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 res/scripts/components/pathfinding.lua diff --git a/res/scripts/components/pathfinding.lua b/res/scripts/components/pathfinding.lua new file mode 100644 index 00000000..31d6d780 --- /dev/null +++ b/res/scripts/components/pathfinding.lua @@ -0,0 +1,39 @@ +local target +local route +local started + +local tsf = entity.transform + +agent = pathfinding.create_agent() +pathfinding.set_max_visited(agent, 100000) + +function set_target(new_target) + target = new_target +end + +function get_target() + return target +end + +function get_route() + return route +end + +function on_update() + if not started then + if target then + pathfinding.make_route_async(agent, tsf:get_pos(), target) + started = true + end + else + local new_route = pathfinding.pull_route(agent) + if new_route then + route = new_route + started = false + end + end +end + +function on_despawn() + pathfinding.remove_agent(agent) +end diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 935b887c..eef4c41e 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -380,9 +380,11 @@ void WorldRenderer::renderFrame( auto& linesShader = assets.require("lines"); linesShader.use(); - debugLines->render( - ctx, camera, *lines, *lineBatch, linesShader, showChunkBorders - ); + if (debug && hudVisible) { + debugLines->render( + ctx, camera, *lines, *lineBatch, linesShader, showChunkBorders + ); + } linesShader.uniformMatrix("u_projview", projView); lines->draw(*lineBatch); lineBatch->flush(); diff --git a/src/voxels/Pathfinding.cpp b/src/voxels/Pathfinding.cpp index 51079f61..1a34a8b2 100644 --- a/src/voxels/Pathfinding.cpp +++ b/src/voxels/Pathfinding.cpp @@ -181,6 +181,7 @@ Route Pathfinding::perform(Agent& agent, int maxVisited) { } } } + state.finished = true; agent.state = std::move(state); return {}; }