forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam_thirdperson.cpp
216 lines (161 loc) · 5.13 KB
/
cam_thirdperson.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "cam_thirdperson.h"
#include "gamerules.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
static Vector CAM_HULL_MIN(-CAM_HULL_OFFSET,-CAM_HULL_OFFSET,-CAM_HULL_OFFSET);
static Vector CAM_HULL_MAX( CAM_HULL_OFFSET, CAM_HULL_OFFSET, CAM_HULL_OFFSET);
#ifdef CLIENT_DLL
#include "input.h"
extern const ConVar *sv_cheats;
void CAM_ToThirdPerson(void);
void CAM_ToFirstPerson(void);
void ToggleThirdPerson( bool bValue )
{
if ( bValue == true )
{
CAM_ToThirdPerson();
}
else
{
CAM_ToFirstPerson();
}
}
void ThirdPersonChange( IConVar *pConVar, const char *pOldValue, float flOldValue )
{
ConVarRef var( pConVar );
ToggleThirdPerson( var.GetBool() );
}
ConVar cl_thirdperson( "cl_thirdperson", "0", FCVAR_NOT_CONNECTED | FCVAR_USERINFO | FCVAR_ARCHIVE | FCVAR_DEVELOPMENTONLY, "Enables/Disables third person", ThirdPersonChange );
#endif
CThirdPersonManager::CThirdPersonManager( void )
{
}
void CThirdPersonManager::Init( void )
{
m_bOverrideThirdPerson = false;
m_bForced = false;
m_flUpFraction = 0.0f;
m_flFraction = 1.0f;
m_flUpLerpTime = 0.0f;
m_flLerpTime = 0.0f;
m_flUpOffset = CAMERA_UP_OFFSET;
if ( input )
{
input->CAM_SetCameraThirdData( NULL, vec3_angle );
}
}
void CThirdPersonManager::Update( void )
{
#ifdef CLIENT_DLL
if ( !sv_cheats )
{
sv_cheats = cvar->FindVar( "sv_cheats" );
}
// If cheats have been disabled, pull us back out of third-person view.
if ( sv_cheats && !sv_cheats->GetBool() && GameRules() && GameRules()->AllowThirdPersonCamera() == false )
{
if ( (bool)input->CAM_IsThirdPerson() == true )
{
input->CAM_ToFirstPerson();
}
return;
}
if ( IsOverridingThirdPerson() == false )
{
if ( (bool)input->CAM_IsThirdPerson() != ( cl_thirdperson.GetBool() || m_bForced ) && GameRules() && GameRules()->AllowThirdPersonCamera() == true )
{
ToggleThirdPerson( m_bForced || cl_thirdperson.GetBool() );
}
}
#endif
}
Vector CThirdPersonManager::GetFinalCameraOffset( void )
{
Vector vDesired = GetDesiredCameraOffset();
if ( m_flUpFraction != 1.0f )
{
vDesired.z += m_flUpOffset;
}
return vDesired;
}
Vector CThirdPersonManager::GetDistanceFraction( void )
{
if ( IsOverridingThirdPerson() == true )
{
return Vector( m_flTargetFraction, m_flTargetFraction, m_flTargetFraction );
}
float flFraction = m_flFraction;
float flUpFraction = m_flUpFraction;
float flFrac = RemapValClamped( gpGlobals->curtime - m_flLerpTime, 0, CAMERA_OFFSET_LERP_TIME, 0, 1 );
flFraction = Lerp( flFrac, m_flFraction, m_flTargetFraction );
if ( flFrac == 1.0f )
{
m_flFraction = m_flTargetFraction;
}
flFrac = RemapValClamped( gpGlobals->curtime - m_flUpLerpTime, 0, CAMERA_UP_OFFSET_LERP_TIME, 0, 1 );
flUpFraction = 1.0f - Lerp( flFrac, m_flUpFraction, m_flTargetUpFraction );
if ( flFrac == 1.0f )
{
m_flUpFraction = m_flTargetUpFraction;
}
return Vector( flFraction, flFraction, flUpFraction );
}
void CThirdPersonManager::PositionCamera( CBasePlayer *pPlayer, const QAngle& angles )
{
if ( pPlayer )
{
trace_t trace;
Vector camForward, camRight, camUp;
// find our player's origin, and from there, the eye position
Vector origin = pPlayer->GetLocalOrigin();
origin += pPlayer->GetViewOffset();
AngleVectors( angles, &camForward, &camRight, &camUp );
Vector endPos = origin;
Vector vecCamOffset = endPos + (camForward * - GetDesiredCameraOffset()[DIST_FORWARD]) + (camRight * GetDesiredCameraOffset()[ DIST_RIGHT ]) + (camUp * GetDesiredCameraOffset()[ DIST_UP ] );
// use our previously #defined hull to collision trace
CTraceFilterSimple traceFilter( pPlayer, COLLISION_GROUP_NONE );
UTIL_TraceHull( endPos, vecCamOffset, CAM_HULL_MIN, CAM_HULL_MAX, MASK_SOLID & ~CONTENTS_MONSTER, &traceFilter, &trace );
if ( trace.fraction != m_flTargetFraction )
{
m_flLerpTime = gpGlobals->curtime;
}
m_flTargetFraction = trace.fraction;
m_flTargetUpFraction = 1.0f;
//If we're getting closer to a wall snap the fraction right away.
if ( m_flTargetFraction < m_flFraction )
{
m_flFraction = m_flTargetFraction;
m_flLerpTime = gpGlobals->curtime;
}
// move the camera closer if it hit something
if( trace.fraction < 1.0 )
{
m_vecCameraOffset[ DIST ] *= trace.fraction;
UTIL_TraceHull( endPos, endPos + (camForward * - GetDesiredCameraOffset()[DIST_FORWARD]), CAM_HULL_MIN, CAM_HULL_MAX, MASK_SOLID & ~CONTENTS_MONSTER, &traceFilter, &trace );
if ( trace.fraction != 1.0f )
{
if ( trace.fraction != m_flTargetUpFraction )
{
m_flUpLerpTime = gpGlobals->curtime;
}
m_flTargetUpFraction = trace.fraction;
if ( m_flTargetUpFraction < m_flUpFraction )
{
m_flUpFraction = trace.fraction;
m_flUpLerpTime = gpGlobals->curtime;
}
}
}
}
}
bool CThirdPersonManager::WantToUseGameThirdPerson( void )
{
return cl_thirdperson.GetBool() && GameRules() && GameRules()->AllowThirdPersonCamera() && IsOverridingThirdPerson() == false;
}
CThirdPersonManager g_ThirdPersonManager;