42 lines
1,005 B
GDScript
42 lines
1,005 B
GDScript
class_name Interactable
|
|
extends Area3D
|
|
|
|
signal interacted(player : PlayerCharacter)
|
|
|
|
@export var hint : Hint #talk.tres, etc.
|
|
@export var enabled : bool = true:
|
|
set(value):
|
|
enabled = value
|
|
_refresh()
|
|
|
|
var _player : PlayerCharacter = null
|
|
|
|
func _ready() -> void:
|
|
assert(hint != null, "Interactable : aucun Hint assigne sur %s" % get_path())
|
|
body_entered.connect(_on_body_entered)
|
|
body_exited.connect(_on_body_exited)
|
|
_refresh()
|
|
|
|
func _on_body_entered(body : Node3D) -> void:
|
|
if !(body is PlayerCharacter): return
|
|
_player = body
|
|
_refresh()
|
|
|
|
func _on_body_exited(body : Node3D) -> void:
|
|
if body != _player: return
|
|
_player = null
|
|
_refresh()
|
|
|
|
func _refresh() -> void:
|
|
var active : bool = enabled and _player != null
|
|
set_physics_process(active)
|
|
if active: Hints.show_hints(self, [hint])
|
|
else: Hints.clear_hints(self)
|
|
|
|
func _physics_process(_delta : float) -> void:
|
|
if Input.is_action_just_pressed(hint.action):
|
|
interacted.emit(_player)
|
|
|
|
func _exit_tree() -> void:
|
|
Hints.clear_hints(self)
|