2022-11-25 11:07:23 -08:00
|
|
|
using Godot;
|
|
|
|
using System;
|
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
namespace SupaLidlGame.Utils;
|
|
|
|
|
|
|
|
public partial class PlayerCamera : Camera2D
|
2022-11-25 11:07:23 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
protected float _intensity;
|
2022-11-25 11:07:23 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
protected double _timeLeft;
|
2022-11-25 11:07:23 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
public override void _Ready()
|
|
|
|
{
|
2022-11-25 11:07:23 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
}
|
2022-11-25 11:07:23 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
public override void _Process(double delta)
|
|
|
|
{
|
|
|
|
if (_timeLeft > 0)
|
2022-11-25 11:07:23 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
_timeLeft -= delta;
|
|
|
|
Offset = RandomOffset(_intensity);
|
2022-11-25 11:07:23 -08:00
|
|
|
}
|
2023-06-03 18:21:46 -07:00
|
|
|
else
|
2022-11-25 11:07:23 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
Offset = Vector2.Zero;
|
2022-11-25 11:07:23 -08:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
if (_intensity > 0)
|
2022-11-25 11:07:23 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
_intensity = Mathf.MoveToward(_intensity, 0.0f, (float)delta);
|
2022-11-25 11:07:23 -08:00
|
|
|
}
|
|
|
|
}
|
2023-06-03 18:21:46 -07:00
|
|
|
|
|
|
|
public void Shake(float intensity, double time)
|
|
|
|
{
|
|
|
|
_intensity = Mathf.Max(_intensity, intensity);
|
|
|
|
_timeLeft = Math.Max(_timeLeft, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Vector2 RandomOffset(float intensity)
|
|
|
|
{
|
|
|
|
Vector2 ret = Vector2.Zero;
|
|
|
|
var rng = new RandomNumberGenerator();
|
|
|
|
ret.X = (rng.Randf() - 0.5f) * intensity;
|
|
|
|
ret.Y = (rng.Randf() - 0.5f) * intensity;
|
|
|
|
return ret;
|
|
|
|
}
|
2022-11-25 11:07:23 -08:00
|
|
|
}
|