Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
rangefinder.c
Go to the documentation of this file.
1class 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 Widget m_Root;
7 protected TextWidget m_RangeText;
8
9 protected string m_LayoutPath;
10
12 {
14 }
15
17 {
18 m_IsActionActive = false;
19 }
20
21 protected void InitRangeFinderData()
22 {
23 string path = "cfgVehicles " + GetType();
24 if (GetGame().ConfigIsExisting(path))
25 {
26 string layout;
27 if (GetGame().ConfigIsExisting(path + " rangeFinderLayout"))
28 {
29 GetGame().ConfigGetText(path + " rangeFinderLayout", layout);
30 }
31
32 if (layout != "" && layout.Length() > 0)
33 {
34 m_LayoutPath = layout;
35 }
36 else
37 {
38 m_LayoutPath = "gui/layouts/gameplay/rangefinder_hud.layout";
39 }
40 }
41 }
42
43 // How frequently the measurement should be taken
45 {
46 return 0.5;
47 }
48
49 override void OnWorkStart()
50 {
51 if (GetGame().IsServer() && !m_IsActionActive) // incorrectly synchronized state from EM
52 StopWorkServer();
53
54 if( !GetGame().IsDedicatedServer())
55 {
56 PlayerBase player_this = PlayerBase.Cast( GetGame().GetPlayer() );
57 PlayerBase player_owner = PlayerBase.Cast( GetHierarchyRootPlayer() );
58
59 if ( player_this == player_owner )
60 {
62 }
63 }
64 }
65
66 override void OnWorkStop()
67 {
68 if( !GetGame().IsDedicatedServer())
69 {
70 PlayerBase player_this = PlayerBase.Cast( GetGame().GetPlayer() );
71 PlayerBase player_owner = PlayerBase.Cast( GetHierarchyRootPlayer() );
72
73 if ( player_this == player_owner )
74 {
76 }
77 }
78 }
79
81 {
82 if( !m_Timer )
83 {
85 }
86
87 m_Root = GetGame().GetWorkspace().CreateWidgets( m_LayoutPath );
88
89 // Either use root as text widget directly or find the text as children, arbitrary layout is supported now.
90 m_RangeText = TextWidget.Cast(m_Root);
91 if (!m_RangeText)
92 m_RangeText = TextWidget.Cast(m_Root.FindAnyWidget("RangeText"));
93
94 m_Timer.Run( GetMeasurementUpdateInterval(), this, "DoMeasurement", null, true );
95 }
96
98 {
99 if( m_Timer )
100 {
101 m_Timer.Stop();
102 }
103
104 if (m_Root)
105 {
106 delete m_Root;
107 }
108 }
109
110 protected void SetDistanceText(TextWidget text, float dist)
111 {
112 dist = Math.Round(dist);
113
114 if (dist < RANGEFINDER_MAX_DISTANCE)
115 {
116 if( dist < 10 )
117 text.SetText( "00" + dist.ToString() );
118 else if( dist < 100 )
119 text.SetText( "0" + dist.ToString() );
120 else
121 text.SetText( dist.ToString() );
122 }
123 else
124 {
125 SetInvalidText(text);
126 }
127 }
128
129 protected void SetInvalidText(TextWidget text)
130 {
131 text.SetText( "---" );
132 }
133
134 // Measures the distance and returns result in formated string
136 {
137 PlayerBase player = GetPlayer();
138
139 if ( player )
140 {
141 vector from = GetGame().GetCurrentCameraPosition();
142 vector fromDirection = GetGame().GetCurrentCameraDirection();
143 vector to = from + (fromDirection * RANGEFINDER_MAX_DISTANCE);
144 vector contact_pos;
145 vector contact_dir;
146 int contactComponent;
147
148 bool hit = DayZPhysics.RaycastRV( from, to, contact_pos, contact_dir, contactComponent, NULL , NULL, player, false, false, ObjIntersectIFire);
149
150 // (a)
151 // from -> --- <- horizEnd
152 // (h) \ |
153 // to -> \|
154
155 // Generate result
156 float h = vector.Distance( from, contact_pos );
157
158 if (hit)
159 SetDistanceText( m_RangeText, h );
160 else
161 SetInvalidText( m_RangeText );
162
163 // Horizontal distance
164 TextWidget angleText = TextWidget.Cast(m_Root.FindAnyWidget("AngleText"));
165 TextWidget horizText = TextWidget.Cast(m_Root.FindAnyWidget("RangeHDText"));
166
167 vector horizontalTo = Vector( contact_pos[0], from[1], contact_pos[2] );
168 float a = vector.Distance( from, horizontalTo );
169
170 // Angle between horizontal and actual line
171 float heightDiff = contact_pos[1] - from[1];
172 float angle = Math.Atan( heightDiff / a ) * Math.RAD2DEG;
173 angle = Math.Round(angle);
174
175 if (angleText)
176 {
177 if (hit)
178 angleText.SetText(string.Format("%1", angle));
179 else
180 SetInvalidText( angleText );
181 }
182
183 if (horizText)
184 {
185 if (hit)
186 SetDistanceText( horizText, a );
187 else
188 SetInvalidText( horizText );
189 }
190 }
191 }
192
193 override void SetActions()
194 {
195 super.SetActions();
196
199 }
200
201 override void OnDebugSpawn()
202 {
203 GetInventory().CreateInInventory( "Battery9V" );
204 }
205}
eBleedingSourceType GetType()
void AddAction(typename actionName)
void RemoveAction(typename actionName)
Definition enmath.c:7
override void SetActions()
void SetDistanceText(TextWidget text, float dist)
ref Timer m_Timer
Definition rangefinder.c:5
void SetInvalidText(TextWidget text)
void StopPeriodicMeasurement()
Definition rangefinder.c:97
void InitRangeFinderData()
Definition rangefinder.c:21
void StartPeriodicMeasurement()
Definition rangefinder.c:80
override void OnDebugSpawn()
TextWidget m_RangeText
Definition rangefinder.c:7
float GetMeasurementUpdateInterval()
Definition rangefinder.c:44
override void OnWorkStop()
Definition rangefinder.c:66
override void OnWorkStart()
Definition rangefinder.c:49
ref Timer m_Timer
Definition dayzgame.c:705
proto native CGame GetGame()
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
const int CALL_CATEGORY_GAMEPLAY
Definition tools.c:10
PlayerBase GetPlayer()
Widget m_Root
Definition sizetochild.c:91