Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions scenes/game_elements/fx/shaker/components/shaker.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ signal started
## Emitted when the target stopped shaking
signal finished

## Node that will be shaked
@export var target: CanvasItem
## Node that will be shaked. If the parent node is a CanvasItem and target isn't set,
## the parent node will be automatically assigned to this variable.
@export var target: CanvasItem:
set = _set_target

## Maximum possible value in which the position of the node might be offset.
@export_range(1.0, 100.0, 1.0, "or_greater", "or_less") var shake_intensity: float = 30.0
## How much time (in seconds) the node will be shaken.
Expand Down Expand Up @@ -43,11 +46,28 @@ var current_intensity: float = 0.0
var shake_tween: Tween


func _enter_tree() -> void:
if not target and get_parent() is CanvasItem:
target = get_parent()


func _ready() -> void:
noise.noise_type = FastNoiseLite.TYPE_PERLIN
noise.frequency = 1.0


func _set_target(new_target: CanvasItem) -> void:
target = new_target
update_configuration_warnings()


func _get_configuration_warnings() -> PackedStringArray:
var warnings: PackedStringArray
if not target:
warnings.append("Target must be set.")
return warnings


## Shake the node's position by a maximum of [param intensity] and rotation by
## a maximum of [param intensity] * 0.01 during [param time].
## When the effect finishes, [member target]'s position and rotation end up
Expand All @@ -63,8 +83,10 @@ func _ready() -> void:
func shake(intensity: float = shake_intensity, time: float = duration) -> void:
noise.seed = randi()
started.emit()
if InputHelper.device_index >= 0:
Input.start_joy_vibration(InputHelper.device_index, 0.5, 0.5, time)
if not Engine.is_editor_hint():
# Don't vibrate the joypad when using the Test button in the editor:
if InputHelper.device_index >= 0:
Input.start_joy_vibration(InputHelper.device_index, 0.5, 0.5, time)

var shaking_already_in_progress: bool = shake_tween and shake_tween.is_valid()
if shaking_already_in_progress:
Expand Down
Loading