summaryrefslogtreecommitdiff
path: root/game/client/tf/tf_hud_health.cpp
diff options
context:
space:
mode:
authorFluorescentCIAAfricanAmerican <[email protected]>2020-04-22 12:56:21 -0400
committerFluorescentCIAAfricanAmerican <[email protected]>2020-04-22 12:56:21 -0400
commit3bf9df6b2785fa6d951086978a3e66f49427166a (patch)
tree2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /game/client/tf/tf_hud_health.cpp
downloadarchived-source-engine-2018-hl2-src-master.tar.xz
archived-source-engine-2018-hl2-src-master.zip
Diffstat (limited to 'game/client/tf/tf_hud_health.cpp')
-rw-r--r--game/client/tf/tf_hud_health.cpp313
1 files changed, 313 insertions, 0 deletions
diff --git a/game/client/tf/tf_hud_health.cpp b/game/client/tf/tf_hud_health.cpp
new file mode 100644
index 0000000..2d02c37
--- /dev/null
+++ b/game/client/tf/tf_hud_health.cpp
@@ -0,0 +1,313 @@
+//========= Copyright Valve Corporation, All rights reserved. ============//
+//
+// Purpose: Implementation of CHudHealth class.
+//
+//=============================================================================//
+#include "cbase.h"
+#include "hud.h"
+#include "hud_macros.h"
+#include "view.h"
+
+#include "iclientmode.h"
+
+#include <KeyValues.h>
+#include <vgui/ISurface.h>
+#include <vgui/ISystem.h>
+#include <vgui_controls/AnimationController.h>
+#include <vgui_controls/Panel.h>
+
+using namespace vgui;
+
+#include "hudelement.h"
+#include "hud_numericdisplay.h"
+#include "ConVar.h"
+#include "c_tf_player.h"
+
+//=============================================================================
+//
+// TF Health Hud
+//
+class CHudHealth : public CHudElement, public CHudNumericDisplay
+{
+ DECLARE_CLASS_SIMPLE( CHudHealth, CHudNumericDisplay );
+
+public:
+
+ CHudHealth( const char *pElementName );
+ virtual void Init( void );
+ virtual void ApplySchemeSettings( IScheme *scheme );
+ virtual void Reset( void );
+
+ virtual void OnThink();
+
+ virtual bool ShouldDraw();
+ virtual void Paint( void );
+
+private:
+
+ int m_nHealth;
+
+ CHudTexture *m_pHealthIcon;
+ float m_flHealthIconWidth;
+ float m_flHealthIconHeight;
+
+ CPanelAnimationVar( vgui::HFont, m_hHealthFont, "NumberFont", "HudNumbers" );
+ CPanelAnimationVarAliasType( float, health_xpos, "digit_xpos", "50", "proportional_float" );
+ CPanelAnimationVarAliasType( float, health_ypos, "digit_ypos", "2", "proportional_float" );
+};
+
+DECLARE_HUDELEMENT( CHudHealth );
+
+//-----------------------------------------------------------------------------
+// Purpose: Constructor
+//-----------------------------------------------------------------------------
+CHudHealth::CHudHealth( const char *pElementName ) : CHudElement( pElementName ), CHudNumericDisplay( NULL, "HudHealth" )
+{
+ m_nHealth = 0;
+
+ m_pHealthIcon = NULL;
+ m_flHealthIconHeight = 0;
+ m_flHealthIconWidth = 0;
+
+ SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+void CHudHealth::Init()
+{
+ C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
+ if ( !pTFPlayer )
+ return;
+
+ m_nHealth = pTFPlayer->GetHealth();
+ SetDisplayValue( m_nHealth );
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+void CHudHealth::ApplySchemeSettings( IScheme *scheme )
+{
+ // Fall back to base.
+ BaseClass::ApplySchemeSettings( scheme );
+
+ // Setup the health icon.
+ if( !m_pHealthIcon )
+ {
+ m_pHealthIcon = gHUD.GetIcon( "health_icon" );
+ }
+
+ if( m_pHealthIcon )
+ {
+ m_flHealthIconHeight = GetTall() - YRES( 2 );
+ float flScale = m_flHealthIconHeight / static_cast<float>( m_pHealthIcon->Height() );
+ m_flHealthIconWidth = flScale * static_cast<float>( m_pHealthIcon->Width() );
+ }
+}
+
+//-----------------------------------------------------------------------------
+// Purpose: reset health to normal color at round restart
+//-----------------------------------------------------------------------------
+void CHudHealth::Reset()
+{
+ g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HealthRestored" );
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+void CHudHealth::OnThink()
+{
+ C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
+ if ( !pPlayer )
+ return;
+
+ // Never below zero.
+ int nHealth = MAX( pPlayer->GetHealth(), 0 );
+
+ // Only update the fade if we've changed health
+ if ( nHealth == m_nHealth )
+ return;
+
+ // We are badly injured.
+ if ( nHealth <= 25 )
+ {
+ g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HealthLow" );
+ }
+ // Got hit.
+ else if( nHealth < m_nHealth )
+ {
+ g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HealthTookDamage" );
+ }
+ // Gained health.
+ else if( nHealth > m_nHealth )
+ {
+ g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HealthRestored" );
+ }
+
+ m_nHealth = nHealth;
+// SetDisplayValue( m_nHealth );
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+bool CHudHealth::ShouldDraw()
+{
+ C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
+ if ( !pTFPlayer )
+ return false;
+
+ return !pTFPlayer->IsObserver();
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+void CHudHealth::Paint( void )
+{
+ C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
+ if ( !pTFPlayer )
+ return;
+
+ // Get the team color.
+ Color teamColor;
+ pTFPlayer->GetTeamColor( teamColor );
+
+ // Draw the health icon.
+ if( m_pHealthIcon )
+ {
+ m_pHealthIcon->DrawSelf( 0, 2, m_flHealthIconWidth, m_flHealthIconHeight, teamColor );
+ }
+
+ // Primary grenade count.
+ vgui::surface()->DrawSetTextColor( teamColor );
+ PaintNumbers( m_hHealthFont, health_xpos, health_ypos, m_nHealth );
+
+ PaintLabel();
+}
+
+//=============================================================================
+//
+// TF Armor Hud
+//
+class CHudArmor : public CHudElement, public CHudNumericDisplay
+{
+public:
+
+ DECLARE_CLASS_SIMPLE( CHudArmor, CHudNumericDisplay );
+
+ CHudArmor( const char *pName );
+ virtual void Init();
+ virtual void ApplySchemeSettings( IScheme *scheme );
+
+ virtual void OnThink();
+
+ virtual bool ShouldDraw();
+ virtual void Paint();
+
+private:
+
+ int m_nArmor;
+
+ CHudTexture *m_pArmorIcon;
+ float m_flArmorIconWidth;
+ float m_flArmorIconHeight;
+};
+
+
+DECLARE_HUDELEMENT( CHudArmor );
+
+//-----------------------------------------------------------------------------
+// Purpose: Constructor.
+//-----------------------------------------------------------------------------
+CHudArmor::CHudArmor( const char *pName ) : CHudNumericDisplay( NULL, "HudArmor" ), CHudElement( pName )
+{
+ m_nArmor = 0;
+
+ m_pArmorIcon = NULL;
+ m_flArmorIconHeight = 0;
+ m_flArmorIconWidth = 0;
+
+ SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+void CHudArmor::Init()
+{
+ C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
+ if ( !pTFPlayer )
+ return;
+
+ m_nArmor = pTFPlayer->ArmorValue();
+ SetDisplayValue( m_nArmor );
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+void CHudArmor::ApplySchemeSettings( IScheme *scheme )
+{
+ // Fall back to base.
+ BaseClass::ApplySchemeSettings( scheme );
+
+ // Setup the health icon.
+ if( !m_pArmorIcon )
+ {
+ m_pArmorIcon = gHUD.GetIcon( "shield_bright" );
+ }
+
+ if( m_pArmorIcon )
+ {
+ m_flArmorIconHeight = GetTall() - YRES( 2 );
+ float flScale = m_flArmorIconHeight / static_cast<float>( m_pArmorIcon->Height() );
+ m_flArmorIconWidth = flScale * static_cast<float>( m_pArmorIcon->Width() );
+ }
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+void CHudArmor::OnThink()
+{
+ C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
+ if ( !pTFPlayer )
+ return;
+
+ m_nArmor = pTFPlayer->ArmorValue();
+ SetDisplayValue( m_nArmor );
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+bool CHudArmor::ShouldDraw()
+{
+ C_TFPlayer *pTFPlayer = ToTFPlayer( C_BasePlayer::GetLocalPlayer() );
+ if ( !pTFPlayer )
+ return false;
+
+ // Running the experiment - no armor!
+ return ( pTFPlayer->ArmorValue() != 0 );
+
+ return !pTFPlayer->IsObserver();
+}
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+void CHudArmor::Paint()
+{
+ // Draw the armor icon.
+ if( m_pArmorIcon )
+ {
+ m_pArmorIcon->DrawSelf( 0, 2, m_flArmorIconWidth, m_flArmorIconHeight, GetFgColor() );
+ }
+
+ // Base class paint.
+ BaseClass::Paint();
+}