Inventory.cs

The Inventory component manages the visual positioning and transformation of an inventory display. It updates item placement and orientation based on whether the player is in VR or using a desktop setup, adjusting for player height.

Networking
using Sandbox;

public sealed class Inventory : Component
{
	[Property] public InventoryPanel InventoryPanel;

	// The inventory is a snapshot child, so its changing local transform needs explicit sync.
	[Sync( SyncFlags.Interpolate )] private Transform SlotTransform { get; set; } = global::Transform.Zero;
	[Sync( SyncFlags.Interpolate )] private float SlotHeightScale { get; set; } = 1f;

	private float _appliedSlotHeightScale = 1f;

	protected override void OnStart()
	{
		InventoryPanel.GameObject.Enabled = !IsProxy && !Game.IsRunningInVR;
	}

	/// <summary>
	/// Runs after this frame's locomotion and desktop mouse look. VR reads Input.VR.Head directly
	/// so it does not depend on the camera's VRTrackedObject update order.
	/// </summary>
	protected override void OnPreRender()
	{
		if ( IsProxy )
		{
			LocalTransform = SlotTransform;
			ApplyCalibratedSlotHeights( SlotHeightScale );
			return;
		}

		var player = GetComponentInParent<PlayerCharacterController>();
		if ( !player.IsValid() )
			return;

		// Bots already turn their player root and have no local camera or headset.
		if ( player.GetComponent<Player>() is { IsBot: true } )
			return;

		if ( !Game.IsRunningInVR )
		{
			if ( player.Camera.IsValid() )
			{
				WorldRotation = Rotation.FromYaw( player.Camera.WorldRotation.Angles().yaw );
				UpdateDesktopStowedItems( player.Camera.WorldRotation );
			}

			SlotTransform = LocalTransform;
			return;
		}

		var head = Input.VR.Head;

		// Yaw only so hip/chest slots stay at body height while still tracking look heading.
		WorldRotation = Rotation.FromYaw( head.Rotation.Angles().yaw );

		ApplyCalibratedSlotHeights( player.HeightScale );

		// Keep authored slot offsets relative to the head: standing matches the scaled prefab,
		// crouch/sit drops the holsters by the same amount the headset dropped.
		var standingEyeHeight = player.HeightScale * PlayerCharacterController.ReferenceStandingEyeHeight;
		var headHeight = head.Position.z - player.WorldPosition.z;
		LocalPosition = Vector3.Up * (headHeight - standingEyeHeight);
		SlotHeightScale = player.HeightScale;
		SlotTransform = LocalTransform;
	}

	private void UpdateDesktopStowedItems( Rotation lookRotation )
	{
		foreach ( var slot in GetComponentsInChildren<InventorySlot>() )
		{
			var item = slot.StoredItem;
			if ( !item.IsValid() || item.IsProxy || !item.IsStowed || item.GameObject.Parent != slot.GameObject )
				continue;

			// Desktop holsters keep the slot position, but face the view so active tools
			// (such as a flashlight) shine ahead instead of along the authored VR holster angle.
			item.WorldRotation = lookRotation;
		}
	}

	/// <summary>
	/// Prefab slots are authored for the 64" reference citizen. Scale their local height
	/// so holsters sit on the avatar after VR standing-eye-height calibration.
	/// </summary>
	private void ApplyCalibratedSlotHeights( float heightScale )
	{
		if ( heightScale <= 0f || heightScale.AlmostEqual( _appliedSlotHeightScale ) )
			return;

		var factor = heightScale / _appliedSlotHeightScale;
		foreach ( var slot in GetComponentsInChildren<InventorySlot>() )
		{
			var pos = slot.LocalPosition;
			slot.LocalPosition = pos.WithZ( pos.z * factor );
		}

		_appliedSlotHeightScale = heightScale;
	}
}