A* algorithm
This commit is contained in:
parent
eebd5e4132
commit
827a5dc23c
@ -5,5 +5,6 @@
|
|||||||
"runtime.special": {
|
"runtime.special": {
|
||||||
"love.filesystem.load": "loadfile"
|
"love.filesystem.load": "loadfile"
|
||||||
},
|
},
|
||||||
"workspace.ignoreDir": ["dev_utils"]
|
"workspace.ignoreDir": ["dev_utils"],
|
||||||
|
"diagnostics.ignoredFiles": "Disable"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ for i = 0, #arg - 2, 5 do
|
|||||||
out[idx] = {}
|
out[idx] = {}
|
||||||
for x = tonumber(arg[i + 3]), tonumber(arg[i + 5]), tonumber(out.tileSize) do
|
for x = tonumber(arg[i + 3]), tonumber(arg[i + 5]), tonumber(out.tileSize) do
|
||||||
for y = arg[i + 4], arg[i + 6], out.tileSize do
|
for y = arg[i + 4], arg[i + 6], out.tileSize do
|
||||||
out[idx][idx .. cnt] = { x = x, y = y, w = out.tileSize, h = out.tileSize }
|
out[idx][idx .. cnt] = { quad = { x = x, y = y, w = out.tileSize, h = out.tileSize } }
|
||||||
cnt = cnt + 1
|
cnt = cnt + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -17,6 +17,11 @@ local function new(id, position, size)
|
|||||||
}, logic)
|
}, logic)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- @param path Vec3
|
||||||
|
function logic:followPath(path)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
--- @param target Vec3
|
--- @param target Vec3
|
||||||
function logic:runTo(target)
|
function logic:runTo(target)
|
||||||
self.state = "run"
|
self.state = "run"
|
||||||
|
|||||||
31
lib/pathfinder.lua
Normal file
31
lib/pathfinder.lua
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
--- @param cur Vec3
|
||||||
|
--- @param to Vec3
|
||||||
|
--- @param acc Vec3[]
|
||||||
|
local function greedy_trace_step(cur, to, acc)
|
||||||
|
local lengthTable = {}
|
||||||
|
for x = -1, 1 do
|
||||||
|
for y = -1, 1 do
|
||||||
|
local point = Vec3 { cur.x + x, cur.y + y }
|
||||||
|
table.insert(lengthTable, { point, (point - to):length() })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local min = lengthTable[1]
|
||||||
|
for i = 2, #lengthTable do
|
||||||
|
if lengthTable[i][2] < min[2] then min = lengthTable[i] end
|
||||||
|
end
|
||||||
|
local next = min[1]
|
||||||
|
|
||||||
|
table.insert(acc, cur)
|
||||||
|
if cur == to then
|
||||||
|
return acc
|
||||||
|
end
|
||||||
|
return greedy_trace_step(next, to, acc)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @param from Vec3
|
||||||
|
--- @param to Vec3
|
||||||
|
local function trace(from, to)
|
||||||
|
return greedy_trace_step(from, to, {})
|
||||||
|
end
|
||||||
|
|
||||||
|
return trace
|
||||||
17
main.lua
17
main.lua
@ -67,6 +67,23 @@ function love.draw()
|
|||||||
nil, 1 / 32, 1 / 32)
|
nil, 1 / 32, 1 / 32)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Это отрисовка пути персонажа к мышке
|
||||||
|
if Tree.level.selector.id then
|
||||||
|
local charPos = Tree.level.characters[Tree.level.selector.id].logic.mapLogic.position:floor()
|
||||||
|
--- @type Vec3
|
||||||
|
local mpos = Tree.level.camera:toWorldPosition(Vec3 { love.mouse.getX(), love.mouse.getY() }):floor()
|
||||||
|
local path = (require "lib.pathfinder")(charPos, mpos)
|
||||||
|
|
||||||
|
love.graphics.setColor(1, 0, 0)
|
||||||
|
for _, p in ipairs(path) do
|
||||||
|
love.graphics.rectangle("line", p.x, p.y, 0.1, 0.1)
|
||||||
|
end
|
||||||
|
love.graphics.setColor(1, 1, 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Tree.level:draw()
|
Tree.level:draw()
|
||||||
Tree.level.camera:detach()
|
Tree.level.camera:detach()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user