hurrmmm/godot/control_scheme/controller.gd

51 lines
1.4 KiB
GDScript3
Raw Normal View History

2023-11-11 09:15:31 -07:00
extends Node3D
@export var camera: Camera3D
2023-11-11 13:08:24 -07:00
@export var initial_target: Node3D
var target: Targetable = null
2023-11-11 09:15:31 -07:00
@export var sensitivity := 0.01
2023-11-11 16:12:46 -07:00
@export var charge_time := 2
2023-11-11 09:15:31 -07:00
2023-11-11 13:08:24 -07:00
func _ready():
set_target(initial_target)
2023-11-11 16:12:46 -07:00
func get_charge():
return %radial_ui.charge_amount
2023-11-11 09:15:31 -07:00
func _input(event):
2023-11-11 16:12:46 -07:00
if event is InputEventMouseMotion and MouseControl.mouse_is_locked() and get_charge() == 0.0:
2023-11-11 09:15:31 -07:00
rotate_view(event.relative*sensitivity)
2023-11-11 14:56:43 -07:00
if event.is_action("charge"):
charge(event.get_action_strength("charge"))
2023-11-11 09:15:31 -07:00
func _process(_delta):
transform.origin = target.global_position
camera.global_position = %camera_spot.global_position
camera.look_at(target.global_position)
2023-11-11 14:56:43 -07:00
var charge_tween: Tween = null
func charge(amount: float):
if charge_tween != null:
charge_tween.kill()
if amount == 0.0:
%radial_ui.set_charge(0.0)
2023-11-11 16:12:46 -07:00
$AudioStreamPlayer.stop()
2023-11-11 14:56:43 -07:00
return
charge_tween = create_tween()
charge_tween.tween_method(%radial_ui.set_charge, 0.0, 1.0, charge_time)
2023-11-11 16:12:46 -07:00
$AudioStreamPlayer.play()
2023-11-11 14:56:43 -07:00
2023-11-11 09:15:31 -07:00
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)
2023-11-11 13:08:24 -07:00
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