Add sound effects for player actions and collectables

This commit is contained in:
2025-04-26 02:15:23 +02:00
parent 8959fd4b9f
commit f01645c094
46 changed files with 897 additions and 1058 deletions

View File

@@ -12,10 +12,8 @@ func _ready() -> void:
await get_tree().process_frame
if not lever:
var levers_nodes := get_tree().get_nodes_in_group("levers")
print("Found levers: ", levers_nodes)
for lever_node in levers_nodes:
var lever_component: LeverComponent = lever_node.get_node_or_null("LeverComponent")
print("Lever component: ", lever_component)
if lever_component:
lever_component.activated.connect(on_lever_activated)
else:
@@ -27,7 +25,6 @@ func _ready() -> void:
func on_lever_activated() -> void:
print("Lever activated, moving cage.")
var tween: Tween = create_tween()
var end_position: Vector2 = root.position + move_value
tween.tween_property(root, "position", end_position, tween_duration)

View File

@@ -6,6 +6,7 @@ var has_fade_away: bool = false
@export var area2d: Area2D
@export var collectable_data: CollectableResource
@export var sfx: AudioStreamPlayer2D
signal collected(amount: int)
@@ -24,5 +25,8 @@ func _ready() -> void:
func _on_area2d_body_entered(body: Node2D) -> void:
if body.has_node("CanPickUpComponent"):
collected.emit(collectable_data.amount)
if sfx:
sfx.play()
if not has_fade_away:
await sfx.finished
root.queue_free()

View File

@@ -3,51 +3,70 @@ extends Node
@export var health: float = 1.0
@export var max_health: float = 1.0
@export var hurt_fx: AudioStreamPlayer2D
@export var heal_fx: AudioStreamPlayer2D
signal on_health_change(delta: float, total_health: float)
signal on_death
func _get_delta(value: float) -> float:
var old_value = health
return value - old_value
func set_health(new_value: float):
var delta = _get_delta(new_value)
if sign(delta) > 0:
if heal_fx:
heal_fx.play()
elif sign(delta) < 0:
if hurt_fx:
hurt_fx.play()
await hurt_fx.finished
if new_value >= max_health:
health = max_health
on_health_change.emit(delta, health)
return
health = new_value
if health <= 0:
on_death.emit()
return
on_health_change.emit(delta, health)
func decrease_health(value: float):
var delta = _get_delta(value)
health -= value
if hurt_fx:
hurt_fx.play()
await hurt_fx.finished
if health <= 0:
on_death.emit()
return
on_health_change.emit(delta, health)
func increase_health(value: float):
var delta = _get_delta(value)
health += value
if health >= max_health:
health = max_health
on_health_change.emit(delta, health)
return
health += value
if heal_fx:
heal_fx.play()
on_health_change.emit(delta, health)

View File

@@ -28,6 +28,8 @@ func _on_body_entered(body: Node2D) -> void:
if body is PlayerController:
handle_launchpad_animation()
body.velocity.y = -jump_force
if body.jump_sfx:
body.jump_sfx.play()
func handle_launchpad_animation() -> void:

View File

@@ -29,7 +29,6 @@ func _on_body_entered(body: Node2D) -> void:
func activate() -> void:
print("Lever activated.")
activated.emit()
sprite2d.frame = start_animation_index + 1
var timer := get_tree().create_timer(animation_duration)

View File

@@ -3,8 +3,8 @@ extends CharacterBody2D
@export var speed: float = 300.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var last_direction: Vector2 = Vector2.RIGHT
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var last_direction: Vector2 = Vector2.RIGHT
var previous_velocity: Vector2 = Vector2.ZERO
@onready var root = $Root
@@ -13,19 +13,21 @@ var previous_velocity: Vector2 = Vector2.ZERO
@export var jump_height: float = 100
@export var jump_time_to_peak: float = 0.5
@export var jump_time_to_descent: float = 0.4
@export var coyote_frames: int = 6
@export var coyote_mode: bool = false
@export var was_last_floor: bool = false
@export var jump_sfx: AudioStreamPlayer2D
@onready var jump_velocity: float = ((2.0 * jump_height) / jump_time_to_peak) * -1.0
@onready var jump_gravity: float = ((-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0
@onready var fall_gravity: float = ((-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0
func _ready() -> void:
coyote_timer.timeout.connect(on_coyote_timer_timeout)
coyote_timer.wait_time = coyote_frames / 60.0
func _process(_delta):
if velocity.x > 0.0:
root.rotation = deg_to_rad(-10)
@@ -33,50 +35,56 @@ func _process(_delta):
root.rotation = deg_to_rad(10)
else:
root.rotation = 0
func _physics_process(delta):
if is_on_floor():
was_last_floor = true
coyote_mode = false # Reset coyote mode when back on the floor
coyote_timer.stop() # Stop timer when grounded
else:
if was_last_floor: # Start coyote timer only once
if was_last_floor: # Start coyote timer only once
coyote_mode = true
coyote_timer.start()
was_last_floor = false
if not is_on_floor():
velocity.y += calculate_gravity() * delta
if Input.is_action_pressed("jump") and (is_on_floor() or coyote_mode):
jump()
if Input.is_action_just_pressed("down"):
position.y += 1
var direction := Input.get_axis("left", "right")
if direction != 0:
last_direction = handle_direction(direction)
if direction:
velocity.x = direction * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
previous_velocity = velocity
move_and_slide()
func jump():
velocity.y = jump_velocity
coyote_mode = false
if jump_sfx:
jump_sfx.play()
func calculate_gravity() -> float:
return jump_gravity if velocity.y < 0.0 else fall_gravity
func on_coyote_timer_timeout():
coyote_mode = false
func handle_direction(input_dir: float) -> Vector2:
if input_dir > 0:
return Vector2.RIGHT