146 lines
3.1 KiB
GDScript
146 lines
3.1 KiB
GDScript
extends CharacterBody3D
|
|
|
|
signal item_received
|
|
|
|
var nav_agent: NavigationAgent3D
|
|
|
|
var shirt_shader: ShaderMaterial
|
|
var meshI: MeshInstance3D
|
|
|
|
var start_pos: Vector3
|
|
var target_object: Node3D
|
|
|
|
var is_in_action: bool
|
|
var action: Interactable
|
|
|
|
var is_target_reached: bool
|
|
|
|
var timer: Timer
|
|
|
|
var is_holding_item: bool = false
|
|
var holdingItem: Object
|
|
|
|
|
|
var target_item
|
|
|
|
var target_action: String
|
|
|
|
var is_selected: bool = false
|
|
|
|
func _ready() -> void:
|
|
nav_agent = $NavigationAgent3D
|
|
meshI = $MeshInstance3D
|
|
shirt_shader = meshI.get_surface_override_material(1)
|
|
|
|
|
|
set_random_shirt()
|
|
|
|
start_pos = global_position
|
|
|
|
is_in_action = false
|
|
|
|
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:
|
|
var new_velocity = global_position.direction_to(nav_agent.get_next_path_position()) * 5.0
|
|
if !is_target_reached:
|
|
look_at(nav_agent.target_position)
|
|
velocity = new_velocity
|
|
move_and_slide()
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("test"):
|
|
target_action = "take"
|
|
ItemManager.set_person_target_item(self, "food")
|
|
#set_target_position()
|
|
|
|
|
|
|
|
|
|
func set_random_shirt():
|
|
var new_shirt_color = Vector3(randf(),randf(),randf())
|
|
shirt_shader.set_shader_parameter("shirt_color", new_shirt_color)
|
|
pass
|
|
|
|
|
|
func set_target(object):
|
|
|
|
#if !is_in_action and object.ActionNode.can_be_used():
|
|
if !is_in_action:
|
|
target_object = object
|
|
set_target_position()
|
|
#nav_agent.target_position = target_object.ActionNode.global_position
|
|
func set_target_position():
|
|
is_target_reached = false
|
|
var newPos = target_object.global_position
|
|
newPos.y = self.position.y
|
|
nav_agent.target_position = newPos
|
|
if target_object.name != "ActionNode":
|
|
target_object.ActionNode.area.body_entered.connect(on_target_reached)
|
|
else:
|
|
target_object.area.body_entered.connect(on_target_reached)
|
|
#nav_agent.target_position = start_pos
|
|
|
|
func on_target_reached(body):
|
|
print("I reached ")
|
|
if body == self:
|
|
if target_object != null:
|
|
print("I reached a target")
|
|
is_target_reached = true
|
|
nav_agent.target_position = self.global_position
|
|
Global.interact.emit(self, target_object)
|
|
func _on_navigation_agent_3d_target_reached() -> void:
|
|
|
|
pass
|
|
|
|
func set_timer(time):
|
|
timer.wait_time = time
|
|
timer.one_shot = true
|
|
timer.start()
|
|
|
|
func _on_timer_timeout() -> void:
|
|
if action != null:
|
|
print("action stoped")
|
|
action.stop_action(self,target_object)
|
|
|
|
|
|
func hold_item(item):
|
|
is_holding_item = true
|
|
var newItem = item.duplicate()
|
|
holdingItem = 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:
|
|
if target_action == "eat" and holdingItem.name == "food":
|
|
holdingItem.use()
|
|
print("I ate it")
|
|
|
|
|
|
|
|
|
|
func _on_mouse_entered() -> void:
|
|
Global.object_over_mouse_selected_signal.emit(self)
|
|
enable_outline()
|
|
|
|
|
|
func _on_mouse_exited() -> void:
|
|
if is_selected == false:
|
|
disable_outline()
|
|
|
|
|
|
func enable_outline():
|
|
$MeshOutline.visible = true
|
|
func disable_outline():
|
|
$MeshOutline.visible = false
|