aboutsummaryrefslogtreecommitdiffstats
path: root/code/pawn/Player.cs
blob: a1b126a7f752374981439e99d52c347b374abc9f (plain)
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
using Sandbox;
using Sandbox.UI;
using System.ComponentModel;

namespace MurderGame;

public partial class Player : AnimatedEntity
{
	[ClientInput]
	public Vector3 InputDirection { get; set; }
	
	[ClientInput]
	public Angles ViewAngles { get; set; }

	[Browsable( false )]
	public Vector3 EyePosition
	{
		get => Transform.PointToWorld( EyeLocalPosition );
		set => EyeLocalPosition = Transform.PointToLocal( value );
	}

	[Net, Predicted, Browsable( false )]
	public Vector3 EyeLocalPosition { get; set; }

	[Browsable( false )]
	public Rotation EyeRotation
	{
		get => Transform.RotationToWorld( EyeLocalRotation );
		set => EyeLocalRotation = Transform.RotationToLocal( value );
	}

	[Net, Predicted, Browsable( false )]
	public Rotation EyeLocalRotation { get; set; }

	public BBox Hull
	{
		get => new
		(
			new Vector3( -12, -12, 0 ),
			new Vector3( 12, 12, 64 )
		);
	}

	[Net]
	public Team CurrentTeam { get; set; }

	[BindComponent] public PlayerController Controller { get; }
	[BindComponent] public PlayerAnimator Animator { get; }
	[BindComponent] public PlayerInventory Inventory { get; }

	public Ragdoll PlayerRagdoll { get; set; }
	public ClothingContainer PlayerClothingContainer { get; set; }

	public Vector3 LastAttackForce { get; set; }
	public int LastHitBone { get; set; }

	public override Ray AimRay => new Ray( EyePosition, EyeRotation.Forward );

	public override void Spawn()
	{
		SetModel( "models/citizen/citizen.vmdl" );

		Tags.Add( "player" );
		EnableHitboxes = true;
		EnableDrawing = false;
		EnableHideInFirstPerson = true;
		EnableShadowInFirstPerson = true;
		SetupPhysicsFromAABB( PhysicsMotionType.Keyframed, Hull.Mins, Hull.Maxs );
		EnableSolidCollisions = false;
	}

	public void Respawn()
	{
		Tags.Add( "livingplayer" );
		EnableAllCollisions = true;
		EnableDrawing = true;
		Components.Create<PlayerController>();
		Components.Create<PlayerAnimator>();
		Components.Create<PlayerInventory>();
		Health = 100f;
		DeleteRagdoll();
	}

	public void Cleanup()
	{
		DisablePlayer();
		DeleteRagdoll();
	}

	public void DeleteRagdoll()
	{
		if (PlayerRagdoll != null)
		{
			PlayerRagdoll.Delete();
			PlayerRagdoll = null;
		}
	}

	public void DisablePlayer()
	{
		EnableAllCollisions = false;
		LifeState = LifeState.Dead;
		Tags.Remove( "livingplayer" );
		Inventory?.Clear();
		Components.RemoveAll();
		EnableDrawing = false;
	}

	public override void OnKilled()
	{
		Inventory?.SpillContents(EyePosition, new Vector3(0,0,0));
		DisablePlayer();
		Event.Run( MurderEvent.Kill, LastAttacker, this );
		var ragdoll = new Ragdoll();
		ragdoll.Position = Position;
		ragdoll.Rotation = Rotation;
		ragdoll.CopyFrom(this);
		ragdoll.PhysicsGroup.AddVelocity(LastAttackForce / 100);
		PlayerClothingContainer.DressEntity( ragdoll );
		PlayerRagdoll = ragdoll;
	}

	public override void TakeDamage( DamageInfo info )
	{
		LastAttacker = info.Attacker;
		LastAttackerWeapon = info.Weapon;
		LastAttackForce = info.Force;
		LastHitBone = info.BoneIndex;
		if (Game.IsServer && Health > 0f && LifeState == LifeState.Alive)
		{
			Health -= info.Damage;
			if (Health <= 0f)
			{
				Health = 0f;
				OnKilled();
			}
		}
	}

	public void DressFromClient( IClient cl )
	{
		PlayerClothingContainer = new ClothingContainer();
		PlayerClothingContainer.LoadFromClient( cl );
		PlayerClothingContainer.DressEntity( this );
	}

	public override void Simulate( IClient cl )
	{
		SimulateRotation();
		Controller?.Simulate( cl );
		Animator?.Simulate();
		Inventory?.Simulate( cl );
		EyeLocalPosition = Vector3.Up * (64f * Scale);
	}

	public override void BuildInput()
	{
		InputDirection = Input.AnalogMove;

		if ( Input.StopProcessing )
			return;

		var look = Input.AnalogLook;

		if ( ViewAngles.pitch > 90f || ViewAngles.pitch < -90f )
		{
			look = look.WithYaw( look.yaw * -1f );
		}

		var viewAngles = ViewAngles;
		viewAngles += look;
		viewAngles.pitch = viewAngles.pitch.Clamp( -89f, 89f );
		viewAngles.roll = 0f;
		ViewAngles = viewAngles.Normal;
	}

	bool IsThirdPerson { get; set; } = false;

	public override void FrameSimulate( IClient cl )
	{
		SimulateRotation();

		Camera.Rotation = ViewAngles.ToRotation();
		Camera.FieldOfView = Screen.CreateVerticalFieldOfView( Game.Preferences.FieldOfView );

		Camera.FirstPersonViewer = this;
		Camera.Position = EyePosition;
	}

	public TraceResult TraceBBox( Vector3 start, Vector3 end, float liftFeet = 0.0f )
	{
		return TraceBBox( start, end, Hull.Mins, Hull.Maxs, liftFeet );
	}

	public TraceResult TraceBBox( Vector3 start, Vector3 end, Vector3 mins, Vector3 maxs, float liftFeet = 0.0f )
	{
		if ( liftFeet > 0 )
		{
			start += Vector3.Up * liftFeet;
			maxs = maxs.WithZ( maxs.z - liftFeet );
		}

		var tr = Trace.Ray( start, end )
					.Size( mins, maxs )
					.WithAnyTags( "solid", "player", "passbullets" )
					.Ignore( this )
					.Run();

		return tr;
	}

	protected void SimulateRotation()
	{
		EyeRotation = ViewAngles.ToRotation();
		Rotation = ViewAngles.WithPitch( 0f ).ToRotation();
	}

}