storytelling-assistant/traits.gd

141 lines
4 KiB
GDScript3
Raw Normal View History

2024-11-25 17:16:38 +00:00
extends Control
@export var traits:Array = [
"Courage",
"Passion",
2024-11-28 17:06:02 +00:00
"Creativity",
"Temperance",
"Sensuality",
"Energy",
"Responsibility",
"Empathy",
"Self-esteem",
"Flexibility",
"Hope",
"Will"
2024-11-25 17:16:38 +00:00
]
@export var seed: int = 0
2024-11-28 17:06:02 +00:00
@export var amount: int = 3
var pin_items: Array = []
2024-11-25 17:16:38 +00:00
# variables for nodes
2024-11-28 17:06:02 +00:00
@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")
2024-11-25 17:16:38 +00:00
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
2024-11-28 17:06:02 +00:00
build_traits()
func build_traits() -> void:
# Build the traits list with checkboxes to pin any
var list_amount: int = amount
var pinned: Array = []
# First, clear the box 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:
list_amount -= 1
pinned.append([child.get_index(), child])
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()
2024-11-25 17:16:38 +00:00
2024-11-28 17:06:02 +00:00
# generate the amount of traits needed
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]])
for pin in pinned:
char_traits_list.insert(pin[0], pin[1])
# Generate a line for each trait
for traitline in range(amount):
print("At index: ", traitline, " found: ", char_traits_list[traitline])
if char_traits_list[traitline] is HBoxContainer:
print("Je confirme")
print("Parent: ", char_traits_list[traitline].get_parent())
traits_list.add_child(char_traits_list[traitline])
#char_traits_list[traitline].reparent(traits_list)
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
2024-11-25 17:16:38 +00:00
func generate_traits(traits_list: Array = traits, chosen_seed: int = 0, chosen_amount: int = 3) -> Dictionary:
2024-11-28 17:06:02 +00:00
# Return a dictionary,
# with trait as key and
# an array with index as first value and boolean as second value
# Inputs:
2024-11-25 17:16:38 +00:00
# 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)
2024-11-28 17:06:02 +00:00
# Add the key and array value to the array
picked_traits[traits_duplicate[result]] = [select, value]
2024-11-25 17:16:38 +00:00
# Delete the chosen trait from the possible list
traits_duplicate.pop_at(result)
return picked_traits