Basic game template addon
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
class_name MainMenu
|
||||
extends Control
|
||||
## Base menu scene that links to a game scene, an options menu, and credits.
|
||||
|
||||
signal sub_menu_opened
|
||||
signal sub_menu_closed
|
||||
signal game_started
|
||||
signal game_exited
|
||||
|
||||
## Defines the path to the game scene. Hides the play button if empty.
|
||||
@export_file("*.tscn") var game_scene_path : String
|
||||
## The scene to open when a player clicks the 'Options' button.
|
||||
@export var options_packed_scene : PackedScene
|
||||
## The scene to open when a player clicks the 'Credits' button.
|
||||
@export var credits_packed_scene : PackedScene
|
||||
@export var confirm_exit : bool = true
|
||||
@export_group("Extra Settings")
|
||||
## If true, signals that the game has started loading in the background, instead of directly loading it.
|
||||
## Requires Maaack's Scene Loader.
|
||||
@export var signal_game_start : bool = false
|
||||
## If true, signals that the player clicked the 'Exit' button, instead of immediately exiting.
|
||||
@export var signal_game_exit : bool = false
|
||||
|
||||
var sub_menu : Control
|
||||
|
||||
@onready var menu_container = %MenuContainer
|
||||
@onready var menu_buttons_box_container = %MenuButtonsBoxContainer
|
||||
@onready var new_game_button = %NewGameButton
|
||||
@onready var options_button = %OptionsButton
|
||||
@onready var credits_button = %CreditsButton
|
||||
@onready var exit_button = %ExitButton
|
||||
@onready var exit_confirmation = %ExitConfirmation
|
||||
## If Maaack's Scene Loader is installed, then it will be used to change scenes.
|
||||
@onready var scene_loader_node = get_tree().root.get_node_or_null(^"SceneLoader")
|
||||
|
||||
func get_game_scene_path() -> String:
|
||||
return game_scene_path
|
||||
|
||||
func load_game_scene() -> void:
|
||||
if scene_loader_node:
|
||||
if signal_game_start:
|
||||
scene_loader_node.load_scene(get_game_scene_path(), true)
|
||||
game_started.emit()
|
||||
else:
|
||||
scene_loader_node.load_scene(get_game_scene_path())
|
||||
else:
|
||||
get_tree().change_scene_to_file(get_game_scene_path())
|
||||
|
||||
func new_game() -> void:
|
||||
load_game_scene()
|
||||
|
||||
func try_exit_game() -> void:
|
||||
if confirm_exit and (not exit_confirmation.visible):
|
||||
exit_confirmation.show()
|
||||
else:
|
||||
exit_game()
|
||||
|
||||
func exit_game() -> void:
|
||||
if OS.has_feature("web"):
|
||||
return
|
||||
if signal_game_exit:
|
||||
game_exited.emit()
|
||||
else:
|
||||
get_tree().quit()
|
||||
|
||||
func _open_sub_menu(menu : PackedScene) -> Node:
|
||||
sub_menu = menu.instantiate()
|
||||
add_child(sub_menu)
|
||||
menu_container.hide()
|
||||
sub_menu.hidden.connect(_close_sub_menu, CONNECT_ONE_SHOT)
|
||||
sub_menu.tree_exiting.connect(_close_sub_menu, CONNECT_ONE_SHOT)
|
||||
sub_menu_opened.emit()
|
||||
return sub_menu
|
||||
|
||||
func _close_sub_menu() -> void:
|
||||
if sub_menu == null:
|
||||
return
|
||||
sub_menu.queue_free()
|
||||
sub_menu = null
|
||||
menu_container.show()
|
||||
sub_menu_closed.emit()
|
||||
|
||||
func _event_is_mouse_button_released(event : InputEvent) -> bool:
|
||||
return event is InputEventMouseButton and not event.is_pressed()
|
||||
|
||||
func _input(event : InputEvent) -> void:
|
||||
if event.is_action_released("ui_cancel"):
|
||||
if sub_menu:
|
||||
_close_sub_menu()
|
||||
else:
|
||||
try_exit_game()
|
||||
if event.is_action_released("ui_accept") and get_viewport().gui_get_focus_owner() == null:
|
||||
menu_buttons_box_container.focus_first()
|
||||
|
||||
func _hide_exit_for_web() -> void:
|
||||
if OS.has_feature("web"):
|
||||
exit_button.hide()
|
||||
|
||||
func _hide_new_game_if_unset() -> void:
|
||||
if get_game_scene_path().is_empty():
|
||||
new_game_button.hide()
|
||||
|
||||
func _hide_options_if_unset() -> void:
|
||||
if options_packed_scene == null:
|
||||
options_button.hide()
|
||||
|
||||
func _hide_credits_if_unset() -> void:
|
||||
if credits_packed_scene == null:
|
||||
credits_button.hide()
|
||||
|
||||
func _ready() -> void:
|
||||
_hide_exit_for_web()
|
||||
_hide_options_if_unset()
|
||||
_hide_credits_if_unset()
|
||||
_hide_new_game_if_unset()
|
||||
|
||||
func _on_new_game_button_pressed() -> void:
|
||||
new_game()
|
||||
|
||||
func _on_options_button_pressed() -> void:
|
||||
_open_sub_menu(options_packed_scene)
|
||||
|
||||
func _on_credits_button_pressed() -> void:
|
||||
_open_sub_menu(credits_packed_scene)
|
||||
|
||||
func _on_exit_button_pressed() -> void:
|
||||
try_exit_game()
|
||||
|
||||
func _on_exit_confirmation_confirmed():
|
||||
exit_game()
|
||||
@@ -0,0 +1 @@
|
||||
uid://bhgs1upaahk3y
|
||||
@@ -0,0 +1,175 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://c6k5nnpbypshi"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bhgs1upaahk3y" path="res://addons/maaacks_game_template/base/nodes/menus/main_menu/main_menu.gd" id="1"]
|
||||
[ext_resource type="Script" uid="uid://1nf36h0gms3q" path="res://addons/maaacks_game_template/base/nodes/utilities/capture_focus.gd" id="4_l1ebe"]
|
||||
[ext_resource type="Script" uid="uid://dmkubt2nsnsbn" path="res://addons/maaacks_game_template/base/nodes/labels/config_version_label.gd" id="6_pdiij"]
|
||||
[ext_resource type="PackedScene" uid="uid://cwt4p3bufkke5" path="res://addons/maaacks_game_template/base/nodes/windows/confirmation_overlaid_window.tscn" id="7_im16j"]
|
||||
[ext_resource type="Script" uid="uid://bkwlopi4qn32o" path="res://addons/maaacks_game_template/base/nodes/labels/config_name_label.gd" id="7_j7612"]
|
||||
|
||||
[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")
|
||||
|
||||
[node name="BackgroundTextureRect" type="TextureRect" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
expand_mode = 1
|
||||
stretch_mode = 5
|
||||
|
||||
[node name="MenuContainer" type="MarginContainer" parent="."]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="TitleMargin" type="MarginContainer" parent="MenuContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_top = 24
|
||||
|
||||
[node name="TitleContainer" type="Control" parent="MenuContainer/TitleMargin"]
|
||||
layout_mode = 2
|
||||
mouse_filter = 2
|
||||
|
||||
[node name="TitleLabel" type="Label" parent="MenuContainer/TitleMargin/TitleContainer"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 10
|
||||
anchor_right = 1.0
|
||||
offset_bottom = 67.0
|
||||
grow_horizontal = 2
|
||||
theme_override_font_sizes/font_size = 48
|
||||
text = "Title"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
script = ExtResource("7_j7612")
|
||||
|
||||
[node name="SubTitleMargin" type="MarginContainer" parent="MenuContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_top = 92
|
||||
|
||||
[node name="SubTitleContainer" type="Control" parent="MenuContainer/SubTitleMargin"]
|
||||
layout_mode = 2
|
||||
mouse_filter = 2
|
||||
|
||||
[node name="SubTitleLabel" type="Label" parent="MenuContainer/SubTitleMargin/SubTitleContainer"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 10
|
||||
anchor_right = 1.0
|
||||
offset_bottom = 34.0
|
||||
grow_horizontal = 2
|
||||
theme_override_font_sizes/font_size = 24
|
||||
text = "Subtitle"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
|
||||
[node name="MenuButtonsMargin" type="MarginContainer" parent="MenuContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
theme_override_constants/margin_top = 136
|
||||
theme_override_constants/margin_bottom = 8
|
||||
|
||||
[node name="MenuButtonsContainer" type="Control" parent="MenuContainer/MenuButtonsMargin"]
|
||||
layout_mode = 2
|
||||
mouse_filter = 2
|
||||
|
||||
[node name="MenuButtonsBoxContainer" type="BoxContainer" parent="MenuContainer/MenuButtonsMargin/MenuButtonsContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -64.0
|
||||
offset_top = -104.0
|
||||
offset_right = 64.0
|
||||
offset_bottom = 104.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
size_flags_horizontal = 4
|
||||
theme_override_constants/separation = 16
|
||||
alignment = 1
|
||||
vertical = true
|
||||
script = ExtResource("4_l1ebe")
|
||||
|
||||
[node name="NewGameButton" type="Button" parent="MenuContainer/MenuButtonsMargin/MenuButtonsContainer/MenuButtonsBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "New Game"
|
||||
|
||||
[node name="OptionsButton" type="Button" parent="MenuContainer/MenuButtonsMargin/MenuButtonsContainer/MenuButtonsBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "Options"
|
||||
|
||||
[node name="CreditsButton" type="Button" parent="MenuContainer/MenuButtonsMargin/MenuButtonsContainer/MenuButtonsBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "Credits"
|
||||
|
||||
[node name="ExitButton" type="Button" parent="MenuContainer/MenuButtonsMargin/MenuButtonsContainer/MenuButtonsBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "Exit"
|
||||
|
||||
[node name="VersionMargin" type="MarginContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
mouse_filter = 2
|
||||
theme_override_constants/margin_left = 8
|
||||
theme_override_constants/margin_top = 8
|
||||
theme_override_constants/margin_right = 8
|
||||
theme_override_constants/margin_bottom = 8
|
||||
|
||||
[node name="VersionContainer" type="Control" parent="VersionMargin"]
|
||||
layout_mode = 2
|
||||
mouse_filter = 2
|
||||
|
||||
[node name="VersionLabel" type="Label" parent="VersionMargin/VersionContainer"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 3
|
||||
anchor_left = 1.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -88.0
|
||||
offset_top = -26.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 0
|
||||
text = "v0.0.0"
|
||||
horizontal_alignment = 2
|
||||
script = ExtResource("6_pdiij")
|
||||
|
||||
[node name="ExitConfirmation" parent="." instance=ExtResource("7_im16j")]
|
||||
unique_name_in_owner = true
|
||||
visible = false
|
||||
custom_minimum_size = Vector2(300, 160)
|
||||
layout_mode = 1
|
||||
offset_left = -150.0
|
||||
offset_top = -80.0
|
||||
offset_right = 150.0
|
||||
offset_bottom = 80.0
|
||||
ui_cancel_closes = false
|
||||
text = "Really exit the game?"
|
||||
title_visible = false
|
||||
|
||||
[connection signal="pressed" from="MenuContainer/MenuButtonsMargin/MenuButtonsContainer/MenuButtonsBoxContainer/NewGameButton" to="." method="_on_new_game_button_pressed"]
|
||||
[connection signal="pressed" from="MenuContainer/MenuButtonsMargin/MenuButtonsContainer/MenuButtonsBoxContainer/OptionsButton" to="." method="_on_options_button_pressed"]
|
||||
[connection signal="pressed" from="MenuContainer/MenuButtonsMargin/MenuButtonsContainer/MenuButtonsBoxContainer/CreditsButton" to="." method="_on_credits_button_pressed"]
|
||||
[connection signal="pressed" from="MenuContainer/MenuButtonsMargin/MenuButtonsContainer/MenuButtonsBoxContainer/ExitButton" to="." method="_on_exit_button_pressed"]
|
||||
[connection signal="confirmed" from="ExitConfirmation" to="." method="_on_exit_confirmation_confirmed"]
|
||||
Reference in New Issue
Block a user