From 1062193785eb96616c59106c7b1f2924c7dc5ed2 Mon Sep 17 00:00:00 2001 From: cblech Date: Mon, 14 Apr 2025 16:49:29 +0200 Subject: [PATCH] item on ground and pickup --- prefabs/UI/Inventory/Inventory.tscn | 2 +- .../interactions/generic_item_on_ground.tscn | 22 ++++++++ project.godot | 2 +- .../Babushka_scene_item_pickup_testing.tscn | 53 +++++++++++++++++++ .../Common/Inventory/InventoryManager.cs | 4 ++ .../CSharp/Common/Inventory/InventoryUi.cs | 3 +- .../CSharp/Common/Inventory/ItemOnGround.cs | 46 ++++++++++++++++ .../Common/Inventory/ItemOnGround.cs.uid | 1 + .../Common/Inventory/ItemOnGroundSpawnWith.cs | 16 ++++++ .../Inventory/ItemOnGroundSpawnWith.cs.uid | 1 + 10 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 prefabs/interactions/generic_item_on_ground.tscn create mode 100644 scenes/testing/Babushka_scene_item_pickup_testing.tscn create mode 100644 scripts/CSharp/Common/Inventory/ItemOnGround.cs create mode 100644 scripts/CSharp/Common/Inventory/ItemOnGround.cs.uid create mode 100644 scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs create mode 100644 scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs.uid diff --git a/prefabs/UI/Inventory/Inventory.tscn b/prefabs/UI/Inventory/Inventory.tscn index 5435acc..5d59d26 100644 --- a/prefabs/UI/Inventory/Inventory.tscn +++ b/prefabs/UI/Inventory/Inventory.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=5 format=3 uid="uid://cgjc4wurbgimy"] -[ext_resource type="Script" uid="uid://hg7jay2kt441" path="res://scripts/CSharp/Common/Inventory/InventoryUi.cs" id="1_6wusm"] +[ext_resource type="Script" uid="uid://b7vlkecrn0t5c" path="res://scripts/CSharp/Common/Inventory/InventoryUi.cs" id="1_6wusm"] [ext_resource type="Script" uid="uid://b2jhdxcrhtm2d" path="res://scripts/CSharp/Common/Inventory/InventoryTestScript.cs" id="3_exrk4"] [ext_resource type="Resource" uid="uid://datee0flk1e84" path="res://resources/items/pickaxe.tres" id="4_5fdxq"] [ext_resource type="Texture2D" uid="uid://c7wqla0mbu3np" path="res://graphics/ui/babushka_ui_tmp_inventory_select.png" id="4_tiss4"] diff --git a/prefabs/interactions/generic_item_on_ground.tscn b/prefabs/interactions/generic_item_on_ground.tscn new file mode 100644 index 0000000..576e96d --- /dev/null +++ b/prefabs/interactions/generic_item_on_ground.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=5 format=3 uid="uid://5hltxpdxmqcq"] + +[ext_resource type="Script" uid="uid://udhigottc8rg" path="res://scripts/CSharp/Common/Inventory/ItemOnGround.cs" id="1_mnomu"] +[ext_resource type="PackedScene" uid="uid://ob04y3syvo0e" path="res://prefabs/interaction_area.tscn" id="2_ue6ub"] +[ext_resource type="Script" uid="uid://c8suoi3i6kqai" path="res://scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs" id="3_ue6ub"] +[ext_resource type="Resource" uid="uid://c7viddcd3kywp" path="res://resources/items/axe.tres" id="4_sfad0"] + +[node name="GenericItemOnGround" type="Node3D"] +script = ExtResource("1_mnomu") + +[node name="InteractionArea" parent="." instance=ExtResource("2_ue6ub")] + +[node name="SpawnWithItem" type="Node" parent="."] +script = ExtResource("3_ue6ub") +_blueprint = ExtResource("4_sfad0") + +[node name="ItemLabel" type="Label3D" parent="."] +offset = Vector2(0, 55.64) +billboard = 1 +text = "test" + +[connection signal="Interacted" from="InteractionArea" to="." method="TryPickUp"] diff --git a/project.godot b/project.godot index d61222a..0cc2d20 100644 --- a/project.godot +++ b/project.godot @@ -11,7 +11,7 @@ config_version=5 [application] config/name="Babushka" -run/main_scene="uid://br7yq757cawts" +run/main_scene="uid://dbxy2y85r1d8x" config/features=PackedStringArray("4.4", "C#", "Forward Plus") config/icon="res://icon.svg" diff --git a/scenes/testing/Babushka_scene_item_pickup_testing.tscn b/scenes/testing/Babushka_scene_item_pickup_testing.tscn new file mode 100644 index 0000000..6ca85ff --- /dev/null +++ b/scenes/testing/Babushka_scene_item_pickup_testing.tscn @@ -0,0 +1,53 @@ +[gd_scene load_steps=10 format=3 uid="uid://dbxy2y85r1d8x"] + +[ext_resource type="PackedScene" uid="uid://dbd1niu3tp8y5" path="res://prefabs/Player3D.tscn" id="1_pdc5e"] +[ext_resource type="PackedScene" uid="uid://biwd3jj65qrlh" path="res://prefabs/farming/base_field.tscn" id="2_741gk"] +[ext_resource type="PackedScene" uid="uid://5hltxpdxmqcq" path="res://prefabs/interactions/generic_item_on_ground.tscn" id="5_sjt6k"] +[ext_resource type="PackedScene" uid="uid://cgjc4wurbgimy" path="res://prefabs/UI/Inventory/Inventory.tscn" id="6_876p1"] +[ext_resource type="Resource" uid="uid://cndd64batns31" path="res://resources/items/bucket.tres" id="6_nvnpb"] +[ext_resource type="Script" uid="uid://d178x7agr603o" path="res://scripts/CSharp/Common/Farming/FieldService.cs" id="7_3rkjv"] + +[sub_resource type="PlaneMesh" id="PlaneMesh_8yprl"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_8tgff"] +albedo_color = Color(0.505882, 0.588235, 0.494118, 1) + +[sub_resource type="BoxShape3D" id="BoxShape3D_t58di"] +size = Vector3(2.01563, 0.160522, 2.00824) + +[node name="SceneFarmVesna" type="Node3D"] + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0) +mesh = SubResource("PlaneMesh_8yprl") +skeleton = NodePath("") +surface_material_override/0 = SubResource("StandardMaterial3D_8tgff") + +[node name="StaticBody3D" type="StaticBody3D" parent="MeshInstance3D"] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="MeshInstance3D/StaticBody3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.000976563, -0.0739136, -0.00112915) +shape = SubResource("BoxShape3D_t58di") + +[node name="Player3d" parent="." node_paths=PackedStringArray("_fieldParent") instance=ExtResource("1_pdc5e")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5.51633, 0.725631, 3.57249) +_fieldParent = NodePath("../FieldService") + +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 0.618408, 0.785857, 0, -0.785857, 0.618408, -0.802612, 0, 4.73262) + +[node name="BaseField" parent="." instance=ExtResource("2_741gk")] +transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0.0574723, 0) +visible = false + +[node name="Inventory" parent="." instance=ExtResource("6_876p1")] + +[node name="FieldService" type="Node3D" parent="."] +script = ExtResource("7_3rkjv") + +[node name="GenericItemOnGround" parent="." instance=ExtResource("5_sjt6k")] + +[node name="SpawnWithItem" parent="GenericItemOnGround" index="1"] +_blueprint = ExtResource("6_nvnpb") + +[editable path="GenericItemOnGround"] diff --git a/scripts/CSharp/Common/Inventory/InventoryManager.cs b/scripts/CSharp/Common/Inventory/InventoryManager.cs index cd024aa..ed44999 100644 --- a/scripts/CSharp/Common/Inventory/InventoryManager.cs +++ b/scripts/CSharp/Common/Inventory/InventoryManager.cs @@ -60,4 +60,8 @@ public partial class InventoryManager : Node { return inventory.RemoveItem(inventorySlot); } + public InventoryActionResult CollectItem(ItemInstance itemInstance) + { + return playerInventory.AddItem(itemInstance); + } } diff --git a/scripts/CSharp/Common/Inventory/InventoryUi.cs b/scripts/CSharp/Common/Inventory/InventoryUi.cs index f9241be..3b8ed7d 100644 --- a/scripts/CSharp/Common/Inventory/InventoryUi.cs +++ b/scripts/CSharp/Common/Inventory/InventoryUi.cs @@ -25,6 +25,7 @@ public partial class InventoryUi : Control PopulateSlots(); SetSlotContent(); SetSlotSelectPosition(); + InventoryManager.Instance.playerInventory.InventoryContentsChanged += SetSlotContent; } public override void _ExitTree() @@ -88,7 +89,7 @@ public partial class InventoryUi : Control var destinationSlot = index; InventoryManager.Instance.MoveItem(_playerInventory, sourceSlot, _playerInventory, destinationSlot); _slotOnMouse = null; - SetSlotContent(); + //SetSlotContent(); } } diff --git a/scripts/CSharp/Common/Inventory/ItemOnGround.cs b/scripts/CSharp/Common/Inventory/ItemOnGround.cs new file mode 100644 index 0000000..714174c --- /dev/null +++ b/scripts/CSharp/Common/Inventory/ItemOnGround.cs @@ -0,0 +1,46 @@ +using Godot; +namespace Babushka.scripts.CSharp.Common.Inventory; + +public partial class ItemOnGround : Node3D +{ + private ItemInstance _itemInstance; + + public ItemInstance itemInstance + { + get => _itemInstance; + set + { + _itemInstance = value; + UpdateVisuals(); + } + } + + public void TryPickUp() + { + GD.Print("Trying to pick up item"); + + var result = InventoryManager.Instance.CollectItem(itemInstance); + if (result == InventoryActionResult.Success) + { + QueueFree(); + } + else + { + GD.Print("Inventory is full"); + // TODO: player message + } + } + + public void UpdateVisuals() + { + var label = GetNode("ItemLabel"); + if (itemInstance.blueprint != null) + { + label.Text = itemInstance.blueprint.name; + } + else + { + label.Text = "Error Item"; + } + } +} diff --git a/scripts/CSharp/Common/Inventory/ItemOnGround.cs.uid b/scripts/CSharp/Common/Inventory/ItemOnGround.cs.uid new file mode 100644 index 0000000..c2313d3 --- /dev/null +++ b/scripts/CSharp/Common/Inventory/ItemOnGround.cs.uid @@ -0,0 +1 @@ +uid://udhigottc8rg diff --git a/scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs b/scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs new file mode 100644 index 0000000..3f530ea --- /dev/null +++ b/scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs @@ -0,0 +1,16 @@ +#nullable enable +using Godot; +namespace Babushka.scripts.CSharp.Common.Inventory; + +public partial class ItemOnGroundSpawnWith : Node +{ + [Export] private ItemResource? _blueprint = null; + + public override void _EnterTree() + { + if(_blueprint == null) return; + + var parent = GetParent(); + parent.itemInstance = new ItemInstance { blueprint = _blueprint }; + } +} diff --git a/scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs.uid b/scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs.uid new file mode 100644 index 0000000..bd0685f --- /dev/null +++ b/scripts/CSharp/Common/Inventory/ItemOnGroundSpawnWith.cs.uid @@ -0,0 +1 @@ +uid://c8suoi3i6kqai