diff options
author | cflip <cflip@cflip.net> | 2023-02-01 19:38:41 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-02-01 19:38:41 -0700 |
commit | e9e321315f50233a474ff9e9aab32de9aa350225 (patch) | |
tree | 5a7db54be5a9416182f50db2e9234c9e4ceea6c6 | |
parent | 98739d65647aca264aa150e92340ad19b784af28 (diff) |
Refactor camera code to be responsive to aspect ratio
-rw-r--r-- | scripts/camera.gd | 32 |
1 files 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 |