rangefinder.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. protected ref Timer m_Timer;
  5. protected TextWidget m_RangeText;
  6. void Rangefinder()
  7. {
  8. }
  9. void ~Rangefinder()
  10. {
  11. m_IsActionActive = false;
  12. }
  13. // How frequently the measurement should be taken
  14. float GetMeasurementUpdateInterval()
  15. {
  16. return 0.5;
  17. }
  18. override void OnWorkStart()
  19. {
  20. if (GetGame().IsServer() && !m_IsActionActive) // incorrectly synchronized state from EM
  21. StopWorkServer();
  22. if( !GetGame().IsDedicatedServer())
  23. {
  24. PlayerBase player_this = PlayerBase.Cast( GetGame().GetPlayer() );
  25. PlayerBase player_owner = PlayerBase.Cast( GetHierarchyRootPlayer() );
  26. if ( player_this == player_owner )
  27. {
  28. StartPeriodicMeasurement();
  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. if ( player_this == player_owner )
  39. {
  40. StopPeriodicMeasurement();
  41. }
  42. }
  43. }
  44. void StartPeriodicMeasurement()
  45. {
  46. if( !m_Timer )
  47. {
  48. m_Timer = new Timer( CALL_CATEGORY_GAMEPLAY );
  49. }
  50. m_RangeText = TextWidget.Cast( GetGame().GetWorkspace().CreateWidgets( "gui/layouts/gameplay/rangefinder_hud.layout" ) );
  51. m_Timer.Run( GetMeasurementUpdateInterval(), this, "DoMeasurement", null, true );
  52. }
  53. void StopPeriodicMeasurement()
  54. {
  55. if( m_Timer )
  56. {
  57. m_Timer.Stop();
  58. }
  59. if (m_RangeText)
  60. {
  61. delete m_RangeText;
  62. }
  63. }
  64. // Measures the distance and returns result in formated string
  65. void DoMeasurement()
  66. {
  67. PlayerBase player = GetPlayer();
  68. if ( player )
  69. {
  70. vector from = GetGame().GetCurrentCameraPosition();
  71. vector to = from + (GetGame().GetCurrentCameraDirection() * RANGEFINDER_MAX_DISTANCE);
  72. vector contact_pos;
  73. vector contact_dir;
  74. int contactComponent;
  75. DayZPhysics.RaycastRV( from, to, contact_pos, contact_dir, contactComponent, NULL , NULL, player, false, false, ObjIntersectIFire);
  76. // Generate result
  77. float dist = vector.Distance( from, contact_pos );
  78. dist = Math.Round(dist);
  79. if (dist < RANGEFINDER_MAX_DISTANCE)
  80. {
  81. if( dist < 10 )
  82. m_RangeText.SetText( "00" + dist.ToString() );
  83. else if( dist < 100 )
  84. m_RangeText.SetText( "0" + dist.ToString() );
  85. else
  86. m_RangeText.SetText( dist.ToString() );
  87. }
  88. else
  89. {
  90. m_RangeText.SetText( "- - -" );
  91. }
  92. }
  93. }
  94. override void SetActions()
  95. {
  96. super.SetActions();
  97. RemoveAction(ActionViewOptics);
  98. AddAction(ActionViewBinoculars);
  99. }
  100. override void OnDebugSpawn()
  101. {
  102. GetInventory().CreateInInventory( "Battery9V" );
  103. }
  104. }