aboutsummaryrefslogtreecommitdiff
path: root/scripts/gui_turret_detail.gd
blob: c1e0519ee45c2fa2e42f397a9c8b71e4ed677b2a (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
extends PanelContainer

var hbox_labels 
var hbox_global
var _turret_holder : Node
var load_turrets : Node
var resources : Node

var gui : Control

func _fetch ():
	if gui == null: gui = get_parent()
	if load_turrets != null: return;
	
	var root = get_tree().root.get_node("world")
	_turret_holder = root.get_node("turrets")
	
	hbox_labels = $"vbox/hbox_global/hbox_labels"
	hbox_global = $"vbox/hbox_global"
	
	resources = root.get_node("player").get_node("resources")
	
	load_turrets = root.get_node("saveload").get_node("load_turrets")
	if !load_turrets.loaded: yield(load_turrets, "done_loading")
	
	
func flatten (dict : Dictionary):
	var flat = {}
	for k in dict:
		if dict[k] is Dictionary:
			for kk in dict[k]:
				flat[k+" "+kk] = dict[k][kk]
		else: flat[k] = dict[k]
	return flat

func refresh (turret_info : Dictionary, comp : Dictionary = {}):
	_fetch()
	
	get_node("vbox").get_node("name_label").text = "name: " + turret_info.name

	var base_labels = hbox_global.get_node("hbox_labels")
	var base_values = hbox_global.get_node("hbox_values")
	var base_cmp = hbox_global.get_node("hbox_cmp")
	for child in base_labels.get_children(): child.queue_free()
	for child in base_values.get_children(): child.queue_free()
	for child in base_cmp.get_children(): child.queue_free()
	
	var flat = flatten(turret_info)
	var flat_comp = flatten(comp)
	
	var skip = ["upgrades"]
	for k in flat:
		if k in skip or "name" in k: continue
		
		var label_lab = Label.new() 
		label_lab.text = k
		base_labels.add_child(label_lab)
		
		var val = str(flat[k])
		var label_val = Label.new() 
		label_val.text = val
		base_values.add_child(label_val)
		
		var cmp_val = str(flat_comp.get(k, ""))
		if (cmp_val == val): cmp_val = ""
		var label_cmp = Label.new() 
		label_cmp.text = cmp_val
		base_cmp.add_child(label_cmp)

func _process(delta):
	_fetch()
	
	var info = null;
	var comp = {};
	
	var hovering = null
	if gui.control.state == Globals.PlayerState.PICK and \
			gui.control.statetype == Globals.StateType.TURRET and \
			gui.bottom_bar.picker.hovering != "":
		hovering = gui.bottom_bar.picker.hovering
		
	var highlight = null
	if gui.control.state == Globals.PlayerState.EDIT and \
			(gui.control.statetype == Globals.StateType.TURRET or \
			gui.control.statetype == Globals.StateType.MODULES or \
			gui.control.statetype == Globals.StateType.MODULES_PICK):
		var turret_name = gui.control.editing_turret
		highlight = _turret_holder.get_node(turret_name)
		
	var placing = null
	if gui.control.state == Globals.PlayerState.PLACE and \
			gui.control.statetype == Globals.StateType.TURRET and \
			gui.control.selected != "":
		placing = gui.control.selected
		
	if highlight != null:
		info = highlight.info_mod
		
		var opts = ["back", "targeting", "modules", "sell", "add"]
		if gui.control.state == Globals.PlayerState.EDIT and \
				gui.control.statetype == Globals.StateType.TURRET and \
				gui.bottom_bar.picker.hovering != "" and \
				not gui.bottom_bar.picker.hovering in opts:
			comp = load_turrets.info[gui.bottom_bar.picker.hovering]
			
		elif gui.control.state == Globals.PlayerState.EDIT and \
				gui.control.statetype == Globals.StateType.MODULES_PICK and \
				gui.bottom_bar.picker.hovering != "" and \
				not gui.bottom_bar.picker.hovering in opts:
			comp = highlight.make_info_mod(highlight.modules + [gui.bottom_bar.picker.hovering])
			
	elif placing != null:
		info = load_turrets.info[placing]
	elif hovering != null:
		info = load_turrets.info[hovering]
	elif gui.player.placer.colliding \
			and "turrets" in gui.player.placer.colliding_group:
		info = gui.player.placer.colliding_node.info
		
	if info != null: 
		refresh(info, comp)
		self.visible = true
	else: self.visible = false