2024-02-14 17:05:57 +00:00
|
|
|
class_name AudioComponent
|
2024-02-16 13:46:26 +00:00
|
|
|
extends Node3D
|
2024-02-14 17:05:57 +00:00
|
|
|
|
2024-02-14 19:29:52 +00:00
|
|
|
@export var skeleton: Skeleton3D
|
2024-02-14 17:05:57 +00:00
|
|
|
@export var movement: MovementComponent
|
2024-02-16 13:46:26 +00:00
|
|
|
@export var footsteps: AudioStreamRandomizer
|
2024-02-14 23:20:26 +00:00
|
|
|
@export var step_threshold := 0.1
|
2024-02-14 17:05:57 +00:00
|
|
|
|
2024-02-14 23:20:26 +00:00
|
|
|
const feet_names := ["LeftFoot", "RightFoot"]
|
|
|
|
|
|
|
|
var feet: Array[Foot] = []
|
2024-02-14 17:05:57 +00:00
|
|
|
|
|
|
|
func _ready():
|
2024-02-14 19:29:52 +00:00
|
|
|
for foot_name in feet_names:
|
2024-02-14 23:20:26 +00:00
|
|
|
var foot = Foot.new()
|
|
|
|
foot.bone_name = foot_name
|
|
|
|
foot.bone_id = skeleton.find_bone(foot_name)
|
|
|
|
foot.audio = get_node(foot_name)
|
|
|
|
foot.position = skeleton.to_global(skeleton.get_bone_global_pose(foot.bone_id).origin)
|
|
|
|
foot.attachment = BoneAttachment3D.new()
|
|
|
|
foot.attachment.bone_name = foot_name
|
|
|
|
foot.attachment.bone_idx = foot.bone_id
|
|
|
|
foot.audio.reparent(foot.attachment)
|
|
|
|
skeleton.add_child(foot.attachment)
|
|
|
|
feet.append(foot)
|
|
|
|
|
|
|
|
func _process(delta: float):
|
|
|
|
for foot in feet:
|
|
|
|
var old_position := foot.position
|
|
|
|
foot.position = skeleton.to_global(skeleton.get_bone_global_pose(foot.bone_id).origin)
|
|
|
|
foot.velocity = (foot.position - old_position) / delta
|
|
|
|
|
|
|
|
if foot.position.y > step_threshold:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if foot.velocity.y > 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if foot.velocity.length_squared() < 1.0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if foot.audio.playing && foot.audio.get_playback_position() < 0.3:
|
|
|
|
continue
|
|
|
|
|
|
|
|
play(foot.audio)
|
2024-02-14 17:05:57 +00:00
|
|
|
|
2024-02-14 19:29:52 +00:00
|
|
|
func play(audio_player: AudioStreamPlayer3D):
|
2024-02-16 13:46:26 +00:00
|
|
|
audio_player.stream = footsteps
|
2024-02-14 23:20:26 +00:00
|
|
|
audio_player.play()
|