File size: 3,929 Bytes
0712d5f | 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 | -- src/ServerScriptService/WeatherManager.server.lua
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local WeatherConfig = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("WeatherConfig"))
local NotificationEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("NotificationEvent")
local SoundEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("SoundEvent")
-- Weather states
local WeatherStates = {"Clear", "Rain", "Fog", "Storm"}
local currentWeather = "Clear"
local isRaining = false
-- Determine if a log is under a roof
local function isLogProtected(log)
local origin = log.Position
local direction = Vector3.new(0, 100, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {log}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local result = workspace:Raycast(origin, direction, raycastParams)
if result and result.Instance then
return true
end
return false
end
-- Process Wood Degradation
local function processDegradation()
if not isRaining then return end
for _, log in pairs(CollectionService:GetTagged("TreeSegment")) do
if not isLogProtected(log) then
local currentDegradation = log:GetAttribute("DegradedMult") or 1.0
if currentDegradation > WeatherConfig.DegradationConfig.MaxValueLoss then
local newMult = currentDegradation - WeatherConfig.DegradationConfig.WoodValueLossPerTick
newMult = math.clamp(newMult, WeatherConfig.DegradationConfig.MaxValueLoss, 1.0)
log:SetAttribute("DegradedMult", newMult)
local r, g, b = log.Color.R, log.Color.G, log.Color.B
log.Color = Color3.new(r * 0.95, g * 0.95, b * 0.95)
end
end
end
end
local function setWeather(state)
currentWeather = state
if state == "Rain" or state == "Storm" then
isRaining = true
-- Darken lighting
Lighting.Ambient = Color3.fromRGB(100, 100, 110)
Lighting.OutdoorAmbient = Color3.fromRGB(80, 80, 90)
Lighting.FogEnd = state == "Storm" and 400 or 800
Lighting.FogColor = Color3.fromRGB(140, 140, 150)
-- Notify players
for _, player in ipairs(Players:GetPlayers()) do
NotificationEvent:FireClient(player, "Weather", state == "Storm" and "A storm is rolling in! Protect your wood!" or "It's starting to rain. Wood left outside will degrade.")
SoundEvent:FireClient(player, "RainLoop", nil)
end
-- Start degradation loop
task.spawn(function()
while isRaining do
processDegradation()
task.wait(WeatherConfig.DegradationConfig.TickInterval)
end
end)
elseif state == "Fog" then
isRaining = false
Lighting.Ambient = Color3.fromRGB(120, 120, 125)
Lighting.OutdoorAmbient = Color3.fromRGB(100, 100, 105)
Lighting.FogEnd = 300
Lighting.FogColor = Color3.fromRGB(180, 180, 190)
else -- Clear
isRaining = false
Lighting.Ambient = Color3.fromRGB(150, 150, 150)
Lighting.OutdoorAmbient = Color3.fromRGB(140, 140, 140)
Lighting.FogEnd = 2000
Lighting.FogColor = Color3.fromRGB(200, 210, 220)
end
print("Weather changed to:", state)
end
-- Weather Cycle Loop
task.spawn(function()
while true do
-- Clear period
setWeather("Clear")
local clearTime = math.random(WeatherConfig.CycleDurationMin, WeatherConfig.CycleDurationMax)
task.wait(clearTime)
-- Pick weather event
local roll = math.random()
if roll < 0.5 then
setWeather("Rain")
elseif roll < 0.75 then
setWeather("Fog")
else
setWeather("Storm")
end
local eventTime = math.random(WeatherConfig.RainDurationMin, WeatherConfig.RainDurationMax)
task.wait(eventTime)
end
end)
-- Expose current weather state
_G.GetCurrentWeather = function()
return currentWeather
end
|