Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
rangefinder.c
Go to the documentation of this file.
1 class Rangefinder extends PoweredOptic_Base
2 {
3  static const float RANGEFINDER_MAX_DISTANCE = 913.4856; //TODO adjust maximal distance to match real life rangefinder
4 
5  protected ref Timer m_Timer;
6  protected TextWidget m_RangeText;
7 
8  void Rangefinder()
9  {
10  }
11 
12  // How frequently the measurement should be taken
13  float GetMeasurementUpdateInterval()
14  {
15  return 0.5;
16  }
17 
18  override void OnWorkStart()
19  {
20  if( !GetGame().IsDedicatedServer())
21  {
22  PlayerBase player_this = PlayerBase.Cast( GetGame().GetPlayer() );
23  PlayerBase player_owner = PlayerBase.Cast( GetHierarchyRootPlayer() );
24 
25  if ( player_this == player_owner )
26  {
27  StartPeriodicMeasurement();
28  }
29  }
30  }
31 
32  override void OnWorkStop()
33  {
34  if( !GetGame().IsDedicatedServer())
35  {
36  PlayerBase player_this = PlayerBase.Cast( GetGame().GetPlayer() );
37  PlayerBase player_owner = PlayerBase.Cast( GetHierarchyRootPlayer() );
38 
39  if ( player_this == player_owner )
40  {
41  StopPeriodicMeasurement();
42  }
43  }
44  }
45 
46  void StartPeriodicMeasurement()
47  {
48  if( !m_Timer )
49  {
51  }
52 
53  m_RangeText = TextWidget.Cast( GetGame().GetWorkspace().CreateWidgets( "gui/layouts/gameplay/rangefinder_hud.layout" ) );
54 
55  m_Timer.Run( GetMeasurementUpdateInterval(), this, "DoMeasurement", null, true );
56  }
57 
58  void StopPeriodicMeasurement()
59  {
60  if( m_Timer )
61  {
62  m_Timer.Stop();
63  }
64 
65  if (m_RangeText)
66  {
67  delete m_RangeText;
68  }
69  }
70 
71  // Measures the distance and returns result in formated string
72  void DoMeasurement()
73  {
74  PlayerBase player = GetPlayer();
75 
76  if ( player )
77  {
78  vector from = GetGame().GetCurrentCameraPosition();
79  vector to = from + (GetGame().GetCurrentCameraDirection() * RANGEFINDER_MAX_DISTANCE);
80  vector contact_pos;
81  vector contact_dir;
82  int contactComponent;
83 
84  DayZPhysics.RaycastRV( from, to, contact_pos, contact_dir, contactComponent, NULL , NULL, player, false, false, ObjIntersectIFire);
85 
86  // Generate result
87  float dist = vector.Distance( from, contact_pos );
88  dist = Math.Round(dist);
89 
90  if (dist < RANGEFINDER_MAX_DISTANCE)
91  {
92  if( dist < 10 )
93  m_RangeText.SetText( "00" + dist.ToString() );
94  else if( dist < 100 )
95  m_RangeText.SetText( "0" + dist.ToString() );
96  else
97  m_RangeText.SetText( dist.ToString() );
98  }
99  else
100  {
101  m_RangeText.SetText( "- - -" );
102  }
103  }
104  }
105 
106  override void SetActions()
107  {
108  super.SetActions();
109 
112  }
113 
114  override void OnDebugSpawn()
115  {
116  GetInventory().CreateInInventory( "Battery9V" );
117  }
118 }
GetGame
proto native CGame GetGame()
CALL_CATEGORY_GAMEPLAY
const int CALL_CATEGORY_GAMEPLAY
Definition: tools.c:10
m_Timer
ref Timer m_Timer
Definition: dayzgame.c:690
OnWorkStop
override void OnWorkStop()
Definition: m18smokegrenade_colorbase.c:2
RemoveAction
void RemoveAction(typename actionName)
Definition: advancedcommunication.c:118
OnDebugSpawn
class Hatchback_02_Blue extends Hatchback_02 OnDebugSpawn
Definition: hatchback_02.c:404
PlayerBase
Definition: playerbaseclient.c:1
vector
Definition: enconvert.c:105
TextWidget
Definition: enwidgets.c:219
ActionViewOptics
Definition: actionviewoptics.c:1
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
SetActions
void SetActions()
Definition: advancedcommunication.c:79
GetPlayer
protected void GetPlayer()
Definition: crosshairselector.c:127
DayZPhysics
Definition: dayzphysics.c:123
Timer
Definition: dayzplayerimplement.c:62
PoweredOptic_Base
Definition: nvgoggles.c:1
Math
Definition: enmath.c:6
ActionViewBinoculars
Definition: actionviewbinoculars.c:1
OnWorkStart
override void OnWorkStart()
Definition: smokegrenadebase.c:175