Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_FlashlightComponent.c
Go to the documentation of this file.
3 {
4  [Attribute("", UIWidgets.EditBox, desc: "Lense type", category: "Flashlight")]
5  string m_sDescription;
6 
7  [Attribute("1 1 1", UIWidgets.ColorPicker, desc: "Lense color", category: "Flashlight")]
8  vector m_vLenseColor;
9 
10  [Attribute("5.0", UIWidgets.Slider, desc: "Light Value", "-8.0 20 1", category: "Flashlight")]
11  float m_fLightValue;
12 }
13 
14 [EntityEditorProps(category: "GameScripted/Gadgets", description: "Flashlight", color: "0 0 255 255")]
15 class SCR_FlashlightComponentClass : SCR_GadgetComponentClass
16 {
17 }
18 
19 class SCR_FlashlightComponent : SCR_GadgetComponent
20 {
21  [Attribute("50", UIWidgets.EditBox, desc: "Intensity of the emmissive texture", params: "0 1000", category: "Flashlight")]
22  protected float m_fEmissiveIntensity;
23 
24  [Attribute("", UIWidgets.Object, desc: "Array of lense colors", category: "Flashlight")]
25  protected ref array<ref SCR_LenseColor> m_LenseArray;
26 
27  [Attribute("0.5", UIWidgets.EditBox, desc: "In meters, light ignores objects up to set distance to avoid unwanted shadows", params: "0.1 5", category: "Light near plane")]
28  protected float m_fLightNearPlaneHand;
29 
30  [Attribute("1.2", UIWidgets.EditBox, desc: "In meters, light ignores objects up to set distance to avoid unwanted shadows", params: "0.1 5", category: "Light near plane")]
31  protected float m_fLightNearPlaneStrapped;
32 
33  [Attribute("0.1", UIWidgets.EditBox, desc: "In meters, light ignores objects up to set distance to avoid unwanted shadows", params: "0.1 5", category: "Light near plane")]
34  protected float m_fLightNearPlaneVehicle;
35 
36  [Attribute("0 0 -0.03", UIWidgets.EditBox, desc: "When strapped light entity is being aligned with weapon to adjust light angle, this entity is offset to avoid it visibly floating", category: "Flashlight")]
37  protected vector m_vFlashlightAdjustOffset;
38 
39  protected bool m_bLastLightState; // remember the last light state for cases where you put your flashlight into storage but there is no slot available -> true = active
40  protected bool m_bIsSlottedVehicle; // char owner is in vehicle while flashlight is slotted
41  protected int m_iCurrentLenseColor;
42  protected vector m_vAnglesCurrent; // current local angles
43  protected vector m_vAnglesTarget; // target local angles used to align flashlight with aiming angles while strapped
44  protected vector m_ItemMat[4]; // owner transformation matrix
45  protected LightEntity m_Light;
46  protected ParametricMaterialInstanceComponent m_EmissiveMaterial;
47  protected SCR_CompartmentAccessComponent m_CompartmentComp;
48  protected CharacterControllerComponent m_CharController;
49 
50  //------------------------------------------------------------------------------------------------
51  override void OnToggleActive(bool state)
52  {
53  m_bActivated = state;
54 
55  // Play sound
56  SCR_SoundManagerEntity soundManagerEntity = GetGame().GetSoundManagerEntity();
57  if (soundManagerEntity)
58  {
59  if (m_bActivated)
60  soundManagerEntity.CreateAndPlayAudioSource(GetOwner(), SCR_SoundEvent.SOUND_FLASHLIGHT_ON);
61  else
62  soundManagerEntity.CreateAndPlayAudioSource(GetOwner(), SCR_SoundEvent.SOUND_FLASHLIGHT_OFF);
63  }
64 
65  UpdateLightState();
66  }
67 
68  //------------------------------------------------------------------------------------------------
70  protected void UpdateLightState()
71  {
72  if (m_bActivated)
73  EnableLight();
74  else
75  DisableLight();
76  }
77 
78  //------------------------------------------------------------------------------------------------
81  protected void SwitchLenses(int filter)
82  {
83  m_iCurrentLenseColor = filter;
84 
85  m_Light.SetColor(Color.FromVector(m_LenseArray[m_iCurrentLenseColor].m_vLenseColor), m_LenseArray[m_iCurrentLenseColor].m_fLightValue);
86  }
87 
88  //------------------------------------------------------------------------------------------------
90  protected void EnableLight()
91  {
92  if (m_Light)
93  m_Light.SetEnabled(true);
94 
95  if (m_EmissiveMaterial)
96  m_EmissiveMaterial.SetEmissiveMultiplier(m_fEmissiveIntensity);
97 
98  SwitchLenses(0); // TODO lens switch support
99 
100  if (m_iMode == EGadgetMode.IN_SLOT)
101  SetShadowNearPlane(true);
102  else if (m_iMode == EGadgetMode.IN_HAND)
103  SetShadowNearPlane(false);
104  }
105 
106  //------------------------------------------------------------------------------------------------
108  protected void DisableLight()
109  {
110  if (m_Light)
111  m_Light.SetEnabled(false);
112 
113  if (m_EmissiveMaterial)
114  m_EmissiveMaterial.SetEmissiveMultiplier(0);
115 
116  DeactivateGadgetUpdate();
117  }
118 
119  //------------------------------------------------------------------------------------------------
123  protected void SetShadowNearPlane(bool state, bool isLeavingVehicle = false)
124  {
125  float nearPlane;
126  m_bIsSlottedVehicle = false;
127 
128  if (state) // strapped mode
129  {
130  nearPlane = m_fLightNearPlaneStrapped;
131  if (m_CompartmentComp && m_CompartmentComp.IsInCompartment() && !isLeavingVehicle)
132  {
133  nearPlane = m_fLightNearPlaneVehicle;
134  m_bIsSlottedVehicle = true;
135  DeactivateGadgetUpdate();
136  }
137  else if (m_bActivated)
138  {
139  ActivateGadgetUpdate();
140  }
141  }
142  else // in hand mode
143  {
144  nearPlane = m_fLightNearPlaneHand;
145  }
146 
147  if (m_Light)
148  m_Light.SetNearPlane(nearPlane);
149  }
150 
151  //------------------------------------------------------------------------------------------------
152  protected void AdjustTransform(vector offset = vector.Zero)
153  {
154  InventoryItemComponent itemComponent = InventoryItemComponent.Cast(GetOwner().FindComponent(InventoryItemComponent));
155  if (itemComponent)
156  {
157 
158  InventoryStorageSlot slot = itemComponent.GetParentSlot();
159  ItemAnimationAttributes animAttr = ItemAnimationAttributes.Cast(itemComponent.FindAttribute(ItemAnimationAttributes));
160  if (slot && animAttr)
161  {
162  vector matLS[4];
163  animAttr.GetAdditiveTransformLS(matLS);
164  matLS[3] = matLS[3] + m_vEquipmentSlotOffset + offset;
165  slot.SetAdditiveTransformLS(matLS);
166  }
167  }
168  }
169 
170  //------------------------------------------------------------------------------------------------
172  protected void OnCompartmentEntered(IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
173  {
174  if (!m_bActivated)
175  return;
176 
177  BaseCompartmentSlot compSlot = manager.FindCompartment(slotID, mgrID);
178  IEntity occupant = compSlot.GetOccupant();
179  if (occupant == m_CharacterOwner)
180  {
181  SetShadowNearPlane(true);
182 
183  if (PilotCompartmentSlot.Cast(compSlot))
184  ToggleActive(false);
185  }
186  }
187 
188  //------------------------------------------------------------------------------------------------
190  protected void OnCompartmentLeft(IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
191  {
192  if (!m_bActivated)
193  return;
194 
195  BaseCompartmentSlot compSlot = manager.FindCompartment(slotID, mgrID);
196  IEntity occupant = compSlot.GetOccupant();
197  if (occupant == m_CharacterOwner)
198  SetShadowNearPlane(true, true);
199  }
200 
201  //------------------------------------------------------------------------------------------------
202  override protected void ModeSwitch(EGadgetMode mode, IEntity charOwner)
203  {
204  super.ModeSwitch(mode, charOwner);
205 
206  InventoryItemComponent inventoryItemComp = InventoryItemComponent.Cast(GetOwner().FindComponent(InventoryItemComponent));
207  if (!inventoryItemComp)
208  return;
209 
210  if (mode == EGadgetMode.ON_GROUND || !charOwner)
211  m_CharController = null;
212  else
213  m_CharController = CharacterControllerComponent.Cast( m_CharacterOwner.FindComponent(CharacterControllerComponent) );
214 
215  if (mode == EGadgetMode.ON_GROUND || mode == EGadgetMode.IN_STORAGE)
216  {
217  inventoryItemComp.SetTraceable(true);
218 
219  m_bLastLightState = m_bActivated;
220 
221  if (m_bActivated)
222  ToggleActive(false);
223 
224  if (mode == EGadgetMode.ON_GROUND) // always set the last state as false when removing from inventory
225  m_bLastLightState = false;
226 
227  }
228  else if (mode == EGadgetMode.IN_SLOT)
229  {
230  inventoryItemComp.SetTraceable(false);
231 
232  m_CompartmentComp = SCR_CompartmentAccessComponent.Cast(m_CharacterOwner.FindComponent(SCR_CompartmentAccessComponent));
233  if (m_CompartmentComp)
234  {
235  m_CompartmentComp.GetOnCompartmentEntered().Insert(OnCompartmentEntered);
236  m_CompartmentComp.GetOnCompartmentLeft().Insert(OnCompartmentLeft);
237  }
238 
239  SetShadowNearPlane(true);
240  }
241  else if (mode == EGadgetMode.IN_HAND)
242  {
243  inventoryItemComp.SetTraceable(true);
244 
245  SetShadowNearPlane(false);
246 
247  if (m_bLastLightState)
248  ToggleActive(true);
249  }
250  }
251 
252  //------------------------------------------------------------------------------------------------
253  override protected void ModeClear(EGadgetMode mode)
254  {
255  if (mode == EGadgetMode.IN_SLOT)
256  {
257  if (m_CompartmentComp)
258  {
259  m_CompartmentComp.GetOnCompartmentEntered().Remove(OnCompartmentEntered);
260  m_CompartmentComp.GetOnCompartmentLeft().Remove(OnCompartmentLeft);
261  }
262 
263  SetShadowNearPlane(false);
264  }
265 
266  super.ModeClear(mode);
267  }
268 
269  //------------------------------------------------------------------------------------------------
270  override protected void ToggleActive(bool state)
271  {
272  if (m_iMode == EGadgetMode.IN_STORAGE && !m_bActivated) // trying to activate flashlight hidden in inventory
273  return;
274 
275  // trying to activate flashlight while driving
276  if (!m_bActivated && m_CompartmentComp && PilotCompartmentSlot.Cast(m_CompartmentComp.GetCompartment()))
277  return;
278 
279  super.ToggleActive(state);
280  }
281 
282  //------------------------------------------------------------------------------------------------
283  override void ActivateAction()
284  {
285  if (m_CharController && m_CharController.GetLifeState() != ECharacterLifeState.ALIVE) // if in chars inventory && char is dead/uncon
286  return;
287 
288  ToggleActive(!m_bActivated);
289  }
290 
291  //------------------------------------------------------------------------------------------------
292  override protected void ActivateGadgetUpdate()
293  {
294  super.ActivateGadgetUpdate();
295 
296  AdjustTransform(m_vFlashlightAdjustOffset);
297  }
298 
299  //------------------------------------------------------------------------------------------------
300  override protected void DeactivateGadgetUpdate()
301  {
302  super.DeactivateGadgetUpdate();
303 
304  if (m_iMode == EGadgetMode.IN_SLOT) // reset slot position of the gadget back to its default
305  AdjustTransform();
306  }
307 
308  //------------------------------------------------------------------------------------------------
309  override EGadgetType GetType()
310  {
311  return EGadgetType.FLASHLIGHT;
312  }
313 
314  //------------------------------------------------------------------------------------------------
315  override bool CanBeToggled()
316  {
317  return true;
318  }
319 
320  //------------------------------------------------------------------------------------------------
321  override bool IsVisibleEquipped()
322  {
323  return true;
324  }
325 
326  //------------------------------------------------------------------------------------------------
327  override bool RplSave(ScriptBitWriter writer)
328  {
329  if (!super.RplSave(writer))
330  return false;
331 
332  writer.WriteBool(m_bActivated);
333 
334  return true;
335  }
336 
337  //------------------------------------------------------------------------------------------------
338  override bool RplLoad(ScriptBitReader reader)
339  {
340  if (!super.RplLoad(reader))
341  return false;
342 
343  reader.ReadBool(m_bActivated);
344 
345  UpdateLightState();
346 
347  return true;
348  }
349 
350  //------------------------------------------------------------------------------------------------
351  override bool OnTicksOnRemoteProxy()
352  {
353  return true; // proxies will only tick on owners without this
354  }
355 
356  //------------------------------------------------------------------------------------------------
357  override void EOnActivate(IEntity owner)
358  {
359  super.EOnActivate(owner);
360 
361  if (m_bActivated)
362  EnableLight();
363  }
364 
365  //------------------------------------------------------------------------------------------------
366  override void Update(float timeSlice)
367  {
368  if (!m_CharController || m_CharController.IsDead()) // deactivate frame on death without turning the light off
369  {
370  DeactivateGadgetUpdate();
371  return;
372  }
373 
374  if (m_iMode != EGadgetMode.IN_SLOT || m_CharController.GetLifeState() != ECharacterLifeState.ALIVE) // run logic below only in slot, not needed while uncon + can get quite jittery there
375  return;
376 
377  // adjust angle of the flashlight to provide usable angle within various poses
378  GetOwner().GetTransform(m_ItemMat);
379  m_vAnglesCurrent = GetOwner().GetLocalYawPitchRoll();
380  m_vAnglesTarget = ( Math.RAD2DEG * m_CharController.GetInputContext().GetAimingAngles() - Math3D.MatrixToAngles(m_ItemMat) ) + m_vAnglesCurrent; // diff between WS aiming and item angles, add local to the result
381  m_vAnglesTarget[0] = fixAngle_180_180(m_vAnglesTarget[0]);
382  m_vAnglesTarget[1] = Math.Clamp(m_vAnglesTarget[1], 0, 30); // only need too offset upwards, also avoid glitches with some stances
383  m_vAnglesTarget[2] = 0; // no roll
384 
385  m_vAnglesTarget[0] = Math.Lerp(m_vAnglesCurrent[0], m_vAnglesTarget[0], timeSlice*4);
386  m_vAnglesTarget[1] = Math.Lerp(m_vAnglesCurrent[1], m_vAnglesTarget[1], timeSlice*4);
387  GetOwner().SetYawPitchRoll(m_vAnglesTarget); // sets local angles
388  }
389 
390  //------------------------------------------------------------------------------------------------
391  override void EOnInit(IEntity owner)
392  {
393  m_Light = LightEntity.Cast( owner.GetChildren() );
394  m_EmissiveMaterial = ParametricMaterialInstanceComponent.Cast(owner.FindComponent(ParametricMaterialInstanceComponent));
395 
396  // Insert default entry if none are set
397  if (m_LenseArray.Count() == 0)
398  {
399  SCR_LenseColor defColor = new SCR_LenseColor();
400  defColor.m_sDescription = "CONFIGURE ME";
401  m_LenseArray.Insert(defColor);
402  }
403  }
404 }
m_CharController
protected SCR_CharacterControllerComponent m_CharController
Definition: SCR_ConsumableItemComponent.c:18
SCR_CompartmentAccessComponent
Definition: SCR_CompartmentAccessComponent.c:15
ItemAnimationAttributes
Definition: ItemAnimationAttributes.c:12
ECharacterLifeState
ECharacterLifeState
Definition: ECharacterLifeState.c:12
InventoryStorageSlot
Definition: InventoryStorageSlot.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SoundEvent
Definition: SCR_SoundEvent.c:1
SCR_GadgetComponentClass
Definition: SCR_GadgetComponent.c:2
SCR_LenseColor
Definition: SCR_FlashlightComponent.c:2
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
PilotCompartmentSlot
Definition: PilotCompartmentSlot.c:12
SCR_SoundManagerEntity
Definition: SCR_SoundManagerEntity.c:17
BaseContainerCustomTitleField
class SCR_KeyBindingFilter BaseContainerCustomTitleField("m_sBindString")
Definition: SCR_KeyBindingMenuConfig.c:113
Attribute
typedef Attribute
Post-process effect of scripted camera.
m_CharacterOwner
SCR_RplTestComponentClass m_CharacterOwner
SCR_FlashlightComponent
Definition: SCR_FlashlightComponent.c:19
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
EntityEditorProps
class SCR_LenseColor EntityEditorProps(category:"GameScripted/Gadgets", description:"Flashlight", color:"0 0 255 255")
Definition: SCR_FlashlightComponent.c:14
InventoryItemComponent
Definition: InventoryItemComponent.c:12
ParametricMaterialInstanceComponent
Definition: ParametricMaterialInstanceComponent.c:12
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
fixAngle_180_180
float fixAngle_180_180(float pAngle)
Definition: CharacterCameraBase.c:14
BaseContainerProps
SCR_AIGoalReaction_Follow BaseContainerProps
Handles insects that are supposed to be spawned around selected prefabs defined in prefab names array...
Definition: SCR_AIGoalReaction.c:468
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180