Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 11 additions & 70 deletions source/main.lua
Original file line number Diff line number Diff line change
@@ -1,71 +1,12 @@
-- this is an example love2d project entry-point

local Maze = require "maze"
local list = require "listbox"

local maze
local algo = "aldous_broder"
local time = 0

function love.load()
love.window.setTitle("LuaMaze")
math.randomseed(os.time())
maze = Maze:new(17, 19, true)
local t = os.clock()
Maze.generators[algo](maze)
time = os.clock() - t

local tlist = {
selected=1,
x=550,
y=10,
font=love.graphics.newFont(15),
ismouse=true,
w=230,
h=200,

-- colors mapped to new 0-1 standard
fcolor={0, 0.7450980392156863, 0},
bordercolor={0.19607843137254902, 0.19607843137254902, 0.19607843137254902},
selectedcolor={0.19607843137254902, 0.19607843137254902, 0.19607843137254902},
fselectedcolor={0.7843137254901961, 0.7843137254901961, 0.7843137254901961},
bgcolor={0.0784313725490196, 0.0784313725490196, 0.0784313725490196},
}
list:newprop(tlist)
list:additem("Aldous-Broder","aldous_broder")
list:additem("Binary Tree","binary_tree")
list:additem("Eller's algorithm","eller")
list:additem("Growing Tree","growing_tree")
list:additem("Hunt and Kill","hunt_and_kill")
list:additem("Kruskal's algorithm","kruskal")
list:additem("Prim's algorithm","prim")
list:additem("Recursive Backtracker","recursive_backtracker")
list:additem("Recursive Division","recursive_division")
list:additem("Sidewinder","sidewinder")
list:additem("Wilson's algorithm","wilson")
local function has_matching_arg(query)
for i,val in ipairs(arg) do
if val == query then
return true
end
end
end
if has_matching_arg('tile') then
require "main_tile"
else
require "main_draw"
end

function love.draw()
list:draw()
Maze.love.rect(maze, 10, 10, 20, 10, { 0.58, 0.58, 0.78 }, { 0.07, 0.07, 0.39 })
love.graphics.printf(string.format("took %fms", time), 550, 220, 230 )
end

function love.keypressed(key)
list:key(key, true)
end

function love.wheelmoved(x, y)
list:mousew(x, y)
end

function love.update(dt)
list:update(dt)
local a = list:getdata(list:getselected())
if a ~= algo then
algo = a
local t = os.clock()
Maze.generators[algo](maze)
time = os.clock() - t
end
end
120 changes: 120 additions & 0 deletions source/main_draw.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
-- this is an example love2d project entry-point

local Maze = require "maze"
local list = require "listbox"

local maze
local algo = "aldous_broder"
local time = 0
local walls
local should_draw_walls

local function draw_walls(root_x, root_y, w)
local color = {
[true] = {0.1,0.1,0.1,1},
[false] = {0.5,0.9,0.9,1},
}
-- x,y start at 1 so they'll push us off by one width. Shift back to make the
-- math easier.
root_x = root_x - w
root_y = root_y - w
for y=1,walls.height do
for x=1,walls.width do
local is_wall = walls.walls[x][y]
love.graphics.setColor(unpack(color[is_wall]))
local px,py = root_x + x * w, root_y + y * w
love.graphics.rectangle('fill', px, py, w, w)
end
end
end

local function regenerate_maze(a)
algo = a
local t = os.clock()
Maze.generators[algo](maze)
time = os.clock() - t

walls = maze:AsTileLayout(true)
end

function love.load()
love.window.setTitle("LuaMaze")
math.randomseed(os.time())
maze = Maze:new(17, 19, true)
regenerate_maze(algo)

local tlist = {
selected=1,
x=550,
y=10,
font=love.graphics.newFont(15),
ismouse=true,
w=230,
h=200,

-- colors mapped to new 0-1 standard
fcolor={0, 0.7450980392156863, 0},
bordercolor={0.19607843137254902, 0.19607843137254902, 0.19607843137254902},
selectedcolor={0.19607843137254902, 0.19607843137254902, 0.19607843137254902},
fselectedcolor={0.7843137254901961, 0.7843137254901961, 0.7843137254901961},
bgcolor={0.0784313725490196, 0.0784313725490196, 0.0784313725490196},
}
list:newprop(tlist)
list:additem("Aldous-Broder","aldous_broder")
list:additem("Binary Tree","binary_tree")
list:additem("Eller's algorithm","eller")
list:additem("Growing Tree","growing_tree")
list:additem("Hunt and Kill","hunt_and_kill")
list:additem("Kruskal's algorithm","kruskal")
list:additem("Prim's algorithm","prim")
list:additem("Recursive Backtracker","recursive_backtracker")
list:additem("Recursive Division","recursive_division")
list:additem("Sidewinder","sidewinder")
list:additem("Wilson's algorithm","wilson")
end

function love.draw()
list:draw()
Maze.love.rect(maze, 10, 10, 20, 10, { 0.58, 0.58, 0.78 }, { 0.07, 0.07, 0.39 })
love.graphics.printf(string.format("took %fms", time), 550, 220, 230 )
local controls = [[Controls:
q - quit
space - toggle AsTileLayout display
]]
if should_draw_walls then
controls = controls .. [[k - RemoveDeadEnds
l - ExpandRooms
]]
draw_walls(5, 5, 15)
end
love.graphics.setColor(1,1,1,1)
love.graphics.printf(controls, 550, 260, 230)
end

function love.keypressed(key)
if key == 'q' or key == 'escape' then
love.event.quit()
elseif key == 'space' then
should_draw_walls = not should_draw_walls
elseif key == 'k' then
print("RemoveDeadEnds")
walls:RemoveDeadEnds()
elseif key == 'l' then
print("ExpandRooms")
walls:ExpandRooms()
else
list:key(key, true)
end
end

function love.wheelmoved(x, y)
list:mousew(x, y)
end

function love.update(dt)
list:update(dt)
local a = list:getdata(list:getselected())
if a ~= algo then
regenerate_maze(a)
end
end
29 changes: 17 additions & 12 deletions source/main_tile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ local image
local algo = "aldous_broder"
local time = 0

function love.load()
love.window.setTitle("LuaMaze")
math.randomseed(os.time())
maze = Maze:new(17, 18, true)
local function regenerate_maze(a)
algo = a
local t = os.clock()
Maze.generators[algo](maze)
time = os.clock() - t
batches = Maze.love.tile:setup(maze, image)
end

function love.load()
love.window.setTitle("LuaMaze")
math.randomseed(os.time())
maze = Maze:new(17, 18, true)
image = love.graphics.newImage("assets/maze.png")
batches = Maze.love.tile:setup(maze, image)
regenerate_maze(algo)


local tlist = {
selected=1,
Expand Down Expand Up @@ -58,7 +63,11 @@ function love.draw()
end

function love.keypressed(key)
list:key(key, true)
if key == 'q' or key == 'escape' then
love.event.quit()
else
list:key(key, true)
end
end

function love.wheelmoved(x, y)
Expand All @@ -69,10 +78,6 @@ function love.update(dt)
list:update(dt)
local a = list:getdata(list:getselected())
if a ~= algo then
algo = a
local t = os.clock()
Maze.generators[algo](maze)
time = os.clock() - t
batches = Maze.love.tile:setup(maze, image)
regenerate_maze(a)
end
end
end
10 changes: 5 additions & 5 deletions source/maze/love/rect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

local draw_maze = function(maze, x, y, cell_dim, wall_dim, cell_col, wall_col)
love.graphics.setColor(wall_col)
local maze_width = (cell_dim + wall_dim) * #maze[1] + wall_dim
local maze_height = (cell_dim + wall_dim) * #maze + wall_dim
local maze_width = (cell_dim + wall_dim) * maze:width() + wall_dim
local maze_height = (cell_dim + wall_dim) * maze:height() + wall_dim
love.graphics.rectangle("fill", x, y, maze_width, maze_height)

love.graphics.setColor(cell_col)
for yi = 1, #maze do
for xi = 1, #maze[1] do
for yi = 1, maze:height() do
for xi = 1, maze:width() do
local pos_x = x + (cell_dim + wall_dim) * (xi - 1) + wall_dim
local pos_y = y + (cell_dim + wall_dim) * (yi - 1) + wall_dim
love.graphics.rectangle("fill", pos_x, pos_y, cell_dim, cell_dim)
Expand All @@ -30,4 +30,4 @@ local draw_maze = function(maze, x, y, cell_dim, wall_dim, cell_col, wall_col)
end
end

return draw_maze
return draw_maze
8 changes: 4 additions & 4 deletions source/maze/love/tile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function LoveTileMaze:setup(maze, image)
local batches = {}

for k,v in pairs(tiles) do
batches[k] = love.graphics.newSpriteBatch(image, #maze*#maze[1] )
batches[k] = love.graphics.newSpriteBatch(image, maze:height()*maze:width() )
end

for yi = 1, #maze do
for xi = 1, #maze[1] do
for yi = 1, maze:height() do
for xi = 1, maze:width() do
batches.floor:add( tiles.floor, (xi-1) * tile_size, (yi-1) * tile_size)
if maze[yi][xi].north:IsClosed() then
batches.N:add( tiles.N, (xi-1) * tile_size, (yi-1) * tile_size)
Expand Down Expand Up @@ -50,4 +50,4 @@ function LoveTileMaze:draw(batches, x, y)
love.graphics.draw(batches.E, x, y)
end

return LoveTileMaze
return LoveTileMaze
Loading