Added tool animations to player

This commit is contained in:
2025-05-19 21:17:50 +02:00
parent 6d80253d9c
commit 1094fb2142
4 changed files with 2335 additions and 26 deletions
@@ -7,7 +7,11 @@ public partial class Player2D : CharacterBody2D
[Export] private float _speed = 100f;
[Export] private AnimatedSprite2D _sprite;
// -1 means no tool.
private int _toolID = -1;
private string _toolString;
private bool anyActionPressed;
private Vector2 _lastDirection = Vector2.Zero;
public override void _Process(double delta)
{
@@ -18,8 +22,9 @@ public partial class Player2D : CharacterBody2D
Velocity = new Vector2(_speed, 0);
MoveAndSlide();
_sprite.FlipH = false;
_sprite.Animation = "side_walking";
_sprite.Animation = "side walking" + _toolString;
anyActionPressed = true;
_lastDirection = Vector2.Right;
}
if (Input.IsActionPressed("move_left"))
@@ -27,24 +32,27 @@ public partial class Player2D : CharacterBody2D
Velocity = new Vector2(-_speed, 0);
MoveAndSlide();
_sprite.FlipH = true;
_sprite.Animation = "side_walking";
_sprite.Animation = "side walking" + _toolString;
anyActionPressed = true;
_lastDirection = Vector2.Left;
}
if (Input.IsActionPressed("move_up"))
{
Velocity = new Vector2(0, -_speed);
MoveAndSlide();
_sprite.Animation = "back walking";
_sprite.Animation = "back walking" + _toolString;
anyActionPressed = true;
_lastDirection = Vector2.Up;
}
if (Input.IsActionPressed("move_down"))
{
Velocity = new Vector2(0, _speed);
MoveAndSlide();
_sprite.Animation = "front walking";
_sprite.Animation = "front walking" + _toolString;
anyActionPressed = true;
_lastDirection = Vector2.Down;
}
if (anyActionPressed)
@@ -53,8 +61,33 @@ public partial class Player2D : CharacterBody2D
}
else
{
_sprite.Animation = "front idle";
//idle
if(_lastDirection == Vector2.Zero || _lastDirection == Vector2.Down)
_sprite.Animation = "front idle" + _toolString;
else if(_lastDirection == Vector2.Left || _lastDirection == Vector2.Right)
_sprite.Animation = "side idle" + _toolString;
else if(_lastDirection == Vector2.Up)
_sprite.Animation = "back idle" + _toolString;
}
}
public void ActivateTool(bool success, int id)
{
if (success)
_toolID = id;
else _toolID = -1;
switch (_toolID)
{
case 0:
_toolString = " rake";
break;
case 1:
_toolString = " wateringcan";
break;
default:
_toolString = "";
break;
}
}
}
@@ -1,3 +1,4 @@
using Babushka.scripts.CSharp.Common.Inventory;
using Godot;
using Godot.Collections;