🚧 WIP detection works now

pull/48/head
kziolkowski 1 month ago
parent 7ffbb8e68d
commit 9e0e87ef17

@ -2173,7 +2173,7 @@ script = ExtResource("472_kduih")
_wateringCanFillStateNode = NodePath("../WateringCanFillState")
[node name="DetectionCross" parent="CharacterBody2D" instance=ExtResource("466_e04c3")]
position = Vector2(0, -371)
position = Vector2(0, -260)
[node name="FarmingControls" type="Node2D" parent="." node_paths=PackedStringArray("_movingPlayer")]
script = ExtResource("817_6nrw3")

@ -1,8 +1,7 @@
[gd_scene load_steps=25 format=3 uid="uid://muuxxgvx33fp"]
[gd_scene load_steps=24 format=3 uid="uid://muuxxgvx33fp"]
[ext_resource type="Script" uid="uid://7m1rt7agb6rm" path="res://scripts/CSharp/Common/Temp/MVPDuck.cs" id="1_54k4r"]
[ext_resource type="Texture2D" uid="uid://hvchk6t0xe7j" path="res://art/animals/Ente.png" id="1_cgxhx"]
[ext_resource type="Resource" uid="uid://tt3d166mntmi" path="res://resources/low code/farming/var_sceneNameProvider.tres" id="2_fdf3t"]
[ext_resource type="AudioStream" uid="uid://qv0aubjeyi0u" path="res://audio/sfx/Animals/SFX_Duck_Quack_01.wav" id="3_kjie1"]
[ext_resource type="Script" uid="uid://cfnrd5k1k0gxw" path="res://scripts/CSharp/Common/AudioPlayer2D.cs" id="3_rdn2q"]
[ext_resource type="AudioStream" uid="uid://da84l8e44scwh" path="res://audio/sfx/Animals/SFX_Duck_Quack_02.wav" id="4_54k4r"]
@ -143,7 +142,6 @@ viewport_path = NodePath("SubViewport")
z_index = 1
y_sort_enabled = true
script = ExtResource("1_54k4r")
_sceneKeyProvider = ExtResource("2_fdf3t")
_transferDelayMs = 1000
_animationPlayer = NodePath("AnimationPlayer")

@ -74,6 +74,7 @@ texture = ExtResource("5_wx561")
y_sort_enabled = true
[node name="InteractionArea" parent="FieldBehaviour" node_paths=PackedStringArray("_spritesToOutline") instance=ExtResource("7_2eegd")]
process_mode = 4
position = Vector2(-28.88889, -57)
scale = Vector2(1.1111112, 1)
_active = false

@ -2,19 +2,20 @@
[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"]
[ext_resource type="Script" uid="uid://b4n0nlu4ckqga" path="res://scripts/CSharp/Common/CharacterControls/RaycastDetector.cs" id="2_va8tx"]
[node name="DetectionCross" type="Node2D" node_paths=PackedStringArray("_collider", "_detector")]
position = Vector2(0, 1)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_va8tx"]
[node name="DetectionCross" type="Node2D" node_paths=PackedStringArray("_collider", "_shapeCast2D")]
script = ExtResource("1_va8tx")
_collider = NodePath("detector")
_detector = NodePath("Raycast")
_shapeCast2D = NodePath("ShapeCast2D")
_xOffset = 200.0
_yOffset = 200.0
[node name="Raycast" type="RayCast2D" parent="."]
target_position = Vector2(222, -168)
collide_with_areas = true
script = ExtResource("2_va8tx")
[node name="detector" parent="." node_paths=PackedStringArray("_shapeCast2D") instance=ExtResource("2_8hh05")]
_shapeCast2D = NodePath("../ShapeCast2D")
[node name="detector" parent="." instance=ExtResource("2_8hh05")]
[node name="ShapeCast2D" type="ShapeCast2D" parent="."]
shape = SubResource("RectangleShape2D_va8tx")
target_position = Vector2(200, 0)
collide_with_areas = true

@ -1,13 +1,15 @@
[gd_scene load_steps=3 format=3 uid="uid://dugr6ff1g7hi0"]
[gd_scene load_steps=4 format=3 uid="uid://dugr6ff1g7hi0"]
[ext_resource type="Script" uid="uid://c3pd60biootsx" path="res://scripts/CSharp/Common/CharacterControls/Detector.cs" id="1_6pib0"]
[ext_resource type="Resource" uid="uid://clwqh3w5aqi5e" path="res://resources/low code/interactables/var_interactableToTrigger.tres" id="2_3fanv"]
[sub_resource type="CircleShape2D" id="CircleShape2D_6pib0"]
radius = 200.0
radius = 300.0
[node name="detector" type="Area2D"]
collision_layer = 4
script = ExtResource("1_6pib0")
_itemToTriggerResource = ExtResource("2_3fanv")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_6pib0")

@ -1,9 +1,10 @@
[gd_scene load_steps=7 format=3 uid="uid://cqc72e4hq6bcd"]
[gd_scene load_steps=8 format=3 uid="uid://cqc72e4hq6bcd"]
[ext_resource type="Script" uid="uid://ckp413wrub5fm" path="res://scripts/CSharp/Common/CharacterControls/InteractionArea2D.cs" id="1_5ajrf"]
[ext_resource type="Resource" uid="uid://clwqh3w5aqi5e" path="res://resources/low code/interactables/var_interactableToTrigger.tres" id="2_o1drf"]
[ext_resource type="Material" uid="uid://blch5kdhkbj75" path="res://art/materials/simple_interactable_outline.tres" id="2_qoey7"]
[ext_resource type="Script" uid="uid://cp2q4k62sjo6h" path="res://scripts/CSharp/Common/CharacterControls/DetectableInteractionArea.cs" id="3_2wrrq"]
[ext_resource type="Script" uid="uid://pqemey80frcq" path="res://scripts/CSharp/Low Code/Variables/VariableListener.cs" id="4_gj2nu"]
[sub_resource type="CircleShape2D" id="CircleShape2D_npluf"]
resource_local_to_scene = true
@ -14,7 +15,6 @@ default_font_size = 30
[node name="InteractionArea" type="Node2D" node_paths=PackedStringArray("_area", "_label")]
script = ExtResource("1_5ajrf")
_interactableToTrigger = ExtResource("2_o1drf")
_area = NodePath("Area2D")
_label = NodePath("Area2D/CanvasLayer/MarginContainer/Label")
_outlineMaterial = ExtResource("2_qoey7")
@ -54,5 +54,8 @@ text = "[E]"
horizontal_alignment = 2
vertical_alignment = 2
[connection signal="area_entered" from="Area2D" to="." method="OnPlayerEntered"]
[connection signal="area_exited" from="Area2D" to="." method="OnPlayerExited"]
[node name="SelectionListener" type="Node" parent="."]
script = ExtResource("4_gj2nu")
_variableResources = Array[Object]([ExtResource("2_o1drf")])
[connection signal="NewEventPayload" from="SelectionListener" to="Area2D" method="InteractionAreaSelectionChanged"]

@ -985,7 +985,8 @@ collision_mask = 4
position = Vector2(145.5, -224)
shape = SubResource("RectangleShape2D_0sfl7")
[node name="InteractionArea" parent="YSorted/Well" instance=ExtResource("27_klb81")]
[node name="InteractionArea" parent="YSorted/Well" node_paths=PackedStringArray("_spritesToOutline") instance=ExtResource("27_klb81")]
_spritesToOutline = [NodePath("..")]
_id = 1
metadata/SaveID = "b8f7b7fe-e057-4974-ba12-9134722998de"
@ -2313,9 +2314,8 @@ position = Vector2(3183, 2369)
offset = Vector2(1, -50)
region_rect = Rect2(207, 1184, 149, 142)
[node name="InteractionArea" parent="YSorted/trash/trashObject2" index="0" node_paths=PackedStringArray("_spritesToOutline")]
[node name="InteractionArea" parent="YSorted/trash/trashObject2" index="0"]
position = Vector2(-9, -46)
_spritesToOutline = []
metadata/SaveID = "549bbcf4-ea57-4b8f-80b1-b13ca648559b"
[node name="trashObject3" parent="YSorted/trash" instance=ExtResource("53_ycj14")]
@ -2325,9 +2325,8 @@ position = Vector2(4724, 3519)
offset = Vector2(1, -50)
region_rect = Rect2(400, 1053, 163, 141)
[node name="InteractionArea" parent="YSorted/trash/trashObject3" index="0" node_paths=PackedStringArray("_spritesToOutline")]
[node name="InteractionArea" parent="YSorted/trash/trashObject3" index="0"]
position = Vector2(-13, -53)
_spritesToOutline = []
metadata/SaveID = "29874314-50c1-4a21-9494-18f936d6e097"
[node name="trashObject4" parent="YSorted/trash" instance=ExtResource("53_ycj14")]
@ -2337,9 +2336,8 @@ position = Vector2(5385, 3391)
offset = Vector2(1, -50)
region_rect = Rect2(1048, 1092, 348, 106)
[node name="InteractionArea" parent="YSorted/trash/trashObject4" index="0" node_paths=PackedStringArray("_spritesToOutline")]
[node name="InteractionArea" parent="YSorted/trash/trashObject4" index="0"]
position = Vector2(0, -59)
_spritesToOutline = []
metadata/SaveID = "7ccaa831-5526-40ed-8ca3-31ba2ad929a6"
[node name="trashObject5" parent="YSorted/trash" instance=ExtResource("53_ycj14")]
@ -2376,9 +2374,8 @@ rotation = 1.77025
offset = Vector2(0, 0)
region_rect = Rect2(1048, 1092, 348, 106)
[node name="InteractionArea" parent="YSorted/trash/trashObject9" index="0" node_paths=PackedStringArray("_spritesToOutline")]
[node name="InteractionArea" parent="YSorted/trash/trashObject9" index="0"]
position = Vector2(22.40873, 25.05658)
_spritesToOutline = []
metadata/SaveID = "7bf227d6-3844-41e9-a9cd-524052aced3b"
[node name="CanvasLayer" parent="." instance=ExtResource("32_2nee2")]

@ -9,14 +9,16 @@ public partial class DetectableInteractionArea : Area2D
{
[Export] public InteractionArea2D interactionArea2D;
public void Detected()
public void InteractionAreaSelectionChanged(Variant instanceID)
{
GD.Print("Detected " + interactionArea2D.GetParent().Name);
interactionArea2D.IsSelectedByDetector = true;
}
public void NoLongerDetected()
{
interactionArea2D.IsSelectedByDetector = false;
if (instanceID.AsString() == GetInstanceId().ToString())
{
interactionArea2D.HighlightInteractable();
}
else
{
interactionArea2D.ResetHighlight();
}
}
}

@ -8,7 +8,7 @@ namespace Babushka.scripts.CSharp.Common.CharacterControls;
public partial class DetectionCross : Node2D
{
[Export] private Detector _collider;
[Export] private RaycastDetector _detector;
[Export] private ShapeCast2D _shapeCast2D;
[Export] private float _xOffset;
[Export] private float _yOffset;
@ -20,6 +20,6 @@ public partial class DetectionCross : Node2D
{
Vector2 newPos = new Vector2(direction.X * _xOffset, direction.Y * _yOffset);
_collider.Position = newPos;
_detector.TargetPosition = newPos;
_shapeCast2D.TargetPosition = newPos;
}
}

@ -1,6 +1,7 @@
using System.Collections.Generic;
using Babushka.scripts.CSharp.Common.Services;
using Babushka.scripts.CSharp.Low_Code.Variables;
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Common.CharacterControls;
@ -9,18 +10,11 @@ namespace Babushka.scripts.CSharp.Common.CharacterControls;
/// </summary>
public partial class Detector : Area2D
{
[Export] private bool _active = true;
/// <summary>
/// Called when entering an interactionArea node.
/// </summary>
[Signal] public delegate void InteractableEnteredEventHandler();
[Export] private ShapeCast2D _shapeCast2D;
[Export] private VariableResource _itemToTriggerResource;
/// <summary>
/// Called when exiting an interactionArea node.
/// </summary>
[Signal] public delegate void InteractableExitedEventHandler();
private readonly List<ulong> _areasInDetector = new();
public bool IsActive
{
@ -47,9 +41,11 @@ public partial class Detector : Area2D
if (!_active || !InputService.Instance.InputEnabled)
return;
if (area is DetectableInteractionArea interactionArea2D)
if (area is DetectableInteractionArea detectable)
{
EmitSignal(SignalName.InteractableEntered);
ulong id = detectable.GetInstanceId();
_areasInDetector.Add(id);
CalculateClosestInteractable();
}
}
@ -62,10 +58,35 @@ public partial class Detector : Area2D
if (!_active || !InputService.Instance.InputEnabled)
return;
if (area is DetectableInteractionArea interactionArea2D)
if (area is DetectableInteractionArea detectable)
{
EmitSignal(SignalName.InteractableExited);
ulong id = detectable.GetInstanceId();
if( _areasInDetector.Contains(id))
_areasInDetector.Remove(id);
CalculateClosestInteractable();
}
}
private void CalculateClosestInteractable()
{
GD.Print($"Areas in detector: {_areasInDetector.Count}");
float smallestDistance = float.MaxValue;
string closestInteractable = null;
foreach (var area in _areasInDetector)
{
Area2D? area2D = InstanceFromId(area) as Area2D;
if(area2D == null)
continue;
float distance = area2D.GlobalPosition.DistanceSquaredTo(ToGlobal(_shapeCast2D.TargetPosition));
if (distance < smallestDistance)
{
closestInteractable = area.ToString();
smallestDistance = distance;
}
}
GD.Print($"Closest interactable: {closestInteractable}");
_itemToTriggerResource.Payload = closestInteractable;
}
}

@ -43,10 +43,10 @@ public partial class InteractionArea2D : Node2D
_backupMaterials = _spritesToOutline.Select(s => s.Material).ToArray();
}
}
/*
public void OnPlayerEntered(Node2D player)
{
GD.Print("OnPlayerEntered: " + this.GetParent().Name);
if (!_active || !InputService.Instance.InputEnabled)
return;
@ -59,13 +59,28 @@ public partial class InteractionArea2D : Node2D
if (!IsSelectedByDetector)
return;
ActivateOutline();
}
*/
public void HighlightInteractable()
{
if (!_active || !InputService.Instance.InputEnabled)
return;
if (_showLabel)
_label.Show();
if (!_useOutline)
return;
foreach (var sprite in _spritesToOutline)
{
sprite.Material = _outlineMaterial;
}
}
public void OnPlayerExited(Node2D player)
public void ResetHighlight()
{
_label.Hide();
@ -78,6 +93,13 @@ public partial class InteractionArea2D : Node2D
sprite.Material = _backupMaterials[i];
}
}
/*
public void OnPlayerExited(Node2D player)
{
}
*/
public override void _Input(InputEvent @event)
{
@ -134,4 +156,5 @@ public partial class InteractionArea2D : Node2D
_label.Hide();
}
}

@ -1,41 +0,0 @@
using Godot;
namespace Babushka.scripts.CSharp.Common.CharacterControls;
public partial class RaycastDetector : RayCast2D
{
[Export] private bool _active = true;
private DetectableInteractionArea? _lastDetected;
public bool IsActive
{
get => _active;
set
{
Visible = value;
_active = value;
}
}
public override void _PhysicsProcess(double delta)
{
if (!_active)
return;
if (IsColliding())
{
if (GetCollider() is DetectableInteractionArea interactionArea)
{
if (_lastDetected != null && _lastDetected != interactionArea)
{
_lastDetected.NoLongerDetected();
}
GD.Print("Colliding with: " + interactionArea.GetParent().Name);
_lastDetected = interactionArea;
interactionArea.Detected();
}
}
}
}
Loading…
Cancel
Save