aboutsummaryrefslogtreecommitdiffstats
path: root/code/pawn/Player.cs
blob: 3d92008a1da3e1b18f1e7e5c4862a3ac4ca73c0f (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
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
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( -16, -16, 0 ),
			new Vector3( 16, 16, 72 )
		);
	}


	public BaseCameraComponent Camera => Components.Get<BaseCameraComponent>();
	public BaseControllerComponent Controller => Components.Get<BaseControllerComponent>();
	[BindComponent] public AnimatorComponent Animator { get; }
	[BindComponent] public InventoryComponent Inventory { get; }
	[BindComponent] public FallDamageComponent FallDamage { get; }
	[BindComponent] public FootprintTrackerComponent FootprintTracker { get; }

	[Net]
	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 );

	[Net, Predicted]
	public TimeSince TimeSinceDeath { get; set; } = 0;
	
	public Player LookingAt { get; set; }
	
	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;

		Health = 0f;
		LifeState = LifeState.Dead;
	}

	public void Respawn()
	{
		DeleteRagdoll();
		Tags.Add( "livingplayer" );

		EnableAllCollisions = true;
		EnableDrawing = true;

		Components.Create<WalkControllerComponent>();
		Components.Create<PlayerCameraComponent>();
		Components.Create<AnimatorComponent>();
		Components.Create<InventoryComponent>();
		Components.Create<FallDamageComponent>();

		Health = 100f;
		LifeState = LifeState.Alive;
	}

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

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

	public void DisablePlayer()
	{
		Tags.Remove( "livingplayer" );

		EnableAllCollisions = false;
		EnableDrawing = false;

		Inventory?.Clear();

		LifeState = LifeState.Dead;
	}

	public override void OnKilled()
	{
		TimeSinceDeath = 0;

		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;

		DeathOverlay.Show( To.Single( Client ) );
	}

	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 void Dress()
	{
		PlayerClothingContainer = new ClothingContainer();
		var trousers = new Clothing() { Model = "models/citizen_clothes/trousers/cargopants/models/cargo_pants.vmdl" };
		var tshirt = new Clothing() { Model = "models/citizen_clothes/shirt/Tshirt/Models/tshirt.vmdl" };
		PlayerClothingContainer.Clothing.Add( trousers );
		PlayerClothingContainer.Clothing.Add( tshirt );
		PlayerClothingContainer.DressEntity( this );
	}

	public override void Simulate( IClient cl )
	{
		SimulateRotation();
		TickPlayerUse();

		Controller?.Simulate( cl );
		Camera?.Simulate( cl );
		Animator?.Simulate();
		Inventory?.Simulate( cl );
		FallDamage?.Simulate( cl );
		FootprintTracker?.Simulate( cl );

		if (Game.IsServer && Camera is not SpectatorCameraComponent && LifeState == LifeState.Dead && TimeSinceDeath > 3.5)
		{
			DeathOverlay.Hide( To.Single( Client ) );
			Components.Remove( Controller );
			Components.Remove( Camera );
			Components.Remove( Animator );
			Components.Remove( Inventory );
			Components.Remove( FallDamage );
			Components.Create<SpectatorCameraComponent>();
		}

		if ( Game.IsClient )
		{
			var start = AimRay.Position;
			var end = AimRay.Position + AimRay.Forward * 5000;
			
			var trace = Trace.Ray( start, end )
					.UseHitboxes()
					.WithAnyTags( "solid", "livingplayer" )
					.Ignore( this )
					.Size( 1f );
			
			var tr = trace.Run();
			if ( tr.Hit && tr.Entity.IsValid() && tr.Entity is Player player )
			{
				LookingAt = player;
			}
			else
			{
				LookingAt = null;
			}
		}
			

	}

	public override void BuildInput()
	{
		Controller?.BuildInput();
		Camera?.BuildInput();
	}

	public override void FrameSimulate( IClient cl )
	{
		Controller?.FrameSimulate( cl );
		Camera?.FrameSimulate( cl );
	}

	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();
	}

}