Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_BaseInteractiveLightComponent.c
Go to the documentation of this file.
1[EntityEditorProps(category: "GameScripted/BaseInteractiveLightComponent", description: "Handling behaviour of interactive (can be turned on / off) light on prefabs.")]
3{
4 [Attribute("", UIWidgets.Object, "", category: "")]
5 private ref array<ref SCR_BaseLightData> m_aLightData;
6
7 [Attribute("0", UIWidgets.CheckBox, "Gradual lighting?")]
8 private bool m_bGradualLightning;
9
10 [Attribute("0", UIWidgets.EditBox, "LV of light effect")]
11 private float m_fLV;
12
13 [Attribute("2", UIWidgets.EditBox, "Distance attenuation of light effect")]
14 private float m_fDistanceAttenuation;
15
16 //------------------------------------------------------------------------------------------------
18 float GetLightLV()
19 {
20 return m_fLV;
21 }
22
23 //------------------------------------------------------------------------------------------------
25 float GetDistanceAttenuation()
26 {
27 return m_fDistanceAttenuation;
28 }
29
30 //------------------------------------------------------------------------------------------------
32 // lighting?
33 bool IsGradualLightningOn()
34 {
35 return m_bGradualLightning;
36 }
37
38 //------------------------------------------------------------------------------------------------
40 ref array<ref SCR_BaseLightData> GetLightData()
41 {
42 return m_aLightData;
43 }
44}
45
46enum ELightState
47{
48 LIT = 0,
50 OFF = 2,
51}
52
53class SCR_BaseInteractiveLightComponent : SCR_BaseLightComponent
54{
55 protected ref array<LightEntity> m_aLights;
57 protected float m_fCurLV;
58 protected float m_fCurEM;
59 protected float m_fLightEmisivityStep;
60 protected bool m_bIsOn;
61
62 private bool m_bUpdate;
63
64 protected const static float MATERIAL_EMISSIVITY_STEP = 0.1;
65 protected const static int MATERIAL_EMISSIVITY_ON = 30;
66 protected const static float LIGHT_EMISSIVITY_START = 0.1;
67 protected const static int MATERIAL_EMISSIVITY_START = 1;
68 protected const static int LIGHT_EMISSIVITY_OFF = 0;
69 protected const static int UPDATE_LIGHT_TIME = 100;
70
71 [Attribute(defvalue: "0", uiwidget: UIWidgets.ComboBox, enums: ParamEnumArray.FromEnum(ELightState))]
72 private ELightState m_eInitialLightState;
73
74 //------------------------------------------------------------------------------------------------
76 bool GetInitialState()
77 {
78 switch (m_eInitialLightState)
79 {
80 case ELightState.LIT:
81 return true;
82
83 case ELightState.LIT_ON_SPAWN:
84 if (GetOwner().IsLoaded())
85 return false;
86
87 return true;
88
89 case ELightState.OFF:
90 return false;
91 }
92
93 return false;
94 }
95
96 //------------------------------------------------------------------------------------------------
98 bool IsOn()
99 {
100 return m_bIsOn;
101 }
102
103 //------------------------------------------------------------------------------------------------
108 void ToggleLight(bool turnOn, bool skipTransition = false, bool playSound = true)
109 {
110 if (m_bIsOn == turnOn || !GetGame().InPlayMode())
111 return;
112
113 SCR_BaseInteractiveLightComponentClass componentData = SCR_BaseInteractiveLightComponentClass.Cast(GetComponentData(GetOwner()));
114 if (!componentData)
115 return;
116
117 if (turnOn)
118 TurnOn(componentData, skipTransition, playSound);
119 else
120 TurnOff(componentData, playSound);
121 }
122
123 //------------------------------------------------------------------------------------------------
124 protected void TurnOn(notnull SCR_BaseInteractiveLightComponentClass componentData, bool skipTransition, bool playSound)
125 {
126 IEntity owner = GetOwner();
127
128 // Play sound
129 if (!System.IsConsoleApp())
130 {
131 if (playSound)
132 {
133 SoundComponent soundComponent = SoundComponent.Cast(owner.FindComponent(SoundComponent));
134 if (soundComponent)
135 soundComponent.SoundEvent(SCR_SoundEvent.SOUND_TURN_ON);
136 }
137
139 if (signalsManagerComponent)
140 signalsManagerComponent.SetSignalValue(signalsManagerComponent.AddOrFindSignal("Trigger"), 1);
141 }
142
143 if (!m_aLights)
144 m_aLights = {};
145
146
147 foreach (SCR_BaseLightData lightData : componentData.GetLightData())
148 {
149 if (!lightData)
150 continue;
151
152 vector mat[4];
153 owner.GetWorldTransform(mat);
154
155 vector lightDirection = lightData.GetLightConeDirection().Multiply3(mat).Normalized();
156 vector pos = lightData.GetLightOffset().Multiply4(mat);
157
158 LightEntity light = CreateLight(lightData, pos, lightDirection, LIGHT_EMISSIVITY_START);
159 if (!light)
160 continue;
161
162 light.SetFlags(EntityFlags.PROXY);
163 m_aLights.Insert(light);
164 }
165
166 m_bIsOn = true;
167 m_bUpdate = true;
168
169 // Skip transition phase of the light.
170 if (skipTransition || !componentData.IsGradualLightningOn())
171 {
173 m_EmissiveMaterialComponent.SetEmissiveMultiplier(MATERIAL_EMISSIVITY_ON);
174
175 for (int i = 0, count = m_aLights.Count(); i < count; i++)
176 {
177 m_aLights[i].SetColor(Color.FromVector(componentData.GetLightData()[i].GetLightColor()), componentData.GetLightLV());
178 m_aLights[i].SetDistanceAtt(componentData.GetDistanceAttenuation());
179 }
180
181 return;
182 }
183
184 m_fCurLV = LIGHT_EMISSIVITY_START;
185 m_fCurEM = MATERIAL_EMISSIVITY_START;
186 m_fLightEmisivityStep = componentData.GetLightLV() / ((MATERIAL_EMISSIVITY_ON - MATERIAL_EMISSIVITY_START) / MATERIAL_EMISSIVITY_STEP);
187
188 SetEventMask(GetOwner(), EntityEvent.VISIBLE);
189 }
190
191 //------------------------------------------------------------------------------------------------
192 protected void TurnOff(notnull SCR_BaseInteractiveLightComponentClass componentData, bool playSound)
193 {
194 // Play sound
195 if (!System.IsConsoleApp())
196 {
197 IEntity owner = GetOwner();
199
200 if (playSound)
201 {
202 SoundComponent soundComponent = SoundComponent.Cast(owner.FindComponent(SoundComponent));
203 if (soundComponent)
204 soundComponent.SoundEvent(SCR_SoundEvent.SOUND_TURN_OFF);
205 }
206
207 if (signalsManagerComponent)
208 signalsManagerComponent.SetSignalValue(signalsManagerComponent.AddOrFindSignal("Trigger"), 0);
209 }
210
211 if (!componentData.GetLightData().IsEmpty())
212 {
213 RemoveLights();
214
216 m_EmissiveMaterialComponent.SetEmissiveMultiplier(LIGHT_EMISSIVITY_OFF);
217 }
218
219 ClearEventMask(GetOwner(), EntityEvent.VISIBLE);
220 m_bIsOn = false;
221 m_bUpdate = false;
222 }
223
224 //------------------------------------------------------------------------------------------------
225 override void EOnVisible(IEntity owner, int frameNumber)
226 {
227 if (!m_bUpdate)
228 return;
229
231 if (!componentData)
232 return;
233
234 if (!m_aLights)
235 return;
236
237 bool shouldUpdate = true;
238
239 for (int i = 0, count = Math.Min(componentData.GetLightData().Count(), m_aLights.Count()); i < count; i++)
240 {
241 if (m_fLightEmisivityStep > 0.0 && m_fCurLV < componentData.GetLightLV())
242 UpdateLightEmissivity(componentData.GetLightData()[i], i);
243
244 if (m_EmissiveMaterialComponent && m_fCurEM < MATERIAL_EMISSIVITY_ON)
245 UpdateMaterialEmissivity();
246
247 shouldUpdate &= !(m_fCurLV >= componentData.GetLightLV() && (!m_EmissiveMaterialComponent || m_fCurEM > MATERIAL_EMISSIVITY_ON));
248 }
249
250 m_bUpdate = shouldUpdate;
251 }
252
253 //------------------------------------------------------------------------------------------------
254 private void UpdateLightEmissivity(notnull SCR_BaseLightData lightData, int lightArrayIndex)
255 {
257 m_aLights[lightArrayIndex].SetColor(Color.FromVector(lightData.GetLightColor()), m_fCurLV);
258 }
259
260 //------------------------------------------------------------------------------------------------
261 private void UpdateMaterialEmissivity()
262 {
263 m_fCurEM += MATERIAL_EMISSIVITY_STEP;
264 m_EmissiveMaterialComponent.SetEmissiveMultiplier(m_fCurEM);
265 }
266
267 //------------------------------------------------------------------------------------------------
268 override bool RplSave(ScriptBitWriter writer)
269 {
270 super.RplSave(writer);
271
272 writer.WriteBool(IsOn());
273
274 return true;
275 }
276
277 //------------------------------------------------------------------------------------------------
278 override bool RplLoad(ScriptBitReader reader)
279 {
280 super.RplLoad(reader);
281
282 bool isOn;
283 reader.ReadBool(isOn);
284 ToggleLight(isOn, true, false);
285
286 return true;
287 }
288
289 //------------------------------------------------------------------------------------------------
290 override void OnPostInit(IEntity owner)
291 {
292 super.OnPostInit(owner);
293
294 m_EmissiveMaterialComponent = ParametricMaterialInstanceComponent.Cast(owner.FindComponent(ParametricMaterialInstanceComponent));
295
296 ToggleLight(GetInitialState(), true, false);
297 }
298
299 //------------------------------------------------------------------------------------------------
301 void RemoveLights()
302 {
303 if (m_aLights)
304 {
305 foreach (LightEntity light : m_aLights)
306 {
307 delete light;
308 }
309
310 m_aLights.Clear();
311 }
312 }
313
314 //------------------------------------------------------------------------------------------------
315 override void OnDelete(IEntity owner)
316 {
317 super.OnDelete(owner);
318
319 RemoveLights();
320 }
321}
ArmaReforgerScripted GetGame()
Definition game.c:1398
override bool RplLoad(ScriptBitReader reader)
SCR_BaseInteractiveLightComponentClass OFF
SCR_BaseInteractiveLightComponentClass LIT_ON_SPAWN
SCR_BaseInteractiveLightComponentClass m_aLights
ParametricMaterialInstanceComponent m_EmissiveMaterialComponent
SCR_BaseInteractiveLightComponentClass LIT
SCR_BaseLightComponentClass ScriptComponentClass CreateLight(SCR_BaseLightData lightData, vector position, vector direction, float emissivity)
override bool RplSave(ScriptBitWriter writer)
SCR_CharacterSoundComponentClass GetComponentData()
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
void TurnOn()
override void ToggleLight(bool turnOn, bool skipTransition=false, bool playSound=true)
override void EOnVisible(IEntity owner, int frameNumber)
enum EVehicleType IEntity
Definition Color.c:13
proto external Managed FindComponent(typename typeName)
proto external void GetWorldTransform(out vector mat[])
See IEntity::GetTransform.
Definition Math.c:13
Light parameters.
IEntity GetOwner()
Owner entity of the fuel tank.
SCR_FieldOfViewSettings Attribute
EntityEvent
Various entity events.
Definition EntityEvent.c:14
EntityFlags
Various entity flags.
Definition EntityFlags.c:14
void TurnOff()
Remove every particle associed with this effect.