From 6e833a07356b8370b8fe73c01f90217972373793 Mon Sep 17 00:00:00 2001 From: kziolkowski Date: Fri, 7 Nov 2025 20:29:05 +0100 Subject: [PATCH] :sparkles: added InventoryListener and made field interaction area activate only when the right item (seed) is in the inventory --- prefabs/farm/base_field.tscn | 14 +++++- .../CSharp/Common/Farming/FieldBehaviour2D.cs | 20 +++++++- .../Common/Inventory/InventoryListener.cs | 48 +++++++++++++++++++ .../Common/Inventory/InventoryListener.cs.uid | 1 + 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 scripts/CSharp/Common/Inventory/InventoryListener.cs create mode 100644 scripts/CSharp/Common/Inventory/InventoryListener.cs.uid diff --git a/prefabs/farm/base_field.tscn b/prefabs/farm/base_field.tscn index c35bcc9..89246a5 100644 --- a/prefabs/farm/base_field.tscn +++ b/prefabs/farm/base_field.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=14 format=3 uid="uid://b1d2e7ely6hyw"] +[gd_scene load_steps=17 format=3 uid="uid://b1d2e7ely6hyw"] [ext_resource type="Script" uid="uid://bdffon388rkty" path="res://scripts/CSharp/Common/Farming/FieldBehaviour2D.cs" id="1_qa01x"] [ext_resource type="Texture2D" uid="uid://cgmu3qlovdr22" path="res://art/masks/field_outline_1.png" id="2_w8caw"] @@ -13,10 +13,13 @@ [ext_resource type="Script" uid="uid://b5dotkx17gvxg" path="res://scripts/CSharp/Low Code/Events/EventRaiser.cs" id="9_teirr"] [ext_resource type="Texture2D" uid="uid://bovypw2hsn2nq" path="res://art/masks/field_outline_1_outline.png" id="9_wx561"] [ext_resource type="Resource" uid="uid://b4hawvsc7cmkn" path="res://resources/low code/farming/event_newPlantCreated.tres" id="10_wx561"] +[ext_resource type="Script" uid="uid://3t0af586fimq" path="res://scripts/CSharp/Common/Inventory/InventoryListener.cs" id="14_w08sx"] +[ext_resource type="Resource" uid="uid://d1uuxp1lp4aro" path="res://resources/items/tomato_seed.tres" id="15_i4qwg"] +[ext_resource type="Resource" uid="uid://duq7tshxv6uhp" path="res://resources/items/beet_seed.tres" id="16_i4qwg"] [node name="BaseField" type="Node2D"] -[node name="FieldBehaviour" type="Sprite2D" parent="." node_paths=PackedStringArray("_fieldSprite", "_maskSprite", "_outlineSprite", "PlantingInteraction", "PlantingPlaceholder")] +[node name="FieldBehaviour" type="Sprite2D" parent="." node_paths=PackedStringArray("_fieldSprite", "_maskSprite", "_outlineSprite", "PlantingInteraction", "PlantingPlaceholder", "FieldInteractionArea")] z_index = -1 scale = Vector2(0.9, 1) script = ExtResource("1_qa01x") @@ -30,6 +33,7 @@ Watered = ExtResource("6_7m4xq") PlantingInteraction = NodePath("../InteractionArea") PlantingPlaceholder = NodePath("PlantPlaceholder") ItemRepository = ExtResource("7_w8caw") +FieldInteractionArea = NodePath("../InteractionArea") [node name="MaskedField" type="Sprite2D" parent="FieldBehaviour"] clip_children = 1 @@ -49,11 +53,17 @@ texture = ExtResource("9_wx561") [node name="InteractionArea" parent="." node_paths=PackedStringArray("_spritesToOutline") instance=ExtResource("7_2eegd")] position = Vector2(-26, -57) +_active = false _spritesToOutline = [NodePath("../FieldBehaviour/OutlineSprite")] [node name="PlantCreationEventRaiser" type="Node" parent="."] script = ExtResource("9_teirr") _eventResources = Array[Object]([ExtResource("10_wx561")]) +[node name="InventoryListener" type="Node" parent="."] +script = ExtResource("14_w08sx") +_itemResourcesToListenFor = Array[Object]([ExtResource("15_i4qwg"), ExtResource("16_i4qwg")]) + [connection signal="Planted" from="FieldBehaviour" to="PlantCreationEventRaiser" method="RaiseEvents"] [connection signal="Interacted" from="InteractionArea" to="FieldBehaviour" method="Farm"] +[connection signal="ItemInstanceActivated" from="InventoryListener" to="FieldBehaviour" method="ActivatedSeedInInventory"] diff --git a/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs b/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs index 247304a..08a5e5a 100644 --- a/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs +++ b/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs @@ -19,15 +19,33 @@ public partial class FieldBehaviour2D : Sprite2D [Export] public InteractionArea2D PlantingInteraction; [Export] public Node2D PlantingPlaceholder; [Export] public ItemRepository ItemRepository; - + [Export] public InteractionArea2D FieldInteractionArea; public Vector2 FieldPosition; + private bool _seedsActive; + [Signal] public delegate void PlantedEventHandler(); + private void UpdateInteractionArea() + { + // fieldstate == tilled / watered && samen im Inventar + bool canPlant = (FieldState == FieldState.Tilled || FieldState == FieldState.Watered) && _seedsActive; + // fieldstate == tilled && watering can ausgewählt + bool canWater = false; + FieldInteractionArea.IsActive = canPlant || canWater; + } + + public void ActivatedSeedInInventory(bool activated) + { + _seedsActive = activated; + UpdateInteractionArea(); + } + public override void _Ready() { UpdateFieldState(FieldState); + UpdateInteractionArea(); int randomIndex = new Random().Next(0, _maskTexture.Length); _maskSprite.Texture = _maskTexture[randomIndex]; _outlineSprite.Texture = _maskOutlineTextures[randomIndex]; diff --git a/scripts/CSharp/Common/Inventory/InventoryListener.cs b/scripts/CSharp/Common/Inventory/InventoryListener.cs new file mode 100644 index 0000000..424ebb1 --- /dev/null +++ b/scripts/CSharp/Common/Inventory/InventoryListener.cs @@ -0,0 +1,48 @@ +using Godot; + +namespace Babushka.scripts.CSharp.Common.Inventory; + +public partial class InventoryListener : Node +{ + [Export] private ItemResource[] _itemResourcesToListenFor; + + [Signal] public delegate void ItemInstanceActivatedEventHandler(bool activated); + + public override void _Ready() + { + InventoryManager.Instance.playerInventory.InventoryContentsChanged += HandleNewItemInInventory; + InventoryManager.Instance.SlotIndexChanged += HandleNewItemInInventory; + } + + public override void _ExitTree() + { + InventoryManager.Instance.playerInventory.InventoryContentsChanged -= HandleNewItemInInventory; + InventoryManager.Instance.SlotIndexChanged -= HandleNewItemInInventory; + } + + private void HandleNewItemInInventory(int newIndex) + { + HandleNewItemInInventory(); + } + + private void HandleNewItemInInventory() + { + int currentSlotIndex = InventoryManager.Instance.CurrentSelectedSlotIndex; + ItemInstance? instance = InventoryManager.Instance.playerInventory.Slots[currentSlotIndex].itemInstance; + if (instance != null) + { + ItemResource? item = instance.blueprint; + + foreach (var res in _itemResourcesToListenFor) + { + if (item == res) + { + EmitSignal(SignalName.ItemInstanceActivated, true); + return; + } + } + } + + EmitSignal(SignalName.ItemInstanceActivated, false); + } +} \ No newline at end of file diff --git a/scripts/CSharp/Common/Inventory/InventoryListener.cs.uid b/scripts/CSharp/Common/Inventory/InventoryListener.cs.uid new file mode 100644 index 0000000..42f5a8b --- /dev/null +++ b/scripts/CSharp/Common/Inventory/InventoryListener.cs.uid @@ -0,0 +1 @@ +uid://3t0af586fimq