From e9e321315f50233a474ff9e9aab32de9aa350225 Mon Sep 17 00:00:00 2001 From: cflip Date: Wed, 1 Feb 2023 19:38:41 -0700 Subject: Refactor camera code to be responsive to aspect ratio --- scripts/camera.gd | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/scripts/camera.gd b/scripts/camera.gd index f44e430..773ff75 100644 --- a/scripts/camera.gd +++ b/scripts/camera.gd @@ -9,14 +9,30 @@ func _process(_delta): adjust_zoom() func adjust_zoom(): - if !basketball.waiting_for_shoot and (basketball.position.distance_to(hoop.position) + (basketball.drag_radius * 3)) > limit_right: - set_zoom(Vector2(1, 1) * ((basketball.position.distance_to(hoop.position) + (basketball.drag_radius * 3)) / limit_right)) - elif basketball.waiting_for_shoot and (basketball.start_position.distance_to(hoop.position) + (basketball.drag_radius * 3)) > limit_right: - set_zoom(Vector2(1, 1) * ((basketball.start_position.distance_to(hoop.position) + (basketball.drag_radius * 3)) / limit_right)) - else: - set_zoom(Vector2(1, 1)) - var zoom_level = get_zoom() - set_zoom(Vector2(clamp(zoom_level.x, 1, 1.94), clamp(zoom_level.y, 1, 1.94))) + var dist_to_hoop = basketball.position.distance_to(hoop.position) + if basketball.waiting_for_shoot: + dist_to_hoop = basketball.start_position.distance_to(hoop.position) + + # Min and max widths of the court we want visible on screen at once + var min_width = dist_to_hoop + basketball.drag_radius * 3 + var max_width = limit_right - limit_left + + var zoom_level = Vector2(1, 1) + + # Zoom out the camera if we need to show more on screen + if min_width > limit_right: + zoom_level = zoom_level * (min_width / limit_right) + + # Width of the view when the zoom is set to 1 + var view_width = get_viewport().get_visible_rect().size.x + + # Fit the zoom level within the max width if needed + # TODO: There should also be a vertical limit + var max_zoom = float(max_width) / float(view_width) + zoom_level.x = clamp(zoom_level.x, 1, max_zoom) + zoom_level.y = clamp(zoom_level.y, 1, max_zoom) + + set_zoom(zoom_level) func _on_Basketball_reset(): position = (basketball.start_position + hoop.position) / 2 -- cgit v1.2.3