81 lines
2.3 KiB
GDScript
81 lines
2.3 KiB
GDScript
extends Node
|
|
|
|
var dialogueBox
|
|
|
|
var currentDialogueResource
|
|
var currentDialogueLine
|
|
var currentSpeaker
|
|
|
|
func _ready():
|
|
initialize_variables()
|
|
|
|
func initialize_variables():
|
|
dialogueBox = get_node("/root/MainGame/CanvasLayer/DialogueBox")
|
|
|
|
if GameVariables.player != null:
|
|
GameVariables.player.moved.connect(end_dialogue)
|
|
|
|
func _process(delta):
|
|
if dialogueBox != null and dialogueBox.visible and is_instance_valid(currentSpeaker) and currentSpeaker != null:
|
|
update_speaker_sprite()
|
|
|
|
func set_current_speaker(speaker):
|
|
currentSpeaker = speaker
|
|
update_speaker_sprite()
|
|
|
|
func update_speaker_sprite():
|
|
var spriteHolder = currentSpeaker.get_node("Sprite")
|
|
if spriteHolder != null:
|
|
var sprite = spriteHolder.sprite_frames.get_frame_texture(spriteHolder.animation, spriteHolder.frame)
|
|
set_dialogue_sprite(sprite)
|
|
|
|
func start_dialogue(dialogueResource):
|
|
dialogueBox.visible = true
|
|
|
|
currentDialogueResource = dialogueResource
|
|
currentDialogueLine = await currentDialogueResource.get_next_dialogue_line("start")
|
|
handle_dialogue_line(currentDialogueLine)
|
|
|
|
func option_chosen(nextLineID):
|
|
currentDialogueLine = await currentDialogueResource.get_next_dialogue_line(nextLineID)
|
|
handle_dialogue_line(currentDialogueLine)
|
|
|
|
func handle_dialogue_line(nextLine:DialogueLine):
|
|
if nextLine == null or !("text" in nextLine):
|
|
end_dialogue()
|
|
return
|
|
|
|
set_dialogue_name(nextLine.character)
|
|
set_dialogue_message(nextLine.text)
|
|
|
|
currentDialogueLine = nextLine
|
|
|
|
handle_dialogue_options(currentDialogueLine.responses)
|
|
|
|
func handle_dialogue_options(choices):
|
|
dialogueBox.clear_dialogue_options()
|
|
|
|
if choices.size() == 0:
|
|
var newOption = dialogueBox.add_dialogue_option("continue")
|
|
newOption.pressed.connect(option_chosen.bind(currentDialogueLine.next_id))
|
|
return
|
|
|
|
for choice in choices:
|
|
var newOption = dialogueBox.add_dialogue_option(choice.text)
|
|
newOption.pressed.connect(option_chosen.bind(choice.next_id))
|
|
|
|
func end_dialogue():
|
|
dialogueBox.visible = false
|
|
if DialogueBoxManager.currentSpeaker != null:
|
|
DialogueBoxManager.currentSpeaker.dialogue_cancel()
|
|
|
|
func set_dialogue_sprite(sprite):
|
|
dialogueBox.set_character_sprite(sprite)
|
|
|
|
func set_dialogue_name(dialogueName):
|
|
dialogueBox.set_character_name(dialogueName)
|
|
|
|
func set_dialogue_message(message):
|
|
dialogueBox.set_message(message)
|
|
|