hitdirectionbase.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. class HitDirectionEffectBase
  2. {
  3. const float DURATION_COEF_MIN = 0.6;
  4. const float INTENSITY_MIN = 0.6;
  5. float m_HitDirection;
  6. float m_Duration;
  7. float m_BreakPoint;
  8. float m_TimeActive;
  9. float m_IntensityMax;
  10. Widget m_LayoutRoot;
  11. Widget m_Image;
  12. DayZPlayer m_Player;
  13. bool m_Initialized;
  14. int m_SizeXEnf;
  15. int m_SizeYEnf;
  16. float m_PosX;
  17. float m_PosY;
  18. float m_PosXScreenEdge;
  19. float m_PosYScreenEdge;
  20. float m_AngleRad;
  21. float m_AngleRadPrev;
  22. float m_SmoothVel[1];
  23. ref HitDirectionImagesBase m_ImageData;
  24. void HitDirectionEffectBase()
  25. {
  26. m_Initialized = false;
  27. m_PosX = 0.0;
  28. m_PosY = 0.0;
  29. m_AngleRad = 0.0;
  30. m_SmoothVel[0] = 0.0;
  31. m_ImageData = GetImageData();
  32. m_ImageData.GetCurrentImageData(m_LayoutRoot,m_Image);
  33. }
  34. //! Called manually after object spawn
  35. void Init(DayZPlayer player, float hit_direction, float intensity_max)
  36. {
  37. m_Player = player;
  38. float duration_coef = Math.Clamp(intensity_max,DURATION_COEF_MIN,1);
  39. m_IntensityMax = Math.Clamp(intensity_max,INTENSITY_MIN,1);
  40. m_Duration = m_DurationMax * duration_coef;
  41. m_BreakPoint = Math.Clamp(m_BreakPointBase * m_Duration,0,m_Duration);
  42. m_Scatter = Math.Clamp(m_Scatter,0.0,180.0);
  43. m_HitDirection = hit_direction + (Math.RandomFloatInclusive(0,m_Scatter) * Math.Pow(-1.0,Math.RandomIntInclusive(0,1)));
  44. Widget w = m_LayoutRoot;
  45. w = w.GetChildren();
  46. while (w)
  47. {
  48. w.Show(m_Image == w);
  49. w = w.GetSibling();
  50. }
  51. m_Image.SetColor(m_Color);
  52. m_LayoutRoot.Show(true);
  53. CalculateArrowPosition();
  54. SetIndicatorRotation();
  55. SetIndicatorPositon();
  56. m_Initialized = true;
  57. }
  58. HitDirectionImagesBase GetImageData(){}
  59. void ~HitDirectionEffectBase()
  60. {
  61. m_LayoutRoot.Show(false);
  62. delete m_LayoutRoot;
  63. }
  64. bool DurationCheck()
  65. {
  66. if ( m_TimeActive >= m_Duration )
  67. {
  68. return true;
  69. }
  70. return false;
  71. }
  72. void Update( float timeslice )
  73. {
  74. float intensity;
  75. if ( m_TimeActive <= m_BreakPoint )
  76. {
  77. intensity = m_IntensityMax;
  78. }
  79. else
  80. {
  81. float tmp_value = Math.InverseLerp(m_BreakPoint, m_Duration, m_TimeActive);
  82. intensity = Math.Lerp(m_IntensityMax,0.0,tmp_value);
  83. }
  84. m_TimeActive += timeslice;
  85. intensity = Math.Clamp(intensity,0,1);
  86. if ( m_TimeActive >= m_Duration )
  87. {
  88. m_LayoutRoot.Show(false);
  89. }
  90. else
  91. {
  92. m_LayoutRoot.SetAlpha(intensity);
  93. if ( m_Mode == HitDirectionModes.DYNAMIC )
  94. {
  95. CalculateArrowPosition(timeslice);
  96. SetIndicatorPositon(timeslice);
  97. SetIndicatorRotation(timeslice);
  98. }
  99. }
  100. }
  101. void CalculateArrowPosition(float timeslice = -1.0)
  102. {
  103. m_HitDirection = Math.NormalizeAngle(m_HitDirection);
  104. float angle_direction = m_Player.GetOrientation()[0];
  105. angle_direction = Math.NormalizeAngle(angle_direction);
  106. float camera_angle = GetGame().GetCurrentCameraDirection().VectorToAngles()[0];
  107. camera_angle = Math.NormalizeAngle(camera_angle);
  108. float angle_camera_diff = angle_direction - camera_angle;
  109. m_AngleRad = m_HitDirection + angle_camera_diff;
  110. m_AngleRad = Math.NormalizeAngle(m_AngleRad);
  111. m_AngleRad = m_AngleRad * Math.DEG2RAD;
  112. m_PosX = 0.0;
  113. m_PosY = 0.0;
  114. GetScreenSize(m_SizeXEnf,m_SizeYEnf);
  115. if ( m_Initialized && timeslice != -1.0 )
  116. {
  117. float val = m_AngleRadPrev - m_AngleRad + Math.PI;
  118. val = Math.ModFloat(val, Math.PI2);
  119. val -= Math.PI;
  120. m_AngleRad = m_AngleRadPrev - Math.SmoothCD(0, val, m_SmoothVel, 0.1, 1000, timeslice);
  121. }
  122. m_AngleRadPrev = m_AngleRad;
  123. m_PosXScreenEdge = Math.Clamp(Math.Sin(m_AngleRad)/Math.Sin(Math.PI/4),-1,1) * ( m_SizeXEnf/2 + m_DistanceAdjust * m_SizeXEnf);
  124. m_PosYScreenEdge = Math.Clamp(-1 * Math.Cos(m_AngleRad)/Math.Cos(Math.PI/4),-1,1) * ( m_SizeYEnf/2 + m_DistanceAdjust * m_SizeYEnf);
  125. FinalizePositionCalculation();
  126. }
  127. //! specific handling on individual indicator type
  128. void FinalizePositionCalculation(){}
  129. void SetIndicatorRotation(float timeslice = -1.0){}
  130. void SetIndicatorPositon(float timeslice = -1.0)
  131. {
  132. m_LayoutRoot.SetPos(m_PosX,m_PosY,true);
  133. }
  134. //-----------------------------------------------------------------------
  135. //Static stuff below
  136. //-----------------------------------------------------------------------
  137. static bool m_ServerOverrideEnabled;
  138. static int m_Mode;
  139. static int m_ID;
  140. static int m_Color;
  141. static protected typename m_Type;
  142. static float m_DurationMax;
  143. static float m_BreakPointBase; //! range 0..1, a point where the fading starts
  144. static float m_DistanceAdjust;
  145. static int m_RotationOverride;
  146. static float m_Scatter; //! range 0..180, randomized offset of direction to make it less acurate
  147. //!sets override values, or defaults
  148. static void CheckValues()
  149. {
  150. m_ServerOverrideEnabled = CfgGameplayHandler.GetHitIndicationOverrideEnabled();
  151. if (m_ServerOverrideEnabled)
  152. {
  153. m_Mode = CfgGameplayHandler.GetHitIndicationMode();
  154. m_ID = CfgGameplayHandler.GetHitIndicationTypeID();
  155. m_Color = CfgGameplayHandler.GetHitIndicationIndicatorColor();
  156. m_DurationMax = CfgGameplayHandler.GetHitIndicationMaxDuration();
  157. m_BreakPointBase = CfgGameplayHandler.GetHitIndicationBreakPoint();
  158. m_Scatter = CfgGameplayHandler.GetHitIndicationScatter();
  159. }
  160. else
  161. {
  162. m_Mode = HitDirectionModes.STATIC;
  163. m_ID = HitIndicatorType.SPLASH;
  164. m_Color = HitDirectionConstants.COLOR_DEFAULT;
  165. m_DurationMax = HitDirectionConstants.DURATION_BASE;
  166. m_BreakPointBase = HitDirectionConstants.BREAKPOINT_BASE;
  167. m_Scatter = HitDirectionConstants.SCATTER;
  168. }
  169. m_DistanceAdjust = HitDirectionConstants.DISTANCE_ADJUST;
  170. m_RotationOverride = HitDirectionConstants.ROTATION_DEFAULT;
  171. }
  172. static typename GetCurrentType()
  173. {
  174. switch (m_ID)
  175. {
  176. case HitIndicatorType.SPLASH:
  177. m_Type = HitDirectionEffectSplash;
  178. break;
  179. case HitIndicatorType.SPIKE:
  180. m_Type = HitDirectionEffectSpike;
  181. break;
  182. case HitIndicatorType.ARROW:
  183. m_Type = HitDirectionEffectArrow;
  184. break;
  185. default:
  186. ErrorEx("Unknown HitDirection mode, using HitIndicatorType.SPLASH",ErrorExSeverity.INFO);
  187. m_Type = HitDirectionEffectSplash;
  188. break;
  189. }
  190. return m_Type;
  191. }
  192. }