Refactor skill management and marketplace UI for improved functionality

This commit is contained in:
2025-06-19 16:37:31 +02:00
parent d73931dad4
commit 58eeef60dc
10 changed files with 202 additions and 45 deletions

View File

@@ -3,15 +3,19 @@ extends Node
@export var root: Control
@export var skill_data: Array[SkillData] = []
@export var grid: GridContainer
@export var to_unlock_grid: GridContainer
@export var unlocked_grid: GridContainer
@export var font: Font
@export var skill_unlocker: SkillUnlockerComponent
@export var components_to_disable: Array[Node] = []
@export var marketplace_button: PackedScene
@export var skill_button: PackedScene
@onready var game_manager: GM = $"/root/GameManager"
var buttons: Array[Button] = []
var unlock_buttons: Array[Button] = []
var skill_buttons: Array[SkillButton] = []
func _ready() -> void:
@@ -21,13 +25,17 @@ func _ready() -> void:
var skills_to_unlock: Array[SkillData] = []
for skill in skill_data:
if skill.name in game_manager.player_state['unlocked_skills']:
continue
skills_to_unlock.append(skill)
for skill in skills_to_unlock:
create_upgrade_button(skill)
var unlocked_skills := game_manager.get_unlocked_skills()
for skill in unlocked_skills:
create_skill_button(skill)
skill_unlocker.skill_unlocked.connect(on_skill_unlocked)
func _input(event: InputEvent) -> void:
if event.is_action_pressed("show_marketplace"):
@@ -45,7 +53,8 @@ func get_button_text(skill: SkillData) -> String:
return tr(skill.name) + " " + str(skill.cost)
func create_upgrade_button(skill: SkillData):
func create_upgrade_button(skill: SkillData) -> void:
var button := marketplace_button.instantiate() as MarketplaceButton
button.text = get_button_text(skill)
button.icon = skill.icon
@@ -53,13 +62,25 @@ func create_upgrade_button(skill: SkillData):
button.pressed.connect(func () -> void: _on_button_pressed(skill))
buttons.append(button)
grid.add_child(button)
grid.queue_sort()
unlock_buttons.append(button)
to_unlock_grid.add_child(button)
to_unlock_grid.queue_sort()
func create_skill_button(skill: SkillData) -> void:
var button := skill_button.instantiate() as SkillButton
button.skill_data = skill
button.setup()
button.pressed.connect(func() -> void: on_skill_button_pressed(button))
button.activate()
skill_buttons.append(button)
unlocked_grid.add_child(button)
unlocked_grid.queue_sort()
func remove_button(skill: SkillData):
for child in grid.get_children():
for child in to_unlock_grid.get_children():
if child.text == get_button_text(skill):
child.queue_free()
break
@@ -69,6 +90,38 @@ func _on_button_pressed(skill: SkillData) -> void:
if not skill_unlocker:
return
if skill_unlocker.try_unlock_skill(skill):
remove_button(skill)
if game_manager.is_skill_unlocked(skill):
if skill.level < skill.max_level:
skill_unlocker.try_upgrade_skill(skill)
if not skill.is_active:
skill_unlocker.skill_manager.toggle_skill_activation(skill)
else:
skill_unlocker.skill_manager.toggle_skill_activation(skill)
else:
skill_unlocker.try_unlock_skill(skill)
func on_skill_unlocked(skill: SkillData) -> void:
# need to fix this method
if not skill:
return
if skill_buttons.size() == 0:
create_skill_button(skill)
for button in skill_buttons:
if button.skill_data.is_active:
button.activate()
else:
button.deactivate()
func on_skill_button_pressed(button: SkillButton) -> void:
if not skill_unlocker or not button.skill_data:
return
skill_unlocker.skill_manager.toggle_skill_activation(button.skill_data)
button.activate()
for other_button in skill_buttons:
if other_button != button:
other_button.deactivate()

View File

@@ -0,0 +1,22 @@
class_name SkillButton
extends Button
@export var skill_data: SkillData
func setup() -> void:
if not skill_data:
return
icon = skill_data.icon
text = tr(skill_data.name)
func activate() -> void:
set("theme_override_colors/font_color", Color("#49aa10"))
func deactivate() -> void:
set("theme_override_colors/font_color", Color("#ffffff"))

View File

@@ -0,0 +1 @@
uid://0obbehfd8fki