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.

191 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 <KeyValues.h>
  19. #include <vgui/ISurface.h>
  20. #include <vgui/ISystem.h>
  21. #include <vgui_controls/AnimationController.h>
  22. #include <vgui_controls/Panel.h>
  23. using namespace vgui;
  24. #include "hudelement.h"
  25. #include "convar.h"
  26. #include "c_dod_player.h"
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Health panel
  29. //-----------------------------------------------------------------------------
  30. class CHudHealth : public CHudElement, public vgui::Panel
  31. {
  32. DECLARE_CLASS_SIMPLE( CHudHealth, vgui::Panel );
  33. public:
  34. CHudHealth( const char *pElementName );
  35. virtual void Init( void );
  36. virtual void VidInit( void );
  37. virtual void Reset( void );
  38. virtual void OnThink();
  39. virtual bool ShouldDraw( void );
  40. virtual void Paint( void );
  41. virtual void ApplySchemeSettings( IScheme *scheme );
  42. private:
  43. int m_iHealth;
  44. int m_iStamina;
  45. CHudTexture *m_pIconHealthBar;
  46. CHudTexture *m_pIconHealthOverlay;
  47. CHudTexture *m_pIconBG;
  48. CHudTexture *m_pIconStaminaBar;
  49. };
  50. //DECLARE_HUDELEMENT( CHudHealth );
  51. //-----------------------------------------------------------------------------
  52. // Purpose: Constructor
  53. //-----------------------------------------------------------------------------
  54. CHudHealth::CHudHealth( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudHealth")
  55. {
  56. SetParent( g_pClientMode->GetViewport() );
  57. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose:
  61. //-----------------------------------------------------------------------------
  62. void CHudHealth::Init()
  63. {
  64. m_iHealth = 100;
  65. }
  66. void CHudHealth::ApplySchemeSettings( IScheme *scheme )
  67. {
  68. BaseClass::ApplySchemeSettings( scheme );
  69. if( !m_pIconHealthBar )
  70. {
  71. m_pIconHealthBar = gHUD.GetIcon( "hud_healthbar" );
  72. }
  73. if( !m_pIconHealthOverlay )
  74. {
  75. m_pIconHealthOverlay = gHUD.GetIcon( "hud_health_overlay" );
  76. }
  77. if( !m_pIconBG )
  78. {
  79. m_pIconBG = gHUD.GetIcon( "hud_main" );
  80. }
  81. if( !m_pIconStaminaBar )
  82. {
  83. m_pIconStaminaBar = gHUD.GetIcon( "hud_staminabar" );
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. //-----------------------------------------------------------------------------
  89. void CHudHealth::Reset()
  90. {
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose:
  94. //-----------------------------------------------------------------------------
  95. void CHudHealth::VidInit()
  96. {
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose:
  100. //-----------------------------------------------------------------------------
  101. void CHudHealth::OnThink()
  102. {
  103. int realHealth = 0;
  104. C_DODPlayer *local = C_DODPlayer::GetLocalDODPlayer();
  105. if ( local )
  106. {
  107. // Never below zero
  108. realHealth = MAX( local->GetHealth(), 0 );
  109. m_iStamina = local->m_Shared.GetStamina();
  110. }
  111. // Only update the fade if we've changed health
  112. if ( realHealth == m_iHealth )
  113. {
  114. return;
  115. }
  116. #ifdef _DEBUG
  117. g_pClientMode->GetViewportAnimationController()->SetAutoReloadScript(true);
  118. #endif
  119. m_iHealth = realHealth;
  120. }
  121. bool CHudHealth::ShouldDraw( void )
  122. {
  123. // No local player yet?
  124. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  125. if ( !pPlayer )
  126. return false;
  127. if( pPlayer->GetTeamNumber() != TEAM_ALLIES &&
  128. pPlayer->GetTeamNumber() != TEAM_AXIS )
  129. return false;
  130. return CHudElement::ShouldDraw();
  131. }
  132. void CHudHealth::Paint( void )
  133. {
  134. int x, y, w, h;
  135. GetBounds( x, y, w, h );
  136. Color clrIcon(255,255,255,255);
  137. int xpos = 0;
  138. int ypos = h - m_pIconBG->Height();
  139. m_pIconBG->DrawSelf( xpos, ypos, clrIcon );
  140. int nOffset = m_pIconHealthOverlay->Height() * ( 1.0 - ( (float)m_iHealth / 100.0 ) );
  141. if ( nOffset < m_pIconHealthOverlay->Height() )
  142. {
  143. m_pIconHealthOverlay->DrawSelfCropped( xpos + 55, ypos + 27, 0, 0, m_pIconHealthOverlay->Width(), nOffset, clrIcon );
  144. }
  145. nOffset = m_pIconHealthBar->Height() * ( 1.0 - ( (float)m_iHealth / 100.0 ) );
  146. if ( nOffset < m_pIconHealthBar->Height() )
  147. {
  148. m_pIconHealthBar->DrawSelfCropped( xpos + 99, ypos + 27 + nOffset, 0, nOffset, m_pIconHealthBar->Width(), m_pIconHealthBar->Height() - nOffset, clrIcon );
  149. }
  150. nOffset = m_pIconStaminaBar->Height() * ( 1.0 - ( (float)m_iStamina / 100.0 ) );
  151. if ( nOffset < m_pIconStaminaBar->Height() )
  152. {
  153. m_pIconStaminaBar->DrawSelfCropped( xpos + 8, ypos + 12 + nOffset, 0, nOffset, m_pIconStaminaBar->Width(), m_pIconStaminaBar->Height() - nOffset, clrIcon );
  154. }
  155. BaseClass::Paint();
  156. }