Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
8.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud.h"
  8. #include "text_message.h"
  9. #include "hud_macros.h"
  10. #include "iclientmode.h"
  11. #include "view.h"
  12. #include <KeyValues.h>
  13. #include <vgui/ISurface.h>
  14. #include <vgui_controls/Panel.h>
  15. #include "VGuiMatSurface/IMatSystemSurface.h"
  16. #include "materialsystem/imaterial.h"
  17. #include "materialsystem/imesh.h"
  18. #include "materialsystem/imaterialvar.h"
  19. #include "IEffects.h"
  20. #include "hudelement.h"
  21. // NVNT damage
  22. #include "haptics/haptic_utils.h"
  23. using namespace vgui;
  24. // memdbgon must be the last include file in a .cpp file!!!
  25. #include "tier0/memdbgon.h"
  26. //-----------------------------------------------------------------------------
  27. // Purpose: HDU Damage indication
  28. //-----------------------------------------------------------------------------
  29. class CHudDamageIndicator : public CHudElement, public vgui::Panel
  30. {
  31. DECLARE_CLASS_SIMPLE( CHudDamageIndicator, vgui::Panel );
  32. public:
  33. CHudDamageIndicator( const char *pElementName );
  34. void Init( void );
  35. void Reset( void );
  36. bool ShouldDraw( void );
  37. // Handler for our message
  38. void MsgFunc_Damage( bf_read &msg );
  39. private:
  40. void Paint();
  41. void ApplySchemeSettings(vgui::IScheme *pScheme);
  42. private:
  43. void CalcDamageDirection( const Vector &vecFrom );
  44. void DrawDamageIndicatorFront( float flFade );
  45. void DrawDamageIndicatorRear( float flFade );
  46. void DrawDamageIndicatorLeft( float flFade );
  47. void DrawDamageIndicatorRight( float flFade );
  48. private:
  49. float m_flAttackFront;
  50. float m_flAttackRear;
  51. float m_flAttackLeft;
  52. float m_flAttackRight;
  53. Color m_clrIndicator;
  54. CHudTexture *icon_up;
  55. CHudTexture *icon_down;
  56. CHudTexture *icon_left;
  57. CHudTexture *icon_right;
  58. float m_flFadeCompleteTime; //don't draw past this time
  59. };
  60. DECLARE_HUDELEMENT( CHudDamageIndicator );
  61. DECLARE_HUD_MESSAGE( CHudDamageIndicator, Damage );
  62. //-----------------------------------------------------------------------------
  63. // Purpose: Constructor
  64. //-----------------------------------------------------------------------------
  65. CHudDamageIndicator::CHudDamageIndicator( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudDamageIndicator")
  66. {
  67. vgui::Panel *pParent = g_pClientMode->GetViewport();
  68. SetParent( pParent );
  69. SetHiddenBits( HIDEHUD_HEALTH );
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose:
  73. //-----------------------------------------------------------------------------
  74. void CHudDamageIndicator::Reset( void )
  75. {
  76. m_flAttackFront = 0.0;
  77. m_flAttackRear = 0.0;
  78. m_flAttackRight = 0.0;
  79. m_flAttackLeft = 0.0;
  80. m_flFadeCompleteTime = 0.0;
  81. m_clrIndicator.SetColor( 250, 0, 0, 255 );
  82. }
  83. void CHudDamageIndicator::Init( void )
  84. {
  85. HOOK_HUD_MESSAGE( CHudDamageIndicator, Damage );
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose:
  89. //-----------------------------------------------------------------------------
  90. bool CHudDamageIndicator::ShouldDraw( void )
  91. {
  92. if ( !CHudElement::ShouldDraw() )
  93. return false;
  94. if ( ( m_flAttackFront <= 0.0 ) && ( m_flAttackRear <= 0.0 ) && ( m_flAttackLeft <= 0.0 ) && ( m_flAttackRight <= 0.0 ) )
  95. return false;
  96. return true;
  97. }
  98. void CHudDamageIndicator::DrawDamageIndicatorFront( float flFade )
  99. {
  100. if ( m_flAttackFront > 0.4 )
  101. {
  102. if ( !icon_up )
  103. {
  104. icon_up = gHUD.GetIcon( "pain_up" );
  105. }
  106. if ( !icon_up )
  107. {
  108. return;
  109. }
  110. int x = ( ScreenWidth() / 2 ) - icon_up->Width() / 2;
  111. int y = ( ScreenHeight() / 2 ) - icon_up->Height() * 3;
  112. icon_up->DrawSelf( x, y, m_clrIndicator );
  113. m_flAttackFront = MAX( 0.0, m_flAttackFront - flFade );
  114. }
  115. else
  116. {
  117. m_flAttackFront = 0.0;
  118. }
  119. }
  120. void CHudDamageIndicator::DrawDamageIndicatorRear( float flFade )
  121. {
  122. if ( m_flAttackRear > 0.4 )
  123. {
  124. if ( !icon_down )
  125. {
  126. icon_down = gHUD.GetIcon( "pain_down" );
  127. }
  128. if ( !icon_down )
  129. {
  130. return;
  131. }
  132. int x = ( ScreenWidth() / 2 ) - icon_down->Width() / 2;
  133. int y = ( ScreenHeight() / 2 ) + icon_down->Height() * 2;
  134. icon_down->DrawSelf( x, y, m_clrIndicator );
  135. m_flAttackRear = MAX( 0.0, m_flAttackRear - flFade );
  136. }
  137. else
  138. {
  139. m_flAttackRear = 0.0;
  140. }
  141. }
  142. void CHudDamageIndicator::DrawDamageIndicatorLeft( float flFade )
  143. {
  144. if ( m_flAttackLeft > 0.4 )
  145. {
  146. if ( !icon_left )
  147. {
  148. icon_left = gHUD.GetIcon( "pain_left" );
  149. }
  150. if ( !icon_left )
  151. {
  152. return;
  153. }
  154. int x = ( ScreenWidth() / 2 ) - icon_left->Width() * 3;
  155. int y = ( ScreenHeight() / 2 ) - icon_left->Height() / 2;
  156. icon_left->DrawSelf( x, y, m_clrIndicator );
  157. m_flAttackLeft = MAX( 0.0, m_flAttackLeft - flFade );
  158. }
  159. else
  160. {
  161. m_flAttackLeft = 0.0;
  162. }
  163. }
  164. void CHudDamageIndicator::DrawDamageIndicatorRight( float flFade )
  165. {
  166. if ( m_flAttackRight > 0.4 )
  167. {
  168. if ( !icon_right )
  169. {
  170. icon_right = gHUD.GetIcon( "pain_right" );
  171. }
  172. if ( !icon_right )
  173. {
  174. return;
  175. }
  176. int x = ( ScreenWidth() / 2 ) + icon_right->Width() * 2;
  177. int y = ( ScreenHeight() / 2 ) - icon_right->Height() / 2;
  178. icon_right->DrawSelf( x, y, m_clrIndicator );
  179. m_flAttackRight = MAX( 0.0, m_flAttackRight - flFade );
  180. }
  181. else
  182. {
  183. m_flAttackRight = 0.0;
  184. }
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Purpose: Paints the damage display
  188. //-----------------------------------------------------------------------------
  189. void CHudDamageIndicator::Paint()
  190. {
  191. if( m_flFadeCompleteTime > gpGlobals->curtime )
  192. {
  193. float flFade = gpGlobals->frametime * 2;
  194. // draw damage indicators
  195. DrawDamageIndicatorFront( flFade );
  196. DrawDamageIndicatorRear( flFade );
  197. DrawDamageIndicatorLeft( flFade );
  198. DrawDamageIndicatorRight( flFade );
  199. }
  200. }
  201. // NVNT static to pass damage
  202. static float hap_damage_amount = 0;
  203. //-----------------------------------------------------------------------------
  204. // Purpose: Message handler for Damage message
  205. //-----------------------------------------------------------------------------
  206. void CHudDamageIndicator::MsgFunc_Damage( bf_read &msg )
  207. {
  208. int damageTaken = msg.ReadByte();
  209. Vector vecFrom;
  210. msg.ReadBitVec3Coord( vecFrom );
  211. // NVNT pass damage to static holder
  212. hap_damage_amount = damageTaken;
  213. if ( damageTaken > 0 )
  214. {
  215. m_flFadeCompleteTime = gpGlobals->curtime + 1.0;
  216. CalcDamageDirection( vecFrom );
  217. }
  218. //=============================================================================
  219. // HPE_BEGIN:
  220. // [menglish] Added reads for the added location based parameters to this message
  221. //=============================================================================
  222. msg.ReadLong();
  223. msg.ReadBitVec3Coord( vecFrom );
  224. //=============================================================================
  225. // HPE_END
  226. //=============================================================================
  227. }
  228. void CHudDamageIndicator::CalcDamageDirection( const Vector &vecFrom )
  229. {
  230. if ( vecFrom == vec3_origin )
  231. {
  232. m_flAttackFront = 0.0;
  233. m_flAttackRear = 0.0;
  234. m_flAttackRight = 0.0;
  235. m_flAttackLeft = 0.0;
  236. return;
  237. }
  238. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  239. if ( !pLocalPlayer )
  240. {
  241. return;
  242. }
  243. Vector vecDelta = ( vecFrom - pLocalPlayer->GetRenderOrigin() );
  244. if ( vecDelta.Length() <= 50 )
  245. {
  246. m_flAttackFront = 1.0;
  247. m_flAttackRear = 1.0;
  248. m_flAttackRight = 1.0;
  249. m_flAttackLeft = 1.0;
  250. return;
  251. }
  252. VectorNormalize( vecDelta );
  253. Vector forward;
  254. Vector right;
  255. Vector up;
  256. AngleVectors( MainViewAngles(), &forward, &right, &up );
  257. float flFront = DotProduct( vecDelta, forward );
  258. float flSide = DotProduct( vecDelta, right );
  259. float flUp = DotProduct( vecDelta, up);
  260. if ( flFront > 0 )
  261. {
  262. if ( flFront > 0.3 )
  263. m_flAttackFront = MAX( m_flAttackFront, flFront );
  264. }
  265. else
  266. {
  267. float f = fabs( flFront );
  268. if ( f > 0.3 )
  269. m_flAttackRear = MAX( m_flAttackRear, f );
  270. }
  271. if ( flSide > 0 )
  272. {
  273. if ( flSide > 0.3 )
  274. m_flAttackRight = MAX( m_flAttackRight, flSide );
  275. }
  276. else
  277. {
  278. float f = fabs( flSide );
  279. if ( f > 0.3 )
  280. m_flAttackLeft = MAX( m_flAttackLeft, f );
  281. }
  282. // NVNT pass damage. (use hap_damage amount to apply)
  283. // do rotation
  284. Vector hapDir(-flSide,-flUp,flFront);
  285. if ( haptics )
  286. haptics->ApplyDamageEffect(hap_damage_amount, 0, hapDir);
  287. }
  288. //-----------------------------------------------------------------------------
  289. // Purpose: hud scheme settings
  290. //-----------------------------------------------------------------------------
  291. void CHudDamageIndicator::ApplySchemeSettings(vgui::IScheme *pScheme)
  292. {
  293. BaseClass::ApplySchemeSettings(pScheme);
  294. SetPaintBackgroundEnabled(false);
  295. int wide, tall;
  296. GetHudSize(wide, tall);
  297. SetSize(wide, tall);
  298. }