-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePlatforms.lua
63 lines (50 loc) · 1.72 KB
/
generatePlatforms.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
-- lastPlatform is information about the previous platform, from which we
-- continue adding new platforms.
-- lastPlatform.x
-- lastPlatform.y
-- lastPlatform.width (in pixels)
--
-- TILE_SIZE is size of one tile in pixels.
local generatePlatforms = function (lastPlatform, tileSize)
local minPlatformLengthInTiles = 4
local maxPlatformLengthInTiles = 20
local minY = -100
local maxY = love.graphics.getHeight() + 100
local minXDist = 20
local maxXDist = 300
local minYDist = 50
local maxYDist = 250
local platforms = {}
local platformX = lastPlatform.x
local platformY = lastPlatform.y
local platformLengthInTiles = lastPlatform.width / tileSize
for i=1, 20 do
-- Determine data for the next platform.
local newPlatformLengthInTiles = math.random(
minPlatformLengthInTiles, maxPlatformLengthInTiles
)
local newPlatformX = platformX + platformLengthInTiles * tileSize
+ math.random (minXDist, maxXDist)
local newPlatformY = clamp(
platformY + getRandomSign() * math.random(minYDist, maxYDist),
minY, maxY
)
platformX = newPlatformX
platformY = newPlatformY
platformLengthInTiles = newPlatformLengthInTiles
table.insert(platforms,
Platform(
platformX, platformY,
platformLengthInTiles * tileSize, tileSize * 2
)
)
end
return platforms
end
function getRandomSign()
if math.random(0, 1) == 0 then return -1 else return 1 end
end
function clamp(val, lower, upper)
return math.max(lower, math.min(upper, val))
end
return generatePlatforms