From 0a88bc0db9304f61f177f6f7a4c6fcad1907a514 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 13 Apr 2024 04:04:43 +0200 Subject: [PATCH] Add a basic main menu --- main.gd | 56 +++++++++++++++++++++++++++++++++ main.tscn | 4 +-- scenes/interface/main_menu.gd | 2 ++ scenes/interface/main_menu.tscn | 39 +++++++++++++++++++++++ 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 scenes/interface/main_menu.gd create mode 100644 scenes/interface/main_menu.tscn diff --git a/main.gd b/main.gd index dd20c28..333eefb 100644 --- a/main.gd +++ b/main.gd @@ -1,2 +1,58 @@ extends Node class_name Main + + +@onready var container: Control = $"PrimaryCanvas/SafeMarginContainer" + + +## The possible states the game can be in. +enum Stage { + NONE = 0, + MENU = 1, +} + +## The [Stage] the game is currently in. +## +## Changing this will automatically destroy and build the relevant scenes. +var current_stage: Stage: + get: + return current_stage + set(value): + # Do not rebuild scenes if the stage is set to the value it's already set to + if value == current_stage: + return + # Destroy the current scenes + match current_stage: + Stage.MENU: + destroy_menu() + # Update the current stage + current_stage = value + # Build the next scenes + match current_stage: + Stage.MENU: + build_menu() + +## The [Stage] that [field current_stage] should be set to upon starting the game. +@export var starting_stage: Stage + + +## The main menu scene. +const SCENE_MENU: PackedScene = preload("res://scenes/interface/main_menu.tscn") + +## The main menu node. +var scene_menu: MainMenu = null + +## Destroy the main menu. +func destroy_menu() -> void: + scene_menu.queue_free() + scene_menu = null + +## Build the main menu. +func build_menu() -> void: + scene_menu = SCENE_MENU.instantiate() + container.add_child(scene_menu) + + + +func _ready() -> void: + current_stage = starting_stage diff --git a/main.tscn b/main.tscn index ce73e42..4f1fcf6 100644 --- a/main.tscn +++ b/main.tscn @@ -5,6 +5,7 @@ [node name="Main" type="Node"] script = ExtResource("1_jyg3q") +starting_stage = 1 [node name="PrimaryCanvas" type="CanvasLayer" parent="."] @@ -13,6 +14,3 @@ min_margin_left = 16 min_margin_right = 16 min_margin_top = 16 min_margin_bottom = 16 - -[node name="Panel" type="Panel" parent="PrimaryCanvas/SafeMarginContainer"] -layout_mode = 2 diff --git a/scenes/interface/main_menu.gd b/scenes/interface/main_menu.gd new file mode 100644 index 0000000..c9d8a2d --- /dev/null +++ b/scenes/interface/main_menu.gd @@ -0,0 +1,2 @@ +extends Control +class_name MainMenu diff --git a/scenes/interface/main_menu.tscn b/scenes/interface/main_menu.tscn new file mode 100644 index 0000000..2e76081 --- /dev/null +++ b/scenes/interface/main_menu.tscn @@ -0,0 +1,39 @@ +[gd_scene load_steps=2 format=3 uid="uid://3yqvhxeq03rk"] + +[ext_resource type="Script" path="res://scenes/interface/main_menu.gd" id="1_jkswn"] + +[node name="MainMenu" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_jkswn") + +[node name="Panel" type="Panel" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="Layout" type="VBoxContainer" parent="Panel"] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +alignment = 1 + +[node name="RichTextLabel" type="RichTextLabel" parent="Panel/Layout"] +layout_mode = 2 +size_flags_vertical = 6 +bbcode_enabled = true +text = "[center][font_size=144px]Garasauto[/font_size] +[font_size=64px]Il casello del destino[/font_size] + +[font_size=32px]Un gioco del Garasautomobileclub Italia[/font_size][/center]" +fit_content = true