:memo::fire:🔨

- removed a lot of unnecessary code
- Made a minimal working version
- Added documentation
This commit is contained in:
2025-10-24 15:13:35 +02:00
parent 0e55394699
commit 8f097de476
9 changed files with 68 additions and 189 deletions
@@ -15,10 +15,10 @@ public partial class VesnaAnimations : Node
private int _toolID = -1; // -1 means no tool.
private Vector2 _lastDirection = Vector2.Zero;
[Signal] public delegate void LookingRightEventHandler();
[Signal] public delegate void LookingLeftEventHandler();
[Signal] public delegate void LookingUpEventHandler();
[Signal] public delegate void LookingDownEventHandler();
/// <summary>
/// Emits the last look direction of the player to other scripts.
/// </summary>
[Signal] public delegate void LookDirectionEventHandler(Vector2 direction);
public override void _Ready()
{
@@ -49,7 +49,6 @@ public partial class VesnaAnimations : Node
_sprite.Animation = "back walking" + _toolString;
anyActionPressed = true;
_lastDirection = Vector2.Up;
EmitSignal(SignalName.LookingUp);
walkingAnimationPicked = true;
}
@@ -58,7 +57,6 @@ public partial class VesnaAnimations : Node
_sprite.Animation = "front walking" + _toolString;
anyActionPressed = true;
_lastDirection = Vector2.Down;
EmitSignal(SignalName.LookingDown);
walkingAnimationPicked = true;
}
@@ -68,7 +66,6 @@ public partial class VesnaAnimations : Node
_sprite.Animation = "side walking" + _toolString;
anyActionPressed = true;
_lastDirection = Vector2.Right;
EmitSignal(SignalName.LookingRight);
walkingAnimationPicked = true;
}
@@ -78,7 +75,6 @@ public partial class VesnaAnimations : Node
_sprite.Animation = "side walking" + _toolString;
anyActionPressed = true;
_lastDirection = Vector2.Left;
EmitSignal(SignalName.LookingLeft);
walkingAnimationPicked = true;
}
@@ -98,6 +94,8 @@ public partial class VesnaAnimations : Node
}
*/
EmitSignal(SignalName.LookDirection, _lastDirection);
if (anyActionPressed)
{
_sprite.Play();
@@ -2,13 +2,9 @@ using Godot;
namespace Babushka.scripts.CSharp.Common.CharacterControls;
/// <summary>
/// Used for identifying <see cref="InteractionArea2D"/> scenes when listening for area overlaps.
/// </summary>
public partial class DetectableInteractionArea : Area2D
{
[Export] private InteractionArea2D _interactionArea2D;
public void ActivateInteractionArea(bool activate)
{
_interactionArea2D.IsActive = activate;
}
}
@@ -3,123 +3,20 @@ using Godot;
namespace Babushka.scripts.CSharp.Common.CharacterControls;
/// <summary>
/// Tracks where an interactable object is with regards to the player.
/// Moves the Detector to the position in accordance with the player view to limit the player's range of actions to the ones in front of them.
/// </summary>
public partial class DetectionCross : Node2D
{
[Export] private Detector _left;
[Export] private Detector _right;
[Export] private Detector _up;
[Export] private Detector _down;
[Export] private bool _active = true;
[Export] private Detector _detector;
[Export] private float _xOffset;
[Export] private float _yOffset;
[Signal] public delegate void InteractableDetectedLeftEventHandler(bool flag);
[Signal] public delegate void InteractableDetectedRightEventHandler(bool flag);
[Signal] public delegate void InteractableDetectedUpEventHandler(bool flag);
[Signal] public delegate void InteractableDetectedDownEventHandler(bool flag);
private bool _leftOccupied;
private bool _rightOccupied;
private bool _upOccupied;
private bool _downOccupied;
public bool LeftOccupied {
get
{
return _leftOccupied;
}
private set
{
EmitSignal(SignalName.InteractableDetectedLeft, value);
_leftOccupied = value;
}
}
public bool RightOccupied
/// <summary>
/// Gets the current look direction of the player and moves the detection shape with it.
/// </summary>
/// <param name="direction"></param>
public void SetDirection(Vector2 direction)
{
get
{
return _rightOccupied;
}
private set
{
EmitSignal(SignalName.InteractableDetectedRight, value);
_rightOccupied = value;
}
}
public bool UpOccupied
{
get
{
return _upOccupied;
}
private set
{
EmitSignal(SignalName.InteractableDetectedUp, value);
_upOccupied = value;
}
}
public bool DownOccupied
{
get
{
return _downOccupied;
}
private set
{
EmitSignal(SignalName.InteractableDetectedDown, value);
_downOccupied = value;
}
}
public bool IsActive
{
get => _active;
set => _active = value;
}
public override void _Ready()
{
_right.InteractableEntered += () => RightOccupied = true;
_right.InteractableExited += () => RightOccupied = false;
_left.InteractableEntered += () => LeftOccupied = true;
_left.InteractableExited += () => LeftOccupied = false;
_up.InteractableEntered += () => UpOccupied = true;
_up.InteractableExited += () => UpOccupied = false;
_down.InteractableEntered += () => DownOccupied = true;
_down.InteractableExited += () => DownOccupied = false;
}
public void ActivateUp()
{
_up.IsActive = true;
_down.IsActive = false;
_left.IsActive = false;
_right.IsActive = false;
}
public void ActivateDown()
{
_up.IsActive = false;
_down.IsActive = true;
_left.IsActive = false;
_right.IsActive = false;
}
public void ActivateRight()
{
_up.IsActive = false;
_down.IsActive = false;
_left.IsActive = false;
_right.IsActive = true;
}
public void ActivateLeft()
{
_up.IsActive = false;
_down.IsActive = false;
_left.IsActive = true;
_right.IsActive = false;
_detector.Position = new Vector2(direction.X * _xOffset, direction.Y * _yOffset);
}
}
@@ -3,20 +3,32 @@ using Godot;
namespace Babushka.scripts.CSharp.Common.CharacterControls;
/// <summary>
/// Defines an <see cref="Area2D"/> Node with a <see cref="CollisionShape2D"/> used for detecting <see cref="InteractionArea2D"/> nodes.
/// </summary>
public partial class Detector : Area2D
{
[Export] private bool _active = true;
[Export] private bool _oneAtATime = true;
/// <summary>
/// Called when entering an interactionArea node.
/// </summary>
[Signal] public delegate void InteractableEnteredEventHandler();
[Signal] public delegate void InteractableExitedEventHandler();
private DetectableInteractionArea? _currentInteractionArea;
/// <summary>
/// Called when exiting an interactionArea node.
/// </summary>
[Signal] public delegate void InteractableExitedEventHandler();
public bool IsActive
{
get => _active;
set => _active = value;
set
{
Visible = value;
_active = value;
}
}
public override void _Ready()
@@ -25,39 +37,33 @@ public partial class Detector : Area2D
AreaExited += OnExitedInteractable;
}
public void OnEnteredInteractable(Node body)
/// <summary>
/// Called every time this node enters an Area2D.
/// </summary>
/// <param name="area"></param>
public void OnEnteredInteractable(Node area)
{
if (!_active || !InputService.Instance.InputEnabled)
return;
if (area is DetectableInteractionArea interactionArea2D)
{
EmitSignal(SignalName.InteractableEntered);
}
}
/// <summary>
/// Called whenever this node exits an Area2D.
/// </summary>
/// <param name="area"></param>
public void OnExitedInteractable(Node area)
{
if (!_active || !InputService.Instance.InputEnabled)
return;
GD.Print("Entered Node2D.");
if (body is DetectableInteractionArea interactionArea2D)
if (area is DetectableInteractionArea interactionArea2D)
{
GD.Print("Entered interactable.");
_currentInteractionArea = interactionArea2D;
interactionArea2D.ActivateInteractionArea(true);
EmitSignal(SignalName.InteractableEntered);
if (_oneAtATime)
_active = false;
}
}
public void OnExitedInteractable(Node body)
{
GD.Print("Exited Node2D.");
if (body is DetectableInteractionArea interactionArea2D)
{
GD.Print("Exited interactable.");
if (_oneAtATime && _currentInteractionArea != interactionArea2D)
return;
interactionArea2D.ActivateInteractionArea(false);
_currentInteractionArea = null;
EmitSignal(SignalName.InteractableExited);
_active = true;
}
}
}
@@ -92,7 +92,7 @@ public partial class InteractionArea2D : Node2D
private void TryInteract()
{
if (_area.HasOverlappingBodies())
if (_area.HasOverlappingAreas())
{
_label.Hide();