diff --git a/lib/classes/actor.gd b/lib/classes/actor.gd index 90ad5d9..9e6cca7 100644 --- a/lib/classes/actor.gd +++ b/lib/classes/actor.gd @@ -6,7 +6,6 @@ extends KinematicBody2D export(String, "Player", "Enemy", "NPC", "Object") var actor_type #export var sprite_facing_right: bool = true -export(Array, Resource) var SoundEffects 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: StateMachine = $Movement_StateMachine -var sound_effects_dict: Dictionary ##TODO: # 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. func _ready() -> void: + pass #movement_state_machine.init_animated_actor(self) #movement_component.attack_function = funcref(self, "attack") # 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: if actor_type == "Player": @@ -64,20 +54,6 @@ func _physics_process(delta: float) -> void: # #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(): #print(self.name, ": It's not inheritence, it's funcrefs") diff --git a/lib/classes/animated_sprite_state_receiver.gd b/lib/classes/animated_sprite_state_receiver.gd index 470b2fd..fbc5394 100644 --- a/lib/classes/animated_sprite_state_receiver.gd +++ b/lib/classes/animated_sprite_state_receiver.gd @@ -42,6 +42,7 @@ func _ready(): # Sprites should only have a process function # but if I have this will the sprite still animate? func _process(delta): + current_state.animation_frame = frame ##TODO: Put in to try to buffer animation switches on frame change if _frame_switch_tracker != -1 and frame == _frame_switch_tracker: print ("time to switch", _frame_switch_tracker) diff --git a/lib/classes/audiostreamplayer_state_receiver.gd b/lib/classes/audiostreamplayer_state_receiver.gd new file mode 100644 index 0000000..d6f068a --- /dev/null +++ b/lib/classes/audiostreamplayer_state_receiver.gd @@ -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 diff --git a/lib/classes/sound_effect_state_frame.gd b/lib/classes/sound_effect_state_frame.gd new file mode 100644 index 0000000..abe7924 --- /dev/null +++ b/lib/classes/sound_effect_state_frame.gd @@ -0,0 +1,7 @@ +class_name SE_StateFrame +extends Resource + + +export (String) var state_name +export(int) var frame_number +export (AudioStreamSample) var sound diff --git a/lib/classes/state_animated_actor.gd b/lib/classes/state_animated_actor.gd index 3ce39f9..dc6ca42 100644 --- a/lib/classes/state_animated_actor.gd +++ b/lib/classes/state_animated_actor.gd @@ -26,6 +26,7 @@ var jerk: float ## Variables intended to allow state receivers to communicate var animation_finished: bool = false +var animation_frame :int = -1 var movement_stopped: bool = false # Not sure if this should be here. probably not @@ -37,6 +38,7 @@ func enter() -> void: .enter() animation_finished = false movement_stopped = false + animation_frame = -1 jerk = 0 return diff --git a/lib/templates/Actor/ActorTemplate.tscn b/lib/templates/Actor/ActorTemplate.tscn index e3fb4a8..a06e9ee 100644 --- a/lib/templates/Actor/ActorTemplate.tscn +++ b/lib/templates/Actor/ActorTemplate.tscn @@ -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/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/actor.gd" type="Script" id=8] @@ -33,4 +34,7 @@ callable_state_machine = NodePath("../Movement_StateMachine") script = ExtResource( 5 ) 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 ] diff --git a/project.godot b/project.godot index a2e1ff4..af138d7 100644 --- a/project.godot +++ b/project.godot @@ -19,6 +19,11 @@ _global_script_classes=[ { "language": "GDScript", "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": "", "class": "GitAPI", "language": "NativeScript", @@ -70,6 +75,11 @@ _global_script_classes=[ { "path": "res://lib/classes/movement_state_receiver.gd" }, { "base": "Resource", +"class": "SE_StateFrame", +"language": "GDScript", +"path": "res://lib/classes/sound_effect_state_frame.gd" +}, { +"base": "Resource", "class": "State", "language": "GDScript", "path": "res://lib/classes/state.gd" @@ -107,6 +117,7 @@ _global_script_classes=[ { _global_script_class_icons={ "Actor": "", "AnimatedSprite_StateReceiver": "", +"AudioStreamPlayer_StateReceiver": "", "GitAPI": "", "HealthComponent": "", "HealthPickup": "", @@ -117,6 +128,7 @@ _global_script_class_icons={ "Modifier_Receiver": "", "MovementComponent": "", "Movement_StateReceiver": "", +"SE_StateFrame": "", "State": "", "StateAnimatedActor": "", "StateMachine": "",