Files

169 lines
3.6 KiB
C#
Raw Permalink Normal View History

2025-05-19 23:49:00 +02:00
2025-05-19 22:06:40 +02:00
using System.Threading.Tasks;
using Godot;
2025-04-06 18:46:59 +02:00
namespace Babushka.scripts.CSharp.Common.CharacterControls;
public partial class Player2D : CharacterBody2D
{
[Export] private float _speed = 100f;
2025-04-30 17:36:38 +02:00
[Export] private AnimatedSprite2D _sprite;
2025-05-19 23:49:00 +02:00
[Export] private SceneTree.GroupCallFlags _fieldFlags;
2025-04-30 17:36:38 +02:00
2025-05-19 21:17:50 +02:00
// -1 means no tool.
private int _toolID = -1;
private string _toolString;
2025-04-30 17:36:38 +02:00
private bool anyActionPressed;
2025-05-19 22:06:40 +02:00
private bool _wateringInProgress;
2025-05-19 22:39:30 +02:00
private bool _pickupAnimationInProgress;
2025-05-19 23:49:00 +02:00
private bool _farmingAnimationInProgress;
2025-05-19 21:17:50 +02:00
private Vector2 _lastDirection = Vector2.Zero;
2025-05-19 23:49:00 +02:00
public override void _Process(double delta)
{
2025-04-30 17:36:38 +02:00
anyActionPressed = false;
2025-05-19 22:39:30 +02:00
2025-05-19 23:49:00 +02:00
if (_pickupAnimationInProgress || _wateringInProgress || _farmingAnimationInProgress)
2025-05-19 22:39:30 +02:00
return;
2025-04-30 17:36:38 +02:00
if (Input.IsActionPressed("move_right"))
{
Velocity = new Vector2(_speed, 0);
MoveAndSlide();
2025-04-30 17:36:38 +02:00
_sprite.FlipH = false;
2025-05-19 21:17:50 +02:00
_sprite.Animation = "side walking" + _toolString;
2025-04-30 17:36:38 +02:00
anyActionPressed = true;
2025-05-19 21:17:50 +02:00
_lastDirection = Vector2.Right;
2025-04-30 17:36:38 +02:00
}
if (Input.IsActionPressed("move_left"))
{
Velocity = new Vector2(-_speed, 0);
MoveAndSlide();
2025-04-30 17:36:38 +02:00
_sprite.FlipH = true;
2025-05-19 21:17:50 +02:00
_sprite.Animation = "side walking" + _toolString;
2025-04-30 17:36:38 +02:00
anyActionPressed = true;
2025-05-19 21:17:50 +02:00
_lastDirection = Vector2.Left;
2025-04-30 17:36:38 +02:00
}
if (Input.IsActionPressed("move_up"))
{
Velocity = new Vector2(0, -_speed);
MoveAndSlide();
2025-05-19 21:17:50 +02:00
_sprite.Animation = "back walking" + _toolString;
2025-04-30 17:36:38 +02:00
anyActionPressed = true;
2025-05-19 21:17:50 +02:00
_lastDirection = Vector2.Up;
2025-04-30 17:36:38 +02:00
}
if (Input.IsActionPressed("move_down"))
{
Velocity = new Vector2(0, _speed);
MoveAndSlide();
2025-05-19 21:17:50 +02:00
_sprite.Animation = "front walking" + _toolString;
2025-04-30 17:36:38 +02:00
anyActionPressed = true;
2025-05-19 21:17:50 +02:00
_lastDirection = Vector2.Down;
2025-04-30 17:36:38 +02:00
}
2025-05-19 22:50:28 +02:00
if (Input.IsActionPressed("interact2"))
{
_sprite.Animation = "back interact";
anyActionPressed = true;
_lastDirection = Vector2.Up;
}
if (Input.IsActionPressed("item"))
{
_sprite.Animation = "diagonal item";
anyActionPressed = true;
_lastDirection = Vector2.Right;
}
2025-04-30 17:36:38 +02:00
if (anyActionPressed)
{
_sprite.Play();
}
else
{
2025-05-19 21:17:50 +02:00
//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)
2025-05-19 22:39:30 +02:00
{
2025-05-19 21:17:50 +02:00
_toolID = id;
2025-05-19 22:39:30 +02:00
PlayPickUpAnimation();
}
2025-05-19 21:17:50 +02:00
else _toolID = -1;
switch (_toolID)
{
case 0:
_toolString = " rake";
break;
case 1:
_toolString = " wateringcan";
break;
default:
_toolString = "";
break;
2025-04-30 17:36:38 +02:00
}
}
2025-05-19 22:06:40 +02:00
2025-05-19 23:49:00 +02:00
/// <summary>
/// Called by FarmingControls Signal.
/// </summary>
2025-05-19 22:06:40 +02:00
public void PlayWateringAnimation()
{
if (_toolID == 1 && !_wateringInProgress)
{
_sprite.Animation = "diagonal wateringcan";
_sprite.Play();
_wateringInProgress = true;
Task.Run(DelayedWateringCanReset);
}
}
private async Task DelayedWateringCanReset()
{
await Task.Delay(1000);
_wateringInProgress = false;
}
2025-05-19 22:39:30 +02:00
public void PlayPickUpAnimation()
{
_sprite.Animation = "side pickup";
_sprite.Play();
_pickupAnimationInProgress = true;
Task.Run(DelayedPickUpReset);
}
private async Task DelayedPickUpReset()
{
await Task.Delay(1000);
_pickupAnimationInProgress = false;
}
2025-05-19 22:06:40 +02:00
2025-05-19 23:49:00 +02:00
public void PlayFarmingAnimation()
{
_sprite.Animation = "diagonal farming";
_sprite.Play();
_farmingAnimationInProgress = true;
Task.Run(DelayedFarmingReset);
}
private async Task DelayedFarmingReset()
{
await Task.Delay(1000);
_farmingAnimationInProgress = false;
}
}