Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_2DSightsComponent.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/Weapon/Sights", description: "", color: "0 0 255 255")]
3 {
4 }
5 
6 //------------------------------------------------------------------------------------------------
7 class SCR_2DSightsComponent : SCR_2DOpticsComponent
8 {
9  [Attribute("0", UIWidgets.CheckBox, "Should hide parent of parent object when using 2D sights", category: "2DSights")]
10  protected bool m_bShouldHideParentParentObject;
11 
12  [Attribute("0", UIWidgets.CheckBox, "Should reticle be scaled with current zoom", category: "2DSights")]
13  protected bool m_bDynamicReticle;
14 
15  [Attribute("1.4", UIWidgets.Slider, "Scale mult. of scope when target recoil is applied.", category: "2DSights", params: "1 3 0.01")]
16  protected float m_fRecoilScaleMax;
17 
18  [Attribute("0.12", UIWidgets.Slider, "The amount of linear translation of recoil in meters that is deemed as target and applies the most scale, no scale is applied beyond this value.", category: "2DSights", params: "0.001 0.5 0.001")]
19  protected float m_fRecoilTranslationTarget;
20 
21  [Attribute("10", UIWidgets.Slider, "Scale mult. of scope movement when target recoil is applied.", category: "2DSights", params: "1 50 0.01")]
22  protected float m_fRecoilScaleTranslation;
23 
24  [Attribute("1", UIWidgets.EditBox, desc: "", params: "1 2000", category: "Behavior")]
25  protected bool m_bAllowRecoil;
26 
27  // sway, taken from binocs
28  protected vector m_vLastCameraDir;
29 
30  // sway
31  protected vector m_vLastPos;
32 
33  // recoil
34  protected CharacterAimingComponent m_CharacterAiming;
36 
38 
39  //------------------------------------------------------------------------------------------------
40  protected override void OnSightADSActivated()
41  {
42  super.OnSightADSActivated();
43 
44  // Set fov
45  if (!m_SightsFovInfo)
46  {
48  SetupFovInfo();
49  }
50 
51  SelectZoomLevel(m_iSelectedZoomLevel);
52 
53  // Set parent
54  IEntity owner = GetOwner();
55  if (owner)
56  {
57  IEntity parent = owner.GetParent();
58  if (m_bShouldHideParentObject)
59  {
60  owner.ClearFlags(EntityFlags.VISIBLE, false);
61  if (parent && m_bShouldHideParentParentObject)
62  {
63  parent.ClearFlags(EntityFlags.VISIBLE, false);
64  }
65  }
66 
69  {
70  BaseCompartmentSlot slot = m_TurretController.GetCompartmentSlot();
71  if (slot)
72  {
73  ChimeraCharacter character = ChimeraCharacter.Cast(slot.GetOccupant());
74  if (character)
75  {
76  m_ParentCharacter = character;
77  }
78  }
79  }
80  else
81  {
82  IEntity parentParent = parent;
83  while (parentParent)
84  {
85  ChimeraCharacter character = ChimeraCharacter.Cast(parentParent);
86  if (character)
87  {
88  m_ParentCharacter = character;
89  m_CharacterAiming = CharacterAimingComponent.Cast(character.FindComponent(CharacterAimingComponent));
90  break;
91  }
92 
93  parentParent = parentParent.GetParent();
94  }
95  }
96  }
97 
99  {
100  // due to sway
101  vector m[4];
102  GetGame().GetWorld().GetCurrentCamera(m);
103  m_vLastCameraDir = Math3D.MatrixToAngles(m);
104  m_vLastPos = GetSightsRelPosition();
105  }
106 
107  // Switching input
108  if (m_SightsFovInfo.GetStepsCount() > 1)
109  GetGame().GetInputManager().AddActionListener(ACTION_WHEEL, EActionTrigger.VALUE, SelectNextZoomLevel);
110 
111  // Setup illumination
112  if (m_bHasIllumination)
113  GetGame().GetInputManager().AddActionListener(ACTION_ILLUMINATION, EActionTrigger.DOWN, ToggleIllumination);
114  }
115 
116  //------------------------------------------------------------------------------------------------
117  protected override void OnSightADSDeactivated()
118  {
119  IEntity owner = GetOwner();
120  if (owner && m_bShouldHideParentObject)
121  {
122  owner.SetFlags(EntityFlags.VISIBLE, true);
123  IEntity parent = owner.GetParent();
124  if (parent && m_bShouldHideParentParentObject)
125  {
126  parent.SetFlags(EntityFlags.VISIBLE, true);
127  }
128  }
129 
130  if (m_bShouldHideParentCharacter && m_ParentCharacter)
131  {
132  m_ParentCharacter.SetFlags(EntityFlags.VISIBLE, false);
133  }
134 
135  m_ParentCharacter = null;
136 
137  super.OnSightADSDeactivated();
138 
139  // Removing switching input
140  GetGame().GetInputManager().RemoveActionListener(ACTION_WHEEL, EActionTrigger.VALUE, SelectNextZoomLevel);
141 
142  // Removing illumination
143  if (m_bHasIllumination)
144  GetGame().GetInputManager().RemoveActionListener(ACTION_ILLUMINATION, EActionTrigger.DOWN, ToggleIllumination);
145  }
146 
147  //------------------------------------------------------------------------------------------------
148  override bool CanFreelook()
149  {
150  return false;
151  }
152 
153  //------------------------------------------------------------------------------------------------
154  protected override void ApplyRecoilToCamera(inout vector pOutCameraTransform[4], vector aimModAngles)
155  {
156  vector weaponAnglesMat[3];
157  Math3D.AnglesToMatrix(aimModAngles, weaponAnglesMat);
158  Math3D.MatrixMultiply3(pOutCameraTransform, weaponAnglesMat, pOutCameraTransform);
159  }
160 
161  //------------------------------------------------------------------------------------------------
163  void UpdateRecoil(float timeSlice)
164  {
165 
166  float zScale = 1;
167 
168  if (m_ParentCharacter)
169  {
170  AimingComponent aimingComponent;
171  if (m_ParentCharacter.IsInVehicle() && m_TurretController)
172  {
173  aimingComponent = m_TurretController.GetTurretComponent();
174  }
175  else
176  {
177  aimingComponent = m_CharacterAiming;
178  }
179 
180  if (aimingComponent)
181  {
182  vector aimingTranslation = aimingComponent.GetRawAimingTranslation();
183  float zAmount = aimingTranslation[2];
184  float zMag = Math.AbsFloat(zAmount / m_fRecoilTranslationTarget);
185  // kick back only, TODO@AS: make sure we work both directions
186  zScale = Math.Clamp(1 + zMag, 1 / m_fRecoilScaleMax, m_fRecoilScaleMax);
187  }
188  }
189  }
190 
191  //------------------------------------------------------------------------------------------------
192  override void OnSightADSPostFrame(IEntity owner, float timeSlice)
193  {
194  super.OnSightADSPostFrame(owner, timeSlice);
195 
196  if (!m_bZoomed)
197  return;
198 
199  if (m_bAllowRecoil)
200  UpdateRecoil(timeSlice);
201  }
202 
203  //------------------------------------------------------------------------------------------------
204  protected void SetupFovInfo()
205  {
206  if (!m_SightsFovInfo)
207  return;
208 
209  // Set zooms
210  //m_SightsFovInfo.SetupZooms();
211  array<float> zooms = m_SightsFovInfo.ZoomsToArray();
212 
213  // Get zooms
214  //int zoomCount = m_SightsFovInfo.GetStepsCount();
215  int zoomCount = zooms.Count();
216 
217  for (int i = 0; i < zoomCount; i++)
218  {
219  // Calculate fov for each zoom
220  float zoom = zooms[i];
221  float fov = CalculateZoomFOV(zoom);
222  m_SightsFovInfo.InsertFov(fov);
223 
224  // Reticle sizes
225  float initalZoom = zooms[0];
226 
227  if (i == 0)
228  {
229  // Initial setup
230  m_aReticleSizes.Insert(m_fDefaultSize);
231  m_SightsFovInfo.SetCurrentFov(fov);
232  }
233  else
234  {
235  // Calulate size
236  float zoomProgress = zooms[i] / initalZoom;
237  float size = m_fDefaultSize * zoomProgress;
238  m_aReticleSizes.Insert(size);
239  }
240  }
241  }
242 
243  //------------------------------------------------------------------------------------------------
245  protected void SelectZoomLevel(int id)
246  {
247  // Is zoomed
248  if (!m_bZoomed)
249  return;
250 
251  // Check sights and id
252  if (!m_SightsFovInfo)
253  return;
254 
255  if (id < 0 || id > m_SightsFovInfo.GetStepsCount() - 1)
256  return;
257 
258  // Set zoom
259  m_SightsFovInfo.SetIndex(id);
260  m_fMagnification = m_SightsFovInfo.GetCurrentZoom();
261 
262  // Reticle size - dynamic reticle size
263  if (m_bDynamicReticle)
264  m_fCurrentReticleSize = m_aReticleSizes[id];
265  }
266 
267  //------------------------------------------------------------------------------------------------
269  protected void SelectNextZoomLevel(float value)
270  {
271  if (!m_SightsFovInfo || value == 0)
272  return;
273 
274  // TEMPORARY SOLUTION
275  // There is a 'mistake' in inheritance, m_pParentCharacter is never set because ADS changes are not propagated (super is not called in SCR_2DPIPSightsComponent)
276  SCR_ChimeraCharacter pParentCharacter = null;
277  IEntity parent = null;
278  IEntity owner = GetOwner();
279  if (owner)
280  {
281  parent = owner.GetParent();
282 
283  IEntity parentParent = parent;
284  while (parentParent)
285  {
286  SCR_ChimeraCharacter character = SCR_ChimeraCharacter.Cast(parentParent);
287  if (character)
288  {
289  pParentCharacter = character;
290  break;
291  }
292 
293  parentParent = parentParent.GetParent();
294  }
295  }
296 
297  // Down
298  if (value > 0 && m_iSelectedZoomLevel < m_SightsFovInfo.GetStepsCount())
299  {
300  m_iSelectedZoomLevel++;
301  SelectZoomLevel(m_iSelectedZoomLevel);
302 
303  if (parent && pParentCharacter)
304  pParentCharacter.SetNewZoomLevel(m_iSelectedZoomLevel, true, Replication.FindId(parent.FindComponent(WeaponComponent)));
305  }
306 
307  // Up
308  if (value < 0 && m_iSelectedZoomLevel > 0)
309  {
310  m_iSelectedZoomLevel--;
311  SelectZoomLevel(m_iSelectedZoomLevel);
312 
313  if (parent && pParentCharacter)
314  pParentCharacter.SetNewZoomLevel(m_iSelectedZoomLevel, false, Replication.FindId(parent.FindComponent(WeaponComponent)));
315  }
316  }
317 
318  //------------------------------------------------------------------------------------------------
320  protected void ToggleIllumination(float value, EActionTrigger trigger)
321  {
322  m_bIsIlluminationOn = !m_bIsIlluminationOn;
323  EnableReticleIllumination(m_bIsIlluminationOn);
324 
325  // TEMPORARY SOLUTION
326  // There is a 'mistake' in inheritance, m_pParentCharacter is never set because ADS changes are not propagated (super is not called in SCR_2DPIPSightsComponent)
327  IEntity owner = GetOwner();
328  if (owner)
329  {
330  IEntity parent = owner.GetParent();
331  SCR_ChimeraCharacter pParentCharacter = null;
332  IEntity parentParent = parent;
333  while (parentParent)
334  {
335  SCR_ChimeraCharacter character = SCR_ChimeraCharacter.Cast(parentParent);
336  if (character)
337  {
338  pParentCharacter = character;
339  break;
340  }
341 
342  parentParent = parentParent.GetParent();
343  }
344 
345  if (pParentCharacter)
346  {
347  pParentCharacter.SetIllumination(m_bIsIlluminationOn, Replication.FindId(WeaponComponent.Cast(parent.FindComponent(WeaponComponent))));
348  }
349  }
350  }
351 
352  //------------------------------------------------------------------------------------------------
353  protected override float GetReticleOffsetYTarget()
354  {
355  if (m_eZeroingType == SCR_EPIPZeroingType.EPZ_RETICLE_OFFSET)
356  return m_fReticleOffsetY + GetCurrentSightsRange()[0];
357 
358  return m_fReticleOffsetY;
359  }
360 
361  //------------------------------------------------------------------------------------------------
362  // Get Set API
363  //------------------------------------------------------------------------------------------------
364 
365  //------------------------------------------------------------------------------------------------
367  {
368  return m_SightsFovInfo;
369  }
370 }
GetReticleOffsetYTarget
protected override float GetReticleOffsetYTarget()
Definition: SCR_2DSightsComponent.c:353
OnSightADSPostFrame
override void OnSightADSPostFrame(IEntity owner, float timeSlice)
Definition: SCR_2DSightsComponent.c:192
ToggleIllumination
protected void ToggleIllumination(float value, EActionTrigger trigger)
Action for toggling illumination.
Definition: SCR_2DSightsComponent.c:320
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
OnSightADSActivated
protected override void OnSightADSActivated()
Called every time the sight is activated.
Definition: SCR_2DSightsComponent.c:40
m_fRecoilScaleMax
protected float m_fRecoilScaleMax
Definition: SCR_2DSightsComponent.c:16
SetupFovInfo
protected void SetupFovInfo()
Definition: SCR_2DSightsComponent.c:204
SelectNextZoomLevel
protected void SelectNextZoomLevel(float value)
Move zoom level up and down based on mouse wheel value.
Definition: SCR_2DSightsComponent.c:269
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_2DOpticsComponentClass
Definition: SCR_2DOpticsComponent.c:2
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
m_bDynamicReticle
protected bool m_bDynamicReticle
Definition: SCR_2DSightsComponent.c:13
SCR_2DOpticsComponent
Definition: SCR_2DOpticsComponent.c:12
m_fRecoilTranslationTarget
protected float m_fRecoilTranslationTarget
Definition: SCR_2DSightsComponent.c:19
Attribute
SCR_2DSightsComponentClass SCR_2DOpticsComponentClass Attribute("0", UIWidgets.CheckBox, "Should hide parent of parent object when using 2D sights", category:"2DSights")] protected bool m_bShouldHideParentParentObject
SCR_SightsZoomFOVInfo
Definition: SCR_SightsZoomFOVInfo.c:6
CanFreelook
override bool CanFreelook()
Definition: SCR_2DSightsComponent.c:148
SelectZoomLevel
protected void SelectZoomLevel(int id)
Switch betweem zoom levels from sights.
Definition: SCR_2DSightsComponent.c:245
m_CharacterAiming
protected CharacterAimingComponent m_CharacterAiming
Definition: SCR_2DSightsComponent.c:34
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
GetCurrentSightsRange
proto external vector GetCurrentSightsRange()
m_vLastCameraDir
protected vector m_vLastCameraDir
Definition: SCR_2DSightsComponent.c:28
UpdateRecoil
void UpdateRecoil(float timeSlice)
Update vignettes scale based on weapon recoil.
Definition: SCR_2DSightsComponent.c:163
m_vLastPos
protected vector m_vLastPos
Definition: SCR_2DSightsComponent.c:31
m_SightsFovInfo
protected SCR_SightsZoomFOVInfo m_SightsFovInfo
Definition: SCR_2DSightsComponent.c:37
GetFOVInfo
proto external SightsFOVInfo GetFOVInfo()
TurretControllerComponent
Definition: TurretControllerComponent.c:12
m_TurretController
protected TurretControllerComponent m_TurretController
Definition: SCR_2DSightsComponent.c:35
ApplyRecoilToCamera
protected override void ApplyRecoilToCamera(inout vector pOutCameraTransform[4], vector aimModAngles)
Definition: SCR_2DSightsComponent.c:154
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
GetSightsFovInfo
SCR_SightsZoomFOVInfo GetSightsFovInfo()
Definition: SCR_2DSightsComponent.c:366
OnSightADSDeactivated
protected override void OnSightADSDeactivated()
Called every time the sight is deactivated.
Definition: SCR_2DSightsComponent.c:117
m_fRecoilScaleTranslation
protected float m_fRecoilScaleTranslation
Definition: SCR_2DSightsComponent.c:22
SCR_2DSightsComponentClass
Definition: SCR_2DSightsComponent.c:2
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
m_bAllowRecoil
protected bool m_bAllowRecoil
Definition: SCR_2DSightsComponent.c:25