]> git.lizzy.rs Git - lua-star.git/blob - example/main.lua
460d99c80b91b34d270f92a3359adf50d75caa6a
[lua-star.git] / example / main.lua
1 --[[
2
3    Lua star example - Run with love (https://love2d.org/)
4
5    Copyright 2017 wesley werner <wesley.werner@gmail.com>
6
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
10    any later version.
11
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.
16
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/.
19
20 ]]--
21
22 local luastar = require("lua-star")
23
24 -- a 2D map where true is open and false is blocked
25 local map = { }
26 local mapsize = 10
27 local screensize = 500
28 local tilesize = screensize / mapsize
29
30 -- path start and end
31 local path = nil
32 local start = { x = 1, y = 10 }
33 local goal = { x = 10, y = 1 }
34
35 function love.load()
36
37     love.window.setMode( screensize, screensize )
38
39     -- build an open map
40     for x=1, mapsize do
41         map[x] = {}
42         for y=1, mapsize do
43             map[x][y] = true
44         end
45     end
46
47     requestPath()
48
49 end
50
51 function love.keypressed(key)
52
53     if key == "escape" then
54         love.event.quit()
55     end
56
57 end
58
59 function love.draw()
60
61     -- draw walls
62     love.graphics.setColor(255, 255, 255)
63     for x=1, mapsize do
64         for y=1, mapsize do
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)
68         end
69     end
70
71     -- draw start and end
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)
74
75     -- draw the path
76     if path then
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)
82         end
83     end
84
85 end
86
87 function love.mousepressed( x, y, button, istouch )
88
89     local dx = math.floor(x / tilesize) + 1
90     local dy = math.floor(y / tilesize) + 1
91     map[dx][dy] = not map[dx][dy]
92     requestPath()
93
94 end
95
96 function positionIsOpenFunc(x, y)
97
98     -- should return true if the position is open to walk
99     return map[x][y]
100
101 end
102
103 function requestPath()
104
105     path = luastar:find(mapsize, mapsize, start, goal, positionIsOpenFunc)
106
107 end