52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using Chickensoft.AutoInject;
|
|
using Chickensoft.Introspection;
|
|
using Godot;
|
|
using System;
|
|
|
|
[Meta(typeof(IAutoNode))]
|
|
public partial class ShakeCamera : Camera3D
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
[Export] private double _shakeIntensity = 1.0;
|
|
|
|
[Export] private double _maxX = 10;
|
|
[Export] private double _maxY = 10;
|
|
[Export] private double _maxZ = 5;
|
|
|
|
[Export] private FastNoiseLite _noise;
|
|
[Export] private double _noiseSpeed = 50.0;
|
|
|
|
private double _shake = 0.0;
|
|
private double _time = 0.0;
|
|
|
|
private Vector3 _initialRotation;
|
|
|
|
public override void _Ready()
|
|
{
|
|
_initialRotation = RotationDegrees;
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
_time += delta;
|
|
_shake = Mathf.Max(_shake - delta * _shakeIntensity, 0.0);
|
|
|
|
RotationDegrees = new Vector3(
|
|
(float)(_initialRotation.X + _maxX * Mathf.Pow(_shake, 2) * GetNoiseFromSeed(0)),
|
|
(float)(_initialRotation.Y + _maxY * Mathf.Pow(_shake, 2) * GetNoiseFromSeed(1)),
|
|
(float)(_initialRotation.Z + _maxZ * Mathf.Pow(_shake, 2) * GetNoiseFromSeed(2)));
|
|
}
|
|
|
|
public void AddShake(float shakeAmount)
|
|
{
|
|
_shake = Mathf.Clamp(_shake + shakeAmount, 0.0, 1.0);
|
|
}
|
|
|
|
private double GetNoiseFromSeed(int seed)
|
|
{
|
|
_noise.Seed = seed;
|
|
return _noise.GetNoise1D((float)(_time * _noiseSpeed));
|
|
}
|
|
}
|