summaryrefslogtreecommitdiff
path: root/game/shared/dod/dod_round_timer.cpp
blob: cf15855597b617ebad402330b0dea610b0ca73a9 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Dod gamerules round timer 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "dod_round_timer.h"

#ifdef CLIENT_DLL

	#include "iclientmode.h"
	#include "vgui_controls/AnimationController.h"

#endif

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

#ifdef CLIENT_DLL

	// Use this proxy to flash the round timer whenever the timer is restarted
	// because trapping the round start event doesn't work ( the event also flushes
	// all hud events and obliterates our TimerFlash event )
	static void RecvProxy_TimerPaused( const CRecvProxyData *pData, void *pStruct, void *pOut )
	{
		CDODRoundTimer *pTimer = (CDODRoundTimer *) pStruct;

		bool bTimerPaused = ( pData->m_Value.m_Int > 0 );

		if ( bTimerPaused == false )
		{
			g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "TimerFlash" ); 
		}

		pTimer->InternalSetPaused( bTimerPaused );
	}

#endif

LINK_ENTITY_TO_CLASS( dod_round_timer, CDODRoundTimer );

IMPLEMENT_NETWORKCLASS_ALIASED( DODRoundTimer, DT_DODRoundTimer )

BEGIN_NETWORK_TABLE_NOBASE( CDODRoundTimer, DT_DODRoundTimer )
	#ifdef CLIENT_DLL

		RecvPropInt( RECVINFO( m_bTimerPaused ), 0, RecvProxy_TimerPaused ),
		RecvPropTime( RECVINFO( m_flTimeRemaining ) ),
		RecvPropTime( RECVINFO( m_flTimerEndTime ) ),

	#else

		SendPropBool( SENDINFO( m_bTimerPaused ) ),
		SendPropTime( SENDINFO( m_flTimeRemaining ) ),
		SendPropTime( SENDINFO( m_flTimerEndTime ) ),

	#endif
END_NETWORK_TABLE()

#ifdef CLIENT_DLL
	CDODRoundTimer *g_DODRoundTimer = NULL;
#endif

//-----------------------------------------------------------------------------
// Purpose: constructor
//-----------------------------------------------------------------------------
CDODRoundTimer::CDODRoundTimer( void )
{
#ifndef CLIENT_DLL
	m_bTimerPaused = true;
	m_flTimeRemaining = 0;
	m_iTimerMaxLength = 0;
#else
	g_DODRoundTimer = this;
#endif
}

//-----------------------------------------------------------------------------
// Purpose: destructor
//-----------------------------------------------------------------------------
CDODRoundTimer::~CDODRoundTimer( void )
{
#ifdef CLIENT_DLL
	g_DODRoundTimer = NULL;
#endif
}

#ifndef CLIENT_DLL

//-----------------------------------------------------------------------------
// Purpose: The timer is always transmitted to clients
//-----------------------------------------------------------------------------
int CDODRoundTimer::UpdateTransmitState()
{
	// ALWAYS transmit to all clients.
	return SetTransmitState( FL_EDICT_ALWAYS );
}

#endif

//-----------------------------------------------------------------------------
// Purpose: To set the initial timer duration
//-----------------------------------------------------------------------------
void CDODRoundTimer::SetTimeRemaining( int iTimerSeconds )
{
	m_flTimeRemaining = (float)iTimerSeconds;
	m_flTimerEndTime = gpGlobals->curtime + m_flTimeRemaining;
	m_iTimerMaxLength = iTimerSeconds;
}

//-----------------------------------------------------------------------------
// Purpose: Timer is paused at round end, stops the countdown
//-----------------------------------------------------------------------------
void CDODRoundTimer::PauseTimer( void )
{
	if ( m_bTimerPaused == false )
	{
		m_bTimerPaused = true;

		m_flTimeRemaining = m_flTimerEndTime - gpGlobals->curtime;
	}
}

//-----------------------------------------------------------------------------
// Purpose: To start or re-start the timer after a pause
//-----------------------------------------------------------------------------
void CDODRoundTimer::ResumeTimer( void )
{
	if ( m_bTimerPaused == true )
	{
		m_bTimerPaused = false;

		m_flTimerEndTime = gpGlobals->curtime + m_flTimeRemaining;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Gets the seconds left on the timer, paused or not.
//-----------------------------------------------------------------------------
float CDODRoundTimer::GetTimeRemaining( void )
{
	float flSecondsRemaining;

	if ( m_bTimerPaused )
	{
		flSecondsRemaining = m_flTimeRemaining;
	}
	else
	{
		flSecondsRemaining = m_flTimerEndTime - gpGlobals->curtime;
	}

	if ( flSecondsRemaining < 0 )
		flSecondsRemaining = 0;

	return flSecondsRemaining;
}

//-----------------------------------------------------------------------------
// Purpose: Add seconds to the timer while it is running or paused
//-----------------------------------------------------------------------------
void CDODRoundTimer::AddTimerSeconds( int iSecondsToAdd )
{
	// do a hud animation indicating that time has been added

	if ( m_bTimerPaused )
	{
		m_flTimeRemaining += (float)iSecondsToAdd;
	}
	else
	{
		m_flTimerEndTime += (float)iSecondsToAdd;
	}

	m_iTimerMaxLength += iSecondsToAdd;
}

int CDODRoundTimer::GetTimerMaxLength( void )
{
	return m_iTimerMaxLength;
}