-
Notifications
You must be signed in to change notification settings - Fork 0
/
room_gen.lua
72 lines (59 loc) · 2.29 KB
/
room_gen.lua
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
local Geometry = require "geometry"
local Vector = require "vector"
local TileGrid = Geometry.TileGrid
local function generate(room)
local props = {}
local FREQUENCY = 5
local propgeometry = TileGrid:new()
local size = room.geometry:size()
for propType = 1, #propTypes do
local prop = propTypes[propType]
if not prop.rooms or prop.rooms[room.type] then
count = 0
for i = 1, prop.frequency or FREQUENCY do
if prop.max and count >= prop.max then break end
local roomX = love.math.random(1, size.x)
local roomY = love.math.random(1, size.y)
local _, _, pixelWidth, pixelHeight = prop.quad:getViewport()
if prop.rotate then
rot = love.math.random(4)
else
rot = 1
end
local angle = ({0,math.rad(90),math.rad(180),math.rad(270)})[rot]
local offset = ({Vector(0,0), Vector(pixelHeight,0), Vector(pixelWidth,pixelHeight), Vector(0,pixelWidth)})[rot]
if rot % 2 == 0 then
pixelWidth,pixelHeight = pixelHeight,pixelWidth
end
local gridWidth = math.ceil(pixelWidth/TILE_WIDTH)
local gridHeight = math.ceil(pixelHeight/TILE_WIDTH)
local br = false
for checkX = roomX, roomX+gridWidth-1 do
for checkY = roomY, roomY+gridHeight-1 do
if not room.geometry:get(checkX, checkY) or propgeometry:get(checkX,checkY) then
br = true
end
if br then break end
end
if br then break end
end
-- Placement is unobstructed!
if not br then
for setX = roomX, roomX+gridWidth-1 do
for setY = roomY, roomY+gridHeight-1 do
propgeometry:set(setX, setY, "p")
end
end
local jitter = Vector(love.math.random(1,(gridWidth*TILE_WIDTH)-pixelWidth), love.math.random(1,(gridHeight*TILE_WIDTH)-pixelHeight))
jitter = jitter - Vector(1,1)
offset = offset + jitter
pos = Vector(roomX,roomY)
props[#props+1] = {quad=prop.quad, position = pos, angle=angle, offset=offset, jitter=jitter, type=propType}
count = count + 1
end
end
end
end
return props
end
return {generate = generate}