Vesna can walk diagonally now
This commit is contained in:
@@ -1,199 +0,0 @@
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Babushka.scripts.CSharp.Common.Inventory;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
public partial class Player2D : CharacterBody2D
|
||||
{
|
||||
[Export] private float _speed = 100f;
|
||||
[Export] private AnimatedSprite2D _sprite;
|
||||
[Export] private SceneTree.GroupCallFlags _fieldFlags;
|
||||
[Export] private CpuParticles2D _wateringParticles;
|
||||
|
||||
// -1 means no tool.
|
||||
private int _toolID = -1;
|
||||
private string _toolString;
|
||||
private bool anyActionPressed;
|
||||
private bool _canHandleInput = true;
|
||||
private Vector2 _lastDirection = Vector2.Zero;
|
||||
private InventoryManager _inventoryManager;
|
||||
|
||||
public bool InputEnabled
|
||||
{
|
||||
get => _canHandleInput;
|
||||
set => _canHandleInput = value;
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
InventoryManager.Instance.playerInventory.InventoryContentsChanged += HandleNewItemInInventory;
|
||||
}
|
||||
|
||||
private void HandleNewItemInInventory()
|
||||
{
|
||||
// for future Kathi: this does not, in fact, check if an item has been added only, but triggers on every content change!
|
||||
PlayPickUpAnimation();
|
||||
}
|
||||
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
anyActionPressed = false;
|
||||
|
||||
if (!_canHandleInput)
|
||||
return;
|
||||
|
||||
bool right = Input.IsActionPressed("move_right");
|
||||
bool left = Input.IsActionPressed("move_left");
|
||||
bool up = Input.IsActionPressed("move_up");
|
||||
bool down = Input.IsActionPressed("move_down");
|
||||
bool walkingAnimationPicked = false;
|
||||
|
||||
if (up)
|
||||
{
|
||||
Velocity = new Vector2(0, -_speed);
|
||||
MoveAndSlide();
|
||||
_sprite.Animation = "back walking" + _toolString;
|
||||
anyActionPressed = true;
|
||||
_lastDirection = Vector2.Up;
|
||||
walkingAnimationPicked = true;
|
||||
}
|
||||
|
||||
if (down && !walkingAnimationPicked)
|
||||
{
|
||||
Velocity = new Vector2(0, _speed);
|
||||
MoveAndSlide();
|
||||
_sprite.Animation = "front walking" + _toolString;
|
||||
anyActionPressed = true;
|
||||
_lastDirection = Vector2.Down;
|
||||
walkingAnimationPicked = true;
|
||||
}
|
||||
|
||||
if (right && !walkingAnimationPicked)
|
||||
{
|
||||
Velocity = new Vector2(_speed, 0);
|
||||
MoveAndSlide();
|
||||
_sprite.FlipH = false;
|
||||
_sprite.Animation = "side walking" + _toolString;
|
||||
anyActionPressed = true;
|
||||
_lastDirection = Vector2.Right;
|
||||
walkingAnimationPicked = true;
|
||||
}
|
||||
|
||||
if (left && !walkingAnimationPicked)
|
||||
{
|
||||
Velocity = new Vector2(-_speed, 0);
|
||||
MoveAndSlide();
|
||||
_sprite.FlipH = true;
|
||||
_sprite.Animation = "side walking" + _toolString;
|
||||
anyActionPressed = true;
|
||||
_lastDirection = Vector2.Left;
|
||||
walkingAnimationPicked = true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
*/
|
||||
|
||||
if (anyActionPressed)
|
||||
{
|
||||
_sprite.Play();
|
||||
}
|
||||
else
|
||||
{
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by FarmingControls Signal.
|
||||
/// </summary>
|
||||
public void PlayWateringAnimation()
|
||||
{
|
||||
if (_toolID == 1 && _canHandleInput)
|
||||
{
|
||||
_sprite.Animation = "diagonal wateringcan";
|
||||
_sprite.Play();
|
||||
_canHandleInput = false;
|
||||
_wateringParticles.Emitting = true;
|
||||
Task.Run(DelayedInputHandlerReset);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DelayedInputHandlerReset()
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
_wateringParticles.Emitting = false;
|
||||
_canHandleInput = true;
|
||||
}
|
||||
|
||||
public void PlayPickUpAnimation()
|
||||
{
|
||||
_sprite.Animation = "side pickup";
|
||||
_sprite.Play();
|
||||
_canHandleInput = false;
|
||||
Task.Run(DelayedInputHandlerReset);
|
||||
}
|
||||
|
||||
public void PlayFarmingAnimation()
|
||||
{
|
||||
_sprite.Animation = "diagonal farming";
|
||||
_sprite.Play();
|
||||
_canHandleInput = false;
|
||||
Task.Run(DelayedInputHandlerReset);
|
||||
}
|
||||
|
||||
|
||||
public void PlayWateringCanFillupAnimation()
|
||||
{
|
||||
_sprite.Animation = "back interact";
|
||||
_sprite.Play();
|
||||
_canHandleInput = false;
|
||||
_lastDirection = Vector2.Up;
|
||||
Task.Run(DelayedInputHandlerReset);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Babushka.scripts.CSharp.Common.Inventory;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
public partial class PlayerMovement : CharacterBody2D
|
||||
{
|
||||
[Export] private float _speed = 1000f;
|
||||
|
||||
private InventoryManager _inventoryManager;
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
bool anyActionPressed = false;
|
||||
Vector2 currentVelocity = Vector2.Zero;
|
||||
|
||||
if (!InputService.Instance.InputEnabled)
|
||||
return;
|
||||
|
||||
bool right = Input.IsActionPressed("move_right");
|
||||
bool left = Input.IsActionPressed("move_left");
|
||||
bool up = Input.IsActionPressed("move_up");
|
||||
bool down = Input.IsActionPressed("move_down");
|
||||
|
||||
if (up)
|
||||
{
|
||||
currentVelocity += new Vector2(0, -_speed);
|
||||
anyActionPressed = true;
|
||||
}
|
||||
|
||||
if (down)
|
||||
{
|
||||
currentVelocity += new Vector2(0, _speed);
|
||||
anyActionPressed = true;
|
||||
}
|
||||
|
||||
if (right)
|
||||
{
|
||||
currentVelocity += new Vector2(_speed, 0);
|
||||
|
||||
anyActionPressed = true;
|
||||
}
|
||||
|
||||
if (left)
|
||||
{
|
||||
currentVelocity += new Vector2(-_speed, 0);
|
||||
|
||||
anyActionPressed = true;
|
||||
}
|
||||
|
||||
if (anyActionPressed)
|
||||
{
|
||||
Velocity = currentVelocity;
|
||||
MoveAndSlide();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user