Skip to content

Commit

Permalink
Added ScreenUtil class + small reformat/relocation
Browse files Browse the repository at this point in the history
  • Loading branch information
timmybo5 committed Jul 11, 2021
1 parent fdba73d commit 5dcc01b
Show file tree
Hide file tree
Showing 23 changed files with 446 additions and 1,600 deletions.
532 changes: 266 additions & 266 deletions code/swb_base/ViewModelBase.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions code/swb_base/WeaponBase.Attack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public virtual void Attack( ClipInfo clipInfo, bool isPrimary )
(Owner as AnimEntity).SetAnimBool( "b_attack", true );

// Tell the clients to play the shoot effects
BlastUtil.ScreenShakeRPC( clipInfo.ScreenShake.Length, clipInfo.ScreenShake.Speed, clipInfo.ScreenShake.Size, clipInfo.ScreenShake.Rotation );
ScreenUtil.Shake( clipInfo.ScreenShake );
ShootEffects( clipInfo.MuzzleFlashParticle, clipInfo.BulletEjectParticle, clipInfo.ShootAnim );

if ( clipInfo.ShootSound != null )
Expand Down Expand Up @@ -100,7 +100,7 @@ async Task AsyncAttack( ClipInfo clipInfo, bool isPrimary, float delay )
TakeAmmo( 1 );

// Play shoot effects
BlastUtil.ScreenShakeRPC( clipInfo.ScreenShake.Length, clipInfo.ScreenShake.Speed, clipInfo.ScreenShake.Size, clipInfo.ScreenShake.Rotation );
ScreenUtil.Shake( clipInfo.ScreenShake );
ShootEffects( clipInfo.MuzzleFlashParticle, clipInfo.BulletEjectParticle, null );

if ( clipInfo.ShootSound != null )
Expand Down
8 changes: 4 additions & 4 deletions code/swb_base/WeaponBase.Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public virtual void FireEntity( ClipInfo clipInfo, bool isPrimary )
firedEntity.SetModel( EntityModel );

firedEntity.Owner = Owner;
firedEntity.Position = MathZ.RelativeAdd( Position, EntitySpawnOffset, Owner.EyeRot );
firedEntity.Position = MathUtil.RelativeAdd( Position, EntitySpawnOffset, Owner.EyeRot );
firedEntity.Rotation = Owner.EyeRot * Rotation.From( EntityAngles );

firedEntity.RemoveDelay = RemoveDelay;
Expand All @@ -98,7 +98,7 @@ public virtual void FireEntity( ClipInfo clipInfo, bool isPrimary )
firedEntity.IsSticky = IsSticky;
firedEntity.Damage = clipInfo.Damage;
firedEntity.Force = clipInfo.Force;
firedEntity.StartVelocity = MathZ.RelativeAdd( Vector3.Zero, EntityVelocity, Owner.EyeRot );
firedEntity.StartVelocity = MathUtil.RelativeAdd( Vector3.Zero, EntityVelocity, Owner.EyeRot );

firedEntity.Start();
}
Expand All @@ -120,7 +120,7 @@ public override void Attack( ClipInfo clipInfo, bool isPrimary )
(Owner as AnimEntity).SetAnimBool( "b_attack", true );

// Weapon anim
BlastUtil.ScreenShakeRPC( clipInfo.ScreenShake.Length, clipInfo.ScreenShake.Speed, clipInfo.ScreenShake.Size, clipInfo.ScreenShake.Rotation );
ScreenUtil.Shake( clipInfo.ScreenShake );
ShootEffects( clipInfo.MuzzleFlashParticle, clipInfo.BulletEjectParticle, clipInfo.ShootAnim );

if ( !string.IsNullOrEmpty( clipInfo.ShootSound ) )
Expand Down Expand Up @@ -161,7 +161,7 @@ async Task AsyncAttack( ClipInfo clipInfo, bool isPrimary, float delay )
TakeAmmo( 1 );

// Play shoot effects
BlastUtil.ScreenShakeRPC( clipInfo.ScreenShake.Length, clipInfo.ScreenShake.Speed, clipInfo.ScreenShake.Size, clipInfo.ScreenShake.Rotation );
ScreenUtil.Shake( clipInfo.ScreenShake );
ShootEffects( clipInfo.MuzzleFlashParticle, clipInfo.BulletEjectParticle, null );

if ( clipInfo.ShootSound != null )
Expand Down
2 changes: 1 addition & 1 deletion code/swb_base/WeaponBase.Sniper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public override void PostCameraSetup( ref CameraSetup camSetup )
if ( lerpZoomAmount == 0 )
lerpZoomAmount = camSetup.FieldOfView;

lerpZoomAmount = MathZ.FILerp( lerpZoomAmount, ZoomAmount, 10f );
lerpZoomAmount = MathUtil.FILerp( lerpZoomAmount, ZoomAmount, 10f );
camSetup.FieldOfView = lerpZoomAmount;
}
}
Expand Down
2 changes: 1 addition & 1 deletion code/swb_base/WeaponBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public override void BuildInput( InputBuilder input )
// Mouse sensitivity
if ( IsZooming )
{
input.ViewAngles = MathZ.FILerp( input.OriginalViewAngles, input.ViewAngles, AimSensitivity * 90 );
input.ViewAngles = MathUtil.FILerp( input.OriginalViewAngles, input.ViewAngles, AimSensitivity * 90 );
}

// Recoil
Expand Down
4 changes: 2 additions & 2 deletions code/swb_base/entities/explosives/Grenade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async Task DelayedExplode()
public virtual void Explode()
{
// Explosion sound
var explosionSound = MathZ.GetRandom( ExplosionSounds );
var explosionSound = MathUtil.GetRandom( ExplosionSounds );

if ( !string.IsNullOrEmpty( explosionSound ) )
PlaySound( explosionSound );
Expand All @@ -49,7 +49,7 @@ public virtual void Explode()
Particles.Create( ExplosionEffect, PhysicsBody.MassCenter );

// Screenshake
BlastUtil.ScreenShake( PhysicsBody.MassCenter, ExplosionRadius*1.5f, ExplosionShake );
ScreenUtil.ShakeAt( PhysicsBody.MassCenter, ExplosionRadius*1.5f, ExplosionShake );

// Damage
BlastUtil.Explode( PhysicsBody.MassCenter, ExplosionRadius, ExplosionDamage, ExplosionForce, Owner, Weapon, this );
Expand Down
4 changes: 2 additions & 2 deletions code/swb_base/entities/explosives/Rocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public virtual void Explode()
}

// Explosion sound
var explosionSound = MathZ.GetRandom( ExplosionSounds );
var explosionSound = MathUtil.GetRandom( ExplosionSounds );

if ( !string.IsNullOrEmpty( explosionSound ) )
PlaySound( explosionSound );
Expand All @@ -75,7 +75,7 @@ public virtual void Explode()
Particles.Create( ExplosionEffect, PhysicsBody.MassCenter );

// Screenshake
BlastUtil.ScreenShake( PhysicsBody.MassCenter, ExplosionRadius*2, ExplosionShake );
ScreenUtil.ShakeAt( PhysicsBody.MassCenter, ExplosionRadius*2, ExplosionShake );

// Damage
BlastUtil.Explode( PhysicsBody.MassCenter, ExplosionRadius, ExplosionDamage, ExplosionForce, Owner, Weapon, this );
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 0 additions & 30 deletions code/swb_base/util/BlastUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,5 @@ public static void Explode( Vector3 origin, float radius, float damage, float fo
.WithWeapon( weapon ) );
}
}

[ClientRpc]
public static void ScreenShakeRPC( float length, float speed, float size, float rotation )
{
new Sandbox.ScreenShake.Perlin( length, speed, size, rotation );
}

public static void ScreenShake( Vector3 origin, float radius, ScreenShake screenShake )
{
var objects = Physics.GetEntitiesInSphere( origin, radius );

foreach ( var obj in objects )
{
// Player check
if ( obj is not Player ply || !ply.IsValid() )
continue;

// Dist check
var targetPos = ply.PhysicsBody.MassCenter;
var dist = Vector3.DistanceBetween( origin, targetPos );
if ( dist > radius )
continue;

var distanceMul = 1.0f - Math.Clamp( dist / radius, 0.0f, 0.75f );
screenShake.Rotation *= distanceMul;
screenShake.Size *= distanceMul;

ScreenShakeRPC( To.Single( ply ), screenShake.Length, screenShake.Speed, screenShake.Size, screenShake.Rotation );
}
}
}
}
46 changes: 46 additions & 0 deletions code/swb_base/util/MathUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Sandbox;
using System;
using System.Collections.Generic;

/*
* Utility class to handle framerate independent + useful calculations
*/

namespace SWB_Base
{
class MathUtil
{
public static float FILerp( float fromF, float toF, float amount )
{
return fromF.LerpTo( toF, amount * RealTime.Delta );
}

public static Vector3 FILerp( Vector3 fromVec, Vector3 toVec, float amount )
{
return fromVec.LerpTo( toVec, amount * RealTime.Delta );
}

public static Angles FILerp( Angles fromAng, Angles toAng, float amount )
{
return Angles.Lerp( fromAng, toAng, amount * RealTime.Delta );
}

public static Vector3 RelativeAdd( Vector3 vec1, Vector3 vec2, Rotation rot )
{
vec1 += vec2.x * rot.Right;
vec1 += vec2.y * rot.Up;
vec1 += vec2.z * rot.Forward;

return vec1;
}

public static T GetRandom<T>( List<T> list )
{
if ( list.Count == 0 ) return default;

var random = new Random();
var randI = random.Next( list.Count );
return list[randI];
}
}
}
46 changes: 0 additions & 46 deletions code/swb_base/util/MathZ.cs

This file was deleted.

67 changes: 67 additions & 0 deletions code/swb_base/util/ScreenUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Sandbox;
using System;

namespace SWB_Base
{
partial class ScreenUtil
{
public static void Shake( float length = 0, float speed = 0, float size = 0, float rotation = 0 )
{
ShakeRPC( length, speed, size, rotation );
}

public static void Shake( To to, float length = 0, float speed = 0, float size = 0, float rotation = 0 )
{
ShakeRPC( to, length, speed, size, rotation );
}

public static void Shake( To to, ScreenShake screenShake )
{
if ( screenShake != null )
Shake( to, screenShake.Length, screenShake.Speed, screenShake.Size, screenShake.Rotation );
}

public static void Shake( ScreenShake screenShake )
{
if ( screenShake != null )
Shake( screenShake.Length, screenShake.Speed, screenShake.Size, screenShake.Rotation );
}

public static void ShakeAt( Vector3 origin, float radius = 1, float length = 0, float speed = 0, float size = 0, float rotation = 0 )
{
var objects = Physics.GetEntitiesInSphere( origin, radius );

foreach ( var obj in objects )
{
// Player check
if ( obj is not Player ply || !ply.IsValid() )
continue;

// Distance check
var targetPos = ply.PhysicsBody.MassCenter;
var dist = Vector3.DistanceBetween( origin, targetPos );
if ( dist > radius )
continue;

// Intensity calculation
var distanceMul = 1.0f - Math.Clamp( dist / radius, 0.0f, 0.75f );
rotation *= distanceMul;
size *= distanceMul;

ShakeRPC( To.Single( ply ), length, speed, size, rotation );
}
}

public static void ShakeAt( Vector3 position, float radius, ScreenShake screenShake )
{
if ( screenShake != null )
ShakeAt( position, radius, screenShake.Length, screenShake.Speed, screenShake.Size, screenShake.Rotation );
}

[ClientRpc]
public static void ShakeRPC( float length = 0, float speed = 0, float size = 0, float rotation = 0 )
{
new Sandbox.ScreenShake.Perlin( length, speed, size, rotation );
}
}
}
2 changes: 1 addition & 1 deletion code/swb_css/GrenadeHE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private FiredEntity CreateGrenadeEntity( ClipInfo clipInfo, bool isPrimary )
{
"css_grenade_he.explode"
};
grenade.ExplosionEffect = "particles/explosion_fireball.vpcf";
grenade.ExplosionEffect = "weapons/css_grenade_he/particles/grenade_he_explosion.vpcf";
grenade.ExplosionShake = new ScreenShake
{
Length = 1f,
Expand Down
2 changes: 1 addition & 1 deletion code/swb_css/M249HE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private FiredEntity CreateGrenadeEntity( ClipInfo clipInfo, bool isPrimary )
{
"css_grenade_he.explode"
};
grenade.ExplosionEffect = "particles/explosion_fireball.vpcf";
grenade.ExplosionEffect = "weapons/css_grenade_he/particles/grenade_he_explosion.vpcf";
grenade.ExplosionShake = new ScreenShake
{
Length = 1f,
Expand Down
52 changes: 52 additions & 0 deletions code/swb_explosives/RPG-7-Hotdog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Sandbox;
using SWB_Base;
using System;
using System.Collections.Generic;

namespace SWB_EXPLOSIVES
{
[Library( "swb_explosives_rpg7_hotdog", Title = "RPG-7-Hotdog" )]
public class RPG7Hotdog : RPG7
{
public override string ViewModelPath => "weapons/swb/explosives/rpg-7-hotdog/swb_v_rpg7_hotdog.vmdl";
public override string WorldModelPath => "weapons/swb/explosives/rpg-7-hotdog/swb_w_rpg7_hotdog.vmdl";
public override string Icon => "/swb_explosives/textures/ui/icon_rpg7.png";

public override Func<ClipInfo, bool, FiredEntity> CreateEntity => CreateHotdogEntity;
public override Vector3 EntityVelocity => new Vector3( 0, 0, 50 );
public override Angles EntityAngles => new Angles( 0, 180, 0 );
public override string EntityModel => "models/citizen_props/hotdog01.vmdl";

public RPG7Hotdog() : base() { }

private FiredEntity CreateHotdogEntity( ClipInfo clipInfo, bool isPrimary )
{
var rocket = new Rocket();
rocket.Weapon = this;
rocket.ExplosionDelay = 3f;
rocket.ExplosionRadius = 400f;
rocket.ExplosionDamage = 300f;
rocket.ExplosionForce = 500f;
rocket.ExplosionSounds = new List<string>
{
"css_grenade_he.explode"
};
rocket.ExplosionEffect = "particles/swb/explosion/hotdogs.vpcf";
rocket.RocketSound = "swb_explosives_rpg7.rocketloop";
rocket.RocketEffects = new List<string>
{
"particles/swb/smoke/swb_smoketrail_1.vpcf",
"particles/swb/fire/swb_fire_rocket_1.vpcf"
};
rocket.ExplosionShake = new ScreenShake
{
Length = 1f,
Speed = 5f,
Size = 7f,
Rotation = 3f
};

return rocket;
}
}
}
2 changes: 1 addition & 1 deletion code/swb_explosives/RPG-7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private FiredEntity CreateRocketEntity( ClipInfo clipInfo, bool isPrimary )
"swb_explosion_4",
"swb_explosion_5"
};
rocket.ExplosionEffect = "particles/explosion_fireball.vpcf";
rocket.ExplosionEffect = "weapons/css_grenade_he/particles/grenade_he_explosion.vpcf";
rocket.RocketSound = "swb_explosives_rpg7.rocketloop";
rocket.RocketEffects = new List<string>
{
Expand Down
11 changes: 0 additions & 11 deletions weapons/css_grenade_he/particles/css_grenade_he_explosion.vpcf

This file was deleted.

Binary file not shown.
Loading

0 comments on commit 5dcc01b

Please sign in to comment.