-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.lua
More file actions
93 lines (75 loc) · 2.66 KB
/
grid.lua
File metadata and controls
93 lines (75 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-- Module for managing the grid, drawing it, managing zoom levels, etc.
function round(num, numDecimalPlaces)
local mult = 10 ^ (numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
new = function(w, h, create_func) -- Creates a w*h grid and fills in with dv
-- create_func must be a function with 0 args which creates a default "case"
-- /!\ Make sure you call g.grid_image = g:generate_grid() atleast one time
local g = {}
-- Scale multipliers of the grid used for drawing
g.sx = 1
g.sy = 1
g.font = love.graphics.newFont(12) -- Font for debugging
for i=1, h do
local y = {}
for j=1, w do
local c = create_func()
c.x = j
c.y = i
table.insert(y, c)
end
table.insert(g, y)
end
function g:draw_grid(openset, closeset, debug)
local ww, wh = love.window.getMode()
local cw = ww / #g[1] * g.sx-- Case width
local ch = wh / #g * g.sy-- Case height
--print("Case dimensions are:"..tostring(cw).."; "..tostring(ch))
love.graphics.draw(g.grid_image)
-- openset
for i, c in ipairs(openset) do
c:draw(cw, ch, {255, 255, 0}, debug)
end
-- closeset
for i, c in ipairs(closeset) do
c:draw(cw, ch, {100, 0, 0}, debug)
end
end
function g:print_grid()
for i, y in ipairs(g) do
for j, x in ipairs(y) do
io.write("["..tostring(x.x)..","..tostring(x.y).."] ")
end
io.write("\n")
end
end
function g:generate_grid()
-- Used to regenerate the grid image (i.e:when scale changes for), returns an Image
local ww, wh = love.window.getMode()
local cw = ww / #g[1] * g.sx-- Case width
local ch = wh / #g * g.sy-- Case height
--print("Case dimensions are:"..tostring(cw).."; "..tostring(ch))
local tmp_canvas = love.graphics.newCanvas(ww, wh)
love.graphics.setCanvas(tmp_canvas)
love.graphics.clear(0, 0, 0, 0)
for i, y in ipairs(g) do
for j, x in ipairs(y) do
local tx = (j-1) * cw
local ty = (i-1) * ch
local c = {0, 0, 0, 1}
if x.walkable then
c = {0, 0, 0, 0}
end
love.graphics.setColor(c)
love.graphics.rectangle("fill", tx, ty, cw, ch)
end
end
love.graphics.setCanvas()
return love.graphics.newImage(tmp_canvas:newImageData())
end
return g
end
return {
new = new
}