mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-12 03:53:44 +00:00
feat(feedback): add responsive camera and impacts
This commit is contained in:
@@ -7,6 +7,7 @@ extends Node3D
|
||||
# themselves are camera-free (a headless/AI ship never needs one).
|
||||
|
||||
signal camera_mode_changed(is_ball_cam: bool)
|
||||
signal impact_feedback(intensity: float)
|
||||
|
||||
@export var camera_distance := 8.0
|
||||
@export var camera_height := 3.0
|
||||
@@ -14,8 +15,24 @@ signal camera_mode_changed(is_ball_cam: bool)
|
||||
@export var orbit_smoothing := 6.0 # How fast the camera swings around the ship in ball cam
|
||||
@export var look_smoothing := 20.0 # How fast the camera re-centres its look target
|
||||
@export var min_camera_height := 1.0 # Keeps the camera from dipping through the arena floor
|
||||
@export_group("Speed Feel")
|
||||
@export var base_fov := 70.0
|
||||
@export var speed_fov_add := 11.0
|
||||
@export var turbo_fov_kick := 7.0
|
||||
@export var turbo_distance_kick := 0.9
|
||||
@export var feel_smoothing := 7.0
|
||||
@export_group("Impact Shake")
|
||||
@export var max_shake_offset := 0.32
|
||||
@export var shake_decay := 8.0
|
||||
|
||||
var target: Ship
|
||||
var target: Ship:
|
||||
set(value):
|
||||
if target == value:
|
||||
return
|
||||
if is_instance_valid(target) and target.ball_contact.is_connected(_on_target_ball_contact):
|
||||
target.ball_contact.disconnect(_on_target_ball_contact)
|
||||
target = value
|
||||
_connect_target()
|
||||
var ball_cam_enabled := true
|
||||
|
||||
@onready var camera: Camera3D = $Camera3D
|
||||
@@ -25,11 +42,34 @@ var _ball: Node3D
|
||||
# this so the camera swings smoothly around the ship instead of chasing a
|
||||
# raw position (which whips when ball and ship are close together).
|
||||
var _orbit_dir := Vector3.BACK
|
||||
var _turbo_blend := 0.0
|
||||
var _effective_distance := 8.0
|
||||
var _shake_strength := 0.0
|
||||
var _shake_noise := FastNoiseLite.new()
|
||||
var _shake_time := 0.0
|
||||
var _last_shake_offset := Vector3.ZERO
|
||||
|
||||
|
||||
func _ready():
|
||||
# Group lets the HUD discover the rig for the camera-mode instrument
|
||||
add_to_group("ship_camera")
|
||||
camera.fov = base_fov
|
||||
_effective_distance = camera_distance
|
||||
_shake_noise.noise_type = FastNoiseLite.TYPE_SIMPLEX_SMOOTH
|
||||
_shake_noise.frequency = 2.5
|
||||
_connect_target()
|
||||
|
||||
|
||||
func _connect_target() -> void:
|
||||
if not is_inside_tree() or not is_instance_valid(target):
|
||||
return
|
||||
if not target.ball_contact.is_connected(_on_target_ball_contact):
|
||||
target.ball_contact.connect(_on_target_ball_contact)
|
||||
|
||||
|
||||
func _exit_tree() -> void:
|
||||
if is_instance_valid(target) and target.ball_contact.is_connected(_on_target_ball_contact):
|
||||
target.ball_contact.disconnect(_on_target_ball_contact)
|
||||
|
||||
|
||||
func _input(event):
|
||||
@@ -39,8 +79,14 @@ func _input(event):
|
||||
|
||||
|
||||
func _physics_process(delta):
|
||||
# Camera position smoothing operates on the unshaken chase position. Remove
|
||||
# last frame's presentation-only offset first so shake never accumulates or
|
||||
# exceeds its configured amplitude during a low-time-scale hit-stop.
|
||||
camera.global_position -= _last_shake_offset
|
||||
_last_shake_offset = Vector3.ZERO
|
||||
if not is_instance_valid(target):
|
||||
return
|
||||
_update_speed_feel(delta)
|
||||
var ball := _get_ball()
|
||||
if ball_cam_enabled and ball:
|
||||
_update_ball_cam(delta, ball)
|
||||
@@ -77,11 +123,12 @@ func _update_ball_cam(delta, ball: Node3D):
|
||||
var swing := _orbit_dir.signed_angle_to(flat.normalized(), Vector3.UP)
|
||||
_orbit_dir = _orbit_dir.rotated(Vector3.UP, swing * t).normalized()
|
||||
|
||||
var camera_target_pos := ship_pos + _orbit_dir * camera_distance + Vector3.UP * camera_height
|
||||
var camera_target_pos := ship_pos + _orbit_dir * _effective_distance + Vector3.UP * camera_height
|
||||
camera_target_pos.y = maxf(camera_target_pos.y, min_camera_height)
|
||||
|
||||
var pos_t := 1.0 - exp(-camera_smoothing * delta)
|
||||
camera.global_position = camera.global_position.lerp(camera_target_pos, pos_t)
|
||||
_apply_shake(delta)
|
||||
|
||||
# Look slightly above the ball's centre; look smoothing is faster than
|
||||
# position smoothing so the ball never drifts out of frame while the
|
||||
@@ -95,11 +142,12 @@ func _update_ship_cam(delta):
|
||||
var ship_forward: Vector3 = -target.global_transform.basis.z
|
||||
|
||||
# Position camera behind and above the ship
|
||||
var camera_target_pos := ship_pos - ship_forward * camera_distance + Vector3.UP * camera_height
|
||||
var camera_target_pos := ship_pos - ship_forward * _effective_distance + Vector3.UP * camera_height
|
||||
camera_target_pos.y = maxf(camera_target_pos.y, min_camera_height)
|
||||
|
||||
var pos_t := 1.0 - exp(-camera_smoothing * delta)
|
||||
camera.global_position = camera.global_position.lerp(camera_target_pos, pos_t)
|
||||
_apply_shake(delta)
|
||||
|
||||
# Look ahead of the ship
|
||||
_smooth_look_at(ship_pos + ship_forward * 10.0, delta)
|
||||
@@ -115,3 +163,39 @@ func _smooth_look_at(point: Vector3, delta: float) -> void:
|
||||
var look_basis := Basis.looking_at(dir, Vector3.UP)
|
||||
var look_t := 1.0 - exp(-look_smoothing * delta)
|
||||
camera.global_basis = camera.global_basis.slerp(look_basis, look_t).orthonormalized()
|
||||
|
||||
|
||||
func _update_speed_feel(delta: float) -> void:
|
||||
var feel_t := 1.0 - exp(-feel_smoothing * delta)
|
||||
var turbo_target := 1.0 if target.is_turbo_active() else 0.0
|
||||
_turbo_blend = lerpf(_turbo_blend, turbo_target, feel_t)
|
||||
var target_fov := base_fov + target.get_speed_ratio() * speed_fov_add + _turbo_blend * turbo_fov_kick
|
||||
camera.fov = lerpf(camera.fov, target_fov, feel_t)
|
||||
_effective_distance = lerpf(
|
||||
_effective_distance, camera_distance + _turbo_blend * turbo_distance_kick, feel_t
|
||||
)
|
||||
|
||||
|
||||
func _on_target_ball_contact(intensity: float, _world_position: Vector3) -> void:
|
||||
_shake_strength = maxf(_shake_strength, clampf(intensity, 0.0, 1.0))
|
||||
for device in Input.get_connected_joypads():
|
||||
Input.start_joy_vibration(
|
||||
device, lerpf(0.18, 0.65, intensity), lerpf(0.32, 1.0, intensity),
|
||||
lerpf(0.08, 0.2, intensity)
|
||||
)
|
||||
impact_feedback.emit(intensity)
|
||||
|
||||
|
||||
func _apply_shake(delta: float) -> void:
|
||||
if _shake_strength <= 0.001:
|
||||
_shake_strength = 0.0
|
||||
return
|
||||
_shake_time += delta * 60.0
|
||||
var amplitude := max_shake_offset * _shake_strength * _shake_strength
|
||||
var noise := Vector2(
|
||||
_shake_noise.get_noise_1d(_shake_time),
|
||||
_shake_noise.get_noise_1d(_shake_time + 100.0)
|
||||
).limit_length(1.0) * amplitude
|
||||
_last_shake_offset = camera.global_basis.x * noise.x + camera.global_basis.y * noise.y
|
||||
camera.global_position += _last_shake_offset
|
||||
_shake_strength = move_toward(_shake_strength, 0.0, shake_decay * delta)
|
||||
|
||||
Reference in New Issue
Block a user