Starting State Aware Sound Effects

This commit is contained in:
Dustin 2025-03-30 19:24:19 -07:00
parent 85653ba634
commit f0a60217bc
7 changed files with 93 additions and 27 deletions

View File

@ -6,7 +6,6 @@ extends KinematicBody2D
export(String, "Player", "Enemy", "NPC", "Object") var actor_type export(String, "Player", "Enemy", "NPC", "Object") var actor_type
#export var sprite_facing_right: bool = true #export var sprite_facing_right: bool = true
export(Array, Resource) var SoundEffects
export var debug_actor: bool = false export var debug_actor: bool = false
@ -20,7 +19,6 @@ onready var movement_component: Movement_StateReceiver = $Movement_StateReceiver
#onready var movement_state_machine: StateMachineAnimatedActor = $movement_state_machine #onready var movement_state_machine: StateMachineAnimatedActor = $movement_state_machine
onready var movement_state_machine: StateMachine = $Movement_StateMachine onready var movement_state_machine: StateMachine = $Movement_StateMachine
var sound_effects_dict: Dictionary
##TODO: ##TODO:
# Can't seem to implement the ready and process node functions # Can't seem to implement the ready and process node functions
@ -29,19 +27,11 @@ var sound_effects_dict: Dictionary
# interfaces for the state machines. # interfaces for the state machines.
func _ready() -> void: func _ready() -> void:
pass
#movement_state_machine.init_animated_actor(self) #movement_state_machine.init_animated_actor(self)
#movement_component.attack_function = funcref(self, "attack") #movement_component.attack_function = funcref(self, "attack")
# movement_component.player_number = player_number # movement_component.player_number = player_number
# Experimenting with sound based resources I did this and it worked
#sound_effect_player.stream = sound_effects.sounds["pew"]
#print(sounds.sounds["pew"])
#sound_effects.play()
# Trying something new with a custom resourse.
for i in SoundEffects:
sound_effects_dict[i.animation_name + str(i.frame_number)] = i.sound
print (sound_effects_dict)
func _unhandled_input(event: InputEvent) -> void: func _unhandled_input(event: InputEvent) -> void:
if actor_type == "Player": if actor_type == "Player":
@ -64,20 +54,6 @@ func _physics_process(delta: float) -> void:
# #play_sound_frame(movement_animations.animation , movement_animations.frame) # #play_sound_frame(movement_animations.animation , movement_animations.frame)
var last_sound_frame_animation: String = ''
var last_sound_frame: int = -1
func play_sound_frame(animation: String, frame: int ):
##DEBUG:
#if animation == 'run' and frame == 6:
# print("Do the thign! (", animation + str(frame), ")", " (", last_sound_frame_animation + str(last_sound_frame), ")")
if animation != last_sound_frame_animation or frame != last_sound_frame:
last_sound_frame = frame
last_sound_frame_animation = animation
var sound_index = animation + str(frame)
if sound_effects_dict.has(sound_index):
sound_effect_player.stream = sound_effects_dict[sound_index]
sound_effect_player.play()
print(self.name, " Playing SE: ", sound_index)
func attack(): func attack():
#print(self.name, ": It's not inheritence, it's funcrefs") #print(self.name, ": It's not inheritence, it's funcrefs")

View File

@ -42,6 +42,7 @@ func _ready():
# Sprites should only have a process function # Sprites should only have a process function
# but if I have this will the sprite still animate? # but if I have this will the sprite still animate?
func _process(delta): func _process(delta):
current_state.animation_frame = frame
##TODO: Put in to try to buffer animation switches on frame change ##TODO: Put in to try to buffer animation switches on frame change
if _frame_switch_tracker != -1 and frame == _frame_switch_tracker: if _frame_switch_tracker != -1 and frame == _frame_switch_tracker:
print ("time to switch", _frame_switch_tracker) print ("time to switch", _frame_switch_tracker)

View File

@ -0,0 +1,64 @@
class_name AudioStreamPlayer_StateReceiver
extends AudioStreamPlayer
export var debug_component: bool = false
export var callable_state_machine :NodePath
var request_state_change: FuncRef
export(Array, Resource) var sound_effects
onready var current_state = StateAnimatedActor.new()
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var sound_effects_dict: Dictionary
#class_name FrameSoundEffects
#extends Resource
#
#
#export (String) var animation_name
#export(int) var frame_number
#export (AudioStreamSample) var sound
# Called when the node enters the scene tree for the first time.
func _ready():
# Trying something new with a custom resourse.
for i in sound_effects:
sound_effects_dict[i.animation_name + str(i.frame_number)] = i.sound
print (sound_effects_dict)
request_state_change = funcref(get_node(callable_state_machine), 'change_to_known_state')
## Connect signal to get alerted of state change
get_node(callable_state_machine).connect("state_changed", self,"_on_state_change")
## A reference to the current state machine state
current_state = get_node(callable_state_machine).current_state
# Experimenting with sound based resources I did this and it worked
#sound_effect_player.stream = sound_effects.sounds["pew"]
#print(sounds.sounds["pew"])
#sound_effects.play()
var last_sound_frame_animation: String = ''
var last_sound_frame: int = -1
func play_sound_frame(animation: String, frame: int ):
##DEBUG:
#if animation == 'run' and frame == 6:
# print("Do the thign! (", animation + str(frame), ")", " (", last_sound_frame_animation + str(last_sound_frame), ")")
if animation != last_sound_frame_animation or frame != last_sound_frame:
last_sound_frame = frame
last_sound_frame_animation = animation
var sound_index = animation + str(frame)
if sound_effects_dict.has(sound_index):
stream = sound_effects_dict[sound_index]
play()
print(self.name, " Playing SE: ", sound_index)
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

View File

@ -0,0 +1,7 @@
class_name SE_StateFrame
extends Resource
export (String) var state_name
export(int) var frame_number
export (AudioStreamSample) var sound

View File

@ -26,6 +26,7 @@ var jerk: float
## Variables intended to allow state receivers to communicate ## Variables intended to allow state receivers to communicate
var animation_finished: bool = false var animation_finished: bool = false
var animation_frame :int = -1
var movement_stopped: bool = false var movement_stopped: bool = false
# Not sure if this should be here. probably not # Not sure if this should be here. probably not
@ -37,6 +38,7 @@ func enter() -> void:
.enter() .enter()
animation_finished = false animation_finished = false
movement_stopped = false movement_stopped = false
animation_frame = -1
jerk = 0 jerk = 0
return return

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=5 format=2] [gd_scene load_steps=6 format=2]
[ext_resource path="res://lib/classes/animated_sprite_state_receiver.gd" type="Script" id=1] [ext_resource path="res://lib/classes/animated_sprite_state_receiver.gd" type="Script" id=1]
[ext_resource path="res://lib/classes/state_machine.gd" type="Script" id=2] [ext_resource path="res://lib/classes/state_machine.gd" type="Script" id=2]
[ext_resource path="res://lib/classes/audiostreamplayer_state_receiver.gd" type="Script" id=3]
[ext_resource path="res://lib/classes/movement_state_receiver.gd" type="Script" id=5] [ext_resource path="res://lib/classes/movement_state_receiver.gd" type="Script" id=5]
[ext_resource path="res://lib/classes/actor.gd" type="Script" id=8] [ext_resource path="res://lib/classes/actor.gd" type="Script" id=8]
@ -33,4 +34,7 @@ callable_state_machine = NodePath("../Movement_StateMachine")
script = ExtResource( 5 ) script = ExtResource( 5 )
callable_state_machine = NodePath("../Movement_StateMachine") callable_state_machine = NodePath("../Movement_StateMachine")
[node name="SE_Player" type="AudioStreamPlayer" parent="."] [node name="AudioStreamPlayer_StateReceiver" type="AudioStreamPlayer" parent="."]
script = ExtResource( 3 )
callable_state_machine = NodePath("../Movement_StateMachine")
sound_effects = [ null ]

View File

@ -19,6 +19,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://lib/classes/animated_sprite_state_receiver.gd" "path": "res://lib/classes/animated_sprite_state_receiver.gd"
}, { }, {
"base": "AudioStreamPlayer",
"class": "AudioStreamPlayer_StateReceiver",
"language": "GDScript",
"path": "res://lib/classes/audiostreamplayer_state_receiver.gd"
}, {
"base": "", "base": "",
"class": "GitAPI", "class": "GitAPI",
"language": "NativeScript", "language": "NativeScript",
@ -70,6 +75,11 @@ _global_script_classes=[ {
"path": "res://lib/classes/movement_state_receiver.gd" "path": "res://lib/classes/movement_state_receiver.gd"
}, { }, {
"base": "Resource", "base": "Resource",
"class": "SE_StateFrame",
"language": "GDScript",
"path": "res://lib/classes/sound_effect_state_frame.gd"
}, {
"base": "Resource",
"class": "State", "class": "State",
"language": "GDScript", "language": "GDScript",
"path": "res://lib/classes/state.gd" "path": "res://lib/classes/state.gd"
@ -107,6 +117,7 @@ _global_script_classes=[ {
_global_script_class_icons={ _global_script_class_icons={
"Actor": "", "Actor": "",
"AnimatedSprite_StateReceiver": "", "AnimatedSprite_StateReceiver": "",
"AudioStreamPlayer_StateReceiver": "",
"GitAPI": "", "GitAPI": "",
"HealthComponent": "", "HealthComponent": "",
"HealthPickup": "", "HealthPickup": "",
@ -117,6 +128,7 @@ _global_script_class_icons={
"Modifier_Receiver": "", "Modifier_Receiver": "",
"MovementComponent": "", "MovementComponent": "",
"Movement_StateReceiver": "", "Movement_StateReceiver": "",
"SE_StateFrame": "",
"State": "", "State": "",
"StateAnimatedActor": "", "StateAnimatedActor": "",
"StateMachine": "", "StateMachine": "",