Improved camera
This commit is contained in:
@ -1,46 +1,32 @@
|
||||
extends Camera3D
|
||||
|
||||
@export_group("Main")
|
||||
@export var follow_node: Node3D
|
||||
@export var pivot_node: Node3D
|
||||
@export_group("Zoom")
|
||||
@export var zoom_speed := 0.5
|
||||
@export var zoom_interpolation := 10.0
|
||||
@export var zoom_min := 2.0
|
||||
@export var zoom_max := 8.0
|
||||
|
||||
@export_group("Follow")
|
||||
@export var follow_enabled: bool
|
||||
@export var follow_speed: float
|
||||
|
||||
@export_group("Shake")
|
||||
@export var shake_strength := 1.0
|
||||
@export var shake_decay := 0.8
|
||||
@export var shake_noise: Noise
|
||||
|
||||
var trauma := 0.0
|
||||
var trauma_time := 0.0
|
||||
var target_distance: float
|
||||
|
||||
func _ready():
|
||||
target_distance = position.z
|
||||
Global.camera = self
|
||||
|
||||
func _unhandled_input(event):
|
||||
if event.is_action_pressed("zoom_in"):
|
||||
target_distance -= zoom_speed
|
||||
on_distance_changed()
|
||||
|
||||
if event.is_action_pressed("zoom_out"):
|
||||
target_distance += zoom_speed
|
||||
on_distance_changed()
|
||||
|
||||
func on_distance_changed():
|
||||
target_distance = clampf(target_distance, zoom_min, zoom_max)
|
||||
Global.camera_attributes.dof_blur_far_distance = target_distance + 1.0
|
||||
|
||||
func _process(delta):
|
||||
if Global.player == null:
|
||||
if abs(target_distance - position.z) < 0.01:
|
||||
return
|
||||
|
||||
if follow_enabled:
|
||||
follow_node.position = lerp(follow_node.position, Global.player.position, follow_speed * delta)
|
||||
else:
|
||||
follow_node.position = Global.player.position
|
||||
|
||||
if trauma:
|
||||
trauma = max(trauma - shake_decay * delta, 0)
|
||||
trauma_time += 4096 * delta
|
||||
_shake(trauma_time)
|
||||
else:
|
||||
look_at(pivot_node.global_position)
|
||||
trauma_time = 0
|
||||
|
||||
func _shake(time):
|
||||
var trauma_sq := trauma * trauma
|
||||
rotation.x += shake_noise.get_noise_2d(time, 0) * trauma_sq * shake_strength
|
||||
rotation.y += shake_noise.get_noise_2d(time, 1) * trauma_sq * shake_strength
|
||||
rotation.z += shake_noise.get_noise_2d(time, 2) * trauma_sq * shake_strength
|
||||
|
||||
func add_shake(amount: float):
|
||||
trauma = min(trauma + amount, 1.0)
|
||||
|
||||
position.z = lerpf(position.z, target_distance, zoom_interpolation * delta)
|
Reference in New Issue
Block a user