52 lines
1.3 KiB
GDScript
52 lines
1.3 KiB
GDScript
class_name AudioComponent
|
|
extends Node
|
|
|
|
@export var skeleton: Skeleton3D
|
|
@export var movement: MovementComponent
|
|
@export var footsteps: Array[AudioStream]
|
|
@export var raise_threshold := 0.25
|
|
@export var step_threshold := 0.15
|
|
|
|
var feet_names := ["LeftFoot", "RightFoot"]
|
|
var feet := []
|
|
var feet_audio := []
|
|
var feet_raised := []
|
|
var feet_position := []
|
|
|
|
func _ready():
|
|
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)
|
|
|
|
func _process(_delta):
|
|
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
|
|
|
|
if !movement.body.is_on_floor():
|
|
return
|
|
|
|
for i in range(feet.size()):
|
|
if feet_raised[i] && feet_position[i].y < step_threshold:
|
|
play(feet_audio[i])
|
|
feet_raised[i] = false
|
|
|
|
func play(audio_player: AudioStreamPlayer3D):
|
|
audio_player.stream = footsteps[randi_range(0, footsteps.size()-1)]
|
|
audio_player.pitch_scale = randf_range(0.9, 1.1)
|
|
audio_player.play() |