aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/simple_keys.cpp
blob: 1a5af2808823edbdff31a7dba15e4a27d74b0d6a (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "cbase.h"
#include "simple_keys.h"

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

//-----------------------------------------------------------------------------
// Simple key interpolations
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &out - 
//			t - 
//			&start - 
//			&end - 
//-----------------------------------------------------------------------------
void CSimpleKeyInterp::Interp( Vector &out, float t, const CSimpleKeyInterp &start, const CSimpleKeyInterp &end )
{
	float delta = end.GetTime() - start.GetTime();
	t = clamp( t-start.GetTime(), 0.f, delta );

	float unitT = (delta > 0) ? (t / delta) : 1;

	switch( end.m_interp )
	{
	case KEY_SPLINE:
		unitT = SimpleSpline( unitT );
		break;
	case KEY_ACCELERATE:
		unitT *= unitT;
		break;
	case KEY_DECELERATE:
		unitT = sqrt(unitT);
		break;
	default:
	case KEY_LINEAR:
		//unitT = unitT;
		break;
	}
	out = (1-unitT) * ((Vector)start) + unitT * ((Vector)end);
}

//-----------------------------------------------------------------------------
// Simple key list
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &key - 
// Output : int
//-----------------------------------------------------------------------------
int CSimpleKeyList::Insert( const CSimpleKeyInterp &key )
{
	for ( int i = 0; i < m_list.Count(); i++ )
	{
		if ( key.GetTime() < m_list[i].GetTime() )
			return m_list.InsertBefore( i, key );
	}

	return m_list.AddToTail( key );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &out - 
//			t - 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CSimpleKeyList::Interp( Vector &out, float t )
{
	int startIndex = -1;

	out.Init();
	for ( int i = 0; i < m_list.Count(); i++ )
	{
		if ( t < m_list[i].GetTime() )
		{
			// before start
			if ( startIndex < 0 )
				return false;
			CSimpleKeyInterp::Interp( out, t, m_list[startIndex], m_list[i] );
			return true;
		}
		startIndex = i;
	}

	// past end
	return false;
}