Camera.gd (1818B)
1 extends Camera 2 3 4 # Space Vroom, an unoriginal space racer. 5 # Copyright (C) 2021 Yuval Langer 6 # 7 # This program is free software: you can redistribute it and/or modify 8 # it under the terms of the GNU Affero General Public License as 9 # published by the Free Software Foundation, either version 3 of the 10 # License, or (at your option) any later version. 11 # 12 # This program is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU Affero General Public License for more details. 16 # 17 # You should have received a copy of the GNU Affero General Public License 18 # along with this program. If not, see <https://www.gnu.org/licenses/>. 19 20 onready var player: KinematicBody = get_parent().get_node("Player") 21 onready var mesh_instance: MeshInstance = $MeshInstance 22 23 24 var offset 25 26 27 func _ready() -> void: 28 offset = translation - player.translation 29 30 31 func _process(delta: float) -> void: 32 translation.x = player.translation.x + offset.x 33 # translation.y = player.translation.y + offset.y 34 translation.z = player.translation.z + offset.z 35 36 # display_background() 37 38 39 func display_background() -> void: 40 # TODO: Crop excess background image? 41 42 var viewport_size: Vector2 = get_viewport().size 43 var mesh_distance_from_camera: float = abs(mesh_instance.translation.z) 44 var worldspace_top_left: Vector3 = project_position(Vector2(0, 0), mesh_distance_from_camera) 45 var worldspace_bottom_right: Vector3 = project_position(viewport_size, mesh_distance_from_camera) 46 var local_top_left: Vector3 = to_local(worldspace_top_left) 47 var local_bottom_right: Vector3 = to_local(worldspace_bottom_right) 48 mesh_instance.mesh.size = Vector2(abs(local_top_left.x-local_bottom_right.x), abs(local_top_left.y - local_bottom_right.y))