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.

147 lines
4.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //
  9. // battery.cpp
  10. //
  11. // implementation of CHudBattery class
  12. //
  13. #include "cbase.h"
  14. #include "hud.h"
  15. #include "hudelement.h"
  16. #include "hud_macros.h"
  17. #include "hud_numericdisplay.h"
  18. #include "iclientmode.h"
  19. #include "vgui_controls/AnimationController.h"
  20. #include "vgui/ILocalize.h"
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. #define INIT_BAT -1
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Displays suit power (armor) on hud
  26. //-----------------------------------------------------------------------------
  27. class CHudBattery : public CHudNumericDisplay, public CHudElement
  28. {
  29. DECLARE_CLASS_SIMPLE( CHudBattery, CHudNumericDisplay );
  30. public:
  31. CHudBattery( const char *pElementName );
  32. void Init( void );
  33. void Reset( void );
  34. void VidInit( void );
  35. void OnThink( void );
  36. void MsgFunc_Battery(bf_read &msg );
  37. bool ShouldDraw();
  38. private:
  39. int m_iBat;
  40. int m_iNewBat;
  41. };
  42. DECLARE_HUDELEMENT( CHudBattery );
  43. DECLARE_HUD_MESSAGE( CHudBattery, Battery );
  44. //-----------------------------------------------------------------------------
  45. // Purpose: Constructor
  46. //-----------------------------------------------------------------------------
  47. CHudBattery::CHudBattery( const char *pElementName ) : BaseClass(NULL, "HudSuit"), CHudElement( pElementName )
  48. {
  49. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_NEEDSUIT );
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Purpose:
  53. //-----------------------------------------------------------------------------
  54. void CHudBattery::Init( void )
  55. {
  56. HOOK_HUD_MESSAGE( CHudBattery, Battery);
  57. Reset();
  58. m_iBat = INIT_BAT;
  59. m_iNewBat = 0;
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. void CHudBattery::Reset( void )
  65. {
  66. SetLabelText(g_pVGuiLocalize->Find("#Valve_Hud_SUIT"));
  67. SetDisplayValue(m_iBat);
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. //-----------------------------------------------------------------------------
  72. void CHudBattery::VidInit( void )
  73. {
  74. Reset();
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose: Save CPU cycles by letting the HUD system early cull
  78. // costly traversal. Called per frame, return true if thinking and
  79. // painting need to occur.
  80. //-----------------------------------------------------------------------------
  81. bool CHudBattery::ShouldDraw( void )
  82. {
  83. bool bNeedsDraw = ( m_iBat != m_iNewBat ) || ( GetAlpha() > 0 );
  84. return ( bNeedsDraw && CHudElement::ShouldDraw() );
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. //-----------------------------------------------------------------------------
  89. void CHudBattery::OnThink( void )
  90. {
  91. if ( m_iBat == m_iNewBat )
  92. return;
  93. if ( !m_iNewBat )
  94. {
  95. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitPowerZero");
  96. }
  97. else if ( m_iNewBat < m_iBat )
  98. {
  99. // battery power has decreased, so play the damaged animation
  100. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitDamageTaken");
  101. // play an extra animation if we're super low
  102. if ( m_iNewBat < 20 )
  103. {
  104. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitArmorLow");
  105. }
  106. }
  107. else
  108. {
  109. // battery power has increased (if we had no previous armor, or if we just loaded the game, don't use alert state)
  110. if ( m_iBat == INIT_BAT || m_iBat == 0 || m_iNewBat >= 20)
  111. {
  112. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitPowerIncreasedAbove20");
  113. }
  114. else
  115. {
  116. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitPowerIncreasedBelow20");
  117. }
  118. }
  119. m_iBat = m_iNewBat;
  120. SetDisplayValue(m_iBat);
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose:
  124. //-----------------------------------------------------------------------------
  125. void CHudBattery::MsgFunc_Battery( bf_read &msg )
  126. {
  127. m_iNewBat = msg.ReadShort();
  128. }