cirnogodot/Scripts/Components/Actors/PlayerAnimationProvider.cs

106 lines
2.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]
public string WalkRightAnimationName {get; private set;} = "walk_right";
[Export]
public string WalkLeftAnimationName {get; private set;} = "walk_left";
[Export]
public string WalkDownAnimationName {get; private set;} = "walk_down";
[Export]
public string WalkUpAnimationName {get; private set;} = "walk_up";
2025-03-01 14:08:31 +01:00
[ExportCategory("Shaders")]
[Export] public ShaderMaterial BlinkMaterial {get; private set;}
[Export] public StringName BlinkShaderPropertyName { get; private set; } = new StringName("blink_intensity");
private GTween _blinkTween;
2025-02-28 22:49:55 +01:00
public void SetAnimation(Vector2 velocity)
{
if (velocity.X == 0 && velocity.Y == 0)
{
_animatedSprite.SpeedScale = 0;
}
else
{
_animatedSprite.SpeedScale = 1;
}
if (velocity.X > 0)
{
_animatedSprite.Play(WalkRightAnimationName);
}
else if (velocity.X < 0)
{
_animatedSprite.Play(WalkLeftAnimationName);
}
else if (velocity.Y > 0)
{
_animatedSprite.Play(WalkDownAnimationName);
}
else if (velocity.Y < 0)
{
_animatedSprite.Play(WalkUpAnimationName);
}
}
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();
}
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()
{
this.CreateSibling<AutodeleteParticle>(_deathParticles, this.GlobalPosition);
_animatedSprite.Visible = false;
}
public void PlayShieldAnimation()
{
_shieldParticles.Emitting = true;
}
2025-02-28 22:49:55 +01:00
}