cirnogodot/Scripts/Components/Actors/PlayerAnimationProvider.cs

206 lines
5.6 KiB
C#
Raw Normal View History

2025-03-01 14:08:31 +01:00
using System.Threading.Tasks;
using Cirno.Scripts;
using Cirno.Scripts.Components;
2025-02-28 22:49:55 +01:00
using Godot;
2025-03-01 14:08:31 +01:00
using GTweens.Builders;
using GTweens.Tweens;
using GTweensGodot.Extensions;
2025-02-28 22:49:55 +01:00
public partial class PlayerAnimationProvider : Node2D
{
[Export]
public AnimatedSprite2D _animatedSprite {get; private set;}
2025-03-01 14:08:31 +01:00
[Export] private PackedScene _deathParticles;
[Export] private GpuParticles2D _shieldParticles;
2025-02-28 22:49:55 +01:00
[ExportCategory("Animation Names")]
[Export]
2025-03-02 11:58:30 +01:00
public StringName WalkRightAnimationName {get; private set;} = "walk_right";
2025-02-28 22:49:55 +01:00
[Export]
2025-03-02 11:58:30 +01:00
public StringName WalkLeftAnimationName {get; private set;} = "walk_left";
2025-02-28 22:49:55 +01:00
[Export]
2025-03-02 11:58:30 +01:00
public StringName WalkDownAnimationName {get; private set;} = "walk_down";
2025-02-28 22:49:55 +01:00
[Export]
2025-03-02 11:58:30 +01:00
public StringName WalkUpAnimationName {get; private set;} = "walk_up";
2025-03-11 17:58:46 +01:00
[Export]
public StringName DrowningAnimationName {get; private set;} = "Drowning";
2025-02-28 22:49:55 +01:00
2025-03-01 14:08:31 +01:00
[ExportCategory("Shaders")]
[Export] public ShaderMaterial BlinkMaterial {get; private set;}
2025-03-02 11:58:30 +01:00
[Export] public StringName BlinkShaderPropertyName { get; private set; } = new("blink_intensity");
[Export] public StringName TeleportProgressPropertyName { get; private set; } = new("teleport_progress");
[Export] public StringName ScanlineDensityPropertyName { get; private set; } = new("scanline_density");
2025-03-01 14:08:31 +01:00
private GTween _blinkTween;
2025-03-11 17:58:46 +01:00
[Signal] public delegate void OnAnimationEndedEventHandler(StringName animationName);
public override void _Ready()
{
_animatedSprite.AnimationFinished += () => EmitSignal(SignalName.OnAnimationEnded, _animatedSprite.Animation);
}
2025-03-02 11:58:30 +01:00
public void ShowSprite()
{
_animatedSprite.Show();
}
public void HideSprite()
{
_animatedSprite.Hide();
}
2025-04-08 10:44:06 +02:00
public void SetAnimationSpeed(Vector2 velocity)
{
if (velocity.Length() == 0)
{
_animatedSprite.SpeedScale = 0;
}
else
{
if (velocity.Length() > 40)
{
_animatedSprite.SpeedScale = 1;
}
else
{
_animatedSprite.SpeedScale = 0.8f;
}
}
}
2025-05-24 22:45:17 +02:00
public void SweepSprite(float angle, float sweepAngle)
{
if (_animatedSprite == null) return;
var frames = _animatedSprite.SpriteFrames.GetFrameCount("default");
// Map angle (-SweepAngle/2 to +SweepAngle/2) to frame (0 to 5)
float normalizedAngle = (angle + (sweepAngle / 2)) / sweepAngle;
int frame = Mathf.Clamp((int)(normalizedAngle * frames), 0, frames);
_animatedSprite.Frame = frame;
}
2025-03-01 14:08:31 +01:00
2025-04-08 10:44:06 +02:00
public void SetAnimation(Vector2 direction)
2025-02-28 22:49:55 +01:00
{
2025-04-08 10:44:06 +02:00
if (direction == Vector2.Zero)
return;
2025-02-28 22:49:55 +01:00
2025-04-08 10:44:06 +02:00
float angle = Mathf.RadToDeg(direction.Angle());
// Normalize to 0-360
if (angle < 0)
angle += 360;
string animToPlay;
switch (angle)
2025-02-28 22:49:55 +01:00
{
2025-04-08 10:44:06 +02:00
case >= 45 and < 135:
_animatedSprite.Play(WalkDownAnimationName);
break;
case >= 135 and < 225:
_animatedSprite.Play(WalkLeftAnimationName);
break;
case >= 225 and < 315:
_animatedSprite.Play(WalkUpAnimationName);
break;
default:
_animatedSprite.Play(WalkRightAnimationName);
break;
2025-02-28 22:49:55 +01:00
}
2025-04-08 10:44:06 +02:00
2025-02-28 22:49:55 +01:00
}
2025-03-01 14:08:31 +01:00
public void Blink()
{
if (BlinkMaterial == null) return;
_animatedSprite.Material = BlinkMaterial;
var material = ((ShaderMaterial)_animatedSprite.Material);
_blinkTween?.Kill();
_blinkTween = GTweenSequenceBuilder.New()
.Append(material.TweenPropertyFloat(BlinkShaderPropertyName, 1f, 0f))
.Append(material.TweenPropertyFloat(BlinkShaderPropertyName, 0f, 0.5f))
.Build();
_blinkTween.Play();
}
2025-03-02 11:58:30 +01:00
public void PlayTeleportAnimation()
{
if (BlinkMaterial == null) return;
_animatedSprite.Material = BlinkMaterial;
var material = ((ShaderMaterial)_animatedSprite.Material);
_blinkTween?.Kill();
_blinkTween = GTweenSequenceBuilder.New()
.Append(material.TweenPropertyFloat(TeleportProgressPropertyName, 0f, 0f))
.Append(material.TweenPropertyFloat(ScanlineDensityPropertyName, 0f, 0f))
.Append(material.TweenPropertyFloat(ScanlineDensityPropertyName,50f,0.5f))
.Join(material.TweenPropertyFloat(TeleportProgressPropertyName, 1f,0.5f))
.Build();
_blinkTween.Play();
}
public void PlayUnteleportAnimation()
{
if (BlinkMaterial == null) return;
_animatedSprite.Material = BlinkMaterial;
var material = ((ShaderMaterial)_animatedSprite.Material);
_blinkTween?.Kill();
_blinkTween = GTweenSequenceBuilder.New()
.Append(material.TweenPropertyFloat(TeleportProgressPropertyName, 1f, 0f))
.Append(material.TweenPropertyFloat(ScanlineDensityPropertyName, 50f, 0f))
.Append(material.TweenPropertyFloat(ScanlineDensityPropertyName,0f,0.5f))
.Join(material.TweenPropertyFloat(TeleportProgressPropertyName, 0f,0.5f))
.Build();
_blinkTween.Play();
}
2025-03-01 14:08:31 +01:00
private void SetShaderTeleportProgress(float value)
{
((ShaderMaterial)_animatedSprite.Material).SetShaderParameter("teleport_progress", value);
}
private void SetShaderScanlineDensity(float value)
{
((ShaderMaterial)_animatedSprite.Material).SetShaderParameter("scanline_density", value);
}
private void SetShaderBlinkIntensity(float newValue)
{
((ShaderMaterial)_animatedSprite.Material).SetShaderParameter("blink_intensity", newValue);
}
public void PlayDeathAnimation()
{
2025-03-21 15:16:50 +01:00
if (_deathParticles is null) return;
2025-03-01 14:08:31 +01:00
this.CreateSibling<AutodeleteParticle>(_deathParticles, this.GlobalPosition);
_animatedSprite.Visible = false;
}
2025-03-11 17:58:46 +01:00
public void PlayDrowningAnimation()
{
_animatedSprite.Visible = true;
_animatedSprite.Play(DrowningAnimationName);
_animatedSprite.SpeedScale = 1;
}
2025-03-01 14:08:31 +01:00
public void PlayShieldAnimation()
{
2025-05-05 15:44:31 +02:00
if (_shieldParticles is null) return;
2025-03-01 14:08:31 +01:00
_shieldParticles.Emitting = true;
}
2025-03-02 11:58:30 +01:00
2025-02-28 22:49:55 +01:00
}