61 lines
2.0 KiB
GDScript
61 lines
2.0 KiB
GDScript
extends Marker2D
|
|
|
|
@export_group("Status")
|
|
@export var lock_horizontal = true
|
|
@export_group("Config")
|
|
@export var speed := 5.0
|
|
@export var half_screen_size := Vector2(564, 240) / 2.0
|
|
@export var shaded_height := 38
|
|
@export_group("Shake", "shake_")
|
|
@export var shake_enabled := false
|
|
@export var shake_strength := 2.0
|
|
@export var shake_recovery_speed := 8.0
|
|
@export_group("Limit", "limit_")
|
|
@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
|
|
var zoom_tween: Tween
|
|
|
|
|
|
func tweak_position(velocity, facing_direction):
|
|
var ideal_x = facing_direction.x * min(50.0, 0.5 * abs(velocity.x))
|
|
var current_x = target.position.x
|
|
var delta = ideal_x - current_x
|
|
if abs(delta) > 10.0:
|
|
target.position.x = move_toward(current_x, ideal_x, speed * 2.0)
|
|
if lock_horizontal:
|
|
global_position.y = 0
|
|
target.position.y = 0
|
|
else:
|
|
target.position.y = facing_direction.y * abs(velocity.y) * 0.2
|
|
|
|
|
|
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 += shaded_height
|
|
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:
|
|
if zoom_tween and zoom_tween.is_running():
|
|
zoom_tween.kill()
|
|
zoom_tween = create_tween()
|
|
zoom_tween.tween_property(self, "zoom_ratio", ratio, duration).set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_IN_OUT)
|
|
return zoom_tween
|