add 'core:pathfinding' component
This commit is contained in:
parent
e2d0fa830d
commit
a110022ec2
39
res/scripts/components/pathfinding.lua
Normal file
39
res/scripts/components/pathfinding.lua
Normal file
@ -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
|
||||||
@ -380,9 +380,11 @@ void WorldRenderer::renderFrame(
|
|||||||
|
|
||||||
auto& linesShader = assets.require<Shader>("lines");
|
auto& linesShader = assets.require<Shader>("lines");
|
||||||
linesShader.use();
|
linesShader.use();
|
||||||
debugLines->render(
|
if (debug && hudVisible) {
|
||||||
ctx, camera, *lines, *lineBatch, linesShader, showChunkBorders
|
debugLines->render(
|
||||||
);
|
ctx, camera, *lines, *lineBatch, linesShader, showChunkBorders
|
||||||
|
);
|
||||||
|
}
|
||||||
linesShader.uniformMatrix("u_projview", projView);
|
linesShader.uniformMatrix("u_projview", projView);
|
||||||
lines->draw(*lineBatch);
|
lines->draw(*lineBatch);
|
||||||
lineBatch->flush();
|
lineBatch->flush();
|
||||||
|
|||||||
@ -181,6 +181,7 @@ Route Pathfinding::perform(Agent& agent, int maxVisited) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
state.finished = true;
|
||||||
agent.state = std::move(state);
|
agent.state = std::move(state);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user