cirnogodot/Scripts/Components/Actors/AnimationHandler.cs

98 lines
3 KiB
C#
Raw Normal View History

2025-02-18 18:18:13 +01:00
using System.Collections.Generic;
using Godot;
2025-02-18 17:40:33 +01:00
namespace Cirno.Scripts.Components.Actors;
2025-02-20 18:26:53 +01:00
public partial class AnimationHandler : ActorModule
2025-02-18 17:40:33 +01:00
{
[Export]
public AnimatedSprite2D _animatedSprite { get; protected set; }
protected Actor _parent;
2025-02-23 18:08:57 +01:00
public bool IsDestroyed => _parent.IsDestroyed;
2025-02-20 18:26:53 +01:00
public override void Init(Actor parent)
2025-02-18 17:40:33 +01:00
{
_parent = parent;
2025-02-23 19:19:12 +01:00
_parent.OnDeath += ParentOnOnDeath;
2025-02-18 17:40:33 +01:00
// var children = GetChildren();
// foreach (var child in children) {
// if (child is InputProvider inputProvider)
// {
// _inputProviders.Add(inputProvider);
// }
// }
}
2025-02-23 19:19:12 +01:00
protected virtual void ParentOnOnDeath()
{
_animatedSprite.SpeedScale = 0;
_animatedSprite.Hide();
}
2025-02-20 18:26:53 +01:00
public override void Update(double delta)
2025-02-18 17:40:33 +01:00
{
2025-02-23 18:08:57 +01:00
if (IsDestroyed) return;
2025-02-18 17:40:33 +01:00
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;
}
}
2025-02-20 18:26:53 +01:00
public override void PhysicsUpdate(double delta)
{
}
2025-02-23 17:18:48 +01:00
protected virtual string DirectionToString(Vector2 direction)
2025-02-18 17:40:33 +01:00
{
var angle = Mathf.RadToDeg(direction.Angle());
angle = Mathf.PosMod(angle, 360);
2025-02-18 18:18:13 +01:00
if (angle >= 337.5 || angle < 22.5) return _directionsTable[FacingDirection.Right];
if (angle >= 22.5 && angle < 67.5) return _directionsTable[FacingDirection.DownRight];
if (angle >= 67.5 && angle < 112.5) return _directionsTable[FacingDirection.Down];
if (angle >= 112.5 && angle < 157.5) return _directionsTable[FacingDirection.DownLeft];
if (angle >= 157.5 && angle < 202.5) return _directionsTable[FacingDirection.Left];
if (angle >= 202.5 && angle < 247.5) return _directionsTable[FacingDirection.UpLeft];
if (angle >= 247.5 && angle < 292.5) return _directionsTable[FacingDirection.Up];
if (angle >= 292.5 && angle < 337.5) return _directionsTable[FacingDirection.UpRight];
2025-02-18 17:40:33 +01:00
2025-02-18 18:18:13 +01:00
return _directionsTable[FacingDirection.Up];
}
2025-02-23 17:32:36 +01:00
protected readonly Dictionary<FacingDirection, string> _directionsTable = new()
2025-02-18 18:18:13 +01:00
{
{ FacingDirection.Right, "right" },
{ FacingDirection.Left, "left" },
{ FacingDirection.Up, "up" },
{ FacingDirection.Down, "down" },
{ FacingDirection.UpLeft, "up_left" },
{ FacingDirection.UpRight, "up_right" },
{ FacingDirection.DownLeft, "down_left" },
{ FacingDirection.DownRight, "down_right" }
};
2025-02-23 17:32:36 +01:00
protected enum FacingDirection
2025-02-18 18:18:13 +01:00
{
Up,
Down,
Left,
Right,
UpRight,
UpLeft,
DownRight,
DownLeft
2025-02-18 17:40:33 +01:00
}
}