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.

169 lines
4.2 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/ILocalize.h>
  23. using namespace vgui;
  24. #include "hudelement.h"
  25. #include "hud_numericdisplay.h"
  26. #include "convar.h"
  27. // memdbgon must be the last include file in a .cpp file!!!
  28. #include "tier0/memdbgon.h"
  29. #define INIT_HEALTH -1
  30. //-----------------------------------------------------------------------------
  31. // Purpose: Health panel
  32. //-----------------------------------------------------------------------------
  33. class CHudHealth : public CHudElement, public CHudNumericDisplay
  34. {
  35. DECLARE_CLASS_SIMPLE( CHudHealth, CHudNumericDisplay );
  36. public:
  37. CHudHealth( const char *pElementName );
  38. virtual void Init( void );
  39. virtual void VidInit( void );
  40. virtual void Reset( void );
  41. virtual void OnThink();
  42. void MsgFunc_Damage( bf_read &msg );
  43. private:
  44. // old variables
  45. int m_iHealth;
  46. int m_bitsDamage;
  47. };
  48. DECLARE_HUDELEMENT( CHudHealth );
  49. DECLARE_HUD_MESSAGE( CHudHealth, Damage );
  50. //-----------------------------------------------------------------------------
  51. // Purpose: Constructor
  52. //-----------------------------------------------------------------------------
  53. CHudHealth::CHudHealth( const char *pElementName ) : CHudElement( pElementName ), CHudNumericDisplay(NULL, "HudHealth")
  54. {
  55. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose:
  59. //-----------------------------------------------------------------------------
  60. void CHudHealth::Init()
  61. {
  62. HOOK_HUD_MESSAGE( CHudHealth, Damage );
  63. Reset();
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose:
  67. //-----------------------------------------------------------------------------
  68. void CHudHealth::Reset()
  69. {
  70. m_iHealth = INIT_HEALTH;
  71. m_bitsDamage = 0;
  72. wchar_t *tempString = g_pVGuiLocalize->Find("#Valve_Hud_HEALTH");
  73. if (tempString)
  74. {
  75. SetLabelText(tempString);
  76. }
  77. else
  78. {
  79. SetLabelText(L"HEALTH");
  80. }
  81. SetDisplayValue(m_iHealth);
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose:
  85. //-----------------------------------------------------------------------------
  86. void CHudHealth::VidInit()
  87. {
  88. Reset();
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose:
  92. //-----------------------------------------------------------------------------
  93. void CHudHealth::OnThink()
  94. {
  95. int newHealth = 0;
  96. C_BasePlayer *local = C_BasePlayer::GetLocalPlayer();
  97. if ( local )
  98. {
  99. // Never below zero
  100. newHealth = MAX( local->GetHealth(), 0 );
  101. }
  102. // Only update the fade if we've changed health
  103. if ( newHealth == m_iHealth )
  104. {
  105. return;
  106. }
  107. m_iHealth = newHealth;
  108. if ( m_iHealth >= 20 )
  109. {
  110. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("HealthIncreasedAbove20");
  111. }
  112. else if ( m_iHealth > 0 )
  113. {
  114. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("HealthIncreasedBelow20");
  115. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("HealthLow");
  116. }
  117. SetDisplayValue(m_iHealth);
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose:
  121. //-----------------------------------------------------------------------------
  122. void CHudHealth::MsgFunc_Damage( bf_read &msg )
  123. {
  124. int armor = msg.ReadByte(); // armor
  125. int damageTaken = msg.ReadByte(); // health
  126. long bitsDamage = msg.ReadLong(); // damage bits
  127. bitsDamage; // variable still sent but not used
  128. Vector vecFrom;
  129. vecFrom.x = msg.ReadBitCoord();
  130. vecFrom.y = msg.ReadBitCoord();
  131. vecFrom.z = msg.ReadBitCoord();
  132. // Actually took damage?
  133. if ( damageTaken > 0 || armor > 0 )
  134. {
  135. if ( damageTaken > 0 )
  136. {
  137. // start the animation
  138. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("HealthDamageTaken");
  139. }
  140. }
  141. }