rangefinder.c 2.6 KB

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