Swarm/CameraPivot.gd

80 lines
2.9 KiB
GDScript3
Raw Permalink Normal View History

2024-03-23 16:18:30 +01:00
extends Node3D
@onready var azimuth_pivot = $AzimuthPivot
@onready var spring_arm_3d = $AzimuthPivot/SpringArm3D
var zoom_tween: Tween
var min_zoom_distance: float = 2.0
var max_zoom_distance: float = 10.0
var min_camera_angle: float = 15.0
var max_camera_angle: float = 75.0
var rotating: bool = false
var rotate_2d_position: Vector2 = Vector2.ZERO
var rotate_2d_position_old: Vector2 = Vector2.ZERO
var old_y_rotation: float = 0.0
var old_x_rotation: float = 0.0
@onready var camera = $AzimuthPivot/SpringArm3D/Camera
var world_distance_limit: float = 25.0
func _ready() -> void:
if not multiplayer.is_server():
rotation.y = PI
2024-03-23 16:18:30 +01:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if Input.is_action_just_pressed("zoom_camera_in"):
if zoom_tween != null:
zoom_tween.kill()
zoom_tween = get_tree().create_tween()
var new_dist = clamp(spring_arm_3d.spring_length / 1.2, min_zoom_distance, max_zoom_distance)
zoom_tween.tween_property(spring_arm_3d, "spring_length", new_dist, 0.25)
#spring_arm_3d.spring_length -= 0.5
if Input.is_action_just_pressed("zoom_camera_out"):
if zoom_tween != null:
zoom_tween.kill()
zoom_tween = get_tree().create_tween()
var new_dist = clamp(spring_arm_3d.spring_length * 1.2, min_zoom_distance, max_zoom_distance)
zoom_tween.tween_property(spring_arm_3d, "spring_length", new_dist, 0.25)
#spring_arm_3d.spring_length += 0.5
if Input.is_action_just_pressed("rotate_camera"):
rotating = true
# get screen position of cursor?
rotate_2d_position = get_viewport().get_mouse_position()
rotate_2d_position_old = get_viewport().get_mouse_position()
old_y_rotation = rotation.y
old_x_rotation = azimuth_pivot.rotation.x
if Input.is_action_just_released("rotate_camera"):
rotating = false
if rotating:
rotate_2d_position = get_viewport().get_mouse_position()
var mouse_diff = rotate_2d_position - rotate_2d_position_old
2024-04-20 01:26:11 +02:00
rotation.y = old_y_rotation - mouse_diff.x * GameSettings.camera_rotation_speed / 1000.0
azimuth_pivot.rotation.x = old_x_rotation - mouse_diff.y * GameSettings.camera_rotation_speed / 1000.0
2024-03-23 16:18:30 +01:00
pass
if Input.is_action_pressed("camera_left"):
2024-04-20 01:26:11 +02:00
position -= transform.basis.x * GameSettings.camera_movement_speed / 1000.0
2024-03-23 16:18:30 +01:00
if Input.is_action_pressed("camera_right"):
2024-04-20 01:26:11 +02:00
position += transform.basis.x * GameSettings.camera_movement_speed / 1000.0
2024-03-23 16:18:30 +01:00
if Input.is_action_pressed("camera_up"):
2024-04-20 01:26:11 +02:00
position -= transform.basis.z * GameSettings.camera_movement_speed / 1000.0
2024-03-23 16:18:30 +01:00
if Input.is_action_pressed("camera_down"):
2024-04-20 01:26:11 +02:00
position += transform.basis.z * GameSettings.camera_movement_speed / 1000.0
2024-03-23 16:18:30 +01:00
if position.length() > world_distance_limit:
position = position.normalized() * world_distance_limit
if azimuth_pivot.rotation_degrees.x > -min_camera_angle:
azimuth_pivot.rotation_degrees.x = -min_camera_angle
if azimuth_pivot.rotation_degrees.x < -max_camera_angle:
azimuth_pivot.rotation_degrees.x = -max_camera_angle