-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrain.lua
54 lines (46 loc) · 1.4 KB
/
rain.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
local base = require("chunk")
local rain = setmetatable({}, {__index = base})
rain.x = 0
rain.world = nil
rain.spacing = rain.chunkSize + 1
rain.maxRainChance = 0.2
local terrain = require("terrain")
local objects = require("objects")
local perlin = require("lib/perlin")
perlin = setmetatable({}, {__index = perlin})
--[[ Callback ]]
function rain:generateChunk(chunk, dt)
print("Starting rain", chunk)
self:generateAt(chunk)
end
function rain:updateChunk(chunk, dt)
local x = self:getX(chunk)
local index, object
local rainSpot, rainChance
for index, object in ipairs(self[chunk]) do
rainChance = love.math.random(1, 10) / 10
if rainChance < self[chunk][index] then
rainSpot = love.math.random(x + (index - 1) * self.spacing, x + index * self.spacing)
objects:waterCircle(self.world, rainSpot, -500)
end
end
end
function rain:draw()
end
--[[ Utility ]]
function rain:generateAt(chunk)
local x = self:getX(chunk)
local index
for index = x, x+self.chunkSize, self.spacing do
self:setVertex(index, chunk)
end
end
function rain:setVertex(x, chunk)
if chunk < 1 then return end
if self[chunk] == nil then self[chunk] = {} end
local index = self:getChunkIndex(x, chunk)
if self[chunk][index] ~= nil then return end
table.insert(self[chunk], index, (perlin:fbm(x) % self.maxRainChance))
print("RainChance", "Chunk", chunk, "Chance", self[chunk][index])
end
return rain