3 Lua star example - Run with love (https://love2d.org/)
5 Copyright 2017 wesley werner <wesley.werner@gmail.com>
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/.
22 local luastar = require("lua-star")
24 -- a 2D map where true is open and false is blocked
27 local screensize = 500
28 local tilesize = screensize / mapsize
32 local start = { x = 1, y = 10 }
33 local goal = { x = 10, y = 1 }
37 love.window.setMode( screensize, screensize )
51 function love.keypressed(key)
53 if key == "escape" then
62 love.graphics.setColor(255, 255, 255)
65 local fillstyle = "line"
66 if map[x][y] == false then fillstyle = "fill" end
67 love.graphics.rectangle(fillstyle, (x-1)*tilesize, (y-1)*tilesize, tilesize, tilesize)
72 love.graphics.print("START", (start.x-1) * tilesize, (start.y-1) * tilesize)
73 love.graphics.print("GOAL", (goal.x-1) * tilesize, (goal.y-1) * tilesize)
77 for i, p in ipairs(path) do
78 love.graphics.setColor(0, 128, 0)
79 love.graphics.rectangle("fill", (p.x-1)*tilesize, (p.y-1)*tilesize, tilesize, tilesize)
80 love.graphics.setColor(255, 255, 255)
81 love.graphics.print(i, (p.x-1) * tilesize, (p.y-1) * tilesize)
87 function love.mousepressed( x, y, button, istouch )
89 local dx = math.floor(x / tilesize) + 1
90 local dy = math.floor(y / tilesize) + 1
91 map[dx][dy] = not map[dx][dy]
96 function positionIsOpenFunc(x, y)
98 -- should return true if the position is open to walk
103 function requestPath()
105 path = luastar:find(mapsize, mapsize, start, goal, positionIsOpenFunc)