extends Marker2D @export var lock_horizontal = true @export var speed := 3.0 @export var half_screen_size := Vector2(564, 240) / 2.0 @export var top_shaded_length := 38 @export var limit_left := 0 @export var limit_right := 564 @export var limit_top := -158 @export var limit_bottom := 158 @onready var target = %Target as Node2D # @onready var camera = %MainCamera as MainCamera var zoom_ratio := 1.0 func tweak_position(velocity, facing_direction): target.position.x = facing_direction.x * abs(velocity.x) * 0.3 if lock_horizontal: global_position.y = 0 target.position.y = 0 else: target.position.y = facing_direction.y * abs(velocity.y) * 0.3 func _physics_process(delta: float) -> void: # set camera's position var target_position = target.global_position var current_position = MainCamera.global_position # easing with speed var position_delta = (target_position - current_position) * speed * delta var new_position = current_position + position_delta # clamp the position var margin = half_screen_size / zoom_ratio margin.y += top_shaded_length new_position.x = clamp(new_position.x, limit_left + margin.x, limit_right - margin.x) new_position.y = clamp(new_position.y, limit_top + margin.y, limit_bottom - margin.y) MainCamera.global_position = new_position MainCamera.zoom = Vector2(zoom_ratio, zoom_ratio) func tween_zoom(ratio: float, duration := 1.0) -> Tween: var tween = create_tween() tween.tween_property(self, "zoom_ratio", ratio, duration).set_trans(Tween.TRANS_CUBIC) return tween