Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.load()
- player = -- Sets up a table to store all the player variables such as location, speed and angle
- {
- x = 50,
- y = 50,
- speed = 100,
- angle = 0
- }
- --Below here is the map generation algorithm
- -- Proceed with caution (Highly spaghettified)
- -- Create Data Structure
- map = {
- width = 800,
- height = 600,
- }
- for i = 1, #map do
- map[i][j] = 0
- end
- -- rooms and room sizes
- minRooms = (map.width * map.height) / 300
- maxRooms = (map.width * map.height) / 150
- roomCount = math.random(minRooms, maxRooms)
- widthRoot = math.sqrt(map.width * 2)
- heightRoot = math.sqrt(map.height * 2)
- minWidth = (map.width * 0.5) / widthRoot
- maxWidth = (map.width * 2.0) / widthRoot
- minHeight = (map.height * 0.5) / heightRoot
- maxHeight = (map.height * 2.0) / heightRoot
- -- Create the rooms!
- roomList= {}
- room = {}
- for i = 0, roomCount do
- ok = false
- while ok ~= true do
- room.x = math.random(1 , map.width)
- room.y = math.random(1 , map.height)
- room.w = math.random(minWidth, maxWidth)
- room.h = math.random(minHeight, maxHeight)
- if room.x > map.width then
- room.x = map.width - room.x
- roomList.add(room)
- ok = true
- end
- end
- end
- --Connection Area
- connectionCount = roomCount
- connectedCells = {}
- for i = 0, connectionCount do
- roomA = roomList[i]
- roomB = roomList[j]
- -- Increasing this number will make hallways straighter
- --Lowering it will make hallways skewer
- sideStepChance = 10
- pointA = {
- x =(Math.random()*roomA.w) + roomA.x,
- y = (Math.random()*roomA.h) + roomA.y
- }
- pointB = {
- x = (Math.random()*roomB.w) + roomB.x,
- y = (Math.random()*roomB.h) + roomB.y
- }
- -- Drunken/Lazy walk algorithm
- while pointB.x ~= pointA.x or pointB.y ~= pointA.y do
- num = math.random(1, 100)
- if num < sidestepChance then
- if pointB.x ~= pointA.x then
- if pointB.x > pointA.x then
- pointB.x = pointb.X - 1
- else
- pointB.x = pointb.X + 1
- end
- end
- elseif pointB.y ~= pointA.y then
- if pointB.y > pointA.y then
- pointB.y = pointB.y - 1
- else
- pointB.y = pointB.y + 1;
- end
- end
- end
- -- Create map data
- for i = 0, roomList.length do
- for y = roomList[i].y, roomList[i].y + roomList[i].h do
- for x = roomList[i].x, x < roomList[i].x + roomList[i].w do
- -- console.log(y + " : " + x);
- map[y][x] = 1;
- end
- end
- end
- for i = 0, connectedCells.length do
- map[connectedCells[i].y][connectedCells[i].x] = 2;
- end
- for y = 0, y < height do
- for x= 0, x < width do
- if map[y][x] == 0 then
- wall = false;
- for yy = y-2, yy < y+2 do
- for xx = x-2, xx < x+2 do
- if xx > 0 and yy > 0 and xx < width and yy < height then
- if map[yy][xx] == 1 or map[yy][xx] == 2 then
- map[y][x] = 3;
- wall = true;
- end
- end
- end
- if(wall) then
- break
- end
- end
- end
- end
- end
- end
- end
- function love.update(dt)
- if love.keyboard.isDown("d") then -- Right movement
- player.x = player.x + (player.speed * dt)
- elseif love.keyboard.isDown("a") then -- Left Movement
- player.x = player.x - (player.speed * dt)
- end
- if love.keyboard.isDown("s") then -- Down movement
- player.y = player.y + (player.speed * dt)
- elseif love.keyboard.isDown("w") then -- Up movement
- player.y = player.y - (player.speed * dt)
- end
- end
- function love.draw()
- love.graphics.setBackgroundColor(0,0,0)
- love.graphics.rectangle("fill", player.x, player.y, 10, 10)
- for y = 0, #map[x] do
- for x = 0, #map[y] do
- if map[y][x] == 0 then
- elseif map[y][x] == 1 then
- love.graphics.rectangle("fill", map[x], map[y])
- elseif map[y][x] == 2 then
- love.graphics.rectangle("fill", map[x], map[y])
- elseif map[y][x] == 3 then
- love.graphics.rectangle("fill", map[x], map[y])
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement