aboutsummaryrefslogtreecommitdiffstats
path: root/code/weapon/Weapon.cs
blob: 6d6fc20d6d9eb421b8f78e5a9915445a965703f2 (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
using Sandbox;
using System.Collections.Generic;

namespace MurderGame;

public partial class Weapon : AnimatedEntity
{
	public WeaponViewModel ViewModelEntity { get; protected set; }
	public BaseViewModel HandModelEntity { get; protected set; }

	public Player Pawn => Owner as Player;

	public AnimatedEntity EffectEntity => Camera.FirstPersonViewer == Owner ? ViewModelEntity : this;

	public virtual string ViewModelPath => null;
	public virtual string ModelPath => null;
	public virtual string HandsModelPath => null;

	public virtual float PrimaryRate => 5f;
	public virtual float ReloadTime => 3.5f;

	[Net, Predicted] public TimeSince TimeSincePrimaryAttack { get; set; }

	[Net, Predicted] public TimeSince TimeSinceReload { get; set; }
	[Net, Predicted] public TimeUntil TimeUntilReloadComplete { get; set; }
	[Net, Predicted] public bool Reloading { get; set; }

	[Net, Predicted] public int Ammo { get; set; }
	[Net, Predicted] public int MaxAmmo { get; set; }

	public override void Spawn()
	{
		EnableHideInFirstPerson = true;
		EnableShadowInFirstPerson = true;
		EnableDrawing = false;

		if ( ModelPath != null )
		{
			SetModel( ModelPath );
		}
	}

	public void ChangeOwner( Player pawn )
	{
		Owner = pawn;
		SetParent( pawn, true );
	}

	public void OnEquip( Player pawn )
	{
		if (Owner == null)
		{
			Owner = pawn;
			SetParent( pawn, true );
		}
		EnableDrawing = true;
		CreateViewModel( To.Single( pawn ) );
	}

	public void OnHolster()
	{
		Reloading = false;
		EnableDrawing = false;
		DestroyViewModel( To.Single( Owner ) );
		Owner = null;
	}

	public override void Simulate( IClient player )
	{
		Animate();
		if (Reloading && TimeUntilReloadComplete)
		{
			Reloading = false;
			Ammo = MaxAmmo;
		}

		if ( CanPrimaryAttack() )
		{
			using ( LagCompensation() )
			{
				TimeSincePrimaryAttack = 0;
				PrimaryAttack();
			}
		}
		else if (Input.Down("reload") && !Reloading && Ammo != MaxAmmo)
		{
			Reload();
			Reloading = true;
			TimeUntilReloadComplete = ReloadTime;
		}
	}

	public virtual bool CanPrimaryAttack()
	{
		if ( !Owner.IsValid() || !Input.Down( "attack1" ) ) return false;

		var rate = PrimaryRate;
		if ( rate <= 0 ) return true;

		return !Reloading && TimeSincePrimaryAttack > (1 / rate);
	}

	public virtual void PrimaryAttack()
	{
	}

	public virtual void Reload()
	{
	}

	protected virtual void Animate()
	{
	}

	public virtual IEnumerable<TraceResult> TraceBullet( Vector3 start, Vector3 end, float radius = 2.0f )
	{
		bool underWater = Trace.TestPoint( start, "water" );

		var trace = Trace.Ray( start, end )
				.UseHitboxes()
				.WithAnyTags( "solid", "livingplayer", "npc" )
				.Ignore( this )
				.Size( radius );

		if ( !underWater )
			trace = trace.WithAnyTags( "water" );

		var tr = trace.Run();

		if ( tr.Hit )
			yield return tr;
	}

	public virtual void ShootBullet( Vector3 pos, Vector3 dir, float spread, float force, float damage, float bulletSize )
	{
		var forward = dir;
		forward += (Vector3.Random + Vector3.Random + Vector3.Random + Vector3.Random) * spread * 0.25f;
		forward = forward.Normal;

		foreach ( var tr in TraceBullet( pos, pos + forward * 5000, bulletSize ) )
		{
			tr.Surface.DoBulletImpact( tr );

			if ( !Game.IsServer ) continue;
			if ( !tr.Entity.IsValid() || !tr.Entity.Tags.Has("player") ) continue;

			using ( Prediction.Off() )
			{
				var damageInfo = DamageInfo.FromBullet( tr.EndPosition, forward * 100 * force, damage )
					.UsingTraceResult( tr )
					.WithAttacker( Owner )
					.WithWeapon( this );

				tr.Entity.TakeDamage( damageInfo );
			}
		}
	}

	public virtual void ShootBullet( float force, float damage, float bulletSize )
	{
		Game.SetRandomSeed( Time.Tick );

		var ray = Owner.AimRay;
		ShootBullet( ray.Position, ray.Forward, 0, force, damage, bulletSize );
	}
	
	public virtual bool Melee( float force, float damage )
	{
		var ray = Owner.AimRay;
		var forward = ray.Forward.Normal;
		var pos = ray.Position;
		bool hit = false;

		foreach (var tr in TraceBullet(pos, pos + forward * 50, 20))
		{
			tr.Surface.DoBulletImpact(tr);
			hit = true;

			if ( !Game.IsServer ) continue;
			if ( !tr.Entity.IsValid() || !tr.Entity.Tags.Has("player") ) continue;

			using (Prediction.Off())
			{
				var damageInfo = DamageInfo.FromBullet( tr.EndPosition, forward.Normal * 100 * force, damage )
					.UsingTraceResult(tr)
					.WithAttacker(Owner)
					.WithWeapon(this);

				tr.Entity.TakeDamage(damageInfo);
			}
		}
		return hit;
	}

	[ClientRpc]
	public void CreateViewModel()
	{
		DestroyViewModel();
		if ( ViewModelPath == null ) return;

		var vm = new WeaponViewModel( this );
		vm.Model = Model.Load( ViewModelPath );
		vm.Owner = Owner;
		vm.Parent = Game.LocalPawn;
		ViewModelEntity = vm;
		if (!string.IsNullOrEmpty(HandsModelPath))
		{
			HandModelEntity = new BaseViewModel();
			HandModelEntity.Owner = Owner;
			HandModelEntity.EnableViewmodelRendering = true;
			HandModelEntity.SetModel(HandsModelPath);
			HandModelEntity.SetParent(ViewModelEntity, true);
		}
	}

	[ClientRpc]
	public void DestroyViewModel()
	{
		if ( ViewModelEntity.IsValid() )
		{
			ViewModelEntity.Delete();
		}
		if ( HandModelEntity.IsValid() )
		{
			HandModelEntity.Delete();
		}
	}
}