70 lines
2.8 KiB
C#
70 lines
2.8 KiB
C#
|
|
using Godot;
|
||
|
|
using PokemonLike.Library.Support;
|
||
|
|
|
||
|
|
namespace PokemonLike.Library.Characters;
|
||
|
|
|
||
|
|
[GlobalClass]
|
||
|
|
public partial class CharacterAnimation : AnimatedSprite2D
|
||
|
|
{
|
||
|
|
[ExportCategory("Nodes")]
|
||
|
|
[Export] public CharacterInput CharacterInput;
|
||
|
|
[Export] public CharacterMovement CharacterMovement;
|
||
|
|
|
||
|
|
[ExportCategory("Animation Vars")]
|
||
|
|
[Export] public ECharacterAnimation ECharacterAnimation = ECharacterAnimation.IdleDown;
|
||
|
|
|
||
|
|
public override void _Ready()
|
||
|
|
{
|
||
|
|
CharacterMovement.Animation += PlayAnimation;
|
||
|
|
Logger.Info("Loading player animation component ...");
|
||
|
|
}
|
||
|
|
|
||
|
|
public void PlayAnimation(string animationType)
|
||
|
|
{
|
||
|
|
ECharacterAnimation previousAnimation = ECharacterAnimation;
|
||
|
|
|
||
|
|
if (CharacterMovement.IsMoving()) return;
|
||
|
|
|
||
|
|
switch (animationType)
|
||
|
|
{
|
||
|
|
case "walk":
|
||
|
|
if (CharacterInput.Direction == Vector2.Up)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.WalkUp;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Left)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.WalkLeft;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Right)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.WalkRight;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Down)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.WalkDown;
|
||
|
|
break;
|
||
|
|
case "turn":
|
||
|
|
if (CharacterInput.Direction == Vector2.Up)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.TurnUp;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Left)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.TurnLeft;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Right)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.TurnRight;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Down)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.TurnDown;
|
||
|
|
break;
|
||
|
|
case "idle":
|
||
|
|
if (CharacterInput.Direction == Vector2.Up)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.IdleUp;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Left)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.IdleLeft;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Right)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.IdleRight;
|
||
|
|
else if (CharacterInput.Direction == Vector2.Down)
|
||
|
|
ECharacterAnimation = ECharacterAnimation.IdleDown;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (previousAnimation != ECharacterAnimation)
|
||
|
|
{
|
||
|
|
Logger.Debug($"Playing animation {ECharacterAnimation}");
|
||
|
|
Play(ECharacterAnimation.ToString().ToSnakeCase());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|