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.

174 lines
4.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //
  9. // Health.cpp
  10. //
  11. // implementation of CHudHealth class
  12. //
  13. #include "cbase.h"
  14. #include "hud.h"
  15. #include "hud_macros.h"
  16. #include "view.h"
  17. #include "iclientmode.h"
  18. #define PAIN_NAME "sprites/%d_pain.vmt"
  19. #include <KeyValues.h>
  20. #include <vgui/ISurface.h>
  21. #include <vgui/ISystem.h>
  22. #include <vgui_controls/AnimationController.h>
  23. using namespace vgui;
  24. #include "hudelement.h"
  25. #include "hud_numericdisplay.h"
  26. #include "cs_gamerules.h"
  27. #include "convar.h"
  28. //-----------------------------------------------------------------------------
  29. // Purpose: Health panel
  30. //-----------------------------------------------------------------------------
  31. class CHudHealth : public CHudElement, public CHudNumericDisplay
  32. {
  33. DECLARE_CLASS_SIMPLE( CHudHealth, CHudNumericDisplay );
  34. public:
  35. CHudHealth( const char *pElementName );
  36. virtual void Init( void );
  37. virtual void VidInit( void );
  38. virtual void Reset( void );
  39. virtual void OnThink();
  40. virtual void Paint( void );
  41. virtual void ApplySchemeSettings( IScheme *scheme );
  42. private:
  43. // old variables
  44. int m_iHealth;
  45. int m_bitsDamage;
  46. CHudTexture *m_pHealthIcon;
  47. CPanelAnimationVarAliasType( float, icon_xpos, "icon_xpos", "0", "proportional_float" );
  48. CPanelAnimationVarAliasType( float, icon_ypos, "icon_ypos", "0", "proportional_float" );
  49. // CPanelAnimationVar( Color, m_LowHealthColor, "LowHealthColor", "255 0 0 255" );
  50. float icon_tall;
  51. float icon_wide;
  52. };
  53. DECLARE_HUDELEMENT( CHudHealth );
  54. //-----------------------------------------------------------------------------
  55. // Purpose: Constructor
  56. //-----------------------------------------------------------------------------
  57. CHudHealth::CHudHealth( const char *pElementName ) : CHudElement( pElementName ), CHudNumericDisplay(NULL, "HudHealth")
  58. {
  59. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. void CHudHealth::Init()
  65. {
  66. m_iHealth = 100;
  67. m_bitsDamage = 0;
  68. icon_tall = 0;
  69. icon_wide = 0;
  70. SetIndent(true);
  71. SetDisplayValue(m_iHealth);
  72. }
  73. void CHudHealth::ApplySchemeSettings( IScheme *scheme )
  74. {
  75. BaseClass::ApplySchemeSettings( scheme );
  76. if( !m_pHealthIcon )
  77. {
  78. m_pHealthIcon = gHUD.GetIcon( "health_icon" );
  79. }
  80. if( m_pHealthIcon )
  81. {
  82. icon_tall = GetTall() - YRES(2);
  83. float scale = icon_tall / (float)m_pHealthIcon->Height();
  84. icon_wide = ( scale ) * (float)m_pHealthIcon->Width();
  85. }
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose: reset health to normal color at round restart
  89. //-----------------------------------------------------------------------------
  90. void CHudHealth::Reset()
  91. {
  92. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("HealthRestored");
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose:
  96. //-----------------------------------------------------------------------------
  97. void CHudHealth::VidInit()
  98. {
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose:
  102. //-----------------------------------------------------------------------------
  103. void CHudHealth::OnThink()
  104. {
  105. int realHealth = 0;
  106. C_BasePlayer *local = C_BasePlayer::GetLocalPlayer();
  107. if ( local )
  108. {
  109. // Never below zero
  110. realHealth = MAX( local->GetHealth(), 0 );
  111. }
  112. // Only update the fade if we've changed health
  113. if ( realHealth == m_iHealth )
  114. {
  115. return;
  116. }
  117. if( realHealth > m_iHealth)
  118. {
  119. // round restarted, we have 100 again
  120. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("HealthRestored");
  121. }
  122. else if ( realHealth <= 25 )
  123. {
  124. // we are badly injured
  125. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("HealthLow");
  126. }
  127. else if( realHealth < m_iHealth )
  128. {
  129. // took a hit
  130. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("HealthTookDamage");
  131. }
  132. m_iHealth = realHealth;
  133. SetDisplayValue(m_iHealth);
  134. }
  135. void CHudHealth::Paint( void )
  136. {
  137. if( m_pHealthIcon )
  138. {
  139. m_pHealthIcon->DrawSelf( icon_xpos, icon_ypos, icon_wide, icon_tall, GetFgColor() );
  140. }
  141. //draw the health icon
  142. BaseClass::Paint();
  143. }