aboutsummaryrefslogtreecommitdiff
path: root/scripts/gui_turret.gd
blob: 55e3f5afb6ef2060384f2956aa029f53d90199ec (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
extends TextureButton

var option : String

var info : Dictionary

var _color_rect : ColorRect
var _name_label : Label
var _cash_label : Label

var _viewport : Viewport
var _spinner : Node
var _mesh : Node

var _resources : Node

var picked : bool
var enabled : bool = false

var _rotate_timer : float

func init (turret_name : String):
	var root = get_tree().root.get_node("world")
	var saveload = root.get_node("saveload")
	var load_turrets = saveload.get_node("load_turrets")
	if !load_turrets.loaded: yield(load_turrets, "done_loading")
	info = load_turrets.info[turret_name]
	
	_color_rect = $ColorRect
	_name_label = $hbox/name_label
	_cash_label = $cash_label
	_viewport = $viewport
	_spinner = $viewport/spinner
	
	_resources = root.get_node("player").get_node("resources")
	
	_name_label.text = turret_name
	_cash_label.text = _resources.dict_to_str(info.cost)
	
	var model = load_turrets.models[info.model_name]
	_mesh = model.instance()
	_spinner.add_child(_mesh)
	
	texture_normal = _viewport.get_texture()
	
func _process(delta):
	if !has_node("viewport"): return
	if _spinner == null: _spinner = $viewport/spinner
	if _spinner != null:
		if _resources == null:
			var root = get_tree().root.get_node("world")
			_resources = root.get_node("player").get_node("resources")
		
		var afforded = _resources.greater_than(info.cost)
		if afforded: _color_rect.color = Color(0,0,0,0)
		else: _color_rect.color = Color(0,0,0,0.5)
			
		if afforded and (is_hovered() or picked):
			_spinner.transform.basis = _spinner.transform.rotated(Vector3.UP, delta).basis
			_rotate_timer = 0
		else:
			_rotate_timer = min(1, _rotate_timer + delta * 5)
			var idle_rot = Transform.rotated(Vector3.UP, PI-PI/4).basis
			_spinner.transform.basis = _spinner.transform.basis.slerp(idle_rot, _rotate_timer)