Files
sbox-public/engine/Sandbox.Engine/Scene/SceneAnchor.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

61 lines
2.2 KiB
C#

using System.Text.Json.Serialization;
namespace Sandbox;
/// <summary>
/// A serializable transform that is either absolute in world space, or local to a GameObject so it
/// follows that object. It carries position and rotation - so it can describe a direction or a frame
/// as well as a location. Resolve it with <see cref="ToWorld"/>. Implicitly constructible from a
/// <see cref="Vector3"/> (a world position) or a <see cref="Transform"/> (a world transform).
/// </summary>
[Expose]
public struct SceneAnchor
{
/// <summary>
/// Position - in world space when <see cref="Parent"/> is null, otherwise local to it.
/// </summary>
[KeyProperty] public Vector3 LocalPosition { get; set; }
/// <summary>
/// Rotation - in world space when <see cref="Parent"/> is null, otherwise local to it.
/// </summary>
public Rotation LocalRotation { get; set; }
/// <summary>
/// The object this anchor is attached to, or null for a fixed world-space anchor. When set,
/// <see cref="LocalPosition"/> and <see cref="LocalRotation"/> are interpreted relative to it.
/// </summary>
public GameObject Parent { get; set; }
/// <summary>
/// True when this anchor is attached to a valid object.
/// </summary>
[JsonIgnore]
public readonly bool IsAnchored => Parent.IsValid();
/// <summary>
/// Resolves this anchor to a world-space transform.
/// </summary>
public readonly Transform ToWorld()
{
// A never-assigned Rotation is a zero quaternion, not identity - treat it as identity.
var rotation = LocalRotation == default ? Rotation.Identity : LocalRotation;
var local = new Transform( LocalPosition, rotation );
// A destroyed parent still resolves against its last transform - our local position would
// make no sense as a world position.
return Parent is not null ? Parent.WorldTransform.ToWorld( local ) : local;
}
/// <summary>
/// The resolved world-space position.
/// </summary>
[JsonIgnore]
public readonly Vector3 Position => ToWorld().Position;
public static implicit operator SceneAnchor( Vector3 position ) => new() { LocalPosition = position };
public static implicit operator SceneAnchor( Transform transform ) => new() { LocalPosition = transform.Position, LocalRotation = transform.Rotation };
}