From 9a2bcf02c2623c8f3e8f5e74e70b3c0333790484 Mon Sep 17 00:00:00 2001
From: David Luevano Alvarado <david@luevano.xyz>
Date: Sat, 4 Jun 2022 10:27:35 -0600
Subject: refactor nodes to work with native scene changer, add functioning
 main menu

---
 src/ui/main_menu/scripts/main_menu.gd   | 81 +++++++++++++++++++++++++++++++++
 src/ui/main_menu/scripts/menu_option.gd | 35 ++++++++++++++
 2 files changed, 116 insertions(+)
 create mode 100644 src/ui/main_menu/scripts/main_menu.gd
 create mode 100644 src/ui/main_menu/scripts/menu_option.gd

(limited to 'src/ui/main_menu/scripts')

diff --git a/src/ui/main_menu/scripts/main_menu.gd b/src/ui/main_menu/scripts/main_menu.gd
new file mode 100644
index 0000000..7077b68
--- /dev/null
+++ b/src/ui/main_menu/scripts/main_menu.gd
@@ -0,0 +1,81 @@
+extends MarginContainer
+
+export(NodePath) var START_OPTION_NP: NodePath
+export(NodePath) var SETTINGS_OPTION_NP: NodePath
+export(NodePath) var EXIT_OPTION_NP: NodePath
+
+onready var start_option: MenuOption = get_node(START_OPTION_NP)
+onready var settings_option: MenuOption = get_node(SETTINGS_OPTION_NP)
+onready var exit_option: MenuOption = get_node(EXIT_OPTION_NP)
+
+onready var main: Node2D = get_parent().get_parent()
+
+
+enum Option {
+	START,
+	SETTINGS,
+	EXIT
+}
+
+enum {
+	NEXT,
+	PREV
+}
+
+onready var options: Dictionary = {
+	Option.START: start_option,
+	Option.SETTINGS: settings_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
+	settings_option.type = Option.SETTINGS
+	exit_option.type = Option.EXIT
+
+
+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.SETTINGS:
+				print("Option TEST.")
+			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)
diff --git a/src/ui/main_menu/scripts/menu_option.gd b/src/ui/main_menu/scripts/menu_option.gd
new file mode 100644
index 0000000..58b4a50
--- /dev/null
+++ b/src/ui/main_menu/scripts/menu_option.gd
@@ -0,0 +1,35 @@
+class_name MenuOption
+extends CenterContainer
+
+export(String) var label_text: String
+export(bool) var selected: bool = false
+
+onready var label: Label = $HBox/Label
+onready var selector: TextureRect = $HBox/Selector
+var type: int
+
+var time_elapsed: float = 0.0
+var timer: float = 0.2
+var last_frame: int = 0
+var frames: Array = [preload("res://ui/main_menu/sprites/selector1.png"),
+					 preload("res://ui/main_menu/sprites/selector2.png"),
+					 preload("res://ui/main_menu/sprites/selector3.png"),
+					 preload("res://ui/main_menu/sprites/selector4.png"),
+					 preload("res://ui/main_menu/sprites/selector5.png")]
+
+
+func _ready():
+	label.text = label_text
+
+
+func _process(delta: float) -> void:
+	if selected:
+		if time_elapsed >= timer:
+			selector.texture = frames[last_frame]
+			last_frame += 1
+			if last_frame == frames.size() - 1:
+				last_frame = 0
+			time_elapsed = 0.0
+		time_elapsed += delta
+	else:
+		selector.texture = frames[4]
-- 
cgit v1.2.3-70-g09d2