summaryrefslogtreecommitdiff
path: root/GameCode/Screenshaker.cs
diff options
context:
space:
mode:
Diffstat (limited to 'GameCode/Screenshaker.cs')
-rw-r--r--GameCode/Screenshaker.cs53
1 files changed, 53 insertions, 0 deletions
diff --git a/GameCode/Screenshaker.cs b/GameCode/Screenshaker.cs
new file mode 100644
index 0000000..d845eae
--- /dev/null
+++ b/GameCode/Screenshaker.cs
@@ -0,0 +1,53 @@
+using UnityEngine;
+
+public class Screenshaker : GameFeeler
+{
+ public bool ignoreTimeScale;
+
+ public float spring = 100f;
+
+ public float damper = 100f;
+
+ public float shakeforce = 10f;
+
+ public float threshold;
+
+ private Vector2 velocity;
+
+ private float clamp = 100f;
+
+ private void Update()
+ {
+ float num = Mathf.Clamp(ignoreTimeScale ? Time.unscaledDeltaTime : TimeHandler.deltaTime, 0f, 0.015f);
+ velocity -= velocity.normalized * Mathf.Pow(velocity.magnitude, 0.8f) * damper * num;
+ velocity -= (Vector2)base.transform.localPosition * num * spring;
+ base.transform.position += (Vector3)velocity * num;
+ }
+
+ private void ShakeInternal(Vector2 direction)
+ {
+ if (!(direction.magnitude < threshold))
+ {
+ direction = Vector2.ClampMagnitude(direction, clamp);
+ velocity += direction * shakeforce;
+ }
+ }
+
+ public override void OnGameFeel(Vector2 feelDirection)
+ {
+ if (!ignoreTimeScale)
+ {
+ feelDirection = Vector2.ClampMagnitude(feelDirection, clamp);
+ ShakeInternal(feelDirection);
+ }
+ }
+
+ public override void OnUIGameFeel(Vector2 feelDirection)
+ {
+ if (ignoreTimeScale)
+ {
+ feelDirection = Vector2.ClampMagnitude(feelDirection, clamp);
+ ShakeInternal(feelDirection);
+ }
+ }
+}