summaryrefslogtreecommitdiff
path: root/game/server/tf/countdown_announcer.h
blob: a3022d0825b47ca4f0611087d44dea3400dccc69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef COUNTDOWN_ANNOUNCER_H
#define COUNTDOWN_ANNOUNCER_H
#ifdef _WIN32
#pragma once
#endif

#include "mathlib/mathlib.h"
#include "tf/tf_gamerules.h"

//=============================================================================

class CCountdownAnnouncer 
{
public:
	struct TimeSounds {
		const char *at60;
		const char *at30;
		const char *at10;
		const char *at5;
		const char *at4;
		const char *at3;
		const char *at2;
		const char *at1;
	};

	CCountdownAnnouncer(const TimeSounds *pTimeSounds) 
		: m_state( Disabled )
		, m_pTimeSounds( pTimeSounds )
		, m_fTimeRemain( 0 )
		, m_iPrevSecondsRemain( 0 )
	{}

	void Disable() { m_state = Disabled; }

	void Start( int durationSec ) 
	{
		if ( m_state == Running )
			return;
		m_state = Running;
		m_fTimeRemain = (float) durationSec;
		m_iPrevSecondsRemain = 1 + (int) m_fTimeRemain;
		Tick( 0 );
	}

	// returns true once when time expires
	bool Tick( float delta ) 
	{
		assert( delta >= 0 );
		switch ( m_state )
		{
		case Disabled:
		case Expired:
			break;
			
		case Running: 
			{
				m_fTimeRemain = MAX( 0, m_fTimeRemain - delta );
				const int iSecondsRemain = Ceil2Int( m_fTimeRemain );
				if ( iSecondsRemain == m_iPrevSecondsRemain )
					break;
				m_iPrevSecondsRemain = iSecondsRemain;
				switch( iSecondsRemain ) 
				{
				case 60: BroadcastSound( m_pTimeSounds->at60 ); break;
				case 30: BroadcastSound( m_pTimeSounds->at30 ); break;
				case 10: BroadcastSound( m_pTimeSounds->at10 ); break;
				case 5: BroadcastSound( m_pTimeSounds->at5 ); break;
				case 4: BroadcastSound( m_pTimeSounds->at4 ); break;
				case 3: BroadcastSound( m_pTimeSounds->at3 ); break;
				case 2: BroadcastSound( m_pTimeSounds->at2 ); break;
				case 1: BroadcastSound( m_pTimeSounds->at1 ); break;
				default: break;
				}

				if ( iSecondsRemain == 0 )
				{
					Disable();
					return true;
				}
				break;
			}
		}
		return false;
	}

	bool IsDisabled() const { return m_state == Disabled; }

private:
	void BroadcastSound( const char* name ) 
	{ 
		if ( name && *name )
			TFGameRules()->BroadcastSound( 255, name );
	}

	enum State { Disabled, Running, Expired };
	State m_state;
	const TimeSounds *m_pTimeSounds;
	float m_fTimeRemain;
	int m_iPrevSecondsRemain;
};

#endif // COUNTDOWN_ANNOUNCER_H