gfolf2/src/ui/game_viewport_container.gd

72 lines
1.7 KiB
GDScript3
Raw Normal View History

2024-11-17 18:10:24 -07:00
class_name GameViewportContainer extends SubViewportContainer
## SubViewportContainer with game-specific special effects
2024-11-17 18:51:06 -07:00
const SMALL_HIT_LAG_FRAMES := 5
const BIG_HIT_LAG_FRAMES := 10
const HUGE_HIT_LAG_FRAMES := 20
var _hit_lag_frames := -1
2024-11-17 18:10:24 -07:00
2024-11-17 18:51:06 -07:00
@onready var content: Node = %Content
2024-11-17 20:10:33 -07:00
@onready var rumbler: Rumbler = %Rumbler
2024-11-17 18:10:24 -07:00
## Start playing a screen shake effect.
func screen_shake(intensity: float, duration: float = 0.2) -> void:
2024-11-17 18:51:06 -07:00
if not ProjectSettings.get_setting("game/config/accessibility/enable_screen_shake"):
return
2024-11-17 18:10:24 -07:00
var tween := get_tree().create_tween()
2024-11-17 20:10:33 -07:00
rumbler.intensity = intensity
tween.tween_property(rumbler, "intensity", 0.0, duration).set_trans(Tween.TRANS_CUBIC)
2024-11-17 18:10:24 -07:00
tween.tween_callback(_reset_position)
2024-11-17 20:10:33 -07:00
## Rumble the screen indefinitely.
func set_rumble(intensity: float) -> void:
if not ProjectSettings.get_setting("game/config/accessibility/enable_screen_shake"):
return
rumbler.intensity = intensity
## Stop rumbling the screen.
func stop_rumble() -> void:
set_rumble(0)
2024-11-17 18:51:06 -07:00
## Hit lag for a small impact.
func hit_lag_small() -> void:
hit_lag(SMALL_HIT_LAG_FRAMES)
## Hit lag for a big impact.
func hit_lag_big() -> void:
hit_lag(BIG_HIT_LAG_FRAMES)
## Hit lag for a huge impact.
func hit_lag_huge() -> void:
hit_lag(HUGE_HIT_LAG_FRAMES)
## Stop processing for some number of frames.
func hit_lag(frames: int = 1) -> void:
if not ProjectSettings.get_setting("game/config/accessibility/enable_hit_lag"):
return
_hit_lag_frames = frames
2024-11-17 18:10:24 -07:00
func _reset_position() -> void:
position = Vector2.ZERO
func _process(_delta: float) -> void:
2024-11-17 18:51:06 -07:00
if _hit_lag_frames >= 0:
if _hit_lag_frames == 0:
2024-11-19 18:06:47 -07:00
get_tree().paused = false
2024-11-17 18:51:06 -07:00
else:
2024-11-19 18:06:47 -07:00
get_tree().paused = true
2024-11-17 18:51:06 -07:00
_hit_lag_frames -= 1