mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
60 lines
No EOL
1.8 KiB
C#
60 lines
No EOL
1.8 KiB
C#
using Godot;
|
|
|
|
namespace Cirno.Scripts.Components.Actors;
|
|
|
|
public partial class AnimationHandler : Node2D
|
|
{
|
|
[Export]
|
|
public AnimatedSprite2D _animatedSprite { get; protected set; }
|
|
|
|
protected Actor _parent;
|
|
|
|
public virtual void Init(Actor parent)
|
|
{
|
|
_parent = parent;
|
|
|
|
// var children = GetChildren();
|
|
// foreach (var child in children) {
|
|
// if (child is InputProvider inputProvider)
|
|
// {
|
|
// _inputProviders.Add(inputProvider);
|
|
// }
|
|
// }
|
|
}
|
|
|
|
public virtual void Update(double delta)
|
|
{
|
|
|
|
var direction = _parent.FacingDirection; //GetSnappedDirection();
|
|
|
|
if (_parent.Velocity.Length() > 0)
|
|
{
|
|
_animatedSprite.Play("walk_" + DirectionToString(direction));
|
|
_animatedSprite.SpeedScale = 1;
|
|
}
|
|
else
|
|
{
|
|
//_animatedSprite.Play("idle_" + DirectionToString(direction));
|
|
_animatedSprite.Play("walk_" + DirectionToString(direction));
|
|
_animatedSprite.SpeedScale = 0;
|
|
}
|
|
|
|
}
|
|
|
|
private string DirectionToString(Vector2 direction)
|
|
{
|
|
var angle = Mathf.RadToDeg(direction.Angle());
|
|
angle = Mathf.PosMod(angle, 360);
|
|
|
|
if (angle >= 337.5 || angle < 22.5) return "right";
|
|
if (angle >= 22.5 && angle < 67.5) return "up_right";
|
|
if (angle >= 67.5 && angle < 112.5) return "up";
|
|
if (angle >= 112.5 && angle < 157.5) return "up_left";
|
|
if (angle >= 157.5 && angle < 202.5) return "left";
|
|
if (angle >= 202.5 && angle < 247.5) return "down_left";
|
|
if (angle >= 247.5 && angle < 292.5) return "down";
|
|
if (angle >= 292.5 && angle < 337.5) return "down_right";
|
|
|
|
return "up";
|
|
}
|
|
} |