|
| 1 | +namespace SWB.Player; |
| 2 | + |
| 3 | +public partial class PlayerBase |
| 4 | +{ |
| 5 | + Angles aimPunchAngle; |
| 6 | + float aimPunchRecoverySpeed = 5f; |
| 7 | + RealTimeSince timeSinceLastPunch; |
| 8 | + |
| 9 | + /// <summary> |
| 10 | + /// Apply aimpunch recoil to the camera |
| 11 | + /// </summary> |
| 12 | + /// <param name="punchAmount">The angular offset to apply</param> |
| 13 | + /// <param name="recoverySpeed">How fast the camera recovers (higher = faster)</param> |
| 14 | + public virtual void ApplyAimPunch( Angles punchAmount, float recoverySpeed = 5f ) |
| 15 | + { |
| 16 | + // Accumulate the punch angle |
| 17 | + aimPunchAngle += punchAmount; |
| 18 | + aimPunchRecoverySpeed = recoverySpeed; |
| 19 | + timeSinceLastPunch = 0; |
| 20 | + |
| 21 | + // Apply the kick to the camera immediately |
| 22 | + ApplyEyeAnglesOffset( punchAmount ); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Reset aimpunch angle (called on weapon switch, death, etc.) |
| 27 | + /// </summary> |
| 28 | + public virtual void ResetAimPunch() |
| 29 | + { |
| 30 | + aimPunchAngle = Angles.Zero; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Handle aimpunch recovery over time |
| 35 | + /// </summary> |
| 36 | + public virtual void HandleAimPunch() |
| 37 | + { |
| 38 | + if ( !IsAlive ) |
| 39 | + { |
| 40 | + aimPunchAngle = Angles.Zero; |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // Get the active weapon to check if player is shooting |
| 45 | + var activeWeapon = Inventory?.Active?.GetComponent<SWB.Base.Weapon>(); |
| 46 | + bool isShooting = activeWeapon?.IsShooting() ?? false; |
| 47 | + |
| 48 | + // Only recover when NOT shooting (with small delay after last shot) |
| 49 | + if ( isShooting || timeSinceLastPunch < 0.1f ) |
| 50 | + return; |
| 51 | + |
| 52 | + // Recover the punch angle back toward zero |
| 53 | + if ( aimPunchAngle.pitch != 0 || aimPunchAngle.yaw != 0 || aimPunchAngle.roll != 0 ) |
| 54 | + { |
| 55 | + var decayAmount = aimPunchRecoverySpeed * Time.Delta; |
| 56 | + |
| 57 | + // Store old angle to calculate what changed |
| 58 | + var oldPitch = aimPunchAngle.pitch; |
| 59 | + var oldYaw = aimPunchAngle.yaw; |
| 60 | + var oldRoll = aimPunchAngle.roll; |
| 61 | + |
| 62 | + // Decay toward zero |
| 63 | + aimPunchAngle = new Angles( |
| 64 | + aimPunchAngle.pitch.Approach( 0, decayAmount ), |
| 65 | + aimPunchAngle.yaw.Approach( 0, decayAmount ), |
| 66 | + aimPunchAngle.roll.Approach( 0, decayAmount ) |
| 67 | + ); |
| 68 | + |
| 69 | + // Calculate the change (this will be negative, pulling camera back) |
| 70 | + var deltaAngle = new Angles( |
| 71 | + aimPunchAngle.pitch - oldPitch, |
| 72 | + aimPunchAngle.yaw - oldYaw, |
| 73 | + aimPunchAngle.roll - oldRoll |
| 74 | + ); |
| 75 | + |
| 76 | + // Apply the recovery movement |
| 77 | + ApplyEyeAnglesOffset( deltaAngle ); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments