-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.lua
226 lines (173 loc) · 4.73 KB
/
map.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
local class = require "middleclass"
local Tile = require "tile"
local directions = require "directions"
Map = class("Map")
Map.worldOffsetX = 0
Map.worldOffsetY = 0
Map.currentMap = nil
function Map:initialize(width, height, tileSize)
-- in units
self.width = width
self.height = height
-- pixel
self.tileSize = tileSize
self.tiles = self:constructBlankTileGrid(width, height, tileSize)
self:setCurrentMap()
end
function Map:setCurrentMap()
Map.currentMap = self
end
function Map:updateMapOffset(xOffset, yOffset)
Map.worldOffsetX = xOffset
Map.worldOffsetY = yOffset
end
function Map:constructBlankTileGrid(width, height, tileSize)
local tiles = {}
for x = 0, width do
tiles[x] = {}
for y = 0, height do
tiles[x][y] = Tile:new(x, y, tileSize)
end
end
return tiles
end
function Map:generate(
minNumRooms,
maxNumRooms,
minRoomWidth,
maxRoomWidth,
minRoomHeight,
maxRoomHeight,
minCoridorLength,
maxCoridorLength,
maxCoridorWidth
)
local start = os.clock()
print("Begining map generation...")
local numRooms = math.random(minNumRooms, maxNumRooms)
repeat
xPos = self.width/2 + math.random(-5,5)
until(xPos > 0 and xPos < self.width)
repeat
yPos = self.height/2 + math.random(-5,5)
until(yPos > 0 and yPos < self.height)
for i = 0, numRooms do
repeat
print("Generating room ".. i .."...")
local isInBoundary = false
roomWidth = math.random(minRoomWidth, maxRoomWidth)
roomHeight = math.random(minRoomHeight, maxRoomHeight)
--first room
if ((xPos + roomWidth) < self.width)
and ((yPos + roomHeight) < self.height) then
isInBoundary = true
end
until(isInBoundary)
roomEndX = xPos + roomWidth
roomEndY = yPos + roomHeight
for x = xPos, roomEndX do
for y = yPos, roomEndY do
tile = self.tiles[x][y]
tile.solid = false
end
end
xPos, yPos = self:generateCoridor(
xPos,
yPos,
roomWidth,
roomHeight,
minCoridorLength,
maxCoridorLength,
maxCoridorWidth
)
end
print("Map generation took " .. os.clock() - start)
end
function Map:generateCoridor(
x,
y,
roomWidth,
roomHeight,
minCoridorLength,
maxCoridorLength,
maxCoridorWidth
)
local coridorDx = math.random(minCoridorLength, maxCoridorLength);
local coridorDy = math.random(1, maxCoridorWidth);
local coridorStartX = nil
local coridorStartY = nil
local isInBoundary = true
repeat
print("Generating coridor ...")
direction = math.random(1,4);
if direction == directions.left then
coridorDx = -coridorDx
coridorStartX = x
coridorStartY = math.random(y, y + roomHeight);
elseif direction == directions.right then
coridorStartX = x + roomWidth
coridorStartY = math.random(y, y + roomHeight);
elseif direction == directions.up then
coridorDy = -coridorDy
coridorStartY = y
coridorStartX = math.random(x, x + roomWidth);
elseif direction == directions.down then
coridorStartY = y + roomHeight
coridorStartX = math.random(x, x + roomWidth);
end
until(isInBoundary)
-- swap differences to account for direction
if direction == directions.up
or direction == directions.down
then
coridorDx, coridorDy = coridorDy, coridorDx
end
coridorEndX = coridorStartX + coridorDx
coridorEndY = coridorStartY + coridorDy
for x = coridorStartX, coridorEndX do
for y = coridorStartY, coridorEndY do
tile = self.tiles[x][y]
tile.solid = false
end
end
return coridorEndX, coridorEndY
end
function Map:draw()
for i,row in ipairs(self.tiles) do
for j,tile in ipairs(row) do
tile:draw(
Map.worldOffsetX,
Map.worldOffsetY
)
end
end
end
function Map:checkCollisionWithWorld(ax1,ay1,aw,ah)
local map = Map.currentMap
for i,row in ipairs(map.tiles) do
for j,tile in ipairs(row) do
bx1 = tile:getX()
by1 = tile:getY()
bw = tile:getWidth()
bh = tile:getHeight()
local hasColided = false
local xColision = false
local yColision = false
if(tile.solid) then
local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
xColision = ax1 < bx2 and ax2 > bx1
yColision = ay1 < by2 and ay2 > by1
hasColided = xColision and yColision
end
if hasColided == true then
print("Collision with block ".. tile:getXUnitPosition() ..",".. tile:getYUnitPosition())
return xColision, yColision
end
end
end
end
function Map:getWidth() return self.x * self.tileSize end
function Map:getHeight() return self.y * self.tileSize end
function Map:getUnitWidth() return self.x end
function Map:getUnitHeight() return self.y end
return Map