Vesna can walk diagonally now
This commit is contained in:
+33
-54
@@ -1,48 +1,36 @@
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Babushka.scripts.CSharp.Common.Inventory;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
namespace Babushka.scripts.CSharp.Common.Animation;
|
||||
|
||||
public partial class Player2D : CharacterBody2D
|
||||
public partial class VesnaAnimations : Node
|
||||
{
|
||||
[Export] private float _speed = 100f;
|
||||
[Export] private AnimatedSprite2D _sprite;
|
||||
[Export] private SceneTree.GroupCallFlags _fieldFlags;
|
||||
[Export] private CpuParticles2D _wateringParticles;
|
||||
[Export] private AnimatedSprite2D _sprite;
|
||||
[Export] private CpuParticles2D _wateringParticles;
|
||||
|
||||
private bool anyActionPressed;
|
||||
private string _toolString;
|
||||
private int _toolID = -1; // -1 means no tool.
|
||||
private Vector2 _lastDirection = Vector2.Zero;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
InventoryManager.Instance.playerInventory.InventoryContentsChanged += HandleNewItemInInventory;
|
||||
}
|
||||
|
||||
// -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)
|
||||
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)
|
||||
if (!InputService.Instance.InputEnabled)
|
||||
return;
|
||||
|
||||
bool right = Input.IsActionPressed("move_right");
|
||||
@@ -53,8 +41,6 @@ public partial class Player2D : CharacterBody2D
|
||||
|
||||
if (up)
|
||||
{
|
||||
Velocity = new Vector2(0, -_speed);
|
||||
MoveAndSlide();
|
||||
_sprite.Animation = "back walking" + _toolString;
|
||||
anyActionPressed = true;
|
||||
_lastDirection = Vector2.Up;
|
||||
@@ -63,8 +49,6 @@ public partial class Player2D : CharacterBody2D
|
||||
|
||||
if (down && !walkingAnimationPicked)
|
||||
{
|
||||
Velocity = new Vector2(0, _speed);
|
||||
MoveAndSlide();
|
||||
_sprite.Animation = "front walking" + _toolString;
|
||||
anyActionPressed = true;
|
||||
_lastDirection = Vector2.Down;
|
||||
@@ -73,8 +57,6 @@ public partial class Player2D : CharacterBody2D
|
||||
|
||||
if (right && !walkingAnimationPicked)
|
||||
{
|
||||
Velocity = new Vector2(_speed, 0);
|
||||
MoveAndSlide();
|
||||
_sprite.FlipH = false;
|
||||
_sprite.Animation = "side walking" + _toolString;
|
||||
anyActionPressed = true;
|
||||
@@ -84,8 +66,6 @@ public partial class Player2D : CharacterBody2D
|
||||
|
||||
if (left && !walkingAnimationPicked)
|
||||
{
|
||||
Velocity = new Vector2(-_speed, 0);
|
||||
MoveAndSlide();
|
||||
_sprite.FlipH = true;
|
||||
_sprite.Animation = "side walking" + _toolString;
|
||||
anyActionPressed = true;
|
||||
@@ -124,7 +104,7 @@ public partial class Player2D : CharacterBody2D
|
||||
_sprite.Animation = "back idle" + _toolString;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void ActivateTool(bool success, int id)
|
||||
{
|
||||
if (success)
|
||||
@@ -146,17 +126,17 @@ public partial class Player2D : CharacterBody2D
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Called by FarmingControls Signal.
|
||||
/// </summary>
|
||||
public void PlayWateringAnimation()
|
||||
{
|
||||
if (_toolID == 1 && _canHandleInput)
|
||||
if (_toolID == 1 && InputService.Instance.InputEnabled)
|
||||
{
|
||||
_sprite.Animation = "diagonal wateringcan";
|
||||
_sprite.Play();
|
||||
_canHandleInput = false;
|
||||
InputService.Instance.InputEnabled = false;
|
||||
_wateringParticles.Emitting = true;
|
||||
Task.Run(DelayedInputHandlerReset);
|
||||
}
|
||||
@@ -166,14 +146,14 @@ public partial class Player2D : CharacterBody2D
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
_wateringParticles.Emitting = false;
|
||||
_canHandleInput = true;
|
||||
InputService.Instance.InputEnabled = true;
|
||||
}
|
||||
|
||||
public void PlayPickUpAnimation()
|
||||
{
|
||||
_sprite.Animation = "side pickup";
|
||||
_sprite.Play();
|
||||
_canHandleInput = false;
|
||||
InputService.Instance.InputEnabled = false;
|
||||
Task.Run(DelayedInputHandlerReset);
|
||||
}
|
||||
|
||||
@@ -181,7 +161,7 @@ public partial class Player2D : CharacterBody2D
|
||||
{
|
||||
_sprite.Animation = "diagonal farming";
|
||||
_sprite.Play();
|
||||
_canHandleInput = false;
|
||||
InputService.Instance.InputEnabled = false;
|
||||
Task.Run(DelayedInputHandlerReset);
|
||||
}
|
||||
|
||||
@@ -190,10 +170,9 @@ public partial class Player2D : CharacterBody2D
|
||||
{
|
||||
_sprite.Animation = "back interact";
|
||||
_sprite.Play();
|
||||
_canHandleInput = false;
|
||||
_lastDirection = Vector2.Up;
|
||||
InputService.Instance.InputEnabled = false;
|
||||
Task.Run(DelayedInputHandlerReset);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://n7oihifvqp23
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public partial class PlantBehaviour2D : Node2D
|
||||
if (_field.FieldState != FieldState.Watered || _magicWordSaid != _magicWordNeeded)
|
||||
return;
|
||||
|
||||
GetTree().CallGroup("PlantGrowing", Player2D.MethodName.PlayFarmingAnimation);
|
||||
//GetTree().CallGroup("PlantGrowing", PlayerMovement.MethodName.PlayFarmingAnimation);
|
||||
// todo:
|
||||
// find out why the last plant stage is being skipped the second time around
|
||||
switch (_state)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Babushka.scripts.CSharp.Common.Animation;
|
||||
using Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
using Babushka.scripts.CSharp.Common.Inventory;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Farming;
|
||||
@@ -9,7 +11,8 @@ public partial class VesnaBehaviour2D : Node
|
||||
[ExportGroup("Farming")]
|
||||
[Export] private FieldService2D _fieldParent;
|
||||
[Export] private FarmingControls2D _farmingControls;
|
||||
[Export] private Player2D _player2d;
|
||||
[Export] private PlayerMovement _player2d;
|
||||
[Export] private VesnaAnimations _vesnaAnimations;
|
||||
[Export] private ItemResource _hoe;
|
||||
[Export] private ItemResource _wateringCan;
|
||||
|
||||
@@ -77,7 +80,7 @@ public partial class VesnaBehaviour2D : Node
|
||||
if (toolId == 1)
|
||||
{
|
||||
_farmingControls.FillWateringCan();
|
||||
_player2d.PlayWateringCanFillupAnimation();
|
||||
_vesnaAnimations.PlayWateringCanFillupAnimation();
|
||||
EmitSignal(SignalName.FilledWateringCan);
|
||||
}
|
||||
}
|
||||
@@ -85,18 +88,18 @@ public partial class VesnaBehaviour2D : Node
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Enables the character movement in the Player2D script.
|
||||
/// Enables the input.
|
||||
/// </summary>
|
||||
public void EnableMovement()
|
||||
{
|
||||
_player2d.InputEnabled = true;
|
||||
InputService.Instance.InputEnabled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disables the character movement in the Player2D script.
|
||||
/// Disables the input.
|
||||
/// </summary>
|
||||
public void DisableMovement()
|
||||
{
|
||||
_player2d.InputEnabled = false;
|
||||
InputService.Instance.InputEnabled = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Services;
|
||||
|
||||
public partial class InputService : Node
|
||||
{
|
||||
public static InputService Instance { get; private set; } = null!;
|
||||
|
||||
[Signal]
|
||||
public delegate void InputEnabledChangedEventHandler(bool enabled);
|
||||
|
||||
private static bool _inputEnabled = true;
|
||||
|
||||
public bool InputEnabled
|
||||
{
|
||||
get => _inputEnabled;
|
||||
set
|
||||
{
|
||||
if (_inputEnabled != value)
|
||||
{
|
||||
CallDeferred(nameof(SetInputEnabled), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetInputEnabled(bool enabled)
|
||||
{
|
||||
_inputEnabled = enabled;
|
||||
EmitSignal(SignalName.InputEnabledChanged, _inputEnabled);
|
||||
}
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://blmbgmdv4aui8
|
||||
Reference in New Issue
Block a user