2024-02-14 17:05:57 +00:00
|
|
|
class_name AudioComponent
|
|
|
|
extends Node
|
|
|
|
|
2024-02-14 19:29:52 +00:00
|
|
|
@export var skeleton: Skeleton3D
|
2024-02-14 17:05:57 +00:00
|
|
|
@export var movement: MovementComponent
|
|
|
|
@export var footsteps: Array[AudioStream]
|
2024-02-14 19:29:52 +00:00
|
|
|
@export var raise_threshold := 0.25
|
|
|
|
@export var step_threshold := 0.15
|
2024-02-14 17:05:57 +00:00
|
|
|
|
2024-02-14 19:29:52 +00:00
|
|
|
var feet_names := ["LeftFoot", "RightFoot"]
|
|
|
|
var feet := []
|
|
|
|
var feet_audio := []
|
|
|
|
var feet_raised := []
|
|
|
|
var feet_position := []
|
2024-02-14 17:05:57 +00:00
|
|
|
|
|
|
|
func _ready():
|
2024-02-14 19:29:52 +00:00
|
|
|
for foot_name in feet_names:
|
|
|
|
var bone_id := skeleton.find_bone(foot_name)
|
|
|
|
var audio := get_node(foot_name)
|
|
|
|
|
|
|
|
feet.append(bone_id)
|
|
|
|
feet_audio.append(audio)
|
|
|
|
feet_raised.append(false)
|
|
|
|
feet_position.append(Vector3.ZERO)
|
|
|
|
|
|
|
|
var attachment := BoneAttachment3D.new()
|
|
|
|
attachment.bone_name = foot_name
|
|
|
|
attachment.bone_idx = bone_id
|
|
|
|
audio.reparent(attachment)
|
|
|
|
skeleton.add_child(attachment)
|
2024-02-14 17:05:57 +00:00
|
|
|
|
|
|
|
func _process(_delta):
|
2024-02-14 19:29:52 +00:00
|
|
|
print()
|
|
|
|
for i in range(feet.size()):
|
|
|
|
feet_position[i] = skeleton.get_bone_global_pose(feet[i]).origin
|
|
|
|
print(feet_position[i].y)
|
|
|
|
|
|
|
|
if feet_position[i].y > raise_threshold:
|
|
|
|
feet_raised[i] = true
|
2024-02-14 17:05:57 +00:00
|
|
|
|
|
|
|
if !movement.body.is_on_floor():
|
|
|
|
return
|
2024-02-14 19:29:52 +00:00
|
|
|
|
|
|
|
for i in range(feet.size()):
|
|
|
|
if feet_raised[i] && feet_position[i].y < step_threshold:
|
|
|
|
play(feet_audio[i])
|
|
|
|
feet_raised[i] = false
|
2024-02-14 17:05:57 +00:00
|
|
|
|
2024-02-14 19:29:52 +00:00
|
|
|
func play(audio_player: AudioStreamPlayer3D):
|
2024-02-14 17:05:57 +00:00
|
|
|
audio_player.stream = footsteps[randi_range(0, footsteps.size()-1)]
|
|
|
|
audio_player.pitch_scale = randf_range(0.9, 1.1)
|
2024-02-14 19:29:52 +00:00
|
|
|
audio_player.play()
|