Player Sound refactored

soundplayback moved to states
This commit is contained in:
Streamfire
2020-04-19 12:40:19 +02:00
parent 48a6fe1bd1
commit 9b72400839
4 changed files with 47 additions and 59 deletions

View File

@@ -1,6 +1,6 @@
extends Node
const EFFECT_LAYERS:int = 5
const EFFECT_LAYERS:int = 20
var _effect: Array = []
var _music: AudioStreamPlayer
@@ -34,16 +34,19 @@ func pub_play_music(path:String,should_loop:bool=true)-> void:
_music.play()
_loop=should_loop
func pub_play_effect(path:String,layer:int=0)-> void:
func pub_play_effect(path:String,channel:int=0)-> void:
var stream = load(path)
#AudioServer.set_bus_mute(1, true)
_effect[layer].stop()
_effect[layer].stream = stream
_effect[layer].play()
_effect[channel].stop()
_effect[channel].stream = stream
_effect[channel].play()
func pub_stop_music()-> void:
_music.stop()
func pub_stop_effect(channel:int)-> void:
_effect[channel].stop()
func pub_stop_effects()-> void:
for i in range(0,EFFECT_LAYERS):
_effect[i].stop()

View File

@@ -90,7 +90,6 @@ func movement_move(delta):
animation_state.change_state("run")
velocity = velocity.move_toward(player_stats.speed * input_vector, ACCELERATION * delta)
_play_random_sound(walk_sounds)
if Input.is_action_just_pressed("roll"):
movementState = moveState.ROLL
elif Input.is_action_just_pressed("attack"):
@@ -138,28 +137,3 @@ func _on_Hitbox_area_entered(area):
currency += area.currency_value
player_stats.health = player_stats.health+area.health_value
player_stats.speed -= area.slowdown_value
func _walk_sound_finished():
is_playing_sound = false
func _walk_sound_wait_time():
var x = abs(velocity.length() / 100) - 1
return log(((x+1)/4)+2.5) / 3
# TODO: Rework anyone
func _play_random_sound(path = walk_sounds):
if walk_sound_timer.is_stopped() and not is_playing_sound:
var sound = path.get_children()[_rng.randi_range(0, path.get_child_count() - 1)]
sound.play()
is_playing_sound = true
walk_sound_timer.start(_walk_sound_wait_time())
print(_walk_sound_wait_time())
# Overrides ready method for this entire script, checks for the finished method of each possible sound
func _ready():
for child in walk_sounds.get_children():
child.connect("finished", self, "_walk_sound_finished")

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=66 format=2]
[gd_scene load_steps=61 format=2]
[ext_resource path="res://Player/Player.gd" type="Script" id=1]
[ext_resource path="res://Player/Player.png" type="Texture" id=2]
@@ -6,11 +6,6 @@
[ext_resource path="res://Overlap/HurtHit_Box/Hitbox.tscn" type="PackedScene" id=4]
[ext_resource path="res://Overlap/Stats/Stats.tscn" type="PackedScene" id=5]
[ext_resource path="res://Fonts/Harmonic/Harmonic.ttf" type="DynamicFontData" id=6]
[ext_resource path="res://Player/Sounds/hero_walk_3.ogg" type="AudioStream" id=7]
[ext_resource path="res://Player/Sounds/hero_walk_5.ogg" type="AudioStream" id=8]
[ext_resource path="res://Player/Sounds/hero_walk_4.ogg" type="AudioStream" id=9]
[ext_resource path="res://Player/Sounds/hero_walk_2.ogg" type="AudioStream" id=10]
[ext_resource path="res://Player/Sounds/hero_walk_1.ogg" type="AudioStream" id=11]
[ext_resource path="res://Player/States/Idle.gd" type="Script" id=12]
[ext_resource path="res://Player/States/Run.gd" type="Script" id=13]
[ext_resource path="res://Player/States/Attack.gd" type="Script" id=14]
@@ -725,6 +720,8 @@ START_STATE = NodePath("Idle")
[node name="Run" type="Node" parent="AnimationStates"]
script = ExtResource( 13 )
SoundLibary = PoolStringArray( "res://Player/Sounds/hero_walk_1.ogg", "res://Player/Sounds/hero_walk_2.ogg", "res://Player/Sounds/hero_walk_3.ogg", "res://Player/Sounds/hero_walk_4.ogg", "res://Player/Sounds/hero_walk_5.ogg" )
Delay = 0.25
[node name="Idle" type="Node" parent="AnimationStates"]
script = ExtResource( 12 )
@@ -734,28 +731,6 @@ script = ExtResource( 14 )
[node name="Roll" type="Node" parent="AnimationStates"]
script = ExtResource( 16 )
[node name="Sounds" type="Node" parent="."]
[node name="Walk" type="Node" parent="Sounds"]
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="Sounds/Walk"]
stream = ExtResource( 11 )
[node name="AudioStreamPlayer2" type="AudioStreamPlayer" parent="Sounds/Walk"]
stream = ExtResource( 10 )
[node name="AudioStreamPlayer3" type="AudioStreamPlayer" parent="Sounds/Walk"]
stream = ExtResource( 7 )
[node name="AudioStreamPlayer4" type="AudioStreamPlayer" parent="Sounds/Walk"]
stream = ExtResource( 9 )
[node name="AudioStreamPlayer5" type="AudioStreamPlayer" parent="Sounds/Walk"]
stream = ExtResource( 8 )
[node name="WalkSoundTimer" type="Timer" parent="Sounds"]
one_shot = true
[connection signal="area_entered" from="Hitbox" to="." method="_on_Hitbox_area_entered"]
[connection signal="area_entered" from="Hurtbox" to="." method="_on_Hurtbox_area_entered"]
[connection signal="area_exited" from="Hurtbox" to="." method="_on_Hurtbox_area_exited"]

View File

@@ -1,8 +1,44 @@
extends "res://Player/States/PlayerAnimationState.gd"
#the channel on which the sound is played
const CHANNEL=1
#one sound will be chosen at random
export var SoundLibary :PoolStringArray=[]
#the delay between 2 sounds being played, a new sound will only start if the old one is finished
export(float,0,5) var Delay = 0.2
var is_playing_sound:bool =false
var delay_passed:bool=true
var timer:Timer
func _ready():
timer=Timer.new()
self.add_child(timer)
timer.connect("timeout",self,"sig_timer_timeout")
SoundControler._effect[CHANNEL].connect("finished", self, "sig_walk_sound_finished")
func enter():
animation_playback.travel("run")
func update(delta):
var input_vector = get_input_direction()
animation_tree.set("parameters/run/blend_position", input_vector)
_play_random_sound()
func _play_random_sound():
if delay_passed and is_playing_sound==false:
var sound = SoundLibary[rand_range(0,SoundLibary.size())]
SoundControler.pub_play_effect(sound,CHANNEL)
is_playing_sound = true
delay_passed=false
#timer.start(Delay)
func sig_timer_timeout ():
delay_passed=true
func sig_walk_sound_finished():
is_playing_sound = false
timer.start(Delay)