-
Notifications
You must be signed in to change notification settings - Fork 2
/
PawnController.Util.cs
395 lines (302 loc) · 8.24 KB
/
PawnController.Util.cs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using Sandbox;
using System;
namespace RunnerVision;
public partial class PawnController
{
Rotation GetVelocityRotation()
{
return Entity.Velocity.EulerAngles.ToRotation();
}
bool AngleWithinRange(Vector3 directionVector1, Vector3 directionVector2, float minAngle = 0f, float maxAngle = 360f)
{
if ( directionVector1.Normal.Angle( directionVector2 ) > maxAngle )
return false;
if ( directionVector1.Normal.Angle( directionVector2 ) < minAngle )
return false;
return true;
}
Vector3 GetMoveVector()
{
var movement = Entity.InputDirection.Normal;
var angles = Entity.ViewAngles.WithPitch( 0 );
return Rotation.From( angles ) * movement * CurrentMaxSpeed;
}
void UpdateDash()
{
if ( TimeSinceDash > 0.5f )
Dashing = 0;
}
void InitiateJumpOffWall()
{
var cameraDirection = GetCameraDirection();
var forwardAngle = GetForwardAngle();
var forwardMultiplier = Math.Max( 0.5f, forwardAngle / 90f );
var jumpVector = cameraDirection * 300f * forwardMultiplier + Entity.Rotation.Up * 300f;
Entity.Velocity *= 0.5f;
Entity.ApplyAbsoluteImpulse( jumpVector );
previousWallrunNormal = CurrentWall.Normal;
Wallrunning = 0;
}
Vector3 GetCameraDirection()
{
return Camera.Rotation.Forward.WithZ( 0 );
}
float GetForwardAngle()
{
var cameraDirection = GetCameraDirection();
var forwardAngle = ForwardDirection.Angle( cameraDirection );
// Check angle from movement axis (max 90 degrees)
float dotProduct = Vector3.Dot( ForwardDirection, cameraDirection );
if ( dotProduct < 0 )
{
return 180 - forwardAngle;
}
return forwardAngle;
}
void InitiateDash()
{
if ( TimeSinceDash > 1.0f )
{
var isLeft = Input.Down( "left" );
Dashing = isLeft ? 1 : 2;
Entity.ApplyAbsoluteImpulse( (isLeft ? Entity.Rotation.Left : Entity.Rotation.Right) * 300f );
CurrentMaxSpeed += 200f;
TimeSinceDash = 0.0f;
}
}
void DisableParkourLock()
{
parkouredSinceJumping = false;
wallrunSinceJumping = false;
}
void DoFall()
{
Entity.Velocity += Vector3.Down * (IsWallRunning() ? Gravity * 0.60f : Gravity) * Time.Delta;
}
void InitiateLandingOnFloor()
{
Sound.FromWorld( "concretefootstepland", Entity.Position + Vector3.Down * 10f );
AddEvent( "grounded" );
Wallrunning = 0;
previousWallrunNormal = Vector3.Zero;
parkouredSinceJumping = false;
parkouredBeforeLanding = false;
Jumping = false;
if ( Entity.Velocity.WithZ(0).Length > 100f )
{
CurrentMaxSpeed += 200;
}
}
void UpdateMaxSpeed(Vector3 moveVector)
{
if ( moveVector.LengthSquared != 0 )
{
CurrentMaxSpeed = CurrentMaxSpeed.Approach( MaxSpeed, Time.Delta * 50f * SpeedGrowthRate );
}
else
{
CurrentMaxSpeed = CurrentMaxSpeed.Approach( StartingSpeed, Time.Delta * 50f * SpeedShrinkRate );
}
}
void ClampMaxSpeed()
{
CurrentMaxSpeed = Math.Min( CurrentMaxSpeed, MaxSpeed );
}
bool IsDashing()
{
return Dashing != 0;
}
bool ShouldDash()
{
if ( Ducking )
return false;
if ( !Grounded )
return false;
if ( Input.Down( "forward" ) )
return false;
if ( !Input.Down( "left" ) && !Input.Down( "right" ) )
return false;
return true;
}
void HandleNoclipping()
{
var movement = Entity.InputDirection.Normal;
var angles = Entity.ViewAngles;
var moveVector = Rotation.From( angles ) * movement * 10f;
Entity.Transform = Entity.Transform.Add( moveVector, true );
Entity.Velocity = 0;
}
void IncreaseDeltaTime()
{
TimeSinceLastFootstep += Time.Delta;
TimeSinceLastFootstepRelease += Time.Delta;
TimeSinceDash += Time.Delta;
TimeSinceClimbing += Time.Delta;
TimeSinceWallrun += Time.Delta;
TimeSinceSlideStopped += Time.Delta;
}
void UpdateFootsteps()
{
float speed = Entity.Velocity.Length;
if ( Game.IsServer )
return;
if ( speed == 0f )
return;
if ( !Grounded && !IsWallRunning() && !Climbing )
return;
float nextStep = 70f / speed;
String footstepSound = speed < 300 ? "concretefootstepwalk" : "concretefootsteprun";
String footstepReleaseSound = IsWallRunning() ? "concretefootstepwallrunrelease" : "concretefootsteprunrelease";
if ( IsWallRunning() )
{
nextStep = 60f / speed;
footstepSound = "concretefootstepwallrun";
}
if ( Climbing )
{
nextStep = 0.2f;
footstepSound = "concretefootstepwallrun";
}
if ( TimeSinceLastFootstep > nextStep )
{
Sound.FromWorld( footstepSound, Entity.Position + Vector3.Down * 10f );
TimeSinceLastFootstep = 0f;
}
if ( TimeSinceLastFootstepRelease > nextStep * 1.15 && speed > StartFootSoundVelocity )
{
Sound.FromWorld( footstepReleaseSound, Entity.Position + Vector3.Down * 10f );
TimeSinceLastFootstepRelease = 0f;
}
}
void AdjustSharpTurn( Vector3 moveVector )
{
if ( Entity.Velocity.Angle( moveVector.Normal * moveVector.Length ) > SharpTurnAngle )
{
CurrentMaxSpeed = CurrentMaxSpeed.Approach( StartingSpeed, Time.Delta * 50f * SpeedShrinkRate );
}
}
void InitiateJump()
{
Jumping = true;
Entity.Velocity = ApplyJump( Entity.Velocity, "jump" );
}
bool CanJump()
{
if ( !Grounded )
return false;
if ( IsVaulting() )
return false;
if ( IsDashing() )
return false;
if ( IsWallRunning() )
return false;
if ( IsDucking() )
return false;
return true;
}
Entity CheckForGround()
{
if ( Entity.Velocity.z > 100f )
return null;
var trace = Entity.TraceBBox( Entity.Position, Entity.Position + Vector3.Down, 2f );
if ( !trace.Hit )
return null;
if ( trace.Normal.Angle( Vector3.Up ) > GroundAngle )
return null;
return trace.Entity;
}
Vector3 ApplyFriction( Vector3 velocity, float frictionAmount )
{
float StopSpeed = 100.0f;
var speed = velocity.Length;
if ( speed < 0.1f ) return velocity;
// Bleed off some speed, but if we have less than the bleed
// threshold, bleed the threshold amount.
float control = (speed < StopSpeed) ? StopSpeed : speed;
// Add the amount to the drop amount.
var drop = control * Time.Delta * frictionAmount;
// scale the velocity
float newspeed = speed - drop;
if ( newspeed < 0 ) newspeed = 0;
if ( newspeed == speed ) return velocity;
newspeed /= speed;
velocity *= newspeed;
return velocity;
}
void DoMovement( Vector3 moveVector )
{
if ( ShouldAccelerate() )
DoAccelerate( moveVector );
DoApplyFriction();
}
void DoAccelerate( Vector3 moveVector )
{
Entity.Velocity = Accelerate( Entity.Velocity, moveVector.Normal, moveVector.Length, CurrentMaxSpeed, Acceleration );
}
void DoApplyFriction()
{
Entity.Velocity = ApplyFriction( Entity.Velocity, GetFriction() );
}
bool ShouldAccelerate()
{
if ( IsSliding() )
return false;
return true;
}
float GetFriction()
{
if ( IsSliding() )
return Friction * 0.5f;
return Friction;
}
Vector3 Accelerate( Vector3 velocity, Vector3 wishdir, float wishspeed, float speedLimit, float acceleration )
{
if ( speedLimit > 0 && wishspeed > speedLimit )
wishspeed = speedLimit;
velocity = velocity.LerpTo( wishdir * wishspeed, Time.Delta * 45f * acceleration );
return velocity;
}
Vector3 ApplyJump( Vector3 velocity, string jumpType )
{
AddEvent( jumpType );
return velocity.WithZ(0) + Vector3.Up * JumpSpeed;
}
void UpdateMoveHelper( Entity groundEntity )
{
var mh = new MoveHelper( Entity.Position, Entity.Velocity );
mh.Trace = mh.Trace.Size( Entity.Hull ).Ignore( Entity );
if ( mh.TryMoveWithStep( Time.Delta, StepSize ) > 0 )
{
if ( Grounded )
{
mh.Position = StayOnGround( mh.Position );
}
Entity.Position = mh.Position;
Entity.Velocity = mh.Velocity;
}
Entity.GroundEntity = groundEntity;
}
Vector3 StayOnGround( Vector3 position )
{
var start = position + Vector3.Up * 2;
var end = position + Vector3.Down * StepSize;
// See how far up we can go without getting stuck
var trace = Entity.TraceBBox( position, start );
start = trace.EndPosition;
// Now trace down from a known safe position
trace = Entity.TraceBBox( start, end );
if ( trace.Fraction <= 0 ) return position;
if ( trace.Fraction >= 1 ) return position;
if ( trace.StartedSolid ) return position;
if ( Vector3.GetAngle( Vector3.Up, trace.Normal ) > GroundAngle ) return position;
return trace.EndPosition;
}
float GetSpeed()
{
return Entity.Velocity.Length;
}
float GetHorizontalSpeed()
{
return Entity.Velocity.WithZ( 0 ).Length;
}
}