feat: added end and main menu screens

This commit is contained in:
Sara 2024-05-21 11:54:50 +02:00
parent ba1545333a
commit 9d9acc51d5
7 changed files with 229 additions and 2 deletions

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://yrr5jjbpt60l"]
[ext_resource type="PackedScene" uid="uid://e3dmisrrgqq7" path="res://testmap.tscn" id="1_3f7mp"]
[ext_resource type="PackedScene" uid="uid://73cn4swq2gc7" path="res://game_ui.tscn" id="1_g28e3"]
[node name="GameRoot3D" type="GameRoot3D"]
first_boot_level = ExtResource("1_3f7mp")
first_boot_level = ExtResource("1_g28e3")

View file

@ -0,0 +1,40 @@
[gd_scene load_steps=3 format=3 uid="uid://05prvqpy0r6m"]
[ext_resource type="PackedScene" uid="uid://73cn4swq2gc7" path="res://game_ui.tscn" id="1_c4p6k"]
[ext_resource type="PackedScene" uid="uid://e3dmisrrgqq7" path="res://testmap.tscn" id="2_qhspd"]
[node name="Level3D" type="Level3D"]
[node name="EndScreen" type="EndScreen" parent="."]
main_menu_scene = ExtResource("1_c4p6k")
game_scene = ExtResource("2_qhspd")
[node name="RestartButton" type="Button" parent="EndScreen"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -78.5
offset_top = -29.0
offset_right = 78.5
offset_bottom = 29.0
grow_horizontal = 2
grow_vertical = 2
focus_neighbor_bottom = NodePath("../MainMenuButton")
text = "Restart"
[node name="MainMenuButton" type="Button" parent="EndScreen"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -78.0
offset_top = 55.0
offset_right = 79.0
offset_bottom = 113.0
grow_horizontal = 2
grow_vertical = 2
focus_neighbor_top = NodePath("../RestartButton")
text = "Menu"

46
godot/game_ui.tscn Normal file
View file

@ -0,0 +1,46 @@
[gd_scene load_steps=2 format=3 uid="uid://73cn4swq2gc7"]
[ext_resource type="PackedScene" uid="uid://e3dmisrrgqq7" path="res://testmap.tscn" id="1_kul4y"]
[node name="Level3D" type="Level3D"]
[node name="GameUI" type="MenuUI" parent="."]
game_scene = ExtResource("1_kul4y")
[node name="StartButton" type="Button" parent="GameUI"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -115.0
offset_top = -57.0
offset_right = 115.0
offset_bottom = 57.0
grow_horizontal = 2
grow_vertical = 2
focus_neighbor_bottom = NodePath("../ControlsButton")
text = "Start"
[node name="ControlsButton" type="Button" parent="GameUI"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -115.0
offset_top = 111.0
offset_right = 115.0
offset_bottom = 225.0
grow_horizontal = 2
grow_vertical = 2
focus_neighbor_top = NodePath("../StartButton")
text = "Controls"
[node name="ControlsScreen" type="Panel" parent="GameUI"]
offset_left = 32.0
offset_top = 30.0
offset_right = 437.0
offset_bottom = 339.0
[node name="Camera3D" type="Camera3D" parent="."]

47
src/end_screen.cpp Normal file
View file

@ -0,0 +1,47 @@
#include "end_screen.hpp"
#include "godot_cpp/classes/button.hpp"
#include "godot_cpp/classes/global_constants.hpp"
#include "utils/game_root.hpp"
#include "utils/godot_macros.h"
namespace godot {
void EndScreen::_bind_methods() {
#define CLASSNAME EndScreen
GDPROPERTY_HINTED(main_menu_scene, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
GDPROPERTY_HINTED(game_scene, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
}
void EndScreen::_enter_tree() { GDGAMEONLY();
Button *restart_button = this->get_node<Button>("RestartButton");
restart_button->connect("button_down", callable_mp(this, &EndScreen::restart));
restart_button->grab_focus();
Button *main_button = this->get_node<Button>("MainMenuButton");
main_button->connect("button_down", callable_mp(this, &EndScreen::return_to_main));
}
void EndScreen::return_to_main() {
this->get_owner()->queue_free();
GameRoot3D::get_singleton()->load_level(this->main_menu_scene);
}
void EndScreen::restart() {
this->get_owner()->queue_free();
GameRoot3D::get_singleton()->load_level(this->game_scene);
}
void EndScreen::set_main_menu_scene(Ref<PackedScene> scene) {
this->main_menu_scene = scene;
}
Ref<PackedScene> EndScreen::get_main_menu_scene() const {
return this->main_menu_scene;
}
void EndScreen::set_game_scene(Ref<PackedScene> scene) {
this->game_scene = scene;
}
Ref<PackedScene> EndScreen::get_game_scene() const {
return this->game_scene;
}
}

26
src/end_screen.hpp Normal file
View file

@ -0,0 +1,26 @@
#ifndef END_SCREEN_HPP
#define END_SCREEN_HPP
#include "godot_cpp/classes/canvas_layer.hpp"
#include "godot_cpp/classes/packed_scene.hpp"
namespace godot {
class EndScreen : public CanvasLayer {
GDCLASS(EndScreen, CanvasLayer);
static void _bind_methods();
public:
virtual void _enter_tree() override;
void return_to_main();
void restart();
void set_main_menu_scene(Ref<PackedScene> scene);
Ref<PackedScene> get_main_menu_scene() const;
void set_game_scene(Ref<PackedScene> scene);
Ref<PackedScene> get_game_scene() const;
private:
Ref<PackedScene> game_scene{};
Ref<PackedScene> main_menu_scene{};
};
}
#endif // !END_SCREEN_HPP

41
src/menu_ui.cpp Normal file
View file

@ -0,0 +1,41 @@
#include "menu_ui.hpp"
#include "utils/game_root.hpp"
#include "utils/godot_macros.h"
#include <godot_cpp/classes/button.hpp>
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
namespace godot {
void MenuUI::_bind_methods() {
#define CLASSNAME MenuUI
GDPROPERTY_HINTED(game_scene, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
}
void MenuUI::_enter_tree() { GDGAMEONLY();
Button *start_button = this->get_node<Button>("StartButton");
start_button->connect("button_down", callable_mp(this, &MenuUI::start_game));
start_button->grab_focus();
Button *controls_button = this->get_node<Button>("ControlsButton");
controls_button->connect("button_down", callable_mp(this, &MenuUI::toggle_controls));
this->controls = this->get_node<Control>("ControlsScreen");
this->controls->set_visible(false);
}
void MenuUI::start_game() {
this->get_parent()->queue_free();
GameRoot3D::get_singleton()->load_level(this->game_scene);
}
void MenuUI::toggle_controls() {
this->controls->set_visible(true);
}
void MenuUI::set_game_scene(Ref<PackedScene> scene) {
this->game_scene = scene;
}
Ref<PackedScene> MenuUI::get_game_scene() const {
return this->game_scene;
}
}

27
src/menu_ui.hpp Normal file
View file

@ -0,0 +1,27 @@
#ifndef MENU_UI_HPP
#define MENU_UI_HPP
#include "godot_cpp/classes/control.hpp"
#include <godot_cpp/classes/canvas_layer.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
namespace godot {
class MenuUI : public CanvasLayer {
GDCLASS(MenuUI, CanvasLayer);
static void _bind_methods();
public:
virtual void _enter_tree() override;
void start_game();
void toggle_controls();
void set_game_scene(Ref<PackedScene> scene);
Ref<PackedScene> get_game_scene() const;
private:
Control *controls{nullptr};
Ref<PackedScene> game_scene{};
};
}
#endif // !MENU_UI_HPP