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.

257 lines
7.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "hud.h"
  9. #include "hud_suitpower.h"
  10. #include "hud_macros.h"
  11. #include "c_basehlplayer.h"
  12. #include "iclientmode.h"
  13. #include <vgui_controls/AnimationController.h>
  14. #include <vgui/ISurface.h>
  15. #include <vgui/ILocalize.h>
  16. using namespace vgui;
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. DECLARE_HUDELEMENT( CHudSuitPower );
  20. #define SUITPOWER_INIT -1
  21. //-----------------------------------------------------------------------------
  22. // Purpose: Constructor
  23. //-----------------------------------------------------------------------------
  24. CHudSuitPower::CHudSuitPower( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudSuitPower" )
  25. {
  26. vgui::Panel *pParent = g_pClientMode->GetViewport();
  27. SetParent( pParent );
  28. SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Purpose:
  32. //-----------------------------------------------------------------------------
  33. void CHudSuitPower::Init( void )
  34. {
  35. m_flSuitPower = SUITPOWER_INIT;
  36. m_nSuitPowerLow = -1;
  37. m_iActiveSuitDevices = 0;
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Purpose:
  41. //-----------------------------------------------------------------------------
  42. void CHudSuitPower::Reset( void )
  43. {
  44. Init();
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose: Save CPU cycles by letting the HUD system early cull
  48. // costly traversal. Called per frame, return true if thinking and
  49. // painting need to occur.
  50. //-----------------------------------------------------------------------------
  51. bool CHudSuitPower::ShouldDraw()
  52. {
  53. bool bNeedsDraw = false;
  54. C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
  55. if ( !pPlayer )
  56. return false;
  57. // needs draw if suit power changed or animation in progress
  58. bNeedsDraw = ( ( pPlayer->m_HL2Local.m_flSuitPower != m_flSuitPower ) || ( m_AuxPowerColor[3] > 0 ) );
  59. return ( bNeedsDraw && CHudElement::ShouldDraw() );
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. void CHudSuitPower::OnThink( void )
  65. {
  66. float flCurrentPower = 0;
  67. C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
  68. if ( !pPlayer )
  69. return;
  70. flCurrentPower = pPlayer->m_HL2Local.m_flSuitPower;
  71. // Only update if we've changed suit power
  72. if ( flCurrentPower == m_flSuitPower )
  73. return;
  74. if ( flCurrentPower >= 100.0f && m_flSuitPower < 100.0f )
  75. {
  76. // we've reached max power
  77. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerMax");
  78. }
  79. else if ( flCurrentPower < 100.0f && (m_flSuitPower >= 100.0f || m_flSuitPower == SUITPOWER_INIT) )
  80. {
  81. // we've lost power
  82. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerNotMax");
  83. }
  84. bool flashlightActive = pPlayer->IsFlashlightActive();
  85. bool sprintActive = pPlayer->IsSprinting();
  86. bool breatherActive = pPlayer->IsBreatherActive();
  87. int activeDevices = (int)flashlightActive + (int)sprintActive + (int)breatherActive;
  88. if (activeDevices != m_iActiveSuitDevices)
  89. {
  90. m_iActiveSuitDevices = activeDevices;
  91. switch ( m_iActiveSuitDevices )
  92. {
  93. default:
  94. case 3:
  95. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerThreeItemsActive");
  96. break;
  97. case 2:
  98. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerTwoItemsActive");
  99. break;
  100. case 1:
  101. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerOneItemActive");
  102. break;
  103. case 0:
  104. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerNoItemsActive");
  105. break;
  106. }
  107. }
  108. m_flSuitPower = flCurrentPower;
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose: draws the power bar
  112. //-----------------------------------------------------------------------------
  113. void CHudSuitPower::Paint()
  114. {
  115. C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
  116. if ( !pPlayer )
  117. return;
  118. // get bar chunks
  119. int chunkCount = m_flBarWidth / (m_flBarChunkWidth + m_flBarChunkGap);
  120. int enabledChunks = (int)((float)chunkCount * (m_flSuitPower * 1.0f/100.0f) + 0.5f );
  121. // see if we've changed power state
  122. int lowPower = 0;
  123. if (enabledChunks <= (chunkCount / 4))
  124. {
  125. lowPower = 1;
  126. }
  127. if (m_nSuitPowerLow != lowPower)
  128. {
  129. if (m_iActiveSuitDevices || m_flSuitPower < 100.0f)
  130. {
  131. if (lowPower)
  132. {
  133. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerDecreasedBelow25");
  134. }
  135. else
  136. {
  137. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerIncreasedAbove25");
  138. }
  139. m_nSuitPowerLow = lowPower;
  140. }
  141. }
  142. // draw the suit power bar
  143. surface()->DrawSetColor( m_AuxPowerColor );
  144. int xpos = m_flBarInsetX, ypos = m_flBarInsetY;
  145. for (int i = 0; i < enabledChunks; i++)
  146. {
  147. surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight );
  148. xpos += (m_flBarChunkWidth + m_flBarChunkGap);
  149. }
  150. // draw the exhausted portion of the bar.
  151. surface()->DrawSetColor( Color( m_AuxPowerColor[0], m_AuxPowerColor[1], m_AuxPowerColor[2], m_iAuxPowerDisabledAlpha ) );
  152. for (int i = enabledChunks; i < chunkCount; i++)
  153. {
  154. surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight );
  155. xpos += (m_flBarChunkWidth + m_flBarChunkGap);
  156. }
  157. // draw our name
  158. surface()->DrawSetTextFont(m_hTextFont);
  159. surface()->DrawSetTextColor(m_AuxPowerColor);
  160. surface()->DrawSetTextPos(text_xpos, text_ypos);
  161. wchar_t *tempString = g_pVGuiLocalize->Find("#Valve_Hud_AUX_POWER");
  162. if (tempString)
  163. {
  164. surface()->DrawPrintText(tempString, wcslen(tempString));
  165. }
  166. else
  167. {
  168. surface()->DrawPrintText(L"AUX POWER", wcslen(L"AUX POWER"));
  169. }
  170. if ( m_iActiveSuitDevices )
  171. {
  172. // draw the additional text
  173. int ypos = text2_ypos;
  174. if (pPlayer->IsBreatherActive())
  175. {
  176. tempString = g_pVGuiLocalize->Find("#Valve_Hud_OXYGEN");
  177. surface()->DrawSetTextPos(text2_xpos, ypos);
  178. if (tempString)
  179. {
  180. surface()->DrawPrintText(tempString, wcslen(tempString));
  181. }
  182. else
  183. {
  184. surface()->DrawPrintText(L"OXYGEN", wcslen(L"OXYGEN"));
  185. }
  186. ypos += text2_gap;
  187. }
  188. if (pPlayer->IsFlashlightActive())
  189. {
  190. tempString = g_pVGuiLocalize->Find("#Valve_Hud_FLASHLIGHT");
  191. surface()->DrawSetTextPos(text2_xpos, ypos);
  192. if (tempString)
  193. {
  194. surface()->DrawPrintText(tempString, wcslen(tempString));
  195. }
  196. else
  197. {
  198. surface()->DrawPrintText(L"FLASHLIGHT", wcslen(L"FLASHLIGHT"));
  199. }
  200. ypos += text2_gap;
  201. }
  202. if (pPlayer->IsSprinting())
  203. {
  204. tempString = g_pVGuiLocalize->Find("#Valve_Hud_SPRINT");
  205. surface()->DrawSetTextPos(text2_xpos, ypos);
  206. if (tempString)
  207. {
  208. surface()->DrawPrintText(tempString, wcslen(tempString));
  209. }
  210. else
  211. {
  212. surface()->DrawPrintText(L"SPRINT", wcslen(L"SPRINT"));
  213. }
  214. ypos += text2_gap;
  215. }
  216. }
  217. }