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

var _turret : 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 : Dictionary):
	_turret = turret
	_color_rect = $ColorRect
	_name_label = $hbox/name_label
	_cash_label = $cash_label
	_viewport = $viewport
	_spinner = $viewport/spinner
	
	var root = get_tree().root.get_child(0)
	_resources = root.get_node("player").get_node("resources")
	
	_name_label.text = turret.name
	_cash_label.text = _resources.dict_to_str(turret.cost)
	
	var load_turrets = root.get_node("saveload").get_node("load_turrets")
	if !load_turrets.loaded: yield(load_turrets, "done_loading")
	var model = load_turrets.models[turret.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_child(0)
			_resources = root.get_node("player").get_node("resources")
		
		var afforded = _resources.greater_than(_turret.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)