This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pawn.cs
101 lines (80 loc) · 2.49 KB
/
Pawn.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
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Sandbox.BSP.data;
using System.Collections.Generic;
using Sandbox.BSP.enums;
using Sandbox.BSP;
namespace Sandbox;
partial class Pawn : AnimatedEntity
{
/// <summary>
/// Called when the entity is first created
/// </summary>
public override void Spawn()
{
base.Spawn();
//
// Use a watermelon model
//
SetModel( "models/sbox_props/watermelon/watermelon.vmdl" );
EnableDrawing = true;
EnableHideInFirstPerson = true;
EnableShadowInFirstPerson = true;
}
// An example BuildInput method within a player's Pawn class.
[ClientInput] public Vector3 InputDirection { get; protected set; }
[ClientInput] public Angles ViewAngles { get; set; }
public override void BuildInput()
{
InputDirection = Input.AnalogMove;
var look = Input.AnalogLook;
var viewAngles = ViewAngles;
viewAngles += look;
ViewAngles = viewAngles.Normal;
}
/// <summary>
/// Called every tick, clientside and serverside.
/// </summary>
public override void Simulate( IClient cl )
{
base.Simulate( cl );
Rotation = ViewAngles.ToRotation();
// build movement from the input values
var movement = InputDirection.Normal;
// rotate it to the direction we're facing
Velocity = Rotation * movement;
// apply some speed to it
Velocity *= Input.Down( InputButton.Run ) ? 1000 : 200;
// apply it to our position using MoveHelper, which handles collision
// detection and sliding across surfaces for us
MoveHelper helper = new MoveHelper( Position, Velocity );
helper.Trace = helper.Trace.Size( 16 );
if ( helper.TryMove( Time.Delta ) > 0 )
{
Position = helper.Position;
}
// If we're running serverside and Attack1 was just pressed, spawn a ragdoll
if ( !Game.IsServer && Input.Pressed( InputButton.PrimaryAttack ) )
{
BSPMapEntity mapEntity = BSPParser.CreateMapEntity( "c1a0.bsp" );
mapEntity.Position = Position;
mapEntity.Spawn();
}
}
/// <summary>
/// Called every frame on the client
/// </summary>
public override void FrameSimulate( IClient cl )
{
base.FrameSimulate( cl );
// Update rotation every frame, to keep things smooth
Rotation = ViewAngles.ToRotation();
Camera.Position = Position;
Camera.Rotation = Rotation;
// Set field of view to whatever the user chose in options
Camera.FieldOfView = Screen.CreateVerticalFieldOfView( Game.Preferences.FieldOfView );
// Set the first person viewer to this, so it won't render our model
Camera.FirstPersonViewer = this;
}
}