From a0efc6b9b80604d59e4f66f6ed0a1e8f04b865b0 Mon Sep 17 00:00:00 2001 From: Nikolai Fesenko <152747946+NikolaiFesenko-code@users.noreply.github.com> Date: Wed, 20 Aug 2025 16:45:03 +0200 Subject: [PATCH] context menu added player can find food and eat it --- Scenes/Prefabs/DoubleBeds.tscn | 4 ++- Scenes/Prefabs/food_item.tscn | 1 + Scenes/Prefabs/main_game_ui.tscn | 39 +++++++++++++++++++++++++- Scripts/ContextMenu.gd | 47 ++++++++++++++++++++++++++++++++ Scripts/ContextMenu.gd.uid | 1 + Scripts/camera_3d.gd | 2 +- Scripts/global_script.gd | 19 ++++++++----- Scripts/item_manager.gd | 3 ++ Scripts/objects/shelf_object.gd | 2 +- Scripts/person.gd | 7 ++--- Scripts/person_action.gd | 21 ++++++++++++++ 11 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 Scripts/ContextMenu.gd create mode 100644 Scripts/ContextMenu.gd.uid diff --git a/Scenes/Prefabs/DoubleBeds.tscn b/Scenes/Prefabs/DoubleBeds.tscn index 659379a..ba39d26 100644 --- a/Scenes/Prefabs/DoubleBeds.tscn +++ b/Scenes/Prefabs/DoubleBeds.tscn @@ -52,10 +52,12 @@ size = Vector3(5.62061, 2.52063, 2.12427) [sub_resource type="SphereShape3D" id="SphereShape3D_c1046"] radius = 0.870929 -[node name="DoubleBeds" type="Node3D" node_paths=PackedStringArray("meshInstance")] +[node name="DoubleBeds" type="Node3D" node_paths=PackedStringArray("meshInstance", "ActionNode", "static_body")] script = ExtResource("1_nn5da") meshInstance = NodePath("DoubleBeds") preview_shader = SubResource("ShaderMaterial_dn7et") +ActionNode = NodePath("ActionNode") +static_body = NodePath("StaticBody3D") [node name="DoubleBeds" type="MeshInstance3D" parent="."] transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -1.83435, -0.42751, 0) diff --git a/Scenes/Prefabs/food_item.tscn b/Scenes/Prefabs/food_item.tscn index 4b6f30c..fffafdd 100644 --- a/Scenes/Prefabs/food_item.tscn +++ b/Scenes/Prefabs/food_item.tscn @@ -13,6 +13,7 @@ script = ExtResource("2_cemcp") mesh = NodePath("Cube") [node name="StaticBody3D" type="StaticBody3D" parent="." index="0"] +process_mode = 4 transform = Transform3D(1, -1.44862e-12, -1.09139e-11, -1.56369e-12, 1, 1.81899e-12, 7.27596e-12, 0, 1, 0, 0, 0) [node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D" index="0"] diff --git a/Scenes/Prefabs/main_game_ui.tscn b/Scenes/Prefabs/main_game_ui.tscn index 62ef251..9d2e7f2 100644 --- a/Scenes/Prefabs/main_game_ui.tscn +++ b/Scenes/Prefabs/main_game_ui.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=3 format=3 uid="uid://cyaesj3qjb5um"] +[gd_scene load_steps=4 format=3 uid="uid://cyaesj3qjb5um"] [ext_resource type="Script" uid="uid://bqh6f4f4sc4ba" path="res://Scripts/ui/panel_build_select.gd" id="1_dew6i"] [ext_resource type="Script" uid="uid://3bsllk6udire" path="res://Scripts/button_select_build.gd" id="1_yp16r"] +[ext_resource type="Script" uid="uid://di0njko68ahky" path="res://Scripts/ContextMenu.gd" id="3_yve02"] [node name="MainGameUi" type="Control"] layout_mode = 3 @@ -83,6 +84,42 @@ text = "Shelf" script = ExtResource("1_yp16r") id = 3 +[node name="Panel3" type="Panel" parent="."] +visible = false +clip_contents = true +layout_mode = 0 +offset_right = 119.0 +offset_bottom = 92.0 +script = ExtResource("3_yve02") + +[node name="ScrollContainer" type="ScrollContainer" parent="Panel3"] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="VBoxContainer" type="VBoxContainer" parent="Panel3/ScrollContainer"] +layout_mode = 2 +size_flags_horizontal = 3 + +[node name="Button" type="Button" parent="Panel3/ScrollContainer/VBoxContainer"] +layout_mode = 2 +text = "Action #1" + +[node name="Button2" type="Button" parent="Panel3/ScrollContainer/VBoxContainer"] +layout_mode = 2 +text = "Action #2" + +[node name="Button3" type="Button" parent="Panel3/ScrollContainer/VBoxContainer"] +layout_mode = 2 +text = "Action #1" + +[node name="Button4" type="Button" parent="Panel3/ScrollContainer/VBoxContainer"] +layout_mode = 2 +text = "Action #1" + [connection signal="pressed" from="Panel2/HBoxContainer/ButtonCreateBed" to="Panel2/HBoxContainer/ButtonCreateBed" method="_on_pressed"] [connection signal="pressed" from="Panel2/HBoxContainer/ButtonCreatePot" to="Panel2/HBoxContainer/ButtonCreatePot" method="_on_pressed"] [connection signal="pressed" from="Panel2/HBoxContainer/ButtonCreatePot2" to="Panel2/HBoxContainer/ButtonCreatePot2" method="_on_pressed"] diff --git a/Scripts/ContextMenu.gd b/Scripts/ContextMenu.gd new file mode 100644 index 0000000..b1c3131 --- /dev/null +++ b/Scripts/ContextMenu.gd @@ -0,0 +1,47 @@ +extends Panel + + +signal show_context_menu +signal hide_context_menu + + +func _ready() -> void: + Global.show_context_menu.connect(_on_show_menu) + Global.hide_context_menu.connect(_on_hide_menu) + +func _on_show_menu(): + if self.visible != true: + show_menu() + else: + Global.hide_context_menu.emit() +func _on_hide_menu(): + hide_menu() + + +func show_menu(): + Global.is_context_menu_active = true + self.position = get_viewport().get_mouse_position() + get_context_actions() + self.visible = true +func hide_menu(): + Global.is_context_menu_active = false + self.visible = false + + +func get_context_actions(): + var container = $ScrollContainer/VBoxContainer + remove_children(container) + var newButtons + print(Global.object_selected.name) + if Global.object_selected.name == "Person": + newButtons = PersonAction.get_context_buttons() + + for button in newButtons: + container.add_child(button) + + +func remove_children(object: Node): + var children = object.get_children() + + for child in children: + object.remove_child(child) diff --git a/Scripts/ContextMenu.gd.uid b/Scripts/ContextMenu.gd.uid new file mode 100644 index 0000000..d29ce98 --- /dev/null +++ b/Scripts/ContextMenu.gd.uid @@ -0,0 +1 @@ +uid://di0njko68ahky diff --git a/Scripts/camera_3d.gd b/Scripts/camera_3d.gd index f9eb376..aed604f 100644 --- a/Scripts/camera_3d.gd +++ b/Scripts/camera_3d.gd @@ -36,7 +36,7 @@ func _process(delta: float) -> void: #print(self.position.dot(ownRigidBody.position)) func _input(event: InputEvent) -> void: # zoom control - if(event.is_action("camera_zoom")): + if(event.is_action("camera_zoom") and !Global.is_context_menu_active): if (event.as_text() == "Mouse Wheel Up"): #self.position = self.position.lerp(Vector3.ZERO,global_delta * 5.0) zoom_pos += global_delta * 45.0 diff --git a/Scripts/global_script.gd b/Scripts/global_script.gd index 3967674..023e4e1 100644 --- a/Scripts/global_script.gd +++ b/Scripts/global_script.gd @@ -11,6 +11,9 @@ signal object_unselected_signal signal object_over_mouse_selected_signal(object) signal object_over_mouse_unselected_signal(object) +signal show_context_menu +signal hide_context_menu +var is_context_menu_active: bool = false var object_selected var object_over_mouse @@ -27,27 +30,29 @@ func _ready() -> void: func _input(event: InputEvent) -> void: - if event.is_action_released("mouse_click"): + if event.is_action_released("mouse_click") and !is_context_menu_active: if object_over_mouse != null: object_selected_signal.emit(object_over_mouse) - if event.is_action_released("right_mouse_click"): - if object_selected != object_over_mouse: + else: object_unselected_signal.emit() + if event.is_action_released("right_mouse_click"): + show_context_menu.emit() func _on_interact(person, object): object.start_action(person, object) func _on_object_selected(object): - if object_selected != null: + if object_selected != null and object != object_selected: object_unselected_signal.emit() object_selected = object object_selected.is_selected = true print(object_selected) func _on_object_unselected(): - object_selected.is_selected = false - object_selected.disable_outline() - object_selected = null + if object_selected != null: + object_selected.is_selected = false + object_selected.disable_outline() + object_selected = null func _on_mouse_object_selected(object): object_over_mouse = object diff --git a/Scripts/item_manager.gd b/Scripts/item_manager.gd index 20deb17..c54d3c0 100644 --- a/Scripts/item_manager.gd +++ b/Scripts/item_manager.gd @@ -11,6 +11,9 @@ func set_person_target_item(person,item_name): result.is_being_used = true person.target_item = result person.set_target(result.shelf_owner) + return true + else: + return null diff --git a/Scripts/objects/shelf_object.gd b/Scripts/objects/shelf_object.gd index b2ce355..0e1dd30 100644 --- a/Scripts/objects/shelf_object.gd +++ b/Scripts/objects/shelf_object.gd @@ -18,7 +18,7 @@ func _ready() -> void: func start_action(person, object): - if person.target_action == "take": + if person.target_action == "take" or person.target_action == "eat": give_out(person) diff --git a/Scripts/person.gd b/Scripts/person.gd index ba9e2e3..083394c 100644 --- a/Scripts/person.gd +++ b/Scripts/person.gd @@ -112,12 +112,11 @@ func drop_item(): func _on_item_received(): if holdingItem != null: - pass - #holdingItem.use() + if target_action == "eat" and holdingItem.name == "food": + holdingItem.use() + print("I ate it") -func _on_input_event(camera: Node, event: InputEvent, event_position: Vector3, normal: Vector3, shape_idx: int) -> void: - pass func _on_mouse_entered() -> void: diff --git a/Scripts/person_action.gd b/Scripts/person_action.gd index c2655f6..27d053e 100644 --- a/Scripts/person_action.gd +++ b/Scripts/person_action.gd @@ -1,6 +1,27 @@ extends Node +func get_context_buttons(): + var buttons = Array() + # go eat + var go_eat_button = Button.new() + go_eat_button.text = "Go Eat" + go_eat_button.pressed.connect(go_eat) + buttons.append(go_eat_button) + + return buttons +func go_eat(): + print("I will go eat") + print(Global.object_selected) + var selected_person = Global.object_selected + selected_person.target_action = "eat" + var result = ItemManager.set_person_target_item(selected_person, "food") + if result == null: + print("I cant find food") + else: + print("I found food") + Global.hide_context_menu.emit() + func sleep(person, object, action): person.action = action person.timer.wait_time = 2.0