aboutsummaryrefslogtreecommitdiffstats
path: root/code/weapon/Weapon.cs
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2023-08-06 17:21:45 +0100
committerLeonardo Bishop <me@leonardobishop.com>2023-08-06 17:21:45 +0100
commit0bba89a7858ce091f485e04284f853e1f7f304af (patch)
tree89ba42616e0f245cc84c96cc5b353eea117057d0 /code/weapon/Weapon.cs
parent87d74b50bf443bf199be05bd03afdca6ece082ff (diff)
ReformatHEADmaster
Diffstat (limited to 'code/weapon/Weapon.cs')
-rw-r--r--code/weapon/Weapon.cs103
1 files changed, 67 insertions, 36 deletions
diff --git a/code/weapon/Weapon.cs b/code/weapon/Weapon.cs
index 8eafbc3..2ee5889 100644
--- a/code/weapon/Weapon.cs
+++ b/code/weapon/Weapon.cs
@@ -1,5 +1,5 @@
-using Sandbox;
using System.Collections.Generic;
+using Sandbox;
namespace MurderGame;
@@ -19,14 +19,14 @@ public partial class Weapon : AnimatedEntity
public virtual float PrimaryRate => 5f;
public virtual float ReloadTime => 3.5f;
- [Net, Predicted] public TimeSince TimeSincePrimaryAttack { get; set; }
+ [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 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; }
+ [Net] [Predicted] public int Ammo { get; set; }
+ [Net] [Predicted] public int MaxAmmo { get; set; }
public override void Spawn()
{
@@ -48,11 +48,12 @@ public partial class Weapon : AnimatedEntity
public void OnEquip( Player pawn )
{
- if (Owner == null)
+ if ( Owner == null )
{
Owner = pawn;
SetParent( pawn, true );
}
+
EnableDrawing = true;
CreateViewModel( To.Single( pawn ) );
}
@@ -68,7 +69,7 @@ public partial class Weapon : AnimatedEntity
public override void Simulate( IClient player )
{
Animate();
- if (Reloading && TimeUntilReloadComplete)
+ if ( Reloading && TimeUntilReloadComplete )
{
Reloading = false;
Ammo = MaxAmmo;
@@ -82,7 +83,7 @@ public partial class Weapon : AnimatedEntity
PrimaryAttack();
}
}
- else if (Input.Down("reload") && !Reloading && Ammo != MaxAmmo)
+ else if ( Input.Down( "reload" ) && !Reloading && Ammo != MaxAmmo )
{
Reload();
Reloading = true;
@@ -92,12 +93,18 @@ public partial class Weapon : AnimatedEntity
public virtual bool CanPrimaryAttack()
{
- if ( !Owner.IsValid() || !Input.Down( "attack1" ) ) return false;
+ if ( !Owner.IsValid() || !Input.Down( "attack1" ) )
+ {
+ return false;
+ }
var rate = PrimaryRate;
- if ( rate <= 0 ) return true;
+ if ( rate <= 0 )
+ {
+ return true;
+ }
- return !Reloading && TimeSincePrimaryAttack > (1 / rate);
+ return !Reloading && TimeSincePrimaryAttack > 1 / rate;
}
public virtual void PrimaryAttack()
@@ -114,24 +121,29 @@ public partial class Weapon : AnimatedEntity
public virtual IEnumerable<TraceResult> TraceBullet( Vector3 start, Vector3 end, float radius = 2.0f )
{
- bool underWater = Trace.TestPoint( start, "water" );
+ var underWater = Trace.TestPoint( start, "water" );
var trace = Trace.Ray( start, end )
- .UseHitboxes()
- .WithAnyTags( "solid", "livingplayer", "npc" )
- .Ignore( this )
- .Size( radius );
+ .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 )
+ 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;
@@ -141,8 +153,15 @@ public partial class Weapon : AnimatedEntity
{
tr.Surface.DoBulletImpact( tr );
- if ( !Game.IsServer ) continue;
- if ( !tr.Entity.IsValid() || !tr.Entity.Tags.Has("player") ) continue;
+ if ( !Game.IsServer )
+ {
+ continue;
+ }
+
+ if ( !tr.Entity.IsValid() || !tr.Entity.Tags.Has( "player" ) )
+ {
+ continue;
+ }
using ( Prediction.Off() )
{
@@ -163,32 +182,40 @@ public partial class Weapon : AnimatedEntity
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;
+ var hit = false;
- foreach (var tr in TraceBullet(pos, pos + forward * 80, 20))
+ foreach ( var tr in TraceBullet( pos, pos + forward * 80, 20 ) )
{
- tr.Surface.DoBulletImpact(tr);
+ tr.Surface.DoBulletImpact( tr );
hit = true;
- if ( !Game.IsServer ) continue;
- if ( !tr.Entity.IsValid() || !tr.Entity.Tags.Has("player") ) continue;
+ if ( !Game.IsServer )
+ {
+ continue;
+ }
+
+ if ( !tr.Entity.IsValid() || !tr.Entity.Tags.Has( "player" ) )
+ {
+ continue;
+ }
- using (Prediction.Off())
+ using ( Prediction.Off() )
{
var damageInfo = DamageInfo.FromBullet( tr.EndPosition, forward.Normal * 100 * force, damage )
- .UsingTraceResult(tr)
- .WithAttacker(Owner)
- .WithWeapon(this);
+ .UsingTraceResult( tr )
+ .WithAttacker( Owner )
+ .WithWeapon( this );
- tr.Entity.TakeDamage(damageInfo);
+ tr.Entity.TakeDamage( damageInfo );
}
}
+
return hit;
}
@@ -196,7 +223,10 @@ public partial class Weapon : AnimatedEntity
public void CreateViewModel()
{
DestroyViewModel();
- if ( ViewModelPath == null ) return;
+ if ( ViewModelPath == null )
+ {
+ return;
+ }
var vm = new WeaponViewModel( this );
vm.Model = Model.Load( ViewModelPath );
@@ -204,13 +234,13 @@ public partial class Weapon : AnimatedEntity
vm.Parent = Game.LocalPawn;
vm.SetAnimParameter( "deploy", true );
ViewModelEntity = vm;
- if (!string.IsNullOrEmpty(HandsModelPath))
+ if ( !string.IsNullOrEmpty( HandsModelPath ) )
{
HandModelEntity = new BaseViewModel();
HandModelEntity.Owner = Owner;
HandModelEntity.EnableViewmodelRendering = true;
- HandModelEntity.SetModel(HandsModelPath);
- HandModelEntity.SetParent(ViewModelEntity, true);
+ HandModelEntity.SetModel( HandsModelPath );
+ HandModelEntity.SetParent( ViewModelEntity, true );
}
}
@@ -221,6 +251,7 @@ public partial class Weapon : AnimatedEntity
{
ViewModelEntity.Delete();
}
+
if ( HandModelEntity.IsValid() )
{
HandModelEntity.Delete();