brackeys-12/ui/building_menu.gd
2024-09-15 00:22:02 -04:00

80 lines
2.1 KiB
GDScript

extends Control
@onready var tabs = [
$HBoxContainer/BuildingTab, $HBoxContainer/BuildingTab2, $HBoxContainer/BuildingTab3
]
@onready var building_data = Data.data.buildings
@onready var buildings = building_data.keys()
@onready var v_box_container: HBoxContainer = $Buildings/VBoxContainer
@onready var icons = [
$Buildings/VBoxContainer/BuildingIcon,
$Buildings/VBoxContainer/BuildingIcon2,
$Buildings/VBoxContainer/BuildingIcon3,
$Buildings/VBoxContainer/BuildingIcon4
]
@onready var spacers = [
$Buildings/VBoxContainer/Spacer,
$Buildings/VBoxContainer/Spacer2,
$Buildings/VBoxContainer/Spacer3
]
func _ready() -> void:
swap_to_tab(0)
Triggerer.listen("stone", _trigger_check)
Triggerer.listen("wood", _trigger_check)
func swap_to_tab(index):
for i in range(0, tabs.size()):
if i == index:
tabs[i].select()
else:
tabs[i].unselect()
var filtered_buildings = buildings.filter(func(building): return building_data[building].type == tabs[index].key and (not building_data[building].has("disabled") or not building_data[building].disabled))
for i in range(0, icons.size()):
if i < filtered_buildings.size():
icons[i].visible = true
icons[i].key = filtered_buildings[i]
icons[i].texture_rect.texture = Data.data.images[filtered_buildings[i]]
if i > 0:
spacers[i - 1].visible = true
else:
icons[i].visible = false
if i > 0:
spacers[i - 1].visible = false
_update_icons()
func _trigger_check(_data):
_update_icons()
func _update_icons():
for i in range(0, icons.size()):
if icons[i].visible:
var key = icons[i].key
var build_data = Data.data.buildings[key]
if (build_data.cost.has("stone") and Persister.get_value("stone", PersisterEnums.Scope.UNKNOWN, 0) < int(build_data.cost.stone)) or (build_data.cost.has("wood") and Persister.get_value("wood", PersisterEnums.Scope.UNKNOWN, 0) < int(build_data.cost.wood)):
icons[i].modulate = Color(1, 0.5, 0.5, 0.5)
else:
icons[i].modulate = Color(1, 1, 1, 1)
func _on_building_tab_clicked() -> void:
swap_to_tab(0)
func _on_building_tab_2_clicked() -> void:
swap_to_tab(1)
func _on_building_tab_3_clicked() -> void:
swap_to_tab(2)