swb_player/PlayerBase.ScreenShake.cs

Part of the PlayerBase class that implements screen shake behavior. It stores the last requested ScreenShake, tracks time since the shake started, applies periodic random positional and rotational offsets to the camera, and triggers controller haptics when appropriate.

Native Interop
using SWB.Shared;
using System;

namespace SWB.Player;

public partial class PlayerBase
{
	ScreenShake lastScreenShake;
	RealTimeSince timeSinceShake;
	float nextShake;

	public virtual void ShakeScreen( ScreenShake screenShake )
	{
		lastScreenShake = screenShake;
		timeSinceShake = 0;
		nextShake = 0;
	}

	public virtual void HandleScreenShake()
	{
		if ( timeSinceShake < lastScreenShake?.Duration && timeSinceShake > nextShake )
		{
			var random = Random.Shared;
			var randomPos = new Vector3( random.Float( 0, lastScreenShake.Size ), random.Float( 0, lastScreenShake.Size ), random.Float( 0, lastScreenShake.Size ) );
			var randomRot = new Angles( random.Float( 0, lastScreenShake.Rotation ), random.Float( 0, lastScreenShake.Rotation ), 0 );

			CameraMovement.AnglesOffset += randomRot;
			CameraMovement.PosOffset += randomPos;
			nextShake = timeSinceShake + lastScreenShake.Delay;

			if ( IsUsingController )
				Input.TriggerHaptics( HapticEffect.HardImpact, lastScreenShake.Duration * 2, lastScreenShake.Size, lastScreenShake.Size );
		}
	}
}