mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
add 'core:pathfinding' component
This commit is contained in:
parent
e2d0fa830d
commit
a110022ec2
3 changed files with 45 additions and 3 deletions
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue