-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpgradePanel.gd
58 lines (47 loc) · 1.97 KB
/
UpgradePanel.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
extends Control
@onready var upgrade_choice_scene = load("res://upgrade.tscn")
@onready var upgrades_container = %UpgradesContainer
var upgrade_choices = []
var num_upgrades = 4
func _ready():
Console.add_command("upgrade", func(upgrade_name): add_upgrade(load("res://data/upgrades/%s.tres" % upgrade_name), UpgradeType.new()), 1)
Console.add_command("su", open_and_pause)
for i in num_upgrades:
var upgrade_choice = upgrade_choice_scene.instantiate()
upgrades_container.add_child(upgrade_choice)
upgrade_choices.append(upgrade_choice)
upgrade_choice.chosen.connect(upgrade_chosen)
close_and_resume()
func leveled_up():
open_and_pause()
func upgrade_chosen(upgrade):
add_upgrade(upgrade.upgrade, upgrade.upgrade_type)
close_and_resume()
func close_and_resume():
TimeManager.unpause_tree()
hide()
func open_and_pause():
await get_tree().create_timer(0.7).timeout
TimeManager.pause_tree()
show()
var upgrades = get_available_upgrades()
for i in num_upgrades:
var random = randi_range(0, upgrades.size() - 1)
var upgrade_type = upgrades.keys()[random]
upgrade_choices[i].set_upgrade({upgrade_type = upgrade_type, upgrade = upgrades[upgrade_type]})
upgrades.erase(upgrade_type)
var upgrades_by_type = {}
@onready var upgrade_types: UpgradeTypes = load("res://data/upgrades/upgrade_types.tres")
func get_available_upgrades():
var available_types = {}
for type in upgrade_types.types:
if not upgrades_by_type.has(type):
available_types[type] = type.upgrades[0]
elif upgrades_by_type[type].size() < type.upgrades.size():
available_types[type] = type.upgrades[upgrades_by_type[type].size()]
return available_types
func add_upgrade(upgrade, upgrade_type):
if not upgrades_by_type.has(upgrade_type):
upgrades_by_type[upgrade_type] = []
upgrades_by_type[upgrade_type].append(upgrade)
EventBus.upgrade_added.emit(upgrades_by_type)