storytelling-assistant/traits.gd

140 lines
3.9 KiB
GDScript

extends Control
@export var traits:Array = [
"Courage",
"Passion",
"Creativity",
"Temperance",
"Sensuality",
"Energy",
"Responsibility",
"Empathy",
"Self-esteem",
"Flexibility",
"Hope",
"Will"
]
@export var seed: int = 0
@export var amount: int = 3
var pin_items: Array = []
# variables for nodes
@onready var traits_list: VBoxContainer = $Traits_list
# Variable for icons
@onready var icon_up: Texture2D = load("res://textures/arrowUp.png")
@onready var icon_down: Texture2D = load("res://textures/arrowDown.png")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
build_traits()
func build_traits() -> void:
# Build the traits list with checkboxes to pin any
var list_amount: int = amount
var pinned: Array = []
# Keep track of pinned content
for child in traits_list.get_children():
for sub_child in child.get_children():
if sub_child is CheckBox:
if sub_child.button_pressed:
list_amount -= 1
pinned.append([child.get_index(), child])
# Then clear the Hbox for non-pinned traits
for child in traits_list.get_children():
for sub_child in child.get_children():
if sub_child is CheckBox:
if sub_child.button_pressed:
traits_list.remove_child(child)
else:
child.queue_free()
# Generate the amount of traits needed and store in an array
var char_traits: Dictionary = generate_traits(traits, seed, list_amount)
var char_traits_list: Array = []
for key in char_traits.keys():
char_traits_list.append([key, char_traits[key][0], char_traits[key][1]])
# Insert pinned in their position
for pin in pinned:
char_traits_list.insert(pin[0], pin[1])
# Generate a line for each trait
for traitline in range(amount):
if char_traits_list[traitline] is HBoxContainer:
# For pinned content
traits_list.add_child(char_traits_list[traitline])
else:
# Create horizontal container for elements
var container = HBoxContainer.new()
# Checkbox to pin or not
var checkbox: CheckBox = CheckBox.new()
var checkbox_index: int = pin_items.size()
checkbox.text = "" # No text
checkbox.tooltip_text = "PIN"
checkbox.toggled.connect(Callable(self, "_on_checkbox_toggled").bind(checkbox_index))
# Label for trait name
var label: Label = Label.new()
label.text = char_traits_list[traitline][0]
# Icon for up or down
var icon: Texture2D = icon_down
var icon_rect: TextureRect = TextureRect.new()
icon_rect.texture = icon_down
var value: bool = char_traits_list[traitline][1]
if value:
icon_rect.texture = icon_up
# Add the elements to the horizontal container
container.add_child(checkbox)
container.add_child(label)
container.add_child(icon_rect)
# Add the container
traits_list.add_child(container)
# Stock checkbox reference
pin_items.append(checkbox)
func _on_checkbox_toggled(checked: bool, checkbox: int) -> void:
# Manage checkbox signal
pass
func generate_traits(traits_list: Array = traits, chosen_seed: int = 0, chosen_amount: int = 3) -> Dictionary:
# Return a dictionary,
# with trait as key and
# an array with index as first value and boolean as second value
# Inputs:
# chosen_seed for random seed
# chosen_amount for the amount of traits to return
var picked_traits: Dictionary = {}
var traits_duplicate = traits_list.duplicate()
for select in range(chosen_amount):
# Pick a trait first and then choose if it is up or down
var picked_trait:Dictionary = {}
var random = RandomNumberGenerator.new()
random.seed = chosen_seed
random.randomize()
var result: int = random.randi_range(0, traits_duplicate.size() - 1)
var random_value = RandomNumberGenerator.new()
random_value.seed = chosen_seed
random_value.randomize()
var value: bool = random_value.randi_range(0, 1)
# Add the key and array value to the array
picked_traits[traits_duplicate[result]] = [select, value]
# Delete the chosen trait from the possible list
traits_duplicate.pop_at(result)
return picked_traits