camera shake 优化
This commit is contained in:
parent
8b348970c9
commit
028464e743
@ -14,20 +14,47 @@ var _tweeked_position := Vector2.ZERO
|
|||||||
|
|
||||||
var zoom_tween: Tween
|
var zoom_tween: Tween
|
||||||
var shake_ignore_boundary := false
|
var shake_ignore_boundary := false
|
||||||
|
# default
|
||||||
|
var default_camera_rect := Rect2i(0, -158, 564, 316)
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
reset_speed()
|
reset_speed()
|
||||||
if not focusing_node:
|
if not focusing_node:
|
||||||
push_error("Focusing node not found")
|
push_error("Focusing node not found")
|
||||||
|
|
||||||
|
|
||||||
|
# 默认重置 limits
|
||||||
|
func apply_limits(rect := default_camera_rect, replace_default := true) -> void:
|
||||||
|
if replace_default:
|
||||||
|
default_camera_rect = rect
|
||||||
|
var start = rect.position
|
||||||
|
var end = rect.end
|
||||||
|
limit_left = start.x
|
||||||
|
limit_right = end.x
|
||||||
|
limit_top = start.y
|
||||||
|
limit_bottom = end.y
|
||||||
|
|
||||||
|
|
||||||
func reset_speed() -> void:
|
func reset_speed() -> void:
|
||||||
speed = 2.0
|
speed = 2.0
|
||||||
|
|
||||||
|
|
||||||
func shake_camera(strength := 6.0, recovery_speed := 2.0, ignore_boundary := true):
|
func shake_camera(strength := 6.0, recovery_speed := 2.0, ignore_boundary := true):
|
||||||
shake_strength = strength
|
shake_strength = strength
|
||||||
shake_recovery_speed = recovery_speed
|
shake_recovery_speed = recovery_speed
|
||||||
shake_ignore_boundary = ignore_boundary
|
shake_ignore_boundary = ignore_boundary
|
||||||
|
if shake_ignore_boundary:
|
||||||
|
_update_limit_by_strength()
|
||||||
|
|
||||||
|
|
||||||
|
func _update_limit_by_strength() -> void:
|
||||||
|
var int_s := ceili(shake_strength)
|
||||||
|
var start = default_camera_rect.position
|
||||||
|
var end = default_camera_rect.end
|
||||||
|
limit_left = start.x - int_s
|
||||||
|
limit_right = end.x + int_s
|
||||||
|
limit_top = start.y - int_s
|
||||||
|
limit_bottom = end.y + int_s
|
||||||
|
|
||||||
|
|
||||||
func reset_position_immediately():
|
func reset_position_immediately():
|
||||||
@ -69,7 +96,8 @@ func _physics_process(delta: float) -> void:
|
|||||||
global_position = progressing_position
|
global_position = progressing_position
|
||||||
|
|
||||||
# handle shake
|
# handle shake
|
||||||
if shake_strength > 0.0:
|
if shake_strength > 0.01: #epsilon
|
||||||
|
_update_limit_by_strength()
|
||||||
# 让 shake_strength 逐帧衰减
|
# 让 shake_strength 逐帧衰减
|
||||||
shake_recovery_speed = max(0.1, shake_recovery_speed)
|
shake_recovery_speed = max(0.1, shake_recovery_speed)
|
||||||
shake_strength = lerpf(shake_strength, 0.0, shake_recovery_speed * delta)
|
shake_strength = lerpf(shake_strength, 0.0, shake_recovery_speed * delta)
|
||||||
@ -87,6 +115,12 @@ func _physics_process(delta: float) -> void:
|
|||||||
global_position += shaked_offset
|
global_position += shaked_offset
|
||||||
if not shake_ignore_boundary:
|
if not shake_ignore_boundary:
|
||||||
global_position = _clamp_boundary(global_position)
|
global_position = _clamp_boundary(global_position)
|
||||||
|
elif shake_ignore_boundary:
|
||||||
|
# reset limits
|
||||||
|
shake_ignore_boundary = false
|
||||||
|
shake_strength = 0.0
|
||||||
|
_update_limit_by_strength()
|
||||||
|
|
||||||
|
|
||||||
# var taget_zoom = lerpf(zoom.x, zoom_ratio, speed * delta)
|
# var taget_zoom = lerpf(zoom.x, zoom_ratio, speed * delta)
|
||||||
# zoom = Vector2(taget_zoom, taget_zoom)
|
# zoom = Vector2(taget_zoom, taget_zoom)
|
||||||
|
@ -194,13 +194,10 @@ func _calculate_player_rect() -> Rect2:
|
|||||||
return player_rect
|
return player_rect
|
||||||
|
|
||||||
|
|
||||||
func _apply_camera_limits(camera_rect: Rect2) -> void:
|
func _apply_camera_limits(camera_rect: Rect2i) -> void:
|
||||||
var camera_marker = get_camera()
|
var camera_marker = get_camera()
|
||||||
if camera_marker:
|
if camera_marker:
|
||||||
camera_marker.limit_left = camera_rect.position.x
|
camera_marker.apply_limits(camera_rect)
|
||||||
camera_marker.limit_right = camera_rect.position.x + camera_rect.size.x
|
|
||||||
camera_marker.limit_top = camera_rect.position.y
|
|
||||||
camera_marker.limit_bottom = camera_rect.position.y + camera_rect.size.y
|
|
||||||
|
|
||||||
|
|
||||||
func _apply_player_boundary(player_rect: Rect2) -> void:
|
func _apply_player_boundary(player_rect: Rect2) -> void:
|
||||||
|
@ -31,7 +31,7 @@ func _on_ground_ready() -> void:
|
|||||||
camera.limit_bottom = 158
|
camera.limit_bottom = 158
|
||||||
await SceneManager.ground_start
|
await SceneManager.ground_start
|
||||||
# 相机抖动
|
# 相机抖动
|
||||||
camera.shake_camera(6.0, 1.5)
|
camera.shake_camera(6.0, 2.5)
|
||||||
# 不显示玩家,锁定玩家移动
|
# 不显示玩家,锁定玩家移动
|
||||||
SceneManager.lock_player()
|
SceneManager.lock_player()
|
||||||
main_character = $"../DeployLayer/车夫与吕萍"
|
main_character = $"../DeployLayer/车夫与吕萍"
|
||||||
|
Loading…
Reference in New Issue
Block a user