extends Node3D @export var camera: Camera3D @export var initial_target: Node3D var target: Targetable = null @export var sensitivity := 0.01 @export var charge_time := 1.7 @export var min_charge := 0.2 func _ready(): set_target(initial_target) func get_charge(): return %radial_ui.charge_amount func _input(event): var billiard := target.get_billiard() if event is InputEventMouseMotion and MouseControl.mouse_is_locked() and get_charge() == 0.0: rotate_view(event.relative*sensitivity) if event.is_action_released("charge") and not billiard.can_hit and charge_tween != null: release() if event.is_action_pressed("charge") and billiard.can_hit and charge_tween == null: charge(event.get_action_strength("charge")) func _process(_delta): transform.origin = target.global_position camera.global_position = %camera_spot.global_position camera.look_at(target.global_position) if get_charge() >= 1.0: cancel_charge() func cancel_charge(): %radial_ui.set_charge(0.0) %ChargeSound.stop() if charge_tween != null: charge_tween.kill() charge_tween = null var billiard := target.get_billiard() billiard.can_hit = true func release(): if get_charge() <= min_charge: cancel_charge() return %ChargeReleaseSound.volume_db = get_charge() * 12 %ChargeReleaseSound.play() var billiard := target.get_billiard() billiard.hit((target.global_position - %camera_spot.global_position).normalized()) %radial_ui.set_charge(0.0) %ChargeSound.stop() if charge_tween != null: charge_tween.kill() charge_tween = null var charge_tween: Tween = null func charge(_amount: float): var billiard := target.get_billiard() billiard.can_hit = false if charge_tween != null: charge_tween.kill() charge_tween = create_tween() charge_tween.tween_method(%radial_ui.set_charge, 0.0, 1.0, charge_time) %ChargeSound.play() func rotate_view(amount: Vector2): rotate_y(-amount.x) %rotate_helper.rotate_z(amount.y) %rotate_helper.rotation_degrees.z = clampf(%rotate_helper.rotation_degrees.z, -77, 77) func set_target(node: Node3D): var ntarget = Targetable.is_targetable(node) if ntarget == null: push_error("Node is node targetable", node) return if target != null: target.unmake_target() ntarget.make_target() target = ntarget