extends XRController3D

var grabTarget: NodePath = NodePath()
var grabbing = false
@export var physicsHand: RigidBody3D
@onready var physicsPath: NodePath = physicsHand.get_path()
@onready var hinge = physicsHand.get_node("HingeJoint3D")

func _on_input_float_changed(action_name: String, value: float) -> void:
	if action_name == "grip":
		if value >= 0.5:
			grab(true)
		else:
			grab(false)

func grab(grabORrelease: bool):
	if grabORrelease == true and not grabbing:
		grabbing = true
		if grabTarget != NodePath():
			hinge.node_a = physicsPath
			hinge.node_b = grabTarget
	if grabORrelease == false:
		grabbing = false
		hinge.node_a = NodePath()
		hinge.node_b = NodePath()


func _rigid_body_entered(body: Node) -> void:
	grabTarget = body.get_path()

func _rigid_body_exited(body: Node) -> void:
	grabTarget = NodePath()
