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.

225 lines
4.8 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. #include "hl1_hud_numbers.h"
  19. #include <KeyValues.h>
  20. #include <vgui/ISurface.h>
  21. #include <vgui/ISystem.h>
  22. #include <vgui_controls/Panel.h>
  23. using namespace vgui;
  24. #include "hudelement.h"
  25. #include "convar.h"
  26. #define INIT_HEALTH -1
  27. #define FADE_TIME 100
  28. #define MIN_ALPHA 100
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Health panel
  31. //-----------------------------------------------------------------------------
  32. class CHudHealth : public CHudElement, public CHL1HudNumbers
  33. {
  34. DECLARE_CLASS_SIMPLE( CHudHealth, CHL1HudNumbers );
  35. public:
  36. CHudHealth( const char *pElementName );
  37. void Init( void );
  38. void VidInit( void );
  39. void Reset( void );
  40. void OnThink();
  41. void MsgFunc_Damage(bf_read &msg);
  42. private:
  43. void Paint( void );
  44. void ApplySchemeSettings(vgui::IScheme *pScheme);
  45. private:
  46. CHudTexture *icon_cross;
  47. int m_iHealth;
  48. float m_flFade;
  49. int m_bitsDamage;
  50. };
  51. DECLARE_HUDELEMENT( CHudHealth );
  52. DECLARE_HUD_MESSAGE( CHudHealth, Damage );
  53. //-----------------------------------------------------------------------------
  54. // Purpose: Constructor
  55. //-----------------------------------------------------------------------------
  56. CHudHealth::CHudHealth( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudHealth")
  57. {
  58. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose:
  62. //-----------------------------------------------------------------------------
  63. void CHudHealth::Init()
  64. {
  65. HOOK_HUD_MESSAGE( CHudHealth, Damage );
  66. Reset();
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Purpose:
  70. //-----------------------------------------------------------------------------
  71. void CHudHealth::Reset()
  72. {
  73. m_iHealth = INIT_HEALTH;
  74. m_flFade = 0;
  75. m_bitsDamage = 0;
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Purpose:
  79. //-----------------------------------------------------------------------------
  80. void CHudHealth::VidInit()
  81. {
  82. Reset();
  83. BaseClass::VidInit();
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose:
  87. //-----------------------------------------------------------------------------
  88. void CHudHealth::OnThink()
  89. {
  90. int x = 0;
  91. C_BasePlayer *local = C_BasePlayer::GetLocalPlayer();
  92. if ( local )
  93. {
  94. // Never below zero
  95. x = MAX( local->GetHealth(), 0 );
  96. }
  97. // Only update the fade if we've changed health
  98. if ( x == m_iHealth )
  99. {
  100. return;
  101. }
  102. m_flFade = FADE_TIME;
  103. m_iHealth = x;
  104. }
  105. void CHudHealth::Paint()
  106. {
  107. Color clrHealth;
  108. int a;
  109. int x;
  110. int y;
  111. BaseClass::Paint();
  112. if ( !icon_cross )
  113. {
  114. icon_cross = gHUD.GetIcon( "cross" );
  115. }
  116. if ( !icon_cross )
  117. {
  118. return;
  119. }
  120. // Has health changed? Flash the health #
  121. if ( m_flFade )
  122. {
  123. m_flFade -= ( gpGlobals->frametime * 20 );
  124. if ( m_flFade <= 0 )
  125. {
  126. a = MIN_ALPHA;
  127. m_flFade = 0;
  128. }
  129. else
  130. {
  131. // Fade the health number back to dim
  132. a = MIN_ALPHA + ( m_flFade / FADE_TIME ) * 128;
  133. }
  134. }
  135. else
  136. {
  137. a = MIN_ALPHA;
  138. }
  139. // If health is getting low, make it bright red
  140. if ( m_iHealth <= 15 )
  141. a = 255;
  142. if (m_iHealth > 25)
  143. {
  144. int r, g, b, nUnused;
  145. (gHUD.m_clrYellowish).GetColor( r, g, b, nUnused );
  146. clrHealth.SetColor( r, g, b, a );
  147. }
  148. else
  149. {
  150. clrHealth.SetColor( 250, 0, 0, a );
  151. }
  152. int nFontWidth = GetNumberFontWidth();
  153. int nFontHeight = GetNumberFontHeight();
  154. int nCrossWidth = icon_cross->Width();
  155. x = nCrossWidth / 2;
  156. y = GetTall() - ( nFontHeight * 1.5 );
  157. icon_cross->DrawSelf( x, y, clrHealth );
  158. x = nCrossWidth + ( nFontWidth / 2 );
  159. x = DrawHudNumber( x, y, m_iHealth, clrHealth );
  160. x += nFontWidth / 2;
  161. int iHeight = nFontHeight;
  162. int iWidth = nFontWidth / 10;
  163. clrHealth.SetColor( 255, 160, 0, a );
  164. vgui::surface()->DrawSetColor( clrHealth );
  165. vgui::surface()->DrawFilledRect( x, y, x + iWidth, y + iHeight );
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Purpose:
  169. //-----------------------------------------------------------------------------
  170. void CHudHealth::MsgFunc_Damage(bf_read &msg)
  171. {
  172. msg.ReadByte(); // armor
  173. msg.ReadByte(); // health
  174. msg.ReadLong(); // damage bits
  175. Vector vecFrom;
  176. vecFrom.x = msg.ReadBitCoord();
  177. vecFrom.y = msg.ReadBitCoord();
  178. vecFrom.z = msg.ReadBitCoord();
  179. }
  180. void CHudHealth::ApplySchemeSettings(vgui::IScheme *pScheme)
  181. {
  182. BaseClass::ApplySchemeSettings(pScheme);
  183. SetPaintBackgroundEnabled(false);
  184. }