player camera

item-info
John Montagu, the 4th Earl of Sandvich 2022-11-25 11:07:23 -08:00
parent 32c5eb395e
commit 19aaffbeab
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,46 @@
using Godot;
using System;
namespace SupaLidlGame.Utils
{
public partial class PlayerCamera : Camera2D
{
protected float _intensity;
protected double _timeLeft;
public override void _Ready()
{
}
public override void _Process(double delta)
{
if (_timeLeft > 0)
{
_timeLeft -= delta;
Offset = RandomOffset(_intensity);
}
if (_intensity > 0)
{
_intensity = Mathf.MoveToward(_intensity, 0.0f, (float)delta);
}
}
public void Shake(float intensity, double time)
{
_intensity += intensity;
_timeLeft += time;
}
private Vector2 RandomOffset(float intensity)
{
Vector2 ret = Vector2.Zero;
var rng = new RandomNumberGenerator();
ret.x = (rng.Randf() * 2 - 0.5f) * intensity;
ret.y = (rng.Randf() * 2 - 0.5f) * intensity;
return ret;
}
}
}

View File

@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=3 uid="uid://cl56eadpklnbo"]
[ext_resource type="Script" path="res://Utils/PlayerCamera.cs" id="1_jabhk"]
[node name="Camera2D" type="Camera2D"]
current = true
zoom = Vector2(4, 4)
script = ExtResource("1_jabhk")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]