Files
sbox-public/engine/Sandbox.System/Math/Vector3.Utility.cs
Garry Newman ca96c2a945 Make Inventory, BaseWeapon, AmmoResource, Screenshake first party (#5245)
https://sbox.game/dev/doc/gameplay/inventory-weapons/

Added

InventoryComponent - a slot based inventory, hotbar or shared bucket slots, with a starting loadout
BaseInventoryItem - base class for inventory items, with hooks to veto switching, holstering and dropping
BaseWeapon - a GMod style base weapon - fire modes, magazines and reserve ammo, reloading, dry fire, ballistics, spread, crosshair and HUD
BaseWeaponModel - view/world model base with muzzle flash, brass ejection, tracers and animation hooks
Ammo types are resources (.ammo) - weapons share reserve pools by type, clamped to a max reserve
BaseWeapon.ShootBullets / ShootBullet - bullet tracing with damage, impulse and hitbox tags
Weapon hold types editor
Animgraph enum parameters can be set by option name - Renderer.Set( "holdtype", "pistol" )
NpcUsage on BaseWeapon - engagement range and burst behaviour so NPCs can use any weapon
ICameraModifier - cameras compose their view through an ordered modifier chain, read the result from CameraComponent.View
CameraEffectSystem - screen shake, directional punches and tilts, with world epicenter falloff - camera.AddShake / AddPunch / AddTilt
EnvShake component - shakes the view, rumbles controllers, kicks physics
TracerEffect - an animated tracer line, ITargetedEffect for effects that fly at a target
SceneAnchor - a world position that can follow an object or bone
SkinnedModelRenderer.GetClosestBone( worldPosition )
Vector3.WithAimCone can take a custom Random for reproducible spread
Improve/Fix

PlayerController searches parents for IPressable when pressing
PlayerController camera runs through the modifier chain - IEvents.PostCameraSetup is obsolete
SoundPlayer preview uses shortcuts - Space toggles playback, Ctrl+Space restarts
Fixed Radius( 0 ) traces missing everything
Fixed NRE in AssetPreviewWidget
Removed

Removed CameraComponent.ISceneCameraSetup (obsolete since October, nothing uses)
2026-07-04 12:46:59 +01:00

86 lines
2.7 KiB
C#

using Sandbox;
public partial struct Vector3
{
/// <summary>
/// This direction randomly deflected within a cone <paramref name="degrees"/> wide, centred on it.
/// Use for bullet spread. Pass a <paramref name="random"/> to control the randomness (e.g. a seeded
/// one, so peers agree on the deflection) - null uses the shared random.
/// </summary>
public readonly Vector3 WithAimCone( float degrees, System.Random random = null ) => WithAimCone( degrees, degrees, random );
/// <summary>
/// This direction randomly deflected within a cone <paramref name="horizontalDegrees"/> wide and
/// <paramref name="verticalDegrees"/> tall, centred on it. Use for bullet spread. Pass a
/// <paramref name="random"/> to control the randomness (e.g. a seeded one, so peers agree on the
/// deflection) - null uses the shared random.
/// </summary>
public readonly Vector3 WithAimCone( float horizontalDegrees, float verticalDegrees, System.Random random = null )
{
// LookAt on a near-zero vector would deflect it to an arbitrary direction - leave it alone.
if ( IsNearZeroLength )
return this;
random ??= System.Random.Shared;
var rotation = Rotation.LookAt( this );
rotation *= new Angles(
random.Float( -verticalDegrees, verticalDegrees ) * 0.5f,
random.Float( -horizontalDegrees, horizontalDegrees ) * 0.5f,
0 );
return rotation.Forward;
}
/// <summary>
/// Everything you need to smooth damp a Vector3. Just call Update every frame.
/// </summary>
public record struct SmoothDamped( Vector3 Current, Vector3 Target, float SmoothTime )
{
public Vector3 Velocity;
public void Update( float timeDelta )
{
Current = SmoothDamp( Current, Target, ref Velocity, SmoothTime, timeDelta );
}
}
/// <summary>
/// Everything you need to create a springy Vector3
/// </summary>
public record struct SpringDamped
{
public Vector3 Current;
public Vector3 Target;
public float Frequency;
public float Damping;
public Vector3 Velocity;
[Obsolete]
public float SmoothTime;
public SpringDamped( Vector3 current, Vector3 target, float frequency = 2.0f, float damping = 0.5f )
{
Current = current;
Target = target;
Frequency = frequency;
Damping = damping;
}
[Obsolete( "SmoothTime is deprecated and no longer used. Use the constructor without SmoothTime instead." )]
public SpringDamped( Vector3 current, Vector3 target, float smoothTime, float frequency = 2.0f, float damping = 0.5f )
{
Current = current;
Target = target;
Frequency = frequency;
Damping = damping;
}
public void Update( float timeDelta )
{
Current = SpringDamp( Current, Target, ref Velocity, timeDelta, Frequency, Damping );
}
}
}