Files
ichni_Official/Assets/Scripts/Game/GameElements/GeneralEffects/CameraShakeEffect.cs

76 lines
2.6 KiB
C#
Raw Normal View History

2025-06-03 02:42:28 -04:00
using Ichni.RhythmGame.Beatmap;
2026-03-14 03:13:10 -04:00
using UnityEngine;
2025-06-03 02:42:28 -04:00
namespace Ichni.RhythmGame
{
public class CameraShakeEffect : EffectBase
{
2026-03-14 03:13:10 -04:00
#region [] Effect Parameters
2025-06-03 02:42:28 -04:00
public float frequency;
public float amplitudeX;
public float amplitudeY;
public float amplitudeZ;
2026-03-14 03:13:10 -04:00
private Transform _cameraTransform;
#endregion
2025-06-03 02:42:28 -04:00
2026-03-14 03:13:10 -04:00
#region [] Initialization
2026-03-31 07:51:40 -04:00
public CameraShakeEffect(float effectTime, float frequency, float amplitudeX, float amplitudeY, float amplitudeZ)
: base(effectTime)
2025-06-03 02:42:28 -04:00
{
2026-03-31 07:51:40 -04:00
this.effectTime = effectTime;
2026-03-14 03:13:10 -04:00
this.frequency = frequency; // 数值越大抖动越跳跃
2025-06-03 02:42:28 -04:00
this.amplitudeX = amplitudeX;
this.amplitudeY = amplitudeY;
this.amplitudeZ = amplitudeZ;
}
2026-03-14 03:13:10 -04:00
#endregion
2025-06-03 02:42:28 -04:00
2026-03-14 03:13:10 -04:00
#region [] Effect Pattern Overrides
public override void PreExecute()
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
// 抓取摄像机 Transform
if (_cameraTransform == null)
{
_cameraTransform = GameManager.Instance.cameraManager.gameCamera.cam.transform;
}
2025-06-03 02:42:28 -04:00
}
2026-03-14 03:13:10 -04:00
public override void Execute()
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
if (_cameraTransform != null)
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
// 一套基于柏林噪声和音乐进展时间的稳固伪随机位移计算
// 因为基于 effectProgressPercent 产生,所以录像回退或者暂停时,位置百分百受控复现
float timeFactor = effectProgressPercent * frequency;
// 随着时间推移,让震动平滑消退 (1.0 -> 0.0)
float dampening = 1.0f - effectProgressPercent;
float offsetX = (Mathf.PerlinNoise(timeFactor, 0) - 0.5f) * 2f * amplitudeX * dampening;
float offsetY = (Mathf.PerlinNoise(0, timeFactor) - 0.5f) * 2f * amplitudeY * dampening;
float offsetZ = (Mathf.PerlinNoise(timeFactor, timeFactor) - 0.5f) * 2f * amplitudeZ * dampening;
2026-03-31 07:51:40 -04:00
2026-03-14 03:13:10 -04:00
// 这次应用仅在局部空间偏移,非常干净轻量
_cameraTransform.localPosition = new Vector3(offsetX, offsetY, offsetZ);
2025-06-03 02:42:28 -04:00
}
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
2026-03-14 03:13:10 -04:00
public override void Adjust() { ResetEffect(); }
public override void Recover() { ResetEffect(); }
public override void Disrupt() { ResetEffect(); }
private void ResetEffect()
{
if (_cameraTransform != null)
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
_cameraTransform.localPosition = Vector3.zero;
2025-06-03 02:42:28 -04:00
}
}
2026-03-14 03:13:10 -04:00
#endregion
2025-06-03 02:42:28 -04:00
}
2026-03-14 03:13:10 -04:00
}
2026-03-31 07:51:40 -04:00