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.

193 lines
5.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "dodspectatorgui.h"
  9. #include "hud.h"
  10. #include "dod_shareddefs.h"
  11. #include <vgui/ILocalize.h>
  12. #include <vgui/ISurface.h>
  13. #include <imapoverview.h>
  14. #include "dod_gamerules.h"
  15. #include "c_team.h"
  16. #include "c_dod_team.h"
  17. #include "c_dod_player.h"
  18. #include "c_dod_playerresource.h"
  19. using namespace vgui;
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Constructor
  22. //-----------------------------------------------------------------------------
  23. CDODSpectatorGUI::CDODSpectatorGUI(IViewPort *pViewPort) : CSpectatorGUI(pViewPort)
  24. {
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. bool CDODSpectatorGUI::NeedsUpdate( void )
  30. {
  31. if ( !C_BasePlayer::GetLocalPlayer() )
  32. return false;
  33. if( IsVisible() )
  34. return true;
  35. //if ( DODGameRules()->IsGameUnderTimeLimit() && m_nLastTime != DODGameRules()->GetTimeLeft() )
  36. // return true;
  37. if ( m_nLastSpecMode != C_BasePlayer::GetLocalPlayer()->GetObserverMode() )
  38. return true;
  39. if ( m_nLastSpecTarget != C_BasePlayer::GetLocalPlayer()->GetObserverTarget() )
  40. return true;
  41. return BaseClass::NeedsUpdate();
  42. }
  43. Color CDODSpectatorGUI::GetClientColor(int index)
  44. {
  45. C_BasePlayer *player = ToBasePlayer( ClientEntityList().GetEnt( index) );
  46. int team = player->GetTeamNumber();
  47. Assert( team == TEAM_ALLIES || team == TEAM_AXIS || team == TEAM_SPECTATOR );
  48. if ( GameResources() )
  49. return GameResources()->GetTeamColor( team );
  50. else
  51. return Color( 255, 255, 255, 255 );
  52. }
  53. void CDODSpectatorGUI::Update()
  54. {
  55. BaseClass::Update();
  56. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  57. if( pLocalPlayer )
  58. {
  59. m_nLastSpecMode = pLocalPlayer->GetObserverMode();
  60. m_nLastSpecTarget = pLocalPlayer->GetObserverTarget();
  61. }
  62. UpdateTimer();
  63. UpdateScores();
  64. }
  65. void CDODSpectatorGUI::UpdateTimer( void )
  66. {
  67. C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();
  68. if( !pPlayer || pPlayer->IsHLTV() )
  69. {
  70. wchar_t wText[ 63 ];
  71. int timer;
  72. timer = (int)( DODGameRules()->GetTimeLeft() );
  73. if ( timer < 0 )
  74. timer = 0;
  75. _snwprintf ( wText, sizeof(wText)/sizeof(wchar_t), L"%d:%02d", (timer / 60), (timer % 60) );
  76. wText[62] = 0;
  77. SetDialogVariable( "timer", wText );
  78. SetDialogVariable( "reinforcements", wText );
  79. }
  80. else if( pPlayer->GetTeamNumber() == TEAM_SPECTATOR )
  81. {
  82. SetDialogVariable( "timer", L"" );
  83. SetDialogVariable( "reinforcements", L"" );
  84. }
  85. else
  86. {
  87. SetDialogVariable( "timer", L"" );
  88. // we need to know how much longer we are going to be in death cam
  89. // once we know that, we can ask dodgamerules if we are going to make the next
  90. // wave. If we aren't, gamerules can tell us the new time based on the reserve wave
  91. float flSpawnEligibleTime;
  92. if ( pPlayer->GetObserverMode() == OBS_MODE_DEATHCAM )
  93. {
  94. flSpawnEligibleTime = pPlayer->GetDeathTime() + DEATH_CAM_TIME;
  95. }
  96. else
  97. flSpawnEligibleTime = 0;
  98. //will never return negative seconds
  99. int timer = DODGameRules()->GetReinforcementTimerSeconds( pPlayer->GetTeamNumber(), flSpawnEligibleTime );
  100. if( timer < 0 || ( pPlayer->GetObserverMode() == OBS_MODE_DEATHCAM ) )
  101. {
  102. SetDialogVariable( "reinforcements", L"" );
  103. }
  104. else
  105. {
  106. char szMins[4], szSecs[4];
  107. int mins = timer / 60;
  108. int secs = timer % 60;
  109. Q_snprintf( szMins, sizeof(szMins), "%d", mins );
  110. Q_snprintf( szSecs, sizeof(szSecs), "%d", secs );
  111. wchar_t wMins[4], wSecs[4];
  112. g_pVGuiLocalize->ConvertANSIToUnicode(szMins, wMins, sizeof(wMins));
  113. g_pVGuiLocalize->ConvertANSIToUnicode(szSecs, wSecs, sizeof(wSecs));
  114. wchar_t wLabel[128];
  115. if ( mins == 1 ) //"1 minute"
  116. {
  117. g_pVGuiLocalize->ConstructString( wLabel, sizeof( wLabel ), g_pVGuiLocalize->Find("#Dod_Reinforcements_in_min" ), 2, wMins, wSecs );
  118. }
  119. else if ( mins > 0 ) //"2 minutes"
  120. {
  121. g_pVGuiLocalize->ConstructString( wLabel, sizeof( wLabel ), g_pVGuiLocalize->Find("#Dod_Reinforcements_in_mins" ), 2, wMins, wSecs );
  122. }
  123. else if ( secs == 1 ) //"1 second"
  124. {
  125. g_pVGuiLocalize->ConstructString( wLabel, sizeof( wLabel ), g_pVGuiLocalize->Find("#Dod_Reinforcements_in_sec" ), 1, wSecs );
  126. }
  127. else if ( secs == 0 ) //"Prepare to Respawn"
  128. {
  129. g_pVGuiLocalize->ConstructString( wLabel, sizeof( wLabel ), g_pVGuiLocalize->Find("#Dod_Reinforcements_prepare_to_respawn" ), 0 );
  130. }
  131. else //"2 seconds"
  132. {
  133. g_pVGuiLocalize->ConstructString( wLabel, sizeof( wLabel ), g_pVGuiLocalize->Find("#Dod_Reinforcements_in_secs" ), 1, wSecs );
  134. }
  135. SetDialogVariable( "reinforcements", wLabel );
  136. }
  137. }
  138. }
  139. void CDODSpectatorGUI::UpdateScores( void )
  140. {
  141. C_DODTeam *pAlliesTeam = static_cast<C_DODTeam *>( GetGlobalTeam(TEAM_ALLIES) );
  142. if ( pAlliesTeam )
  143. {
  144. SetDialogVariable( "alliesscore", pAlliesTeam->GetRoundsWon() );
  145. }
  146. C_DODTeam *pAxisTeam = static_cast<C_DODTeam *>( GetGlobalTeam(TEAM_AXIS) );
  147. if ( pAxisTeam )
  148. {
  149. SetDialogVariable( "axisscore", pAxisTeam->GetRoundsWon() );
  150. }
  151. }
  152. bool CDODSpectatorGUI::ShouldShowPlayerLabel( int specmode )
  153. {
  154. return ( (specmode == OBS_MODE_IN_EYE) ||
  155. (specmode == OBS_MODE_CHASE) ||
  156. (specmode == OBS_MODE_DEATHCAM) );
  157. }