From f94c2d0a500fdf870d9098f17e57418b4e1fa8cb Mon Sep 17 00:00:00 2001 From: kziolkowski Date: Fri, 24 Oct 2025 15:13:35 +0200 Subject: [PATCH] :memo::fire::hammer: - removed a lot of unnecessary code - Made a minimal working version - Added documentation --- prefabs/characters/Player2D.tscn | 7 +- prefabs/interactions/detection_cross.tscn | 21 +-- prefabs/interactions/detector.tscn | 7 +- prefabs/interactions/interaction_area_2d.tscn | 5 +- .../Common/Animation/VesnaAnimations.cs | 14 +- .../DetectableInteractionArea.cs | 10 +- .../CharacterControls/DetectionCross.cs | 123 ++---------------- .../Common/CharacterControls/Detector.cs | 54 ++++---- .../CharacterControls/InteractionArea2D.cs | 2 +- 9 files changed, 61 insertions(+), 182 deletions(-) diff --git a/prefabs/characters/Player2D.tscn b/prefabs/characters/Player2D.tscn index 8b1f5be..cb1244a 100644 --- a/prefabs/characters/Player2D.tscn +++ b/prefabs/characters/Player2D.tscn @@ -2142,7 +2142,7 @@ editable = false scrollable = false [node name="DetectionCross" parent="CharacterBody2D" instance=ExtResource("466_e04c3")] -position = Vector2(0, -400) +position = Vector2(0, -200) [node name="FarmingControls" type="Node2D" parent="." node_paths=PackedStringArray("_movingPlayer", "_wateringParticles")] script = ExtResource("817_6nrw3") @@ -2193,10 +2193,7 @@ wait_time = 0.5 [connection signal="InventorySelectionChanged" from="." to="CharacterBody2D/WateringCanUI" method="IsWateringCanActive"] [connection signal="PickedUpTool" from="." to="CharacterBody2D/visuals" method="ActivateTool"] [connection signal="PickedUpTool" from="." to="CharacterBody2D/WateringCanUI" method="IsWateringCanActive"] -[connection signal="LookingDown" from="CharacterBody2D/visuals" to="CharacterBody2D/DetectionCross" method="ActivateDown"] -[connection signal="LookingLeft" from="CharacterBody2D/visuals" to="CharacterBody2D/DetectionCross" method="ActivateLeft"] -[connection signal="LookingRight" from="CharacterBody2D/visuals" to="CharacterBody2D/DetectionCross" method="ActivateRight"] -[connection signal="LookingUp" from="CharacterBody2D/visuals" to="CharacterBody2D/DetectionCross" method="ActivateUp"] +[connection signal="LookDirection" from="CharacterBody2D/visuals" to="CharacterBody2D/DetectionCross" method="SetDirection"] [connection signal="WateringField" from="FarmingControls" to="CharacterBody2D/visuals" method="PlayWateringAnimation"] [connection signal="WateringField" from="FarmingControls" to="CharacterBody2D/WateringCanUI" method="Water"] [connection signal="timelineEnded" from="dialogic toggle" to="." method="EnableMovement"] diff --git a/prefabs/interactions/detection_cross.tscn b/prefabs/interactions/detection_cross.tscn index 496a0ae..57930bc 100644 --- a/prefabs/interactions/detection_cross.tscn +++ b/prefabs/interactions/detection_cross.tscn @@ -3,21 +3,10 @@ [ext_resource type="Script" uid="uid://ccc6m6c5khd2x" path="res://scripts/CSharp/Common/CharacterControls/DetectionCross.cs" id="1_va8tx"] [ext_resource type="PackedScene" uid="uid://dugr6ff1g7hi0" path="res://prefabs/interactions/detector.tscn" id="2_8hh05"] -[node name="DetectionCross" type="Node2D" node_paths=PackedStringArray("_left", "_right", "_up", "_down")] +[node name="DetectionCross" type="Node2D" node_paths=PackedStringArray("_detector")] script = ExtResource("1_va8tx") -_left = NodePath("detector_left") -_right = NodePath("detector_right") -_up = NodePath("detector_top") -_down = NodePath("detector_bottom") +_detector = NodePath("detector") +_xOffset = 300.0 +_yOffset = 300.0 -[node name="detector_right" parent="." instance=ExtResource("2_8hh05")] -position = Vector2(300, 250) - -[node name="detector_left" parent="." instance=ExtResource("2_8hh05")] -position = Vector2(-300, 250) - -[node name="detector_top" parent="." instance=ExtResource("2_8hh05")] -position = Vector2(0, -200) - -[node name="detector_bottom" parent="." instance=ExtResource("2_8hh05")] -position = Vector2(0, 700) +[node name="detector" parent="." instance=ExtResource("2_8hh05")] diff --git a/prefabs/interactions/detector.tscn b/prefabs/interactions/detector.tscn index 2a2bfe7..2b4626e 100644 --- a/prefabs/interactions/detector.tscn +++ b/prefabs/interactions/detector.tscn @@ -3,15 +3,12 @@ [ext_resource type="Script" uid="uid://c3pd60biootsx" path="res://scripts/CSharp/Common/CharacterControls/Detector.cs" id="1_6pib0"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_qwv4c"] -size = Vector2(300, 300) +size = Vector2(100, 200) -[node name="right" type="Area2D"] +[node name="detector" type="Area2D"] collision_layer = 4 script = ExtResource("1_6pib0") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("RectangleShape2D_qwv4c") debug_color = Color(0.9459047, 7.2196126e-06, 0.5925879, 0.41960785) - -[connection signal="InteractableEntered" from="." to="CollisionShape2D" method="hide"] -[connection signal="InteractableExited" from="." to="CollisionShape2D" method="show"] diff --git a/prefabs/interactions/interaction_area_2d.tscn b/prefabs/interactions/interaction_area_2d.tscn index 60cc297..fb04b7f 100644 --- a/prefabs/interactions/interaction_area_2d.tscn +++ b/prefabs/interactions/interaction_area_2d.tscn @@ -6,7 +6,7 @@ [sub_resource type="CircleShape2D" id="CircleShape2D_npluf"] resource_local_to_scene = true -radius = 300.0 +radius = 150.0 [sub_resource type="Theme" id="Theme_5ajrf"] default_font_size = 30 @@ -17,10 +17,9 @@ _area = NodePath("Area2D") _label = NodePath("Area2D/CanvasLayer/MarginContainer/Label") _outlineMaterial = ExtResource("2_qoey7") -[node name="Area2D" type="Area2D" parent="." node_paths=PackedStringArray("_interactionArea2D")] +[node name="Area2D" type="Area2D" parent="."] collision_mask = 4 script = ExtResource("3_2wrrq") -_interactionArea2D = NodePath("..") [node name="CollisionShape3D" type="CollisionShape2D" parent="Area2D"] shape = SubResource("CircleShape2D_npluf") diff --git a/scripts/CSharp/Common/Animation/VesnaAnimations.cs b/scripts/CSharp/Common/Animation/VesnaAnimations.cs index 99435a8..797490c 100644 --- a/scripts/CSharp/Common/Animation/VesnaAnimations.cs +++ b/scripts/CSharp/Common/Animation/VesnaAnimations.cs @@ -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(); + /// + /// Emits the last look direction of the player to other scripts. + /// + [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(); diff --git a/scripts/CSharp/Common/CharacterControls/DetectableInteractionArea.cs b/scripts/CSharp/Common/CharacterControls/DetectableInteractionArea.cs index 301fabf..2d67333 100644 --- a/scripts/CSharp/Common/CharacterControls/DetectableInteractionArea.cs +++ b/scripts/CSharp/Common/CharacterControls/DetectableInteractionArea.cs @@ -2,13 +2,9 @@ using Godot; namespace Babushka.scripts.CSharp.Common.CharacterControls; +/// +/// Used for identifying scenes when listening for area overlaps. +/// public partial class DetectableInteractionArea : Area2D { - [Export] private InteractionArea2D _interactionArea2D; - - public void ActivateInteractionArea(bool activate) - { - _interactionArea2D.IsActive = activate; - } - } \ No newline at end of file diff --git a/scripts/CSharp/Common/CharacterControls/DetectionCross.cs b/scripts/CSharp/Common/CharacterControls/DetectionCross.cs index a804378..1ce4e12 100644 --- a/scripts/CSharp/Common/CharacterControls/DetectionCross.cs +++ b/scripts/CSharp/Common/CharacterControls/DetectionCross.cs @@ -3,123 +3,20 @@ using Godot; namespace Babushka.scripts.CSharp.Common.CharacterControls; /// -/// 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. /// 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 - { - 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() + /// + /// Gets the current look direction of the player and moves the detection shape with it. + /// + /// + public void SetDirection(Vector2 direction) { - _up.IsActive = false; - _down.IsActive = false; - _left.IsActive = true; - _right.IsActive = false; + _detector.Position = new Vector2(direction.X * _xOffset, direction.Y * _yOffset); } } \ No newline at end of file diff --git a/scripts/CSharp/Common/CharacterControls/Detector.cs b/scripts/CSharp/Common/CharacterControls/Detector.cs index 0cc22a2..95767d0 100644 --- a/scripts/CSharp/Common/CharacterControls/Detector.cs +++ b/scripts/CSharp/Common/CharacterControls/Detector.cs @@ -3,20 +3,32 @@ using Godot; namespace Babushka.scripts.CSharp.Common.CharacterControls; +/// +/// Defines an Node with a used for detecting nodes. +/// public partial class Detector : Area2D { + [Export] private bool _active = true; - [Export] private bool _oneAtATime = true; + /// + /// Called when entering an interactionArea node. + /// [Signal] public delegate void InteractableEnteredEventHandler(); - [Signal] public delegate void InteractableExitedEventHandler(); - private DetectableInteractionArea? _currentInteractionArea; + /// + /// Called when exiting an interactionArea node. + /// + [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) + /// + /// Called every time this node enters an Area2D. + /// + /// + public void OnEnteredInteractable(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) + /// + /// Called whenever this node exits an Area2D. + /// + /// + public void OnExitedInteractable(Node area) { - GD.Print("Exited Node2D."); + if (!_active || !InputService.Instance.InputEnabled) + return; - if (body is DetectableInteractionArea interactionArea2D) + if (area is DetectableInteractionArea interactionArea2D) { - GD.Print("Exited interactable."); - - if (_oneAtATime && _currentInteractionArea != interactionArea2D) - return; - - interactionArea2D.ActivateInteractionArea(false); - _currentInteractionArea = null; EmitSignal(SignalName.InteractableExited); - _active = true; } } } \ No newline at end of file diff --git a/scripts/CSharp/Common/CharacterControls/InteractionArea2D.cs b/scripts/CSharp/Common/CharacterControls/InteractionArea2D.cs index 1c93896..fab4097 100644 --- a/scripts/CSharp/Common/CharacterControls/InteractionArea2D.cs +++ b/scripts/CSharp/Common/CharacterControls/InteractionArea2D.cs @@ -92,7 +92,7 @@ public partial class InteractionArea2D : Node2D private void TryInteract() { - if (_area.HasOverlappingBodies()) + if (_area.HasOverlappingAreas()) { _label.Hide();