From 827a5dc23c140be26fd173e448296efb92aa705b Mon Sep 17 00:00:00 2001 From: Neckrat Date: Wed, 13 Aug 2025 01:47:19 +0300 Subject: [PATCH] A* algorithm --- .luarc.json | 3 ++- dev_utils/tile_generator.lua | 2 +- lib/character/logic.lua | 5 +++++ lib/pathfinder.lua | 31 +++++++++++++++++++++++++++++++ main.lua | 17 +++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/pathfinder.lua diff --git a/.luarc.json b/.luarc.json index a4cb138..0bef050 100644 --- a/.luarc.json +++ b/.luarc.json @@ -5,5 +5,6 @@ "runtime.special": { "love.filesystem.load": "loadfile" }, - "workspace.ignoreDir": ["dev_utils"] + "workspace.ignoreDir": ["dev_utils"], + "diagnostics.ignoredFiles": "Disable" } diff --git a/dev_utils/tile_generator.lua b/dev_utils/tile_generator.lua index 42e2daa..ba34669 100644 --- a/dev_utils/tile_generator.lua +++ b/dev_utils/tile_generator.lua @@ -13,7 +13,7 @@ for i = 0, #arg - 2, 5 do out[idx] = {} 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 - 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 end end diff --git a/lib/character/logic.lua b/lib/character/logic.lua index 33700d6..537176e 100644 --- a/lib/character/logic.lua +++ b/lib/character/logic.lua @@ -17,6 +17,11 @@ local function new(id, position, size) }, logic) end +--- @param path Vec3 +function logic:followPath(path) + +end + --- @param target Vec3 function logic:runTo(target) self.state = "run" diff --git a/lib/pathfinder.lua b/lib/pathfinder.lua new file mode 100644 index 0000000..20a7aca --- /dev/null +++ b/lib/pathfinder.lua @@ -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 diff --git a/main.lua b/main.lua index d60446b..80aaddf 100644 --- a/main.lua +++ b/main.lua @@ -67,6 +67,23 @@ function love.draw() nil, 1 / 32, 1 / 32) 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.camera:detach()