Working SoundPlayer.

remotes/1705382094874458415/tmp_refs/heads/godot4-port
Tobiasz Karoń 2020-09-19 11:22:41 +02:00
parent a09ed4eded
commit 73538ea4b6
5 changed files with 88 additions and 36 deletions

BIN
Assets/SFX/Test-01.wav Normal file

Binary file not shown.

60
Audio/SoundPlayer.gd Normal file
View File

@ -0,0 +1,60 @@
extends Spatial
# This Scene is meant for playing back sound groups. For simple, singular sound effects use stock Godot nodes, this is meant to play a random sound among a group with variations.
const SFX_dir = "res://Assets/SFX/" # all sound clips must reside somewhere in this directory
onready var player = $AudioStreamPlayer3D # playback backend
export(String, FILE, "*-01.wav") var SoundClip = "Assets/SFX/"
export(float) var MinimumDistance = 0.35 # gives optimal playback repetition for sound clip groups of different sizes.
var min_distance = 0 # this determines how ofte na sound is allowed to play (any Nth time) this is calculated automatically based on maximum_repetition
var clips = [] # holds loaded sound stream resources
var recently_played = [] # holds indexes of recently played
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
var files = []
var dir = Directory.new()
dir.open(SFX_dir)
dir.list_dir_begin()
#print("SoundClip: ", SoundClip)
var group = SoundClip.left(SoundClip.find_last('-')).right(SoundClip.find_last('/') + 1)
print("group: ", group)
while true:
var file = dir.get_next()
#print(file)
if file == "":
break
elif not file.begins_with(".") and file.begins_with(group) and file.ends_with(".wav"):
files.append(file)
dir.list_dir_end()
print(files)
for f in files:
clips.append(load(SFX_dir + f))
min_distance = floor(len(clips) * MinimumDistance)
#print ("Clips: ", len(clips))
#print ("min_distance: ", min_distance)
func play():
player.stream = clips[randi() % len(clips)]
player.play()
# TODO implement final randomization algorithm
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

9
Audio/SoundPlayer.tscn Normal file
View File

@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Audio/SoundPlayer.gd" type="Script" id=1]
[node name="SoundPlayer" type="Spatial"]
script = ExtResource( 1 )
SoundClip = "res://Assets/SFX"
[node name="AudioStreamPlayer3D" type="AudioStreamPlayer3D" parent="."]

View File

@ -36,16 +36,16 @@ var walkDirInt = Vector2.ZERO
var bulletHitEffect = preload("res://Assets/Effects/BulletHit.tscn")
func sfx_play_footsteps():
if not sfx_footsteps_play:
sfx_footsteps_play = true
while sfx_footsteps_next == sfx_footsteps_last:
sfx_footsteps_next = randi() % len(sfx_foosteps)
sfx_footsteps_last = sfx_footsteps_next
print("Play footstep: ", String(sfx_footsteps_next) )
sfx_foosteps[sfx_footsteps_next].play()
yield(get_tree().create_timer(sfx_footsteps_delay),"timeout")
sfx_footsteps_play = false
#func sfx_play_footsteps():
# if not sfx_footsteps_play:
# sfx_footsteps_play = true
# while sfx_footsteps_next == sfx_footsteps_last:
# sfx_footsteps_next = randi() % len(sfx_foosteps)
# sfx_footsteps_last = sfx_footsteps_next
# print("Play footstep: ", String(sfx_footsteps_next) )
# sfx_foosteps[sfx_footsteps_next].play()
# yield(get_tree().create_timer(sfx_footsteps_delay),"timeout")
# sfx_footsteps_play = false
func gravity():
if not is_on_floor():
@ -79,12 +79,13 @@ remote func walk(direction: Vector2):
velocity.x = lerp(velocity.x, walkVelocity.rotated(- self.rotation.y).y, interpolation)
velocity.z = lerp(velocity.z, - walkVelocity.rotated(- self.rotation.y).x, interpolation)
if walkVelocity.length() > 0 and is_on_floor():
sfx_play_footsteps()
# if walkVelocity.length() > 0 and is_on_floor():
# sfx_play_footsteps()
#
remote func jump():
if is_on_floor():
velocity.y = JUMP_VELOCITY
$Sounds/Jump.play()
remote func mouselook_abs(x, y):
camera.rotation.x = x

View File

@ -1,12 +1,8 @@
[gd_scene load_steps=11 format=2]
[gd_scene load_steps=8 format=2]
[ext_resource path="res://Player.gd" type="Script" id=1]
[ext_resource path="res://Assets/Weapons/Handgun/Handgun.tscn" type="PackedScene" id=2]
[ext_resource path="res://Assets/SFX/Player-Step-Concrete-01.wav" type="AudioStream" id=3]
[ext_resource path="res://Assets/SFX/Player-Step-Concrete-02.wav" type="AudioStream" id=4]
[ext_resource path="res://Assets/SFX/Player-Step-Concrete-04.wav" type="AudioStream" id=5]
[ext_resource path="res://Assets/SFX/Player-Step-Concrete-03.wav" type="AudioStream" id=6]
[ext_resource path="res://SoundPlayer.gd" type="Script" id=7]
[ext_resource path="res://Audio/SoundPlayer.tscn" type="PackedScene" id=7]
[ext_resource path="res://GUI/Hitmarker.gd" type="Script" id=8]
[sub_resource type="CapsuleShape" id=1]
@ -56,22 +52,8 @@ __meta__ = {
[node name="Sounds" type="Spatial" parent="."]
[node name="Footstep-Concrete-01" type="AudioStreamPlayer3D" parent="Sounds"]
stream = ExtResource( 3 )
[node name="Footstep-Concrete-02" type="AudioStreamPlayer3D" parent="Sounds"]
stream = ExtResource( 4 )
[node name="Footstep-Concrete-03" type="AudioStreamPlayer3D" parent="Sounds"]
stream = ExtResource( 6 )
[node name="Footstep-Concrete-04" type="AudioStreamPlayer3D" parent="Sounds"]
stream = ExtResource( 5 )
[node name="SoundPlayer" type="Spatial" parent="Sounds"]
script = ExtResource( 8 )
[node name="AudioStreamPlayer3D" type="AudioStreamPlayer3D" parent="Sounds/SoundPlayer"]
[node name="Jump" parent="Sounds" instance=ExtResource( 7 )]
SoundClip = "res://Assets/SFX/Player-Jump-01.wav"
[node name="CrosshairContainer" type="CenterContainer" parent="."]
anchor_left = 0.5
@ -141,7 +123,7 @@ margin_left = 512.0
margin_top = 300.0
margin_right = 512.0
margin_bottom = 300.0
script = ExtResource( 7 )
script = ExtResource( 8 )
[node name="Line1" type="ColorRect" parent="CrosshairContainer/Hitmarker"]
margin_left = 8.0