56 lines
1.6 KiB
GDScript
56 lines
1.6 KiB
GDScript
extends Camera2D
|
|
class_name RegionCamera
|
|
|
|
const MOVE_LERP_WEIGHT = 0.9
|
|
const ZOOM_LERP_WEIGHT = 0.05
|
|
|
|
const SHAKE_INTENSITY = 10.0
|
|
const SHAKE_SPEED = 500.0
|
|
|
|
const ZOOM_STEP = 0.1
|
|
|
|
@export var following : Node2D
|
|
@export var should_follow : bool = true
|
|
@onready var settings = GameInfo.settings_data
|
|
|
|
@export var shake_active_time = -1
|
|
var shake_noise := FastNoiseLite.new()
|
|
|
|
func _input(_e):
|
|
if Input.is_action_pressed("zoom_in"):
|
|
if settings.zoom < zoom.x: # was zooming out
|
|
settings.zoom = (zoom.x / ZOOM_STEP) * ZOOM_STEP
|
|
settings.zoom = settings.zoom + ZOOM_STEP
|
|
GameInfo.save_settings()
|
|
if Input.is_action_pressed("zoom_out"):
|
|
if settings.zoom > zoom.x: # was zooming in
|
|
settings.zoom = (zoom.x / ZOOM_STEP) * ZOOM_STEP
|
|
settings.zoom = settings.zoom - ZOOM_STEP
|
|
GameInfo.save_settings()
|
|
|
|
func _ready():
|
|
if following and should_follow:
|
|
zoom = Vector2.ONE * settings.zoom
|
|
global_position = following.global_position
|
|
|
|
func _process(delta):
|
|
if following and should_follow:
|
|
global_position = following.global_position.lerp(global_position, MOVE_LERP_WEIGHT)
|
|
|
|
zoom = zoom.lerp(Vector2.ONE * settings.zoom, ZOOM_LERP_WEIGHT)
|
|
|
|
if shake_active_time > 0.:
|
|
shake_active_time -= delta
|
|
|
|
offset = Vector2(
|
|
shake_noise.get_noise_2d(shake_active_time * SHAKE_SPEED, 0) * SHAKE_INTENSITY,
|
|
shake_noise.get_noise_2d(0, shake_active_time * SHAKE_SPEED) * SHAKE_INTENSITY,
|
|
)
|
|
else :
|
|
offset = lerp(offset, Vector2.ZERO, 10.5 * delta)
|
|
|
|
func shake(time := 5):
|
|
shake_noise.seed = randi()
|
|
|
|
shake_active_time = time
|