From 37e8852c51a8dcad47aa276e9d7825d82d94148d Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 14 Feb 2024 18:05:57 +0100 Subject: [PATCH] Added audio component --- client/Main.tscn | 6 +++--- client/player/Player.tscn | 16 ++++++++++++++- client/player/audio/AudioComponent.gd | 27 +++++++++++++++++++++++++ client/player/audio/AudioComponent.tscn | 10 +++++++++ client/project.godot | 4 ++++ client/world/AudioBusLayout.tres | 21 +++++++++++++++++++ client/world/house/House.tscn | 18 +++++++++++++++++ 7 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 client/player/audio/AudioComponent.gd create mode 100644 client/player/audio/AudioComponent.tscn create mode 100644 client/world/AudioBusLayout.tres create mode 100644 client/world/house/House.tscn diff --git a/client/Main.tscn b/client/Main.tscn index 7ef82d9..d572336 100644 --- a/client/Main.tscn +++ b/client/Main.tscn @@ -20,8 +20,8 @@ [ext_resource type="PackedScene" uid="uid://dagn5bf7ou3sd" path="res://ui/UI.tscn" id="17_43qhq"] [ext_resource type="Material" uid="uid://bdsblfaxbipaa" path="res://world/grass/GrassMaterial.tres" id="18_tja64"] [ext_resource type="MultiMesh" uid="uid://dog5aq5n2q025" path="res://assets/grass/grass.multimesh" id="19_ae26a"] +[ext_resource type="PackedScene" uid="uid://djilgnhedvmtm" path="res://world/house/House.tscn" id="20_xjraj"] [ext_resource type="PackedScene" uid="uid://cuuwwdce1u8n8" path="res://world/trees/Sapling.tscn" id="22_5yt7w"] -[ext_resource type="PackedScene" uid="uid://b0w38xjifulq6" path="res://assets/house/House.blend" id="23_5j7b3"] [sub_resource type="FastNoiseLite" id="FastNoiseLite_ql20k"] seed = 1337 @@ -146,8 +146,8 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0) [node name="CollisionShape3D" type="CollisionShape3D" parent="World/Floor/StaticBody3D"] shape = SubResource("BoxShape3D_yg2lt") -[node name="House" parent="World" instance=ExtResource("23_5j7b3")] -transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, -6.04985, 0, -6.15938) +[node name="House" parent="World" instance=ExtResource("20_xjraj")] +transform = Transform3D(-1, 0, 8.74228e-08, 0, 1, 0, -8.74228e-08, 0, -1, -6, 0, -6) [node name="Players" type="Node3D" parent="."] unique_name_in_owner = true diff --git a/client/player/Player.tscn b/client/player/Player.tscn index df50b24..5f8bd26 100644 --- a/client/player/Player.tscn +++ b/client/player/Player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=18 format=3 uid="uid://2lcnu3dy54lx"] +[gd_scene load_steps=28 format=3 uid="uid://2lcnu3dy54lx"] [ext_resource type="Script" path="res://player/Player.gd" id="1_8gebs"] [ext_resource type="PackedScene" uid="uid://c8j7t4yg7anb0" path="res://assets/female/Female.blend" id="2_8nah6"] @@ -15,7 +15,17 @@ [ext_resource type="PackedScene" uid="uid://d0onbq0ad1ap4" path="res://player/rotation/RotationComponent.tscn" id="9_agxqu"] [ext_resource type="PackedScene" uid="uid://bivxnxwi863o0" path="res://player/animation/AnimationComponent.tscn" id="10_bcaeg"] [ext_resource type="AnimationLibrary" uid="uid://d4n0puibh4hyt" path="res://assets/animations/human.blend" id="11_d0e6r"] +[ext_resource type="AudioStream" uid="uid://dlqcgg8v3lvhm" path="res://assets/footsteps/Footsteps-Human-Dirt-00.wav" id="12_h5nrh"] +[ext_resource type="AudioStream" uid="uid://bcn7rvvayhnib" path="res://assets/footsteps/Footsteps-Human-Dirt-02.wav" id="14_6dv65"] [ext_resource type="PackedScene" uid="uid://b8xf4ltqyorjv" path="res://player/skills/SkillsComponent.tscn" id="14_6idcf"] +[ext_resource type="AudioStream" uid="uid://bsc8b4ltoiflu" path="res://assets/footsteps/Footsteps-Human-Dirt-03.wav" id="15_1rjwu"] +[ext_resource type="AudioStream" uid="uid://b5xq4cfmo02vw" path="res://assets/footsteps/Footsteps-Human-Dirt-04.wav" id="16_d8yn0"] +[ext_resource type="AudioStream" uid="uid://cpldevudwvnkt" path="res://assets/footsteps/Footsteps-Human-Dirt-05.wav" id="17_74v1j"] +[ext_resource type="PackedScene" uid="uid://clnfp813k7lep" path="res://player/audio/AudioComponent.tscn" id="17_ovrf7"] +[ext_resource type="AudioStream" uid="uid://biyotdu77a788" path="res://assets/footsteps/Footsteps-Human-Dirt-06.wav" id="18_qkget"] +[ext_resource type="AudioStream" uid="uid://cdywep3dxm0y3" path="res://assets/footsteps/Footsteps-Human-Dirt-07.wav" id="19_vvmo0"] +[ext_resource type="AudioStream" uid="uid://bp8pka7qhcetq" path="res://assets/footsteps/Footsteps-Human-Dirt-08.wav" id="20_srjtb"] +[ext_resource type="AudioStream" uid="uid://bgdodasvt7ier" path="res://assets/footsteps/Footsteps-Human-Dirt-01.wav" id="21_a8ikg"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_2f50n"] radius = 0.3 @@ -86,6 +96,10 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) strength = 15.0 radius = 2.0 +[node name="Audio" parent="." node_paths=PackedStringArray("movement") instance=ExtResource("17_ovrf7")] +movement = NodePath("../Movement") +footsteps = Array[AudioStream]([ExtResource("12_h5nrh"), ExtResource("21_a8ikg"), ExtResource("14_6dv65"), ExtResource("15_1rjwu"), ExtResource("16_d8yn0"), ExtResource("17_74v1j"), ExtResource("18_qkget"), ExtResource("19_vvmo0"), ExtResource("20_srjtb"), ExtResource("21_a8ikg")]) + [node name="Health" parent="." instance=ExtResource("2_np5ag")] max_value = 100.0 diff --git a/client/player/audio/AudioComponent.gd b/client/player/audio/AudioComponent.gd new file mode 100644 index 0000000..afa6f9c --- /dev/null +++ b/client/player/audio/AudioComponent.gd @@ -0,0 +1,27 @@ +class_name AudioComponent +extends Node + +@export var movement: MovementComponent +@export var footsteps: Array[AudioStream] +@export var interval: float = 0.3 + +var audio_player: AudioStreamPlayer3D +var last_footstep: float + +func _ready(): + audio_player = $Footsteps + +func _process(_delta): + if Time.get_unix_time_from_system() - last_footstep < interval: + return + + if movement.direction == Vector3.ZERO: + return + + if !movement.body.is_on_floor(): + return + + audio_player.stream = footsteps[randi_range(0, footsteps.size()-1)] + audio_player.pitch_scale = randf_range(0.9, 1.1) + audio_player.play() + last_footstep = Time.get_unix_time_from_system() diff --git a/client/player/audio/AudioComponent.tscn b/client/player/audio/AudioComponent.tscn new file mode 100644 index 0000000..9f2d2d2 --- /dev/null +++ b/client/player/audio/AudioComponent.tscn @@ -0,0 +1,10 @@ +[gd_scene load_steps=2 format=3 uid="uid://clnfp813k7lep"] + +[ext_resource type="Script" path="res://player/audio/AudioComponent.gd" id="1_0j3jl"] + +[node name="Audio" type="Node3D"] +script = ExtResource("1_0j3jl") + +[node name="Footsteps" type="AudioStreamPlayer3D" parent="."] +max_distance = 20.0 +bus = &"Footsteps" diff --git a/client/project.godot b/client/project.godot index 8e420a5..2701d63 100644 --- a/client/project.godot +++ b/client/project.godot @@ -15,6 +15,10 @@ run/main_scene="res://Start.tscn" config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://ui/icon.svg" +[audio] + +buses/default_bus_layout="res://world/AudioBusLayout.tres" + [autoload] Global="*res://Global.tscn" diff --git a/client/world/AudioBusLayout.tres b/client/world/AudioBusLayout.tres new file mode 100644 index 0000000..cc2b456 --- /dev/null +++ b/client/world/AudioBusLayout.tres @@ -0,0 +1,21 @@ +[gd_resource type="AudioBusLayout" format=3 uid="uid://c4mndkm5mss5f"] + +[resource] +bus/1/name = &"Music" +bus/1/solo = false +bus/1/mute = false +bus/1/bypass_fx = false +bus/1/volume_db = 0.0 +bus/1/send = &"Master" +bus/2/name = &"Effects" +bus/2/solo = false +bus/2/mute = false +bus/2/bypass_fx = false +bus/2/volume_db = 0.0 +bus/2/send = &"Master" +bus/3/name = &"Footsteps" +bus/3/solo = false +bus/3/mute = false +bus/3/bypass_fx = false +bus/3/volume_db = -4.50411 +bus/3/send = &"Effects" diff --git a/client/world/house/House.tscn b/client/world/house/House.tscn new file mode 100644 index 0000000..db723ee --- /dev/null +++ b/client/world/house/House.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=3 format=3 uid="uid://djilgnhedvmtm"] + +[ext_resource type="PackedScene" uid="uid://b0w38xjifulq6" path="res://assets/house/House.blend" id="1_0ihm4"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_egbsr"] +size = Vector3(5, 3.5, 5) + +[node name="House" instance=ExtResource("1_0ihm4")] + +[node name="Area3D" type="Area3D" parent="." index="1"] +monitorable = false +reverb_bus_enabled = true +reverb_bus_name = &"Effects" +reverb_bus_amount = 0.5 + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Area3D" index="0"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.5, 1.72656, 2.5) +shape = SubResource("BoxShape3D_egbsr")