summaryrefslogtreecommitdiff
path: root/src/ui/main_menu/scripts/main_menu.gd
blob: bb96d935c34e14f8ebd0614d6208a54fea762905 (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
extends MarginContainer

export(NodePath) var START_OPTION_NP: NodePath
export(NodePath) var EXIT_OPTION_NP: NodePath
export(NodePath) var ANIMATION_NP: NodePath

onready var start_option: MenuOption = get_node(START_OPTION_NP)
onready var exit_option: MenuOption = get_node(EXIT_OPTION_NP)
onready var animation: AnimatedSprite = get_node(ANIMATION_NP)


enum Option {
	START,
	EXIT
}

enum {
	NEXT,
	PREV
}

onready var options: Dictionary = {
	Option.START: start_option,
	Option.EXIT: exit_option
}

var current_selection: int = Option.START


func _ready():
	Event.connect("game_start", self, "_on_game_start")
	start_option.type = Option.START
	exit_option.type = Option.EXIT
	animation.play()


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_down"):
		_menu_selection(NEXT)
	elif event.is_action_pressed("ui_up"):
		_menu_selection(PREV)


	if event.is_action_pressed("ui_accept"):
		match current_selection:
			Option.START:
				Event.emit_signal("game_start")
			Option.EXIT:
				get_tree().quit()


func _menu_selection(selection: int) -> void:
	match selection:
		NEXT:
			current_selection += 1
			if current_selection == Option.size():
				current_selection = 0
		PREV:
			current_selection -= 1
			if current_selection == -1:
				current_selection = Option.size() - 1
	_update_options()


func _update_options() -> void:
	for i in options:
		if i == current_selection:
			options[i].selected = true
		else:
			options[i].selected = false


func _on_game_start() -> void:
	print("game_start")
	get_tree().change_scene_to(Global.GAME_NODE)