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.

313 lines
8.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implementation of CHudHealth class.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud.h"
  8. #include "hud_macros.h"
  9. #include "view.h"
  10. #include "iclientmode.h"
  11. #include <KeyValues.h>
  12. #include <vgui/ISurface.h>
  13. #include <vgui/ISystem.h>
  14. #include <vgui_controls/AnimationController.h>
  15. #include <vgui_controls/Panel.h>
  16. using namespace vgui;
  17. #include "hudelement.h"
  18. #include "hud_numericdisplay.h"
  19. #include "ConVar.h"
  20. #include "c_tf_player.h"
  21. //=============================================================================
  22. //
  23. // TF Health Hud
  24. //
  25. class CHudHealth : public CHudElement, public CHudNumericDisplay
  26. {
  27. DECLARE_CLASS_SIMPLE( CHudHealth, CHudNumericDisplay );
  28. public:
  29. CHudHealth( const char *pElementName );
  30. virtual void Init( void );
  31. virtual void ApplySchemeSettings( IScheme *scheme );
  32. virtual void Reset( void );
  33. virtual void OnThink();
  34. virtual bool ShouldDraw();
  35. virtual void Paint( void );
  36. private:
  37. int m_nHealth;
  38. CHudTexture *m_pHealthIcon;
  39. float m_flHealthIconWidth;
  40. float m_flHealthIconHeight;
  41. CPanelAnimationVar( vgui::HFont, m_hHealthFont, "NumberFont", "HudNumbers" );
  42. CPanelAnimationVarAliasType( float, health_xpos, "digit_xpos", "50", "proportional_float" );
  43. CPanelAnimationVarAliasType( float, health_ypos, "digit_ypos", "2", "proportional_float" );
  44. };
  45. DECLARE_HUDELEMENT( CHudHealth );
  46. //-----------------------------------------------------------------------------
  47. // Purpose: Constructor
  48. //-----------------------------------------------------------------------------
  49. CHudHealth::CHudHealth( const char *pElementName ) : CHudElement( pElementName ), CHudNumericDisplay( NULL, "HudHealth" )
  50. {
  51. m_nHealth = 0;
  52. m_pHealthIcon = NULL;
  53. m_flHealthIconHeight = 0;
  54. m_flHealthIconWidth = 0;
  55. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose:
  59. //-----------------------------------------------------------------------------
  60. void CHudHealth::Init()
  61. {
  62. C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
  63. if ( !pTFPlayer )
  64. return;
  65. m_nHealth = pTFPlayer->GetHealth();
  66. SetDisplayValue( m_nHealth );
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Purpose:
  70. //-----------------------------------------------------------------------------
  71. void CHudHealth::ApplySchemeSettings( IScheme *scheme )
  72. {
  73. // Fall back to base.
  74. BaseClass::ApplySchemeSettings( scheme );
  75. // Setup the health icon.
  76. if( !m_pHealthIcon )
  77. {
  78. m_pHealthIcon = gHUD.GetIcon( "health_icon" );
  79. }
  80. if( m_pHealthIcon )
  81. {
  82. m_flHealthIconHeight = GetTall() - YRES( 2 );
  83. float flScale = m_flHealthIconHeight / static_cast<float>( m_pHealthIcon->Height() );
  84. m_flHealthIconWidth = flScale * static_cast<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::OnThink()
  98. {
  99. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  100. if ( !pPlayer )
  101. return;
  102. // Never below zero.
  103. int nHealth = MAX( pPlayer->GetHealth(), 0 );
  104. // Only update the fade if we've changed health
  105. if ( nHealth == m_nHealth )
  106. return;
  107. // We are badly injured.
  108. if ( nHealth <= 25 )
  109. {
  110. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HealthLow" );
  111. }
  112. // Got hit.
  113. else if( nHealth < m_nHealth )
  114. {
  115. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HealthTookDamage" );
  116. }
  117. // Gained health.
  118. else if( nHealth > m_nHealth )
  119. {
  120. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HealthRestored" );
  121. }
  122. m_nHealth = nHealth;
  123. // SetDisplayValue( m_nHealth );
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Purpose:
  127. //-----------------------------------------------------------------------------
  128. bool CHudHealth::ShouldDraw()
  129. {
  130. C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
  131. if ( !pTFPlayer )
  132. return false;
  133. return !pTFPlayer->IsObserver();
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Purpose:
  137. //-----------------------------------------------------------------------------
  138. void CHudHealth::Paint( void )
  139. {
  140. C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
  141. if ( !pTFPlayer )
  142. return;
  143. // Get the team color.
  144. Color teamColor;
  145. pTFPlayer->GetTeamColor( teamColor );
  146. // Draw the health icon.
  147. if( m_pHealthIcon )
  148. {
  149. m_pHealthIcon->DrawSelf( 0, 2, m_flHealthIconWidth, m_flHealthIconHeight, teamColor );
  150. }
  151. // Primary grenade count.
  152. vgui::surface()->DrawSetTextColor( teamColor );
  153. PaintNumbers( m_hHealthFont, health_xpos, health_ypos, m_nHealth );
  154. PaintLabel();
  155. }
  156. //=============================================================================
  157. //
  158. // TF Armor Hud
  159. //
  160. class CHudArmor : public CHudElement, public CHudNumericDisplay
  161. {
  162. public:
  163. DECLARE_CLASS_SIMPLE( CHudArmor, CHudNumericDisplay );
  164. CHudArmor( const char *pName );
  165. virtual void Init();
  166. virtual void ApplySchemeSettings( IScheme *scheme );
  167. virtual void OnThink();
  168. virtual bool ShouldDraw();
  169. virtual void Paint();
  170. private:
  171. int m_nArmor;
  172. CHudTexture *m_pArmorIcon;
  173. float m_flArmorIconWidth;
  174. float m_flArmorIconHeight;
  175. };
  176. DECLARE_HUDELEMENT( CHudArmor );
  177. //-----------------------------------------------------------------------------
  178. // Purpose: Constructor.
  179. //-----------------------------------------------------------------------------
  180. CHudArmor::CHudArmor( const char *pName ) : CHudNumericDisplay( NULL, "HudArmor" ), CHudElement( pName )
  181. {
  182. m_nArmor = 0;
  183. m_pArmorIcon = NULL;
  184. m_flArmorIconHeight = 0;
  185. m_flArmorIconWidth = 0;
  186. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );
  187. }
  188. //-----------------------------------------------------------------------------
  189. // Purpose:
  190. //-----------------------------------------------------------------------------
  191. void CHudArmor::Init()
  192. {
  193. C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
  194. if ( !pTFPlayer )
  195. return;
  196. m_nArmor = pTFPlayer->ArmorValue();
  197. SetDisplayValue( m_nArmor );
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Purpose:
  201. //-----------------------------------------------------------------------------
  202. void CHudArmor::ApplySchemeSettings( IScheme *scheme )
  203. {
  204. // Fall back to base.
  205. BaseClass::ApplySchemeSettings( scheme );
  206. // Setup the health icon.
  207. if( !m_pArmorIcon )
  208. {
  209. m_pArmorIcon = gHUD.GetIcon( "shield_bright" );
  210. }
  211. if( m_pArmorIcon )
  212. {
  213. m_flArmorIconHeight = GetTall() - YRES( 2 );
  214. float flScale = m_flArmorIconHeight / static_cast<float>( m_pArmorIcon->Height() );
  215. m_flArmorIconWidth = flScale * static_cast<float>( m_pArmorIcon->Width() );
  216. }
  217. }
  218. //-----------------------------------------------------------------------------
  219. // Purpose:
  220. //-----------------------------------------------------------------------------
  221. void CHudArmor::OnThink()
  222. {
  223. C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
  224. if ( !pTFPlayer )
  225. return;
  226. m_nArmor = pTFPlayer->ArmorValue();
  227. SetDisplayValue( m_nArmor );
  228. }
  229. //-----------------------------------------------------------------------------
  230. // Purpose:
  231. //-----------------------------------------------------------------------------
  232. bool CHudArmor::ShouldDraw()
  233. {
  234. C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
  235. if ( !pTFPlayer )
  236. return false;
  237. // Running the experiment - no armor!
  238. return ( pTFPlayer->ArmorValue() != 0 );
  239. return !pTFPlayer->IsObserver();
  240. }
  241. //-----------------------------------------------------------------------------
  242. // Purpose:
  243. //-----------------------------------------------------------------------------
  244. void CHudArmor::Paint()
  245. {
  246. // Draw the armor icon.
  247. if( m_pArmorIcon )
  248. {
  249. m_pArmorIcon->DrawSelf( 0, 2, m_flArmorIconWidth, m_flArmorIconHeight, GetFgColor() );
  250. }
  251. // Base class paint.
  252. BaseClass::Paint();
  253. }