Vesna walking and idle animations work for all movement directions.

This commit is contained in:
2025-04-09 18:14:05 +02:00
parent 64f039ba19
commit b5dcad5614
6 changed files with 51 additions and 26 deletions
@@ -1,3 +1,4 @@
using System.Diagnostics;
using Godot;
namespace Babushka.scripts.CSharp.Common.CharacterControls;
@@ -17,21 +18,25 @@ public partial class Player3D : CharacterBody3D
[Export] private AnimatedSprite3D[] _backSpritesAnimated;
private bool _sideFlipped;
private Vector2 _lastDirection;
public override void _PhysicsProcess(double delta)
{
var inputDir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
var inputDir = Input.GetVector("move_left", "move_right", "move_down", "move_up");
if (inputDir == Vector2.Zero)
{
if(_lastDirection != Vector2.Zero)
SwitchIdleSprites();
return;
}
MoveOnInput(inputDir, delta);
SwitchSprites(inputDir);
SwitchMovementSprites(inputDir);
}
private void MoveOnInput(Vector2 inputDir, double delta)
{
inputDir = inputDir.Rotated(-_camera.GlobalRotation.Y);
var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y * (-1))).Normalized();
if (direction != Vector3.Zero)
Velocity = new Vector3(direction.X * _speed * (float) delta * Scale.X, Velocity.Y, direction.Z * _speed * (float) delta * Scale.Z);
else
@@ -40,20 +45,34 @@ public partial class Player3D : CharacterBody3D
MoveAndSlide();
}
private void SwitchSprites(Vector2 inputDir)
private void SwitchIdleSprites()
{
float X = inputDir.X;
float Y = inputDir.Y;
if (X == 0.0 && Y == 0.0)
if (_lastDirection.X != 0)
{
ActivateFrontSpriteIdle(0, true, false, false);
ActivateSprite(0, false, true, false);
return;
}
if(_lastDirection.Y <= 0.0f)
ActivateSprite(0, true, false, false);
if(_lastDirection.Y > 0.0f)
ActivateSprite(0, false, false, true);
_lastDirection = Vector2.Zero;
}
private void SwitchMovementSprites(Vector2 inputDir)
{
float X = inputDir.X;
float Y = inputDir.Y;
_lastDirection = new Vector2(X, Y);
Debug.Print(_lastDirection.ToString());
if (X != 0)
{
ActivateFrontSpriteIdle(1, false, true, false);
ActivateSprite(1, false, true, false);
if (X > 0.0f)
{
@@ -74,20 +93,19 @@ public partial class Player3D : CharacterBody3D
if (Y != 0)
{
if (Y < 0.0f)
{
ActivateFrontSpriteIdle(1, false, false, true);
}
if (Y > 0.0f)
{
ActivateFrontSpriteIdle(0, true, false, false);
ActivateSprite(1, false, false, true);
}
if (Y < 0.0f)
{
ActivateSprite(1, true, false, false);
}
}
}
private void ActivateFrontSpriteIdle(int index, bool frontActive, bool sideActive, bool backActive)
private void ActivateSprite(int index, bool frontActive, bool sideActive, bool backActive)
{
DeactivateAll();
_frontSpritesAnimated[index].Visible = frontActive;