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.

109 lines
2.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef COUNTDOWN_ANNOUNCER_H
  8. #define COUNTDOWN_ANNOUNCER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "mathlib/mathlib.h"
  13. #include "tf/tf_gamerules.h"
  14. //=============================================================================
  15. class CCountdownAnnouncer
  16. {
  17. public:
  18. struct TimeSounds {
  19. const char *at60;
  20. const char *at30;
  21. const char *at10;
  22. const char *at5;
  23. const char *at4;
  24. const char *at3;
  25. const char *at2;
  26. const char *at1;
  27. };
  28. CCountdownAnnouncer(const TimeSounds *pTimeSounds)
  29. : m_state( Disabled )
  30. , m_pTimeSounds( pTimeSounds )
  31. , m_fTimeRemain( 0 )
  32. , m_iPrevSecondsRemain( 0 )
  33. {}
  34. void Disable() { m_state = Disabled; }
  35. void Start( int durationSec )
  36. {
  37. if ( m_state == Running )
  38. return;
  39. m_state = Running;
  40. m_fTimeRemain = (float) durationSec;
  41. m_iPrevSecondsRemain = 1 + (int) m_fTimeRemain;
  42. Tick( 0 );
  43. }
  44. // returns true once when time expires
  45. bool Tick( float delta )
  46. {
  47. assert( delta >= 0 );
  48. switch ( m_state )
  49. {
  50. case Disabled:
  51. case Expired:
  52. break;
  53. case Running:
  54. {
  55. m_fTimeRemain = MAX( 0, m_fTimeRemain - delta );
  56. const int iSecondsRemain = Ceil2Int( m_fTimeRemain );
  57. if ( iSecondsRemain == m_iPrevSecondsRemain )
  58. break;
  59. m_iPrevSecondsRemain = iSecondsRemain;
  60. switch( iSecondsRemain )
  61. {
  62. case 60: BroadcastSound( m_pTimeSounds->at60 ); break;
  63. case 30: BroadcastSound( m_pTimeSounds->at30 ); break;
  64. case 10: BroadcastSound( m_pTimeSounds->at10 ); break;
  65. case 5: BroadcastSound( m_pTimeSounds->at5 ); break;
  66. case 4: BroadcastSound( m_pTimeSounds->at4 ); break;
  67. case 3: BroadcastSound( m_pTimeSounds->at3 ); break;
  68. case 2: BroadcastSound( m_pTimeSounds->at2 ); break;
  69. case 1: BroadcastSound( m_pTimeSounds->at1 ); break;
  70. default: break;
  71. }
  72. if ( iSecondsRemain == 0 )
  73. {
  74. Disable();
  75. return true;
  76. }
  77. break;
  78. }
  79. }
  80. return false;
  81. }
  82. bool IsDisabled() const { return m_state == Disabled; }
  83. private:
  84. void BroadcastSound( const char* name )
  85. {
  86. if ( name && *name )
  87. TFGameRules()->BroadcastSound( 255, name );
  88. }
  89. enum State { Disabled, Running, Expired };
  90. State m_state;
  91. const TimeSounds *m_pTimeSounds;
  92. float m_fTimeRemain;
  93. int m_iPrevSecondsRemain;
  94. };
  95. #endif // COUNTDOWN_ANNOUNCER_H