storytelling-assistant/traits.gd

144 lines
4.2 KiB
GDScript3
Raw Permalink 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 = []
2024-11-28 17:52:35 +00:00
var pinned_name: Array = []
2024-11-28 17:06:02 +00:00
2024-11-28 17:25:46 +00:00
# Keep track of pinned content
2024-11-28 17:06:02 +00:00
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])
2024-11-28 17:52:35 +00:00
pinned_name.append(child.get_child(1).text)
2024-11-28 17:25:46 +00:00
# Then clear the Hbox for non-pinned traits
2024-11-28 17:06:02 +00:00
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:25:46 +00:00
# Generate the amount of traits needed and store in an array
2024-11-28 17:52:35 +00:00
var char_traits: Dictionary = generate_traits(traits, pinned_name, seed, list_amount)
2024-11-28 17:06:02 +00:00
var char_traits_list: Array = []
for key in char_traits.keys():
char_traits_list.append([key, char_traits[key][0], char_traits[key][1]])
2024-11-28 17:25:46 +00:00
# Insert pinned in their position
2024-11-28 17:06:02 +00:00
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:
2024-11-28 17:25:46 +00:00
# For pinned content
2024-11-28 17:06:02 +00:00
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_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-28 17:52:35 +00:00
func generate_traits(traits_list: Array = traits, pinned_traits: Array = [], 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-28 17:52:35 +00:00
# traits_list for list of traits
# pinned_traits for pinned traits (to prevent doubles)
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 = {}
2024-11-28 17:52:35 +00:00
var traits_duplicate: Array = traits_list.duplicate()
# Remove the one already used
for item in pinned_traits:
traits_duplicate.erase(item)
2024-11-25 17:16:38 +00:00
for select in range(chosen_amount):
# Pick a trait first and then choose if it is up or down
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