]> git.lizzy.rs Git - lua-star.git/blob - example/main.lua
Change license to MIT
[lua-star.git] / example / main.lua
1 --[[
2     Lua star example - Run with love (https://love2d.org/)
3     Copyright 2018 Wesley Werner <wesley.werner@gmail.com>
4
5     Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
7     The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
9     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 ]]--
11
12 local luastar = require("lua-star")
13
14 -- a 2D map where true is open and false is blocked
15 local map = { }
16 local mapsize = 10
17 local screensize = 500
18 local tilesize = screensize / mapsize
19
20 -- path start and end
21 local path = nil
22 local start = { x = 1, y = 10 }
23 local goal = { x = 10, y = 1 }
24
25 function love.load()
26
27     love.window.setMode( screensize, screensize )
28
29     -- build an open map
30     for x=1, mapsize do
31         map[x] = {}
32         for y=1, mapsize do
33             map[x][y] = true
34         end
35     end
36
37     requestPath()
38
39 end
40
41 function love.keypressed(key)
42
43     if key == "escape" then
44         love.event.quit()
45     end
46
47 end
48
49 function love.draw()
50
51     -- draw walls
52     love.graphics.setColor(255, 255, 255)
53     for x=1, mapsize do
54         for y=1, mapsize do
55             local fillstyle = "line"
56             if map[x][y] == false then fillstyle = "fill" end
57             love.graphics.rectangle(fillstyle, (x-1)*tilesize, (y-1)*tilesize, tilesize, tilesize)
58         end
59     end
60
61     -- draw start and end
62     love.graphics.print("START", (start.x-1) * tilesize, (start.y-1) * tilesize)
63     love.graphics.print("GOAL", (goal.x-1) * tilesize, (goal.y-1) * tilesize)
64
65     -- draw the path
66     if path then
67         for i, p in ipairs(path) do
68             love.graphics.setColor(0, 128, 0)
69             love.graphics.rectangle("fill", (p.x-1)*tilesize, (p.y-1)*tilesize, tilesize, tilesize)
70             love.graphics.setColor(255, 255, 255)
71             love.graphics.print(i, (p.x-1) * tilesize, (p.y-1) * tilesize)
72         end
73     end
74
75 end
76
77 function love.mousepressed( x, y, button, istouch )
78
79     local dx = math.floor(x / tilesize) + 1
80     local dy = math.floor(y / tilesize) + 1
81     map[dx][dy] = not map[dx][dy]
82     requestPath()
83
84 end
85
86 function positionIsOpenFunc(x, y)
87
88     -- should return true if the position is open to walk
89     return map[x][y]
90
91 end
92
93 function requestPath()
94
95     path = luastar:find(mapsize, mapsize, start, goal, positionIsOpenFunc)
96
97 end