]> git.lizzy.rs Git - lua-star.git/blob - example/main.lua
Add example animation.
[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 = 25
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 = mapsize }
23 local goal = { x = mapsize, y = 1 }
24
25 -- remember the tile the mouse is hovered over
26 local hoveredTile
27
28 -- fonts
29 local largeFont = love.graphics.newFont (30)
30 local smallFont = love.graphics.newFont (10)
31
32 -- save a screenshot
33 local saveScreenshot = false
34
35 function randomizeMap ()
36
37     -- build an open map
38     for x=1, mapsize do
39         map[x] = {}
40         for y=1, mapsize do
41             map[x][y] = true
42         end
43     end
44
45     -- add random walls
46     math.randomseed (os.clock ())
47     for i = 1, 45 do
48         -- start point
49         local x = math.random (1, mapsize-2)
50         local y = math.random (1, mapsize-2)
51         -- vertical or horizontal
52         if math.random() > .5 then
53             for n = 1, 5 do
54                 map[x][math.min (mapsize, y+n)] = false
55             end
56         else
57             for n = 1, 5 do
58                 map[math.min (mapsize, x+n)][y] = false
59             end
60         end
61     end
62
63     requestPath()
64     --saveScreenshot = true
65
66 end
67
68 function love.load ()
69
70     love.window.setMode (screensize, screensize)
71     randomizeMap()
72
73 end
74
75 function love.keypressed (key)
76
77     if key == "escape" then
78         love.event.quit()
79     elseif key == "space" then
80         randomizeMap()
81     end
82
83 end
84
85 function love.draw ()
86
87     -- draw walls
88     love.graphics.setColor(.6, .6, .6)
89     for x=1, mapsize do
90         for y=1, mapsize do
91             local fillstyle = "line"
92             if map[x][y] == false then fillstyle = "fill" end
93             love.graphics.rectangle(fillstyle, (x-1)*tilesize, (y-1)*tilesize, tilesize, tilesize)
94         end
95     end
96
97     -- draw the path
98     love.graphics.setFont (smallFont)
99     if path then
100         for i, p in ipairs(path) do
101             love.graphics.setColor(0, 1, 0)
102             love.graphics.rectangle("fill", (p.x-1)*tilesize, (p.y-1)*tilesize, tilesize, tilesize)
103             love.graphics.setColor(0, 0, 0)
104             love.graphics.print(i, (p.x-1) * tilesize, (p.y-1) * tilesize)
105         end
106     end
107
108     -- draw start and end
109     love.graphics.setColor(1, 0, 0)
110     love.graphics.setFont (largeFont)
111     love.graphics.print("*", (start.x-1) * tilesize, (start.y-1) * tilesize)
112     love.graphics.print("*", (goal.x-1) * tilesize, (goal.y-1) * tilesize)
113
114     if saveScreenshot then
115         saveScreenshot = false
116         local filename = string.format("screenshot-%d.png", os.time())
117         love.graphics.captureScreenshot(filename)
118         print (string.format("written %s", filename))
119     end
120
121 end
122
123 function love.mousemoved (x, y, dx, dy, istouch)
124
125     local dx = math.floor(x / tilesize) + 1
126     local dy = math.floor(y / tilesize) + 1
127
128     if hoveredTile then
129         if hoveredTile.dx == dx and hoveredTile.dy == dy then
130             return
131         end
132     end
133
134     hoveredTile = { dx = dx, dy = dy }
135     if love.mouse.isDown (1) then
136         map[dx][dy] = not map[dx][dy]
137         requestPath()
138     end
139
140 end
141
142 function love.mousepressed (x, y, button, istouch)
143
144     local dx = math.floor(x / tilesize) + 1
145     local dy = math.floor(y / tilesize) + 1
146     map[dx][dy] = not map[dx][dy]
147     requestPath()
148
149 end
150
151 function positionIsOpenFunc (x, y)
152
153     -- should return true if the position is open to walk
154     return map[x][y]
155
156 end
157
158 function requestPath ()
159
160     path = luastar:find(mapsize, mapsize, start, goal, positionIsOpenFunc)
161
162 end