summaryrefslogtreecommitdiff
path: root/UI/LevelSelect.gd
blob: dae69bb37a9d67ea5156fca230b2528ab5711af6 (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
extends Control


var selected_idx = 0


func _ready():
	Global.connect("level_map_updated", Callable(self, "draw"))
	draw()


func _input(event):
	if event.is_action_pressed("DIRECTION_LEFT") and event.get_action_strength("DIRECTION_LEFT") == 1.0:
		selected_idx = max(selected_idx - 1, 0)
		set_cell_selected()
	elif event.is_action_pressed("DIRECTION_RIGHT") and event.get_action_strength("DIRECTION_RIGHT") == 1.0:
		selected_idx = min(selected_idx + 1, Global.Level_Map.size() - 1)
		set_cell_selected()
	elif event.is_action_pressed("UP") and (selected_idx - 3 >= 0) and event.get_action_strength("UP") == 1.0:
		selected_idx = max(selected_idx - 3, 0)
		set_cell_selected()
	elif event.is_action_pressed("DOWN") and (selected_idx + 3 <= Global.Level_Map.size() - 1) and event.get_action_strength("DOWN") == 1.0:
		selected_idx = min(selected_idx + 3, Global.Level_Map.size() - 1)
		set_cell_selected()
	elif event.is_action_pressed("ACCEPT"):
		Global.start_level(selected_idx)


func draw():
	var Cell = load("res://UI/LevelSelectCell.tscn")
	
	for idx in range(Global.Level_Map.size()): # size = 9
		var cell = Cell.instantiate()
		cell.level_idx = idx
		
		if idx == selected_idx:
			cell.set_selected(true)
		
		cell.set_rect_size(
			get_viewport_rect().size.x/3,
			get_viewport_rect().size.y/3
		)
		
		var column = idx % 3
		var row = idx / 3
		cell.position.x = cell.size.x*column
		cell.position.y = cell.size.y*row
		
		cell.connect("gui_input", Callable(self, '_button_pressed').bind(idx))
		
		self.add_child(cell)


func set_cell_selected():
	var cells = get_children()
	for cell in cells:
		cell.set_selected(false)
	
	cells[selected_idx].set_selected(true)


func _button_pressed(event, idx):
	if event is InputEventMouseButton or event is InputEventKey:
		if event.pressed:
			Global.start_level(idx)