-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
193 lines (165 loc) · 4.98 KB
/
main.lua
File metadata and controls
193 lines (165 loc) · 4.98 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
-- main.lua
debug = true
local utils = require 'utils'
G = love.graphics
setRGB = utils.setRGB
function love.load()
font = G.newFont('Hack.ttf', 16)
G.setFont(font)
margin = { x = 0, y = 0 }
getHeight, getWidth = G.getHeight(), G.getWidth()
fontHeight = font:getHeight()
fontWidth = font:getWidth('a')
maxLines = math.floor((getHeight - margin.y * 2) / fontHeight)
maxChars = math.floor((getWidth - margin.x * 2) / fontWidth)
-- { '', color='', bgcolor = '', passable= },
tiles = {
{ ' ', color='black', bgcolor='black', passable=false, id='earth' },
{ '#', color='gray', bgcolor='black', passable=false, id='wall' },
{ '.', color='yellow', bgcolor='black', passable=true, id='floor' }
}
map = {}
mapX = 40
mapY = 16
maxRooms = 10
-- first fill floor in
for y = 1, mapY do
map[y] = {}
for x = 1, mapX do
table.insert(map[y], tiles[1])
end
end
-- for first room, until last room
-- repeat while
for i = 1, maxRooms do
local roomDone = false
while not roomDone do
-- pick a random position and size
local randomPos = {}
randomPos.x = math.random(2, mapX)
randomPos.y = math.random(2, mapY)
local roomSize = {}
roomSize.x = math.random(2,6)
roomSize.y = math.random(2,6)
local badRoom = false
-- now loop through and see if any part of room is floor or would extend beyond map size
for y = randomPos.y, randomPos.y + roomSize.y do
for x = randomPos.x, randomPos.x + roomSize.x do
if y > mapY or x > mapX then
badRoom = true
elseif map[y][x].passable then
badRoom = true
end
end
end
-- if badRoom is still false, then make the room
if not badRoom then
for y = randomPos.y, randomPos.y + roomSize.y do
for x = randomPos.x, randomPos.x + roomSize.x do
-- remove earth and insert floor
table.remove(map[y], x)
table.insert(map[y], x, tiles[3])
-- now for each adjacent tile that is not floor, remove and insert wall
for wy = -1, 1 do
for wx = -1, 1 do
if map[y + wy][x + wx].id ~= 'floor' then
table.remove(map[y + wy], x + wx)
table.insert(map[y + wy], x + wx, tiles[2])
end
end
end
end
end
roomDone = true
end
end
end
-- fill map with unpassable tile
-- pick random position
-- see if a room can be made there
-- make the room
-- pick random position until adjacent to a floor tile
-- make corridor
player = { '@', color='fuchsia', bgcolor='black' }
local pos = findRandomTile()
player.x = pos.x
player.y = pos.y
if debug then
print(fontHeight .. ' is the pixel height of the font.')
print(fontWidth .. ' is the pixel width of the font.')
print(maxLines .. ' is the maximum number of lines.')
print(maxChars .. ' is the maximum number of characters.')
end
end
function love.keypressed(key)
if key == 'y' then
-- move northwest
movePlayer(-1, -1)
elseif key == 'u' then
-- move northeast
movePlayer(1, -1)
elseif key == 'h' then
-- move west
movePlayer(-1, 0)
elseif key == 'j' then
-- move south
movePlayer(0, 1)
elseif key == 'k' then
-- move north
movePlayer(0, -1)
elseif key == 'l' then
-- move east
movePlayer(1, 0)
elseif key == 'b' then
-- move southwest
movePlayer(-1, 1)
elseif key == 'n' then
-- move southeast
movePlayer(1, 1)
end
end
function love.update(dt) end
function love.draw()
-- y axis
for y,_ in ipairs(map) do
-- x axis
for x,v in ipairs(map[y]) do
setRGB(v.bgcolor)
G.rectangle('fill', margin.x + fontWidth * x, margin.y + fontHeight * y, fontWidth, fontHeight)
setRGB(v.color)
G.print(v[1], margin.x + fontWidth * x, margin.y + fontHeight * y)
end
end
setRGB(player.bgcolor)
G.rectangle('fill', margin.x + fontWidth * player.x, margin.y + fontHeight * player.y, fontWidth, fontHeight)
setRGB(player.color)
G.print(player[1], margin.x + fontWidth * player.x, margin.y + fontHeight * player.y)
end
function movePlayer(x, y)
local newY, newX = player.y + y, player.x + x
-- first make sure player would not fall off world
if newY >= 1 and newY <= mapY and newX >= 1 and newX <= mapX then
-- now see if tile is passable
if map[player.y + y][player.x + x].passable then
player.y = player.y + y
player.x = player.x + x
end
end
end
-- generate a random tile and see if its id corresponds to arg id
-- if no arg is passed then simply search for a passable tile
function findRandomTile(id)
local t = { x,y }
if id == nil then
repeat
t.x = math.random(mapX)
t.y = math.random(mapY)
until map[t.y][t.x].passable
else
repeat
t.x = math.random(mapX)
t.y = math.random(mapY)
until map[t.y][t.x].id == id
end
return t
end