cosmic/scripts/Ship/player_input_controller.gd
2024-05-15 07:48:03 +03:00

36 lines
1.3 KiB
GDScript

extends Node2D
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
## Reference to the star system
@onready var star_system: StarSystem = get_tree().current_scene
var expected_rotation: float = 0.0:
set(value):
if value > 180:
expected_rotation = value - 360
elif value < -180:
expected_rotation = 360 + value
else:
expected_rotation = value
func _physics_process(_delta) -> void:
if ship.selected_node is Base:
if Input.is_action_just_released("dock"):
ship.selected_node.dock_requested.emit()
if ship.selected_node.dock_state == Base.DockState.Busy:
return
ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate")
ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright")
for weapon in ship.weapons.list:
match weapon.action_id:
"primary":
weapon.shoot_request = Input.get_action_strength("shootprimary")
"secondary":
weapon.shoot_request = Input.get_action_strength("shootsecondary")
if "gun_rotation" in weapon:
var angle_to_mouse = ship.hull.position.angle_to_point(get_global_mouse_position())
expected_rotation = rad_to_deg(angle_to_mouse - ship.hull.rotation)
weapon.gun_rotation = expected_rotation
if Input.is_action_just_released("select_target"):
ship.selected_node = star_system.targeted_node