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.

183 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Dod gamerules round timer
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "dod_round_timer.h"
  9. #ifdef CLIENT_DLL
  10. #include "iclientmode.h"
  11. #include "vgui_controls/AnimationController.h"
  12. #endif
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. #ifdef CLIENT_DLL
  16. // Use this proxy to flash the round timer whenever the timer is restarted
  17. // because trapping the round start event doesn't work ( the event also flushes
  18. // all hud events and obliterates our TimerFlash event )
  19. static void RecvProxy_TimerPaused( const CRecvProxyData *pData, void *pStruct, void *pOut )
  20. {
  21. CDODRoundTimer *pTimer = (CDODRoundTimer *) pStruct;
  22. bool bTimerPaused = ( pData->m_Value.m_Int > 0 );
  23. if ( bTimerPaused == false )
  24. {
  25. g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "TimerFlash" );
  26. }
  27. pTimer->InternalSetPaused( bTimerPaused );
  28. }
  29. #endif
  30. LINK_ENTITY_TO_CLASS( dod_round_timer, CDODRoundTimer );
  31. IMPLEMENT_NETWORKCLASS_ALIASED( DODRoundTimer, DT_DODRoundTimer )
  32. BEGIN_NETWORK_TABLE_NOBASE( CDODRoundTimer, DT_DODRoundTimer )
  33. #ifdef CLIENT_DLL
  34. RecvPropInt( RECVINFO( m_bTimerPaused ), 0, RecvProxy_TimerPaused ),
  35. RecvPropTime( RECVINFO( m_flTimeRemaining ) ),
  36. RecvPropTime( RECVINFO( m_flTimerEndTime ) ),
  37. #else
  38. SendPropBool( SENDINFO( m_bTimerPaused ) ),
  39. SendPropTime( SENDINFO( m_flTimeRemaining ) ),
  40. SendPropTime( SENDINFO( m_flTimerEndTime ) ),
  41. #endif
  42. END_NETWORK_TABLE()
  43. #ifdef CLIENT_DLL
  44. CDODRoundTimer *g_DODRoundTimer = NULL;
  45. #endif
  46. //-----------------------------------------------------------------------------
  47. // Purpose: constructor
  48. //-----------------------------------------------------------------------------
  49. CDODRoundTimer::CDODRoundTimer( void )
  50. {
  51. #ifndef CLIENT_DLL
  52. m_bTimerPaused = true;
  53. m_flTimeRemaining = 0;
  54. m_iTimerMaxLength = 0;
  55. #else
  56. g_DODRoundTimer = this;
  57. #endif
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose: destructor
  61. //-----------------------------------------------------------------------------
  62. CDODRoundTimer::~CDODRoundTimer( void )
  63. {
  64. #ifdef CLIENT_DLL
  65. g_DODRoundTimer = NULL;
  66. #endif
  67. }
  68. #ifndef CLIENT_DLL
  69. //-----------------------------------------------------------------------------
  70. // Purpose: The timer is always transmitted to clients
  71. //-----------------------------------------------------------------------------
  72. int CDODRoundTimer::UpdateTransmitState()
  73. {
  74. // ALWAYS transmit to all clients.
  75. return SetTransmitState( FL_EDICT_ALWAYS );
  76. }
  77. #endif
  78. //-----------------------------------------------------------------------------
  79. // Purpose: To set the initial timer duration
  80. //-----------------------------------------------------------------------------
  81. void CDODRoundTimer::SetTimeRemaining( int iTimerSeconds )
  82. {
  83. m_flTimeRemaining = (float)iTimerSeconds;
  84. m_flTimerEndTime = gpGlobals->curtime + m_flTimeRemaining;
  85. m_iTimerMaxLength = iTimerSeconds;
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose: Timer is paused at round end, stops the countdown
  89. //-----------------------------------------------------------------------------
  90. void CDODRoundTimer::PauseTimer( void )
  91. {
  92. if ( m_bTimerPaused == false )
  93. {
  94. m_bTimerPaused = true;
  95. m_flTimeRemaining = m_flTimerEndTime - gpGlobals->curtime;
  96. }
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose: To start or re-start the timer after a pause
  100. //-----------------------------------------------------------------------------
  101. void CDODRoundTimer::ResumeTimer( void )
  102. {
  103. if ( m_bTimerPaused == true )
  104. {
  105. m_bTimerPaused = false;
  106. m_flTimerEndTime = gpGlobals->curtime + m_flTimeRemaining;
  107. }
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Purpose: Gets the seconds left on the timer, paused or not.
  111. //-----------------------------------------------------------------------------
  112. float CDODRoundTimer::GetTimeRemaining( void )
  113. {
  114. float flSecondsRemaining;
  115. if ( m_bTimerPaused )
  116. {
  117. flSecondsRemaining = m_flTimeRemaining;
  118. }
  119. else
  120. {
  121. flSecondsRemaining = m_flTimerEndTime - gpGlobals->curtime;
  122. }
  123. if ( flSecondsRemaining < 0 )
  124. flSecondsRemaining = 0;
  125. return flSecondsRemaining;
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Purpose: Add seconds to the timer while it is running or paused
  129. //-----------------------------------------------------------------------------
  130. void CDODRoundTimer::AddTimerSeconds( int iSecondsToAdd )
  131. {
  132. // do a hud animation indicating that time has been added
  133. if ( m_bTimerPaused )
  134. {
  135. m_flTimeRemaining += (float)iSecondsToAdd;
  136. }
  137. else
  138. {
  139. m_flTimerEndTime += (float)iSecondsToAdd;
  140. }
  141. m_iTimerMaxLength += iSecondsToAdd;
  142. }
  143. int CDODRoundTimer::GetTimerMaxLength( void )
  144. {
  145. return m_iTimerMaxLength;
  146. }