Refactor skill management and marketplace UI for improved functionality
This commit is contained in:
@@ -15,16 +15,20 @@ func try_unlock_skill(skill_data: SkillData) -> bool:
|
||||
if not game_manager:
|
||||
return false
|
||||
|
||||
if game_manager.is_skill_unlocked(skill_data.name):
|
||||
if game_manager.is_skill_unlocked(skill_data):
|
||||
return false
|
||||
|
||||
if not has_enough_coins(skill_data.cost):
|
||||
return false
|
||||
|
||||
game_manager.remove_coins(skill_data.cost)
|
||||
game_manager.current_session_state["skills_unlocked"].append(skill_data.name)
|
||||
skill_manager.add_skill(skill_data)
|
||||
skill_unlocked.emit(skill_data)
|
||||
var skill: SkillData = skill_data
|
||||
skill.level = 1
|
||||
skill.is_active = true
|
||||
|
||||
game_manager.remove_coins(skill.cost)
|
||||
game_manager.current_session_state["skills_unlocked"].append(skill)
|
||||
skill_manager.add_skill(skill)
|
||||
skill_unlocked.emit(skill)
|
||||
return true
|
||||
|
||||
|
||||
@@ -36,5 +40,25 @@ func unlock_all_skills() -> void:
|
||||
skills.append(skill.name)
|
||||
skill_unlocked.emit(skill)
|
||||
|
||||
game_manager.unlock_skills(skills)
|
||||
game_manager.unlock_skills(available_skills)
|
||||
skill_manager.apply_unlocked_skills()
|
||||
|
||||
|
||||
|
||||
func try_upgrade_skill(skill_data: SkillData) -> bool:
|
||||
if not game_manager:
|
||||
return false
|
||||
|
||||
if not game_manager.is_skill_unlocked(skill_data):
|
||||
return false
|
||||
|
||||
if skill_data.level >= skill_data.max_level:
|
||||
return false
|
||||
|
||||
if not has_enough_coins(skill_data.cost):
|
||||
return false
|
||||
|
||||
game_manager.remove_coins(skill_data.cost)
|
||||
skill_data.level += 1
|
||||
skill_unlocked.emit(skill_data)
|
||||
return true
|
||||
|
@@ -70,12 +70,16 @@ func console_unlock_skill(skill_name: Variant) -> void:
|
||||
Console.print_error("Invalid skill name: " + str(skill_name))
|
||||
return
|
||||
|
||||
game_manager.unlock_skill(skill_name)
|
||||
var skill_data: SkillData = skill_manager.get_skill_by_name(skill_name)
|
||||
skill_data.level = 1
|
||||
|
||||
if not skill_data:
|
||||
Console.print_error("Skill not found: " + str(skill_name))
|
||||
return
|
||||
skill_manager.add_skill(skill_data)
|
||||
|
||||
game_manager.unlock_skill(skill_data)
|
||||
skill_manager.activate_skill(skill_data)
|
||||
skill_unlocker.skill_unlocked.emit(skill_data)
|
||||
Console.print_info("Unlocked skill: " + str(skill_name))
|
||||
|
||||
|
||||
|
@@ -68,7 +68,6 @@ func get_kid_nodes() -> Array[CollectableComponent]:
|
||||
return kid_nodes
|
||||
|
||||
|
||||
|
||||
func get_player_node() -> Node:
|
||||
for node in nodes_in_scene:
|
||||
if node is PlayerController:
|
||||
@@ -118,23 +117,24 @@ func get_lives() -> int:
|
||||
return player_state["lives"]
|
||||
|
||||
|
||||
func is_skill_unlocked(skill_name: String) -> bool:
|
||||
return skill_name in player_state["unlocked_skills"] or skill_name in current_session_state["skills_unlocked"]
|
||||
|
||||
func is_skill_unlocked(skill: SkillData) -> bool:
|
||||
return skill in player_state["unlocked_skills"] or skill in current_session_state["skills_unlocked"]
|
||||
|
||||
|
||||
func unlock_skill(skill_name: String) -> void:
|
||||
if not is_skill_unlocked(skill_name):
|
||||
player_state["unlocked_skills"].append(skill_name)
|
||||
func unlock_skill(skill: SkillData) -> void:
|
||||
if not is_skill_unlocked(skill):
|
||||
player_state["unlocked_skills"].append(skill)
|
||||
|
||||
|
||||
func remove_skill(skill_name: String) -> void:
|
||||
if is_skill_unlocked(skill_name):
|
||||
player_state["unlocked_skills"].erase(skill_name)
|
||||
func remove_skill(skill: SkillData) -> void:
|
||||
if is_skill_unlocked(skill):
|
||||
player_state["unlocked_skills"].erase(skill)
|
||||
|
||||
|
||||
func unlock_skills(skill_names: Array[String]) -> void:
|
||||
for skill_name in skill_names:
|
||||
unlock_skill(skill_name)
|
||||
func unlock_skills(skills: Array[SkillData]) -> void:
|
||||
for skill in skills:
|
||||
unlock_skill(skill)
|
||||
|
||||
|
||||
func reset_player_state() -> void:
|
||||
@@ -213,8 +213,8 @@ func on_level_complete() -> void:
|
||||
var level_index = player_state["current_level"]
|
||||
mark_level_complete(level_index)
|
||||
add_coins(current_session_state["coins_collected"])
|
||||
for skill_name in current_session_state["skills_unlocked"]:
|
||||
unlock_skill(skill_name)
|
||||
for skill in current_session_state["skills_unlocked"]:
|
||||
unlock_skill(skill)
|
||||
|
||||
reset_current_session_state()
|
||||
try_to_go_to_next_level()
|
||||
@@ -228,5 +228,4 @@ func get_unlocked_skills() -> Array:
|
||||
return skills_unlocked
|
||||
if not skills_unlocked:
|
||||
return skills_current_session
|
||||
|
||||
return skills_unlocked + skills_current_session
|
||||
|
@@ -21,11 +21,10 @@ func add_skill(skill_data: SkillData) -> void:
|
||||
for skill in unlocked_skills:
|
||||
var data = null
|
||||
for s in available_skills:
|
||||
if s.name == skill:
|
||||
if s == skill:
|
||||
data = s
|
||||
break
|
||||
if data and data.type == SkillData.SkillType.THROW:
|
||||
print("Removing previous throw skill: ", data.name)
|
||||
remove_skill(data.name)
|
||||
|
||||
var skill_instance := skill_data.node.instantiate()
|
||||
@@ -46,7 +45,7 @@ func add_skill(skill_data: SkillData) -> void:
|
||||
|
||||
owner.add_child(skill_instance)
|
||||
active_components[skill_data.name] = skill_instance
|
||||
|
||||
|
||||
|
||||
func remove_skill(skill_name: String) -> void:
|
||||
if not active_components.has(skill_name):
|
||||
@@ -56,12 +55,17 @@ func remove_skill(skill_name: String) -> void:
|
||||
if is_instance_valid(skill_instance):
|
||||
skill_instance.queue_free()
|
||||
|
||||
var skills: Array = gm.get_unlocked_skills()
|
||||
for s in skills:
|
||||
if s.name == skill_name:
|
||||
s.is_active = false
|
||||
break
|
||||
active_components.erase(skill_name)
|
||||
|
||||
|
||||
func apply_unlocked_skills() -> void:
|
||||
for skill_data in available_skills:
|
||||
if gm.is_skill_unlocked(skill_data.name):
|
||||
if gm.is_skill_unlocked(skill_data):
|
||||
print("Applying skill: ", skill_data.name)
|
||||
call_deferred("add_skill", skill_data)
|
||||
else:
|
||||
@@ -73,3 +77,27 @@ func get_skill_by_name(skill_name: String) -> SkillData:
|
||||
if skill_data.name == skill_name:
|
||||
return skill_data
|
||||
return null
|
||||
|
||||
|
||||
func activate_skill(skill: SkillData) -> void:
|
||||
if not active_components.has(skill.name):
|
||||
if skill:
|
||||
add_skill(skill)
|
||||
skill.is_active = true
|
||||
|
||||
|
||||
func deactivate_skill(skill: SkillData) -> void:
|
||||
if active_components.has(skill.name):
|
||||
remove_skill(skill.name)
|
||||
if skill:
|
||||
skill.is_active = false
|
||||
|
||||
|
||||
func toggle_skill_activation(skill: SkillData) -> void:
|
||||
if not skill:
|
||||
return
|
||||
|
||||
if active_components.has(skill.name):
|
||||
deactivate_skill(skill)
|
||||
else:
|
||||
activate_skill(skill)
|
||||
|
@@ -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()
|
||||
|
||||
|
22
scripts/ui/skill_button.gd
Normal file
22
scripts/ui/skill_button.gd
Normal 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"))
|
||||
|
||||
|
1
scripts/ui/skill_button.gd.uid
Normal file
1
scripts/ui/skill_button.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://0obbehfd8fki
|
Reference in New Issue
Block a user