Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.9 kB
1
Indexable
Never
extends PanelContainer

var dialogue = {
	0: {
		"text": "Hey, [i]wake up![/i] It's time to make video games.",
		"expression": "neutral",
		"buttons":
		{
			"Let me sleep a little longer": 2,
			"Let's do it!": 1,
		}
	},
	1: {
		"text": "Great! Your first task will be to write a [b]dialogue tree[/b].",
		"expression": "neutral",
		"buttons":
		{
			"I'm not sure if I'm ready, but I will do my best": 3,
			"No, let me go back to sleep": 2,
		}
	},
	2: {
		"text": "Oh, come on! It'll be fun.",
		"expression": "sad",
		"buttons":
		{
			"No, really, let me go back to sleep": 0,
			"Alright, I'll try": 3,
		}
	},
	3: {
		"text": "That's the spirit! You can do it!\n[wave][b]YOU WIN[/b][/wave]",
		"expression": "happy",
		"buttons": {}
	}
}

onready var rich_text_label := $MarginContainer/VBoxContainer/HBoxContainer/RichTextLabel
onready var texture_rect := $MarginContainer/VBoxContainer/HBoxContainer/TextureRect
onready var buttons_column := $MarginContainer/VBoxContainer/ButtonsColumn
onready var tween := $Tween
onready var audio_player := $AudioStreamPlayer

func _ready():
	show_line(0)
	tween.connect("tween_all_completed", audio_player, "stop")
	
func show_line(id: int):
	var line_data: Dictionary = dialogue[id]
	set_text(line_data.text)
	set_expression(line_data.expression)
	
	#safely destroy code-generated nodes in queue
	for button in buttons_column.get_children():
		button.queue_free()
	#create new buttons
	if line_data.buttons:
		create_buttons(line_data.buttons)
	else:
		add_quit_button()

func set_text(new_text: String):
	rich_text_label.bbcode_text = new_text
	#tween animation
	var duration : float = rich_text_label.text.length() / 20
	tween.interpolate_property(rich_text_label, "percent_visible", 0.0, 1.0, duration)
	tween.start()
	#we randomize the audio playback's start time to make it sound different every time
	var sound_start_position: float = randf() * audio_player.stream.get_length()
	audio_player.play(sound_start_position)

func set_expression(expression: String):
	if expression == "sad":
		texture_rect.texture = preload("portraits/sophia_sad.png")
	elif expression == "happy":
		texture_rect.texture = preload("portraits/sophia_happy.png")
	else:
		texture_rect.texture = preload("portraits/sophia_neutral.png")	

func create_buttons(buttons_data: Dictionary):
	for text in buttons_data:
		var button := Button.new()
		button.text = text
		buttons_column.add_child(button)
		var target_line_id: int = buttons_data[text]
		#calls show_line(target_line_id). 4th argument is the argument used in called function, in this case, "show_line"
		button.connect("pressed", self, "show_line", [target_line_id])

func add_quit_button():
	var button := Button.new()
	button.text = "Quit"
	buttons_column.add_child(button)
	button.connect("pressed", get_tree(), "quit")
Leave a Comment