From 08216d061ca4f12b11e4eef8072ee6bfc49f65ad Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 31 Aug 2022 01:07:56 -0400 Subject: [PATCH] add a win screen when the flag is reached --- godot/Main.tscn | 6 ++- godot/gui/GameWon.tscn | 48 ++++++++++++++++++++++ godot/gui/GameWonScreen.gdns | 8 ++++ src/goal/GoalReached.cpp | 5 ++- src/godot.cpp | 2 + src/gui/game_won/GameWonScreen.cpp | 65 ++++++++++++++++++++++++++++++ src/gui/game_won/GameWonScreen.h | 51 +++++++++++++++++++++++ 7 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 godot/gui/GameWon.tscn create mode 100644 godot/gui/GameWonScreen.gdns create mode 100644 src/gui/game_won/GameWonScreen.cpp create mode 100644 src/gui/game_won/GameWonScreen.h diff --git a/godot/Main.tscn b/godot/Main.tscn index 2cdfefa..de0bf8a 100644 --- a/godot/Main.tscn +++ b/godot/Main.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://Main.gdns" type="Script" id=1] [ext_resource path="res://levels/Prototype.tscn" type="PackedScene" id=2] [ext_resource path="res://gui/GameOver.tscn" type="PackedScene" id=3] +[ext_resource path="res://gui/GameWon.tscn" type="PackedScene" id=4] [node name="Main" type="Node"] pause_mode = 2 @@ -14,3 +15,6 @@ pause_mode = 1 [node name="GameOver" parent="." instance=ExtResource( 3 )] visible = false + +[node name="GameWon" parent="." instance=ExtResource( 4 )] +visible = false diff --git a/godot/gui/GameWon.tscn b/godot/gui/GameWon.tscn new file mode 100644 index 0000000..49889fc --- /dev/null +++ b/godot/gui/GameWon.tscn @@ -0,0 +1,48 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://assets/fonts/ttf/PixelOperator8.ttf" type="DynamicFontData" id=1] +[ext_resource path="res://assets/fonts/ttf/PixelOperatorHB8.ttf" type="DynamicFontData" id=2] +[ext_resource path="res://gui/GameWonScreen.gdns" type="Script" id=3] + +[sub_resource type="DynamicFont" id=1] +size = 50 +font_data = ExtResource( 1 ) + +[sub_resource type="DynamicFont" id=2] +font_data = ExtResource( 2 ) + +[sub_resource type="StyleBoxFlat" id=3] +bg_color = Color( 0.0705882, 0.917647, 0, 1 ) + +[node name="GameWon" type="CanvasLayer"] +script = ExtResource( 3 ) + +[node name="Control" type="Control" parent="."] +margin_right = 40.0 +margin_bottom = 40.0 + +[node name="ColorRect" type="ColorRect" parent="Control"] +margin_right = 512.0 +margin_bottom = 288.0 +rect_min_size = Vector2( 512, 288 ) +color = Color( 0, 0, 0, 1 ) + +[node name="Label" type="Label" parent="Control"] +margin_left = 66.0 +margin_top = 17.0 +margin_right = 456.0 +margin_bottom = 71.0 +custom_fonts/font = SubResource( 1 ) +text = "GANASTE!" +align = 1 + +[node name="QuitButton" type="Button" parent="Control"] +margin_left = 194.0 +margin_top = 150.0 +margin_right = 338.0 +margin_bottom = 180.0 +custom_fonts/font = SubResource( 2 ) +custom_styles/hover = SubResource( 3 ) +text = "SALIR" + +[connection signal="pressed" from="Control/QuitButton" to="." method="_on_quit_button_pressed"] diff --git a/godot/gui/GameWonScreen.gdns b/godot/gui/GameWonScreen.gdns new file mode 100644 index 0000000..1d6223b --- /dev/null +++ b/godot/gui/GameWonScreen.gdns @@ -0,0 +1,8 @@ +[gd_resource type="NativeScript" load_steps=2 format=2] + +[ext_resource path="res://gdnative/alai.tres" type="GDNativeLibrary" id=1] + +[resource] +resource_name = "GameWonScreen" +class_name = "GameWonScreen" +library = ExtResource( 1 ) diff --git a/src/goal/GoalReached.cpp b/src/goal/GoalReached.cpp index 38fb8fa..6afb5f0 100644 --- a/src/goal/GoalReached.cpp +++ b/src/goal/GoalReached.cpp @@ -1,5 +1,7 @@ #include "goal/GoalReached.h" +#include "Event.h" + #include void alai::GoalReached::_register_methods() @@ -22,7 +24,8 @@ void alai::GoalReached::_init() void alai::GoalReached::_state_enter() { - godot::Godot::print("Flag touched"); + auto event = get_node("/root/Event"); + event->emit_signal("player_won"); } void alai::GoalReached::_state_exit() diff --git a/src/godot.cpp b/src/godot.cpp index bb724e7..5793fee 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -16,6 +16,7 @@ #include "goal/GoalReached.h" #include "goal/GoalNotReached.h" #include "gui/game_over/GameOverScreen.h" +#include "gui/game_won/GameWonScreen.h" /** * @brief This function connects the gdnative init function. @@ -62,4 +63,5 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) godot::register_class(); godot::register_class(); godot::register_class(); + godot::register_class(); } diff --git a/src/gui/game_won/GameWonScreen.cpp b/src/gui/game_won/GameWonScreen.cpp new file mode 100644 index 0000000..d57f950 --- /dev/null +++ b/src/gui/game_won/GameWonScreen.cpp @@ -0,0 +1,65 @@ +#include "gui/game_won/GameWonScreen.h" +#include "Event.h" + +#include +#include + +void alai::GameWonScreen::_register_methods() +{ + godot::register_method("_ready", &GameWonScreen::_ready); + godot::register_method("connect_signal", &GameWonScreen::connect_signal); + godot::register_method("_on_player_won", &GameWonScreen::_on_player_won); + godot::register_method("_on_quit_button_pressed", &GameWonScreen::_on_quit_button_pressed); +} + +alai::GameWonScreen::GameWonScreen() +{ +} + +alai::GameWonScreen::~GameWonScreen() +{ +} + +void alai::GameWonScreen::_init() +{ +} + +void alai::GameWonScreen::_ready() +{ + connect_signal(); +} + +void alai::GameWonScreen::_on_quit_button_pressed() +{ + get_tree()->quit(); +} + +void alai::GameWonScreen::_on_player_won() +{ + auto event = get_node("/root/Event"); + event->disconnect("player_won", this, "_on_player_won"); + set_visible(true); + auto level_node = get_tree()->get_root()->get_node("Main")->find_node("Level"); + if (level_node != nullptr) + { + auto child = level_node->get_child(0); + if (child != nullptr) + { + child->queue_free(); + } + else + { + WARN_PRINT("Child not found!"); + } + } + else + { + WARN_PRINT("Node level not found!"); + } +} + +void alai::GameWonScreen::connect_signal() +{ + auto event = get_node("/root/Event"); + event->connect("player_won", this, "_on_player_won"); +} diff --git a/src/gui/game_won/GameWonScreen.h b/src/gui/game_won/GameWonScreen.h new file mode 100644 index 0000000..987b7a1 --- /dev/null +++ b/src/gui/game_won/GameWonScreen.h @@ -0,0 +1,51 @@ +#ifndef ALAI_GAME_WON_GAME_WON_SCREEN_H +#define ALAI_GAME_WON_GAME_WON_SCREEN_H + +#include +#include + +namespace alai +{ + /** + * @brief This class controls what happens when the game is won. + * + */ + class GameWonScreen : public godot::CanvasLayer + { + GODOT_CLASS(GameWonScreen, godot::CanvasLayer) + + public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ + static void _register_methods(); + + /** + * @brief Construct a new GameWonScreen object. + * + */ + GameWonScreen(); + + /** + * @brief Destroy the GameWonScreen object. + * + */ + ~GameWonScreen(); + + /** + * @brief Initialize the class from Godot. + * + * @details This method is called just once when the Godot engine connects to the instance of the class. + */ + void _init(); + + void _ready(); + void _on_player_won(); + void _on_quit_button_pressed(); + void connect_signal(); + }; +} + +#endif