summaryrefslogtreecommitdiff
path: root/UI/hud.gd
blob: d886a739cf15200403b85d7a08081904938840bf (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
extends CanvasLayer


var selected_button: TextureRect :
	set(value):
		if selected_button:
			(selected_button.material as ShaderMaterial).set_shader_parameter("adjust", 0.0)
		
		selected_button = value
		(selected_button.material as ShaderMaterial).set_shader_parameter("adjust", -1.0)
		%CurrentButton.text = selected_button.tooltip_text


func _ready() -> void:
	Game.score_changed.connect(func():
		%Score.text = str(Game.score)
	)
	
	Game.speed_changed.connect(func():
		%Speed.text = str(Game.speed).pad_decimals(1) + "x"
	)
	
	create_minimap()


func create_minimap():
	var maps := get_tree().get_nodes_in_group("map")
	var image := Image.create_empty(%Minimap.size.x, $"%Minimap".size.y, false, Image.FORMAT_RGB8)
	var minimap_scale = Vector2(image.get_size()) / get_viewport().get_visible_rect().size
	
	for map: TileMapLayer in maps:
		for cell_coords in map.get_used_cells():
			var tile = map.get_cell_tile_data(cell_coords)
			var tile_scale = minimap_scale * Vector2(map.tile_set.tile_size)
			image.fill_rect(
				Rect2(Vector2(cell_coords) * tile_scale, (tile_scale + Vector2(1,1)).max(Vector2(1, 1))),
				tile.get_custom_data("color")
			)
	
	%Minimap.texture = ImageTexture.create_from_image(image)
	
	var markers := {}
	var timer = Timer.new()
	timer.wait_time = 0.5
	timer.one_shot = false
	timer.autostart = true
	timer.timeout.connect(func():
		for node in markers.keys():
			if is_instance_valid(node):
				markers[node].position = (
					node.global_position * minimap_scale
				)
	)
	%Minimap.add_child(timer)
	get_tree().current_scene.child_entered_tree.connect(func(node: Node):
		if node is Unit:
			var texture = Image.create_empty(1, 1, false, Image.FORMAT_RGB8)
			texture.set_pixel(0, 0, Color("#fff"))
			var sprite = Sprite2D.new()
			sprite.texture = ImageTexture.create_from_image(texture)
			sprite.position = Vector2.ZERO
			%Minimap.add_child(sprite)
			markers[node] = sprite
			
			node.tree_exiting.connect(func():
				markers.erase(node)
				sprite.queue_free()
			)
	)


func _on_button_blocker_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		Game.selected_unit_type = preload("res://Units/Blocker.tscn")
		selected_button = %ButtonBlocker

func _on_button_digger_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		Game.selected_unit_type = preload("res://Units/Digger.tscn")
		selected_button = %ButtonDigger

func _on_button_miner_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		Game.selected_unit_type = preload("res://Units/Miner.tscn")
		selected_button = %ButtonMiner

func _on_button_basher_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		Game.selected_unit_type = preload("res://Units/Basher.tscn")
		selected_button = %ButtonBasher

func _on_button_floater_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		#Game.selected_unit_type = preload("res://Units/Floater.tscn")
		selected_button = %ButtonFloater

func _on_button_builder_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		#Game.selected_unit_type = preload("res://Units/Builder.tscn")
		selected_button = %ButtonBuilder

func _on_button_climber_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		Game.selected_unit_type = preload("res://Units/Climber.tscn")
		selected_button = %ButtonClimber



func _on_button_plus_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		Game.speed = min(Game.speed + 0.5, 5)

func _on_button_minus_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		Game.speed = max(Game.speed - 0.5, 0.5)