Files
sbox-public/engine/Sandbox.Engine/Systems/SceneSystem/SceneModel.Parameters.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

228 lines
4.4 KiB
C#

using NativeEngine;
namespace Sandbox;
/// <summary>
/// A model scene object that supports animations and can be rendered within a <see cref="SceneWorld"/>.
/// </summary>
public sealed partial class SceneModel : SceneObject
{
bool FindAnimParam( string name, out IAnimParameterInstance p )
{
p = default;
if ( !AnimationGraph.IsValid() )
return false;
if ( !AnimationGraph.TryGetParameterIndex( name, out var index ) )
return false;
p = animNative.GetAnimParameter( index );
return p.IsValid;
}
/// <summary>
/// Sets a boolean animation graph parameter by name.
/// </summary>
public void SetAnimParameter( string name, bool value )
{
if ( FindAnimParam( name, out var p ) )
{
var t = p.GetParameterType();
if ( t == AnimParamType.Bool )
{
p.SetValue( value );
return;
}
if ( t == AnimParamType.Int )
{
p.SetValue( value ? 1 : 0 );
return;
}
if ( t == AnimParamType.Float )
{
p.SetValue( value ? 1.0f : 0.0f );
return;
}
Log.Warning( $"SetBool: {t}" );
}
}
/// <summary>
/// Sets a float animation graph parameter by name.
/// </summary>
public void SetAnimParameter( string name, float value )
{
if ( !FindAnimParam( name, out var p ) )
return;
var t = p.GetParameterType();
if ( t == AnimParamType.Bool )
{
p.SetValue( !value.AlmostEqual( 0.0f ) );
return;
}
if ( t == AnimParamType.Float )
{
p.SetValue( value );
return;
}
if ( t == AnimParamType.Int )
{
p.SetValue( (int)value );
return;
}
if ( t == AnimParamType.Enum )
{
p.SetEnumValue( (int)value );
return;
}
Log.Warning( $"SetFloat: {t}" );
}
/// <summary>
/// Sets an enum animation graph parameter by option name (e.g. "pistol" on "holdtype").
/// </summary>
public bool SetAnimParameter( string name, string option )
{
if ( !FindAnimParam( name, out var p ) )
return false;
var t = p.GetParameterType();
if ( t == AnimParamType.Enum )
{
if ( AnimationGraph.TryGetEnumOptionIndex( name, option, out var index ) )
{
p.SetEnumValue( index );
return true;
}
Log.Warning( $"SetAnimParameter: enum \"{name}\" has no option \"{option}\"" );
return false;
}
return false;
}
/// <summary>
/// Sets a vector animation graph parameter by name.
/// </summary>
public void SetAnimParameter( string name, Vector3 value )
{
if ( FindAnimParam( name, out var p ) )
{
var t = p.GetParameterType();
if ( t == AnimParamType.Vector )
{
p.SetValue( value );
return;
}
Log.Warning( $"SetVector: {t}" );
}
}
/// <summary>
/// Sets a integer animation graph parameter by name.
/// </summary>
public void SetAnimParameter( string name, int value )
{
if ( FindAnimParam( name, out var p ) )
{
var t = p.GetParameterType();
if ( t == AnimParamType.Bool )
{
p.SetValue( value != 0 );
return;
}
if ( t == AnimParamType.Float )
{
p.SetValue( (float)value );
return;
}
if ( t == AnimParamType.Int )
{
p.SetValue( value );
return;
}
if ( t == AnimParamType.Enum )
{
p.SetEnumValue( value );
return;
}
Log.Warning( $"Set int: {t}" );
}
}
/// <summary>
/// Sets a rotation animation graph parameter by name.
/// </summary>
public void SetAnimParameter( string name, Rotation value )
{
if ( FindAnimParam( name, out var p ) )
{
var t = p.GetParameterType();
if ( t == AnimParamType.Rotation )
{
p.SetValue( value );
return;
}
Log.Warning( $"Set rot: {t}" );
}
}
/// <summary>
/// Reset all animgraph parameters to their default values.
/// </summary>
public void ResetAnimParameters()
{
animNative.ResetGraphParameters();
}
/// <summary>
/// Get an animated parameter
/// </summary>
public Rotation GetRotation( string name ) => animNative.GetParameterRotation( name );
/// <summary>
/// Get an animated parameter
/// </summary>
public Vector3 GetVector3( string name ) => animNative.GetParameterVector3( name );
/// <summary>
/// Get an animated parameter
/// </summary>
public bool GetBool( string name ) => animNative.GetParameterInt( name ) != 0;
/// <summary>
/// Get an animated parameter
/// </summary>
public float GetFloat( string name ) => animNative.GetParameterFloat( name );
/// <summary>
/// Get an animated parameter
/// </summary>
public int GetInt( string name ) => animNative.GetParameterInt( name );
}