diff --git a/default_bus_layout.tres b/default_bus_layout.tres new file mode 100644 index 0000000..46f3686 --- /dev/null +++ b/default_bus_layout.tres @@ -0,0 +1,21 @@ +[gd_resource type="AudioBusLayout" format=3 uid="uid://cv4d8sgujag0l"] + +[resource] +bus/1/name = &"BGM" +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 = &"SFX" +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 = &"VOX" +bus/3/solo = false +bus/3/mute = false +bus/3/bypass_fx = false +bus/3/volume_db = 0.0 +bus/3/send = &"Master" diff --git a/dvn.gd b/dvn.gd new file mode 100644 index 0000000..d275e42 --- /dev/null +++ b/dvn.gd @@ -0,0 +1,316 @@ +@icon("res://dvn/dvn.png") +class_name DVNScene extends Node2D +## The DVNScene Node contains all of the built in functions of DVN, excluding those affecting text. +## Every scene in the DVN engine should extend DVNScene, imagine it as a base scene. + +## Completely Invisible Color, if the alpha is increased it becomes [member Color.WHITE] +const TRANS_WHITE = Color(1,1,1,0) +## Completely Invisible Color, if the alpha is increased it becomes [member Color.BLACK] +const TRANS_BLACK = Color(0,0,0,0) + +## Called when [method fade_out_sceen] has finished fading the screen. +## [br]NOTE: Will only be called if [param async] is false!!! +signal screen_fade_out +## Called when [method fade_in_sceen] has finished fading the screen. +## [br]NOTE: Will only be called if [param async] is false!!! +signal screen_fade_in +## Called when [method fade_out_bgm] has finished fading. +## [br]NOTE: Will only be called if [param async] is false!!! +signal bgm_fade_out +## Called when [method fade_in_bgm] has finished fading. +## [br]NOTE: Will only be called if [param async] is false!!! +signal bgm_fade_in +## Called when [method fade_bgm] has finished fading. +## [br]NOTE: Will only be called if [param async] is false!!! +signal bgm_fade +## Called when [method delay] has counted all the way down. +signal delay_ended +## Called when the sound created by [method play_sound] has finished playing. +## [br]NOTE: Will only be called if [param async] is false!!! +signal sound_ended + +## Default ballon to create with [method create_text_balloon] +@export var Balloon: PackedScene = null +## Default ballon to start with [method create_text_balloon] +@export var Dialogue: DialogueResource = null + +## Set the current dialogue automation type. +## [br]see [enum DialogueAutomationTypes] for more info +@export_enum("Manual", "Auto Text", "Auto Voice") var dialogue_automation_type: int +## [param] MANUAL is the default way of handling text. The next line of dialogue is not +## drawn until the user inputs a command. +## [br][param AUTO_TEXT] Automatically skips to the next text box after the [member auto_text_delay] timer is done counting down. +## [br][param AUTO_VOICE] Automatically skips to the next text box when the voice clip is done playing. +enum DialogueAutomationTypes{MANUAL,AUTO_TEXT,AUTO_VOICE} + +## The wait time until new text is drawn when [member dialogue_automation_type] is set to auto voice. +@export var auto_text_delay:float = 1.0 + +## Default time value for fading the screen +@export var default_screen_fade_time: float = 2.0 +## Default time value for fading the BGM +@export var default_bgm_fade_time: float = 2.0 + +## Reference to BGM Player +@onready var bgm = %BGM +## Reference to built in Animation Player +@onready var animation_player = $DVN/AnimationPlayer + +## The volume of the BGM set in the editor, used +@onready var bgm_initial_volume = bgm.get_volume_db() + +## The currently active text balloon, created from [method create_text_balloon] +var current_balloon = null + + +#region Text Functions +## Create a text balloon starting from [param title] +func create_text_balloon(title:String): + current_balloon = Balloon.instantiate() + add_child(current_balloon) + current_balloon.start(Dialogue,title) + +## Wait a certain amount of [param time] until the next dialogue line is executed +func delay(time:float = 2.0): + await get_tree().create_timer(time).timeout + emit_signal("delay_ended") + screen_fade_out.connect(on_delay_ended) + +## Switch the currently selected [DVNLabel] +## [br]If [param keep_text] is true than the previously selected [DVNLabel] will stay drawn +func switch_label(label,keep_text: bool = false): + if keep_text == false: + current_balloon.dialogue_label.text = "" + current_balloon.dialogue_label = label + +## Move the selected [param label] to a specified [param position] +func move_label(label:PackedScene,position:Vector2): + current_balloon.dialogue_label.position = position + +## Change the [param color] of the currently selected label +func change_text_color(color:Color): + current_balloon.dialogue_label.set_modulate(color) + +## Set the current dialogue automation type. +## [br]see [enum DialogueAutomationTypes] for more info +func set_dialogue_automation_type(type:int): + dialogue_automation_type = type + +## Set the text speed in seconds per step +func set_text_speed(speed:float=0.02): + current_balloon.dialogue_label.seconds_per_step = speed + +## Called when the dialogue reaches an END block. +func dialogue_ended(): + pass +#endregion + +#region Voice Functions +## Set the current voice line position. Useful if you have a lot of different branches +## and loops in dialogue. +func set_voice_line(index:int): + current_balloon.dialogue_label.current_voice_line = index + +## Set if speech sounds should play at all. +func set_can_speak(truefalse:bool=true): + current_balloon.dialogue_label.can_speak = truefalse + +## Set the volume of the currently selected [DVNLabel]'s talktone player in db +func set_voice_volume(volume:float = 0.0): + current_balloon.dialogue_label.talktone_player.set_volume_db(volume) +#endregion + +#region Screen Fade Functions +## Fades in the screen. This is achieved by interpolating the alpha of [DVNScene]'s [ColorRect]. +## [br][br][param color] determines the color to fade in from. +## [br][param time] determines the amount of time for the fade to take. +## [br][param hold_time] determines the time to wait until the screen starts fading. +## [br]if [param async] is true, the fade will be performed alongside other actions, otherwise, other actions won't be performed until the fading is finished. +func fade_in_screen(color:Color = Color.BLACK,time:float = default_screen_fade_time,hold_time:float = 0,async:bool = false): + %FadeColor.color = color + %FadeColor.color.a = 1.0 + await get_tree().create_timer(hold_time).timeout + var tween = get_tree().create_tween() + tween.tween_property(%FadeColor, "color:a", 0.0, time).set_trans(Tween.TRANS_LINEAR) + if !async: + await get_tree().create_timer(time).timeout + emit_signal("screen_fade_in") + screen_fade_out.connect(on_screen_fade_in) + +## Fades out the screen. This is achieved by interpolating the alpha of [DVNScene]'s [ColorRect]. +## [br][br][param color] determines the color to fade to. +## [br][param time] determines the amount of time for the fade to take. +## [br][param hold_time] determines the extra time to hold the faded color, until the fading is considered "finished". +## [br]if [param async] is true, the fade will be performed alongside other actions, otherwise, other actions won't be performed until the fading is finished. +func fade_out_screen(color:Color = Color.BLACK,time:float = default_screen_fade_time,hold_time:float = 0,async:bool = false): + %FadeColor.color = color + %FadeColor.color.a = 0.0 + var tween = get_tree().create_tween() + tween.tween_property(%FadeColor, "color:a", 1.0, time).set_trans(Tween.TRANS_LINEAR) + if !async: + await get_tree().create_timer(time).timeout + await get_tree().create_timer(hold_time).timeout + emit_signal("screen_fade_in") + screen_fade_out.connect(on_screen_fade_in) +#endregion + +#region BGM Fade Functions +## Fades out BGM. This is achieved by interpolating the volume of [DVNScene]'s BGM [AudioStreamPlayer]. +## [br][br][param time] determines the amount of time for the fade to take. +## [br]if [param async] is true, the fade will be performed alongside other actions, otherwise, other actions won't be performed until the fading is finished. +func fade_out_bgm(time:float = default_bgm_fade_time,async:bool = false): + var tween = get_tree().create_tween() + tween.tween_property(bgm, "volume_db", -80, time) + if !async: + await get_tree().create_timer(time).timeout + emit_signal("bgm_fade_out") + bgm_fade_in.connect(on_bgm_fade_in) + +## Fades in BGM. This is achieved by interpolating the volume of [DVNScene]'s BGM [AudioStreamPlayer]. +## [br][br][param time] determines the amount of time for the fade to take. +## [br]if [param async] is true, the fade will be performed alongside other actions, otherwise, other actions won't be performed until the fading is finished. +func fade_in_bgm(time:float = default_bgm_fade_time,async:bool = false): + bgm.set_volume_db(-80) + bgm.play() + var tween = get_tree().create_tween() + tween.tween_property(bgm, "volume_db", bgm_initial_volume, time) + if !async: + await get_tree().create_timer(time).timeout + emit_signal("bgm_fade_in") + bgm_fade_in.connect(on_bgm_fade_in) + +## Fades the BGM from its current value to [param end_volume]. This is achieved by interpolating the volume of [DVNScene]'s BGM [AudioStreamPlayer]. +## [br][br][param time] determines the amount of time for the fade to take. +## [br]if [param async] is true, the fade will be performed alongside other actions, otherwise, other actions won't be performed until the fading is finished. +func fade_bgm(end_volume:float = 0.0,time:float = default_bgm_fade_time,async:bool = false): + var tween = get_tree().create_tween() + tween.tween_property(bgm, "volume_db", end_volume, time) + if !async: + await get_tree().create_timer(time).timeout + emit_signal("bgm_fade") + bgm_fade.connect(on_bgm_fade) + +## Change BGM to [param stream] +func change_bgm(stream:AudioStream): + bgm.stream = stream + +## Pause/Resume BGM +func pause_bgm(pause:bool = true): + bgm.set_stream_paused(pause) + +## STOP BGM. +func stop_bgm(): + bgm.stop() + +## Play the BGM. +##[br]Different from setting [method pause_bgm] to false as this will start the stream from the beginning +func play_bgm(): + bgm.play() + +## Set the BGM Volume in db. +## [br]Does not fade, volume change is abrupt. +func set_bgm_volume(volume:float = 0.0): + bgm.set_volume_db(volume) + +## Skip to a spot in the BGM. see [method AudioStreamPlayer.seek] +func seek_bgm(to_position:float = 0.0): + bgm.seek(to_position) +#endregion + +#region SFX Functions +## Play a sound effect. Achieved by creating an [AudioStreamPlayer] and freeing it when it's done playing. +## [br][br]The specific [param sound] can be changed, along with its [param volume] and [param pitch]. +## [br]if [param async] is true, the fade will be performed alongside other actions, otherwise, other actions won't be performed until the fading is finished. +## [br][br]The Sound effect will play from the SFX audio bus. +func play_sound(sound:AudioStream,volume:float,pitch:float,async:bool = true): + var sfx = load("res://dvn/sound_player/sound.tscn").instantiate() + sfx.stream = sound + sfx.set_volume_db(volume) + sfx.pitch_scale = pitch + add_child(sfx) + sfx.play() + if !async: + await get_tree().create_timer(sound.get_length()).timeout + emit_signal("sound_ended") + sound_ended.connect(on_sound_ended) +#endregion + + +#region Animation Functions +## Sets an [AnimationPlayer]'s [param speed]. +## [br] by default this affects the [DVNScene]'s built in [AnimationPlayer] +## [br][br] see [method AnimationPlayer.set_speed_scale] +func set_animation_speed(speed:float = 1.0,player:Node = animation_player): + animation_player.set_speed_scale(1.0) + +## Play an anim on your [AnimationPlayer]. +## [br] by default this affects the [DVNScene]'s built in [AnimationPlayer] +## [br][br] see [method AnimationPlayer.play] +func play_animtion(animation:String,player = animation_player): + player.play(animation) +#endregion + +#region Scene Functions +## Go to the next [param scene] from a node's path. [param example: "res://dvn/dvn.tscn"] +## [br]can be used to change the current scene to anything, but you generally want +## to use it to move to the next specific "scene" or "level" +func change_scene(scene:String): + get_tree().change_scene_to_file(scene) + +## Ends the scene with a screen and BGM fade out. When the screen and BGM are done fading, the scene switches to [param next_scene] +## [br][param screen_fade_time] and [param bgm_fade_time] can be adjusted, though by default they are set to [member default_screen_fade_time] and [member default_bgm_fade_time] +## [br]You can set the default fade times in [DVNScene]'s exports. +## [br]Ideally this is the kind of function you can use to smoothly end a scene in one line. +func end_scene(next_scene:String,screen_fade_time:float = default_screen_fade_time,bgm_fade_time:float = default_bgm_fade_time): + #Determine whether to end on bgm fade or screen fade + var wait_time = 0.0 + if bgm_fade_time > screen_fade_time: + wait_time = bgm_fade_time + else: + wait_time = screen_fade_time + #fade bgm and screen + fade_out_bgm(bgm_fade_time) + fade_out_screen(%FadeColor.color,screen_fade_time) + await get_tree().create_timer(wait_time).timeout #wait for bgm and screen to finish fading + change_scene(next_scene) +#endregion + +#region Signals +## Called when [method delay] has counted all the way down. +func on_delay_ended(): + pass + +## Called when [method fade_out_sceen] has finished fading the screen and [param hold_time] has counted all the way down. +## [br]NOTE: Will only be called if [param async] is false!!! +func on_screen_fade_out(): + pass + +## Called when [method fade_in_sceen] has finished fading the screen. +## [br]NOTE: Will only be called if [param async] is false!!! +func on_screen_fade_in(): + pass + +## Called when the sound created by [method play_sound] has finished playing. +## [br]NOTE: Will only be called if [param async] is false!!! +func on_sound_ended(): + pass + +## Called when [method fade_out_bgm] has finished fading. +## [br]NOTE: Will only be called if [param async] is false!!! +func on_bgm_fade_out(): + pass + +## Called when [method fade_in_bgm] has finished fading. +## [br]NOTE: Will only be called if [param async] is false!!! +func on_bgm_fade_in(): + pass + +## Called when [method fade_bgm] has finished fading. +## [br]NOTE: Will only be called if [param async] is false!!! +func on_bgm_fade(): + pass + +## Called when... when... when something happens. make good use of that [param anim_name]!!! +func _on_animation_player_animation_finished(anim_name: StringName) -> void: + pass # Replace with function body. +#endregion diff --git a/dvn.png b/dvn.png new file mode 100644 index 0000000..3c53c73 Binary files /dev/null and b/dvn.png differ diff --git a/dvn.png.import b/dvn.png.import new file mode 100644 index 0000000..d74ff47 --- /dev/null +++ b/dvn.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://be46s5lqu5hyb" +path="res://.godot/imported/dvn.png-3a6527566642db13afb0a943aac4e59a.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://dvn/dvn.png" +dest_files=["res://.godot/imported/dvn.png-3a6527566642db13afb0a943aac4e59a.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/dvn.tscn b/dvn.tscn new file mode 100644 index 0000000..968a963 --- /dev/null +++ b/dvn.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://bd7pbhempcian"] + +[ext_resource type="Script" path="res://dvn/dvn.gd" id="1_0ao5y"] +[ext_resource type="AudioStream" uid="uid://7fnndwc2fe7i" path="res://dvn/test_resources/sound_test.wav" id="2_pjx2r"] + +[node name="Scene" type="Node2D"] +script = ExtResource("1_0ao5y") + +[node name="DVN" type="Node" parent="."] + +[node name="FadeColor" type="ColorRect" parent="DVN"] +unique_name_in_owner = true +offset_right = 1920.0 +offset_bottom = 1080.0 +color = Color(0, 0, 0, 0) + +[node name="BGM" type="AudioStreamPlayer" parent="DVN"] +unique_name_in_owner = true +stream = ExtResource("2_pjx2r") +bus = &"BGM" + +[node name="AnimationPlayer" type="AnimationPlayer" parent="DVN"] +root_node = NodePath("../..") + +[connection signal="animation_finished" from="DVN/AnimationPlayer" to="." method="_on_animation_player_animation_finished"] diff --git a/label/dvn_label.gd b/label/dvn_label.gd new file mode 100644 index 0000000..c52f0eb --- /dev/null +++ b/label/dvn_label.gd @@ -0,0 +1,134 @@ +@icon("res://addons/dialogue_manager/assets/icon.svg") +class_name DVNLabel extends DialogueLabel +## The DVNLabel Class takes care of dialogue related functions on the label-level + +## ToneTypes for the Talktone player. +## [br][param NONE] plays no sound when text is outputted +## [br][param VOICE] plays a [AudioStream] from [member voice_clips] at the beginning of each dialogue box, +## the index of [member voice clips] increases with each dialogue box. Use this for full voice acting. +## [br][param HALF_BEEP] plays a [member beep_noise] for every other letter outputted +## [br][param BEEP] plays a [member beep_noise] for every letter outputted +## [br][param BLABBER] plays a random [AudioStream] from [member blabber_noises] for every letter outputted. +## Its pitch is a random value between [member blabber_pitch_minumum] and [member blabber_pitch_maximum] +## [br][param MARISESE] uses rudimentary voice synthesis (a specific sound is mapped to each letter aka each [AudioStream] from values 0-25, with every other symbol being represented by value 26) +## ## Its pitch is a random value between [member marisese_pitch_minumum] and [member marisese_pitch_maximum] +enum ToneTypes{NONE,VOICE,HALF_BEEP,BEEP,BLABBER,MARISESE} +@export var talktone_player_path: NodePath +var talktone_player = null +var current_voice_line:int = 0 +# Current TalkType +@export_enum("None","Voice","Half Beep","Beep","Blabber","Marisese") var tone_type: int +# Tone Noises +@export var voice_clips: Array[Resource] = [] +@export var beep_noise: Resource +@export var blabber_noises: Array[Resource] = [] +@export var blabber_pitch_minumum: float = 1.0 +@export var blabber_pitch_maximum: float = 1.0 +@export var marisese_noises: Array[Resource] = [] +@export var marisese_pitch_minumum: float = 0.9 +@export var marisese_pitch_maximum: float = 1.1 +@export var can_speak: bool = true + +@onready var scene = get_owner().get_parent() +@onready var balloon = get_owner() + +func _init() -> void: + bbcode_enabled = true + fit_content = true + scroll_active = false + shortcut_keys_enabled = false + meta_underlined = false + hint_underlined = false + deselect_on_focus_loss_enabled = false + visible_characters_behavior = TextServer.VisibleCharactersBehavior.VC_CHARS_AFTER_SHAPING + set_anchors_preset(Control.PRESET_TOP_WIDE) + size.x = 640.0 + mouse_filter = MOUSE_FILTER_PASS + + +func _ready() -> void: + talktone_player = get_node(talktone_player_path) + talktone_player.finished.connect(on_talktone_finished) + spoke.connect(_on_spoke) + finished_typing.connect(_on_finished_typing) + +func type_out(): + super() + if tone_type == ToneTypes.VOICE && can_speak: + if current_voice_line <= voice_clips.size() - 1: + talktone_player.stream = voice_clips[current_voice_line] + talktone_player.play() + current_voice_line += 1 + +func play_beep_tone(sound:Resource = beep_noise,pitch_minimum:float = 1.0,pitch_maximum:float = 1.0,player:Node = talktone_player): + var r = randf_range(pitch_minimum,pitch_maximum) + player.stream = sound + player.pitch_scale = r + player.play() + +func play_blabber_tone(sounds:Array = blabber_noises,volume:float = 0.0,pitch_minimum:float = blabber_pitch_minumum,pitch_maximum:float = blabber_pitch_maximum,player:Node = talktone_player): + var r = randi_range(0,sounds.size() - 1) + player.stream = sounds[r] + var r2 = randf_range(pitch_minimum,pitch_maximum) + player.pitch_scale = r2 + player.play() + +func play_marisese(letter:String = "A",sounds:Array = marisese_noises,volume:float = 0.0,pitch_minimum:float = marisese_pitch_minumum,pitch_maximum:float = marisese_pitch_maximum,player:Node = talktone_player): + var snd = 26 +#region check every letter + match letter.to_upper(): + "A": snd = 0 + "B": snd = 1 + "C": snd = 2 + "D": snd = 3 + "E": snd = 4 + "F": snd = 5 + "G": snd = 6 + "H": snd = 7 + "I": snd = 8 + "J": snd = 9 + "K": snd = 10 + "L": snd = 11 + "M": snd = 12 + "N": snd = 13 + "O": snd = 14 + "P": snd = 15 + "Q": snd = 16 + "R": snd = 17 + "S": snd = 18 + "T": snd = 19 + "U": snd = 20 + "V": snd = 21 + "W": snd = 22 + "X": snd = 23 + "Y": snd = 24 + "Z": snd = 25 +#endregion + player.stream = sounds[snd] + var r = randf_range(pitch_minimum,pitch_maximum) + player.pitch_scale = r + player.play() + +func _on_spoke(letter: String, letter_index: int, speed: float) -> void: + if can_speak: + match tone_type: + ToneTypes.HALF_BEEP: + if letter_index % 2 == 0: + play_beep_tone() + ToneTypes.BEEP: + play_beep_tone() + ToneTypes.BLABBER: + play_blabber_tone() + ToneTypes.MARISESE: + play_marisese(letter) + +func on_talktone_finished(): + if scene.dialogue_automation_type == scene.DialogueAutomationTypes.AUTO_VOICE: + balloon.next(balloon.dialogue_line.next_id) + + +func _on_finished_typing(): + if scene.dialogue_automation_type == scene.DialogueAutomationTypes.AUTO_TEXT: + await get_tree().create_timer(scene.auto_text_delay).timeout + if balloon.dialogue_line.responses.size() == 0: + balloon.next(balloon.dialogue_line.next_id) diff --git a/label/dvn_label.tscn b/label/dvn_label.tscn new file mode 100644 index 0000000..fbbee17 --- /dev/null +++ b/label/dvn_label.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=4 format=3 uid="uid://cca3fm3pojb41"] + +[ext_resource type="Script" path="res://dvn/label/dvn_label.gd" id="1_fv617"] +[ext_resource type="AudioStream" uid="uid://sm4230h82f5q" path="res://dvn/test_resources/blabber/pi1.wav" id="2_tuq54"] +[ext_resource type="AudioStream" uid="uid://bg3bavm00pcnk" path="res://dvn/test_resources/blabber/pe1.wav" id="3_d0ft6"] + +[node name="DVNLabel" type="RichTextLabel"] +script = ExtResource("1_fv617") +talktone_player_path = NodePath("../../../../../AudioStreamPlayer") +tone_type = 4 +beep_noise = ExtResource("2_tuq54") +blabber_noises = Array[Resource]([ExtResource("2_tuq54"), ExtResource("3_d0ft6")]) + +[connection signal="spoke" from="." to="." method="_on_spoke"] diff --git a/sound_player/sound.gd b/sound_player/sound.gd new file mode 100644 index 0000000..e4235e8 --- /dev/null +++ b/sound_player/sound.gd @@ -0,0 +1,4 @@ +extends AudioStreamPlayer + +func _on_finished() -> void: + queue_free() diff --git a/sound_player/sound.tscn b/sound_player/sound.tscn new file mode 100644 index 0000000..e7b12e6 --- /dev/null +++ b/sound_player/sound.tscn @@ -0,0 +1,9 @@ +[gd_scene load_steps=2 format=3 uid="uid://bldqqpajcj62n"] + +[ext_resource type="Script" path="res://dvn/sound_player/sound.gd" id="1_boi0g"] + +[node name="Sound" type="AudioStreamPlayer"] +bus = &"SFX" +script = ExtResource("1_boi0g") + +[connection signal="finished" from="." to="." method="_on_finished"]