forked from Tria-Studio/Tria-Escape-MapLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLighting.lua
138 lines (113 loc) · 3.48 KB
/
Lighting.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
--!strict
-- Copyright (C) 2023 Tria
-- This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
-- If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
--< Services >--
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
--< Main >--
local LightingFeature = {}
LightingFeature.__index = LightingFeature
local remote = ReplicatedStorage.Remotes.Features.ChangeLighting
--- @class Lighting
--- This is a MapLib Feature. It can be accessed by `MapLib:GetFeature("Lighting")`.
function LightingFeature.new(MapLib)
local self = setmetatable({}, LightingFeature)
self.map = MapLib.map
self.cache = {}
for _, v in pairs(Lighting:GetChildren()) do
self.cache[v.Name] = v
end
return self
end
--[=[
@within Lighting
@method SetLighting
@since 0.11
@param properties { [string]: any }
@param postEffects { [string]: { [string]: any } }
This function can to be used to change the lighting of a map mid round. We discourage usage of changing lighting
with `game.Lighting[Property] = value` cause it doesnt replicate for spectators.
**Example:**
```lua
-- Changes the fog to 100 and the fog color to white
local LightingFeature = MapLib:GetFeature("Lighting")
LightingFeature:SetLighting({
FogEnd = 100,
FogColor = Color3.fromRGB(255, 255, 255)
})
```
:::info
This function also supports lighting effects to be updated and they will be replicated to specators.
```lua
-- Changes the fog to 100 and the fog color to white and makes everything monochrome.
local LightingFeature = MapLib:GetFeature("Lighting")
LightingFeature:SetLighting({
FogEnd = 100,
FogColor = Color3.fromRGB(255, 255, 255)
}, {
ColorCorrection = {
Saturation = -1,
},
})
```
:::
:::caution
For the game to be able to edit post effects they have to be correctly placed inside the lighting folder inside settings.
If they are created in a script the game will not see these and refuse to update the lighting properties.
:::
:::tip
Since atmosphere instances don't have any enabled or disabled property we can get around that by parenting the instance to ReplicatedStorage
and then we can parent it back to lighting when we need it.
```lua
local LightingFeature = MapLib:GetFeature("Lighting")
--Disables the atmosphere effect
LightingFeature:SetLighting({}, {
Atmosphere = {
Parent = game.ReplicateStorage,
},
})
task.wait(5)
--Enables the atmosphere effect
LightingFeature:SetLighting({}, {
Atmosphere = {
Parent = game.Lighting,
},
})
```
:::
]=]
function LightingFeature:SetLighting(properties: { [string]: any }, postEffects: { [string]: { [string]: any } })
if RunService:IsClient() then
for property, value in pairs(properties) do
Lighting[property] = value
end
if postEffects then
--Update for client
for name, v in pairs(postEffects) do
for prop, value in pairs(v) do
local instance = self.cache[name]
if instance then
instance[prop] = value
end
end
end
end
remote:FireServer({
Values = properties,
Effects = postEffects,
})
end
end
if RunService:IsServer() then
remote.OnServerEvent:Connect(function(player: Player, values: { [string]: any }): ()
for _, v in pairs(Players:GetPlayers()) do
if v ~= player then
remote:FireClient(v, player, values)
end
end
end)
end
return LightingFeature