Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
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  //------------------------------------------------------------------------------------------------
15  float GetLightLV()
16  {
17  return m_fLV;
18  }
19 
20  //------------------------------------------------------------------------------------------------
22  // lighting?
23  bool IsGradualLightningOn()
24  {
25  return m_bGradualLightning;
26  }
27 
28  //------------------------------------------------------------------------------------------------
30  ref array<ref SCR_BaseLightData> GetLightData()
31  {
32  return m_aLightData;
33  }
34 }
35 
36 enum ELightState
37 {
38  LIT = 0,
39  LIT_ON_SPAWN = 1,
40  OFF = 2,
41 }
42 
43 class SCR_BaseInteractiveLightComponent : SCR_BaseLightComponent
44 {
45  protected ref array<LightEntity> m_aLights;
47  protected float m_fCurLV;
48  protected float m_fCurEM;
49  protected float m_fLightEmisivityStep;
50  protected bool m_bIsOn;
51 
52  private bool m_bUpdate;
53 
54  protected const static float MATERIAL_EMISSIVITY_STEP = 0.1;
55  protected const static int MATERIAL_EMISSIVITY_ON = 30;
56  protected const static float LIGHT_EMISSIVITY_START = 0.1;
57  protected const static int MATERIAL_EMISSIVITY_START = 1;
58  protected const static int LIGHT_EMISSIVITY_OFF = 0;
59  protected const static int UPDATE_LIGHT_TIME = 100;
60 
61  [Attribute(defvalue: "0", uiwidget: UIWidgets.ComboBox, enums: ParamEnumArray.FromEnum(ELightState))]
62  private ELightState m_eInitialLightState;
63 
64  //------------------------------------------------------------------------------------------------
67  {
68  switch (m_eInitialLightState)
69  {
70  case ELightState.LIT:
71  return true;
72 
73  case ELightState.LIT_ON_SPAWN:
74  if (GetOwner().IsLoaded())
75  return false;
76 
77  return true;
78 
79  case ELightState.OFF:
80  return false;
81  }
82 
83  return false;
84  }
85 
86  //------------------------------------------------------------------------------------------------
88  bool IsOn()
89  {
90  return m_bIsOn;
91  }
92 
93  //------------------------------------------------------------------------------------------------
98  void ToggleLight(bool turnOn, bool skipTransition = false, bool playSound = true)
99  {
100  if (m_bIsOn == turnOn || !GetGame().InPlayMode())
101  return;
102 
104  if (!componentData)
105  return;
106 
107  if (turnOn)
108  TurnOn(componentData, skipTransition, playSound);
109  else
110  TurnOff(componentData, playSound);
111  }
112 
113  //------------------------------------------------------------------------------------------------
114  protected void TurnOn(notnull SCR_BaseInteractiveLightComponentClass componentData, bool skipTransition, bool playSound)
115  {
116  LightEntity light;
117  IEntity owner = GetOwner();
118  vector lightOffset;
119  vector lightDirection;
120  vector pos;
121 
122  // Play sound
123  if (!System.IsConsoleApp())
124  {
125  if (playSound)
126  {
127  SoundComponent soundComponent = SoundComponent.Cast(owner.FindComponent(SoundComponent));
128  if (soundComponent)
129  soundComponent.SoundEvent(SCR_SoundEvent.SOUND_TURN_ON);
130  }
131 
132  SignalsManagerComponent signalsManagerComponent = SignalsManagerComponent.Cast(owner.FindComponent(SignalsManagerComponent));
133  if (signalsManagerComponent)
134  signalsManagerComponent.SetSignalValue(signalsManagerComponent.AddOrFindSignal("Trigger"), 1);
135  }
136 
137  if (!m_aLights)
138  m_aLights = {};
139 
140 
141  foreach (SCR_BaseLightData lightData : componentData.GetLightData())
142  {
143  if (!lightData)
144  continue;
145 
146  vector mat[4];
147  owner.GetTransform(mat);
148 
149  lightOffset = lightData.GetLightOffset();
150  lightDirection = (lightData.GetLightConeDirection().Multiply4(mat) - lightOffset.Multiply4(mat)).Normalized();
151  pos = owner.GetOrigin() + lightOffset;
152 
153  light = CreateLight(lightData, pos, lightDirection, LIGHT_EMISSIVITY_START);
154  if (!light)
155  continue;
156 
157  owner.AddChild(light, -1, EAddChildFlags.AUTO_TRANSFORM | EAddChildFlags.RECALC_LOCAL_TRANSFORM);
158  light.SetFlags(EntityFlags.PROXY);
159  m_aLights.Insert(light);
160  }
161 
162  m_bIsOn = true;
163  m_bUpdate = true;
164 
165  // Skip transition phase of the light.
166  if (skipTransition || !componentData.IsGradualLightningOn())
167  {
169  m_EmissiveMaterialComponent.SetEmissiveMultiplier(MATERIAL_EMISSIVITY_ON);
170 
171  for (int i = 0, count = m_aLights.Count(); i < count; i++)
172  {
173  m_aLights[i].SetColor(Color.FromVector(componentData.GetLightData()[i].GetLightColor()), componentData.GetLightLV());
174  }
175 
176  return;
177  }
178 
179  m_fCurLV = LIGHT_EMISSIVITY_START;
180  m_fCurEM = MATERIAL_EMISSIVITY_START;
181  m_fLightEmisivityStep = componentData.GetLightLV() / ((MATERIAL_EMISSIVITY_ON - MATERIAL_EMISSIVITY_START) / MATERIAL_EMISSIVITY_STEP);
182 
183  SetEventMask(GetOwner(), EntityEvent.VISIBLE);
184  }
185 
186  //------------------------------------------------------------------------------------------------
187  protected void TurnOff(notnull SCR_BaseInteractiveLightComponentClass componentData, bool playSound)
188  {
189  // Play sound
190  if (!System.IsConsoleApp())
191  {
192  IEntity owner = GetOwner();
193  SignalsManagerComponent signalsManagerComponent = SignalsManagerComponent.Cast(owner.FindComponent(SignalsManagerComponent));
194 
195  if (playSound)
196  {
197  SoundComponent soundComponent = SoundComponent.Cast(owner.FindComponent(SoundComponent));
198  if (soundComponent)
199  soundComponent.SoundEvent(SCR_SoundEvent.SOUND_TURN_OFF);
200  }
201 
202  if (signalsManagerComponent)
203  signalsManagerComponent.SetSignalValue(signalsManagerComponent.AddOrFindSignal("Trigger"), 0);
204  }
205 
206  if (!componentData.GetLightData().IsEmpty())
207  {
208  RemoveLights();
209 
211  m_EmissiveMaterialComponent.SetEmissiveMultiplier(LIGHT_EMISSIVITY_OFF);
212  }
213 
214  ClearEventMask(GetOwner(), EntityEvent.VISIBLE);
215  m_bIsOn = false;
216  m_bUpdate = false;
217  }
218 
219  //------------------------------------------------------------------------------------------------
220  override void EOnVisible(IEntity owner, int frameNumber)
221  {
222  if (!m_bUpdate)
223  return;
224 
226  if (!componentData)
227  return;
228 
229  if (!m_aLights)
230  return;
231 
232  bool shouldUpdate = true;
233 
234  for (int i = 0, count = Math.Min(componentData.GetLightData().Count(), m_aLights.Count()); i < count; i++)
235  {
236  if (m_fLightEmisivityStep > 0.0 && m_fCurLV < componentData.GetLightLV())
237  UpdateLightEmissivity(componentData.GetLightData()[i], i);
238 
239  if (m_EmissiveMaterialComponent && m_fCurEM < MATERIAL_EMISSIVITY_ON)
241 
242  shouldUpdate &= !(m_fCurLV >= componentData.GetLightLV() && (!m_EmissiveMaterialComponent || m_fCurEM > MATERIAL_EMISSIVITY_ON));
243  }
244 
245  m_bUpdate = shouldUpdate;
246  }
247 
248  //------------------------------------------------------------------------------------------------
249  private void UpdateLightEmissivity(notnull SCR_BaseLightData lightData, int lightArrayIndex)
250  {
252  m_aLights[lightArrayIndex].SetColor(Color.FromVector(lightData.GetLightColor()), m_fCurLV);
253  }
254 
255  //------------------------------------------------------------------------------------------------
257  {
258  m_fCurEM += MATERIAL_EMISSIVITY_STEP;
259  m_EmissiveMaterialComponent.SetEmissiveMultiplier(m_fCurEM);
260  }
261 
262  //------------------------------------------------------------------------------------------------
263  override bool RplSave(ScriptBitWriter writer)
264  {
265  super.RplSave(writer);
266 
267  writer.WriteBool(IsOn());
268 
269  return true;
270  }
271 
272  //------------------------------------------------------------------------------------------------
273  override bool RplLoad(ScriptBitReader reader)
274  {
275  super.RplLoad(reader);
276 
277  bool isOn;
278  reader.ReadBool(isOn);
279  ToggleLight(isOn, true, false);
280 
281  return true;
282  }
283 
284  //------------------------------------------------------------------------------------------------
285  override void OnPostInit(IEntity owner)
286  {
287  super.OnPostInit(owner);
288 
290 
291  ToggleLight(GetInitialState(), true, false);
292  }
293 
294  //------------------------------------------------------------------------------------------------
297  {
298  if (m_aLights)
299  {
300  foreach (LightEntity light : m_aLights)
301  {
302  delete light;
303  }
304 
305  m_aLights.Clear();
306  }
307  }
308 
309  //------------------------------------------------------------------------------------------------
310  override void OnDelete(IEntity owner)
311  {
312  super.OnDelete(owner);
313 
314  RemoveLights();
315  }
316 }
UpdateLightEmissivity
private void UpdateLightEmissivity(notnull SCR_BaseLightData lightData, int lightArrayIndex)
Definition: SCR_BaseInteractiveLightComponent.c:249
CreateLight
class SCR_BaseLightData CreateLight
UpdateMaterialEmissivity
private void UpdateMaterialEmissivity()
Definition: SCR_BaseInteractiveLightComponent.c:256
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
EOnVisible
override void EOnVisible(IEntity owner, int frameNumber)
Definition: SCR_BaseInteractiveLightComponent.c:220
SCR_BaseInteractiveLightComponentClass
Definition: SCR_BaseInteractiveLightComponent.c:2
SCR_BaseLightData
Light parameters.
Definition: SCR_BaseLightData.c:5
m_fCurEM
protected float m_fCurEM
Definition: SCR_BaseInteractiveLightComponent.c:48
SCR_BaseLightComponentClass
Definition: SCR_BaseLightComponent.c:2
RemoveLights
void RemoveLights()
Definition: SCR_BaseInteractiveLightComponent.c:296
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SoundEvent
Definition: SCR_SoundEvent.c:1
OnDelete
override void OnDelete(IEntity owner)
Definition: SCR_BaseInteractiveLightComponent.c:310
IsOn
bool IsOn()
Definition: SCR_BaseInteractiveLightComponent.c:88
RplLoad
override bool RplLoad(ScriptBitReader reader)
Definition: SCR_BaseInteractiveLightComponent.c:273
GetInitialState
bool GetInitialState()
Definition: SCR_BaseInteractiveLightComponent.c:66
LIT_ON_SPAWN
SCR_BaseInteractiveLightComponentClass LIT_ON_SPAWN
TurnOn
protected void TurnOn(notnull SCR_BaseInteractiveLightComponentClass componentData, bool skipTransition, bool playSound)
Definition: SCR_BaseInteractiveLightComponent.c:114
m_aLightData
protected ref array< ref SCR_BaseLightData > m_aLightData
Definition: SCR_FlammableHitZone.c:30
m_eInitialLightState
private ELightState m_eInitialLightState
Definition: SCR_BaseInteractiveLightComponent.c:62
Attribute
typedef Attribute
Post-process effect of scripted camera.
m_aLights
SCR_BaseInteractiveLightComponentClass m_aLights
ToggleLight
void ToggleLight(bool turnOn, bool skipTransition=false, bool playSound=true)
Definition: SCR_BaseInteractiveLightComponent.c:98
OnPostInit
override void OnPostInit(IEntity owner)
Editable Mine.
Definition: SCR_BaseInteractiveLightComponent.c:285
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
m_EmissiveMaterialComponent
protected ParametricMaterialInstanceComponent m_EmissiveMaterialComponent
Definition: SCR_BaseInteractiveLightComponent.c:46
m_fCurLV
protected float m_fCurLV
Definition: SCR_BaseInteractiveLightComponent.c:47
m_fLightEmisivityStep
protected float m_fLightEmisivityStep
Definition: SCR_BaseInteractiveLightComponent.c:49
RplSave
override bool RplSave(ScriptBitWriter writer)
Definition: SCR_BaseInteractiveLightComponent.c:263
TurnOff
protected void TurnOff(notnull SCR_BaseInteractiveLightComponentClass componentData, bool playSound)
Definition: SCR_BaseInteractiveLightComponent.c:187
m_bIsOn
protected bool m_bIsOn
Definition: SCR_BaseInteractiveLightComponent.c:50
OFF
SCR_BaseInteractiveLightComponentClass OFF
ParametricMaterialInstanceComponent
Definition: ParametricMaterialInstanceComponent.c:12
m_bUpdate
private bool m_bUpdate
Definition: SCR_BaseInteractiveLightComponent.c:52
LIT
SCR_BaseInteractiveLightComponentClass LIT
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180