person can take and interact with item

This commit is contained in:
Nikolai Fesenko
2025-08-19 13:58:34 +02:00
parent e4667a3a47
commit 31c9a69132
9 changed files with 97 additions and 36 deletions

View File

@@ -2,3 +2,8 @@ extends Node3D
class_name Item
var item_name
var shelf_owner
var is_being_used: bool
func use():
print(str(self) + "is used")

View File

@@ -1,4 +1,12 @@
extends Item
func _init() -> void:
item_name = "food"
is_being_used = false
func use():
print("Object eaten")
self.queue_free()

View File

@@ -5,9 +5,9 @@ extends Node3D
@export var meshInstance: MeshInstance3D
@export var preview_shader: ShaderMaterial
@export var ActionNode: Node3D
@export var static_body: StaticBody3D
var default_shaders: Array
var static_body: StaticBody3D
#var static_body: StaticBody3D
#var ActionNode: Node3D
var is_placed: bool = false
@@ -18,6 +18,8 @@ func _ready() -> void:
static_body = $StaticBody3D
static_body.mouse_entered.connect(_on_mouse_enter)
static_body.mouse_exited.connect(_on_mouse_exit)
ActionNode = $ActionNode
@@ -54,12 +56,8 @@ func _on_preview_create():
func _on_object_placed(build_postion):
is_placed = true
static_body.input_ray_pickable = true
static_body.mouse_entered.connect(_on_mouse_enter)
static_body.mouse_exited.connect(_on_mouse_exit)
restore_default_shaders()
func enable_outline():

View File

@@ -19,7 +19,9 @@ func _input(event: InputEvent) -> void:
func _on_object_placed(build_postion):
var newObject = BuildManager.get_preview_object().instantiate()
newObject.static_body.input_ray_pickable = true
newObject.global_position = build_postion
BuildManager.add_object_to_array(newObject)
newObject.ActionNode.create()
#newObject.restore_default_shaders()
self.add_child(newObject)

View File

@@ -10,5 +10,4 @@ func _ready() -> void:
interact.connect(_on_interact)
func _on_interact(person, object):
object.ActionNode.start_action(person, object)
print(object)
object.start_action(person, object)

View File

@@ -7,10 +7,11 @@ var shelfes: Array
func set_person_target_item(person,item_name):
var result = find_item(item_name)
print(result)
if result != null:
person.set_target(result.shelf_owner.owner)
result.is_being_used = true
person.target_item = result
person.set_target(result.shelf_owner)
func find_item(item_name):
@@ -18,7 +19,7 @@ func find_item(item_name):
for shelf in shelfes:
if shelf != null:
for item in shelf.stored_items:
if item.item_name == item_name:
if item.item_name == item_name and !item.is_being_used:
return item
return null

View File

@@ -6,35 +6,28 @@ var test: bool = false
func _init() -> void:
max_person_using = 1
can_store = true
print("init")
func _ready() -> void:
self.owner = $".."
print(self)
#object_placed.connect(_on_object_placed.bind(self))
print("ready")
func start_action(person, object):
pass
if person.target_action == "take":
give_out(person)
func give_out(person):
pass
person.hold_item(person.target_item)
stored_items.erase(person.target_item)
func _on_object_placed(_owner):
print(2)
print(_owner)
var newItem = ItemManager.get_item(0).instantiate()
ItemManager.add_shelf(_owner)
#ItemManager.shelfes.append(_owner)
ItemManager.add_item_to_shelf(_owner, newItem)
print("placed")
func create():
var newItem = ItemManager.get_item(0).instantiate()
ItemManager.shelfes.append(self)
ItemManager.add_shelf(self)
ItemManager.add_item_to_shelf(self, newItem)
print("placed")

View File

@@ -1,5 +1,7 @@
extends CharacterBody3D
signal item_received
var nav_agent: NavigationAgent3D
var shirt_shader: ShaderMaterial
@@ -19,7 +21,9 @@ var is_holding_item: bool = false
var holdingItem: Object
var target_item: int
var target_item
var target_action: String
func _ready() -> void:
nav_agent = $NavigationAgent3D
@@ -35,6 +39,8 @@ func _ready() -> void:
timer = $Timer
item_received.connect(_on_item_received)
#nav_agent.target_position = Vector3(-15.955,3.486,-58.942)
func _physics_process(delta: float) -> void:
@@ -46,6 +52,7 @@ func _physics_process(delta: float) -> void:
func _input(event: InputEvent) -> void:
if event.is_action_pressed("test"):
target_action = "take"
ItemManager.set_person_target_item(self, "food")
#set_target_position()
@@ -67,16 +74,17 @@ func set_target(object):
#nav_agent.target_position = target_object.ActionNode.global_position
func set_target_position():
is_target_reached = false
var newPos = target_object.ActionNode.global_position
var newPos = target_object.global_position
newPos.y = self.position.y
nav_agent.target_position = newPos
print(target_object.ActionNode.can_be_used())
print(target_object.can_be_used())
#nav_agent.target_position = start_pos
func _on_navigation_agent_3d_target_reached() -> void:
if target_object != null:
print(22)
is_target_reached = true
nav_agent.target_position = self.global_position
Global.interact.emit(self, target_object)
@@ -90,11 +98,16 @@ func _on_timer_timeout() -> void:
func hold_item(item):
var newItem = item.instantiate()
var newItem = item.duplicate()
holdingItem = newItem
newItem.position = Vector3(0,0.976, -0.065)
self.add_child(newItem)
holdingItem.position = Vector3(0,0.976, -0.065)
self.add_child(holdingItem)
item_received.emit()
func drop_item():
pass
func _on_item_received():
if holdingItem != null:
holdingItem.use()