Face player towards aim direction

This commit is contained in:
Marco 2025-04-08 10:44:06 +02:00
commit 25a9bc1d78
9 changed files with 75 additions and 58 deletions

View file

@ -53,42 +53,56 @@ public partial class PlayerAnimationProvider : Node2D
{
_animatedSprite.Hide();
}
public void SetAnimation(Vector2 velocity)
{
if (velocity.Length() == 0)
{
_animatedSprite.SpeedScale = 0;
}
else
{
if (velocity.Length() > 40)
{
_animatedSprite.SpeedScale = 1;
}
else
{
_animatedSprite.SpeedScale = 0.8f;
}
}
if (velocity.X > 0)
public void SetAnimationSpeed(Vector2 velocity)
{
if (velocity.Length() == 0)
{
_animatedSprite.SpeedScale = 0;
}
else
{
if (velocity.Length() > 40)
{
_animatedSprite.SpeedScale = 1;
}
else
{
_animatedSprite.SpeedScale = 0.8f;
}
}
}
public void SetAnimation(Vector2 direction)
{
if (direction == Vector2.Zero)
return;
float angle = Mathf.RadToDeg(direction.Angle());
// Normalize to 0-360
if (angle < 0)
angle += 360;
string animToPlay;
switch (angle)
{
_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);
case >= 45 and < 135:
_animatedSprite.Play(WalkDownAnimationName);
break;
case >= 135 and < 225:
_animatedSprite.Play(WalkLeftAnimationName);
break;
case >= 225 and < 315:
_animatedSprite.Play(WalkUpAnimationName);
break;
default:
_animatedSprite.Play(WalkRightAnimationName);
break;
}
}
public void Blink()

View file

@ -13,7 +13,7 @@ public partial class AnimationModule : ModuleBase<EnemyState, CharacterBody2D>
public override void EnterState(EnemyState state)
{
AnimationProvider.SetAnimation(StorageModule.FacingDirection);
AnimationProvider.SetAnimation(StorageModule.AimingDirection);
AnimationProvider.SetAnimation(Vector2.Zero);
}
@ -29,7 +29,7 @@ public partial class AnimationModule : ModuleBase<EnemyState, CharacterBody2D>
public override void Process(double delta)
{
AnimationProvider.SetAnimation(StorageModule.FacingDirection);
AnimationProvider.SetAnimation(StorageModule.AimingDirection);
if (_machine.MainObject.Velocity == Vector2.Zero)
{

View file

@ -98,7 +98,8 @@ public partial class Active : PlayerStateBase
public override void ExitState()
{
base.ExitState();
_animationProvider.SetAnimation(Vector2.Zero);
_animationProvider.SetAnimationSpeed(Vector2.Zero);
//_animationProvider.SetAnimation(Vector2.Zero);
_crosshairProvider.Hide();
_hitboxSpriteProvider.Hide();
@ -147,7 +148,8 @@ public partial class Active : PlayerStateBase
}
// }
_animationProvider.SetAnimation(MainObject.Velocity);
_animationProvider.SetAnimationSpeed(MainObject.Velocity);
_animationProvider.SetAnimation(FacingDirection);
_crosshairProvider.UpdatePosition(FacingDirection);

View file

@ -15,11 +15,12 @@ public partial class Cutscene : PlayerStateBase
public override void ExitState()
{
_animationProvider.SetAnimation(Vector2.Zero);
_animationProvider.SetAnimationSpeed(Vector2.Zero);
}
public override void ProcessState(double delta)
{
_animationProvider.SetAnimationSpeed(MainObject.Velocity);
_animationProvider.SetAnimation(MainObject.Velocity);
}

View file

@ -8,5 +8,6 @@ public partial class PlayerStorageModule : Node2D
public PlayerFSMProxy Root { get; private set; }
public Node2D RootAsNode => Root;
public Vector2 FacingDirection { get; set; }
public Vector2 AimingDirection { get; set; }
}