From 3bcbde816102878a9b3935b7af7533d8ed4c47c6 Mon Sep 17 00:00:00 2001 From: Martin Araneda Date: Sat, 27 Aug 2022 22:34:18 -0400 Subject: [PATCH] Game over screen finished --- godot/GUI/GameOver.tscn | 19 +++++---- godot/monitor/Monitor.gd | 2 +- src/godot.cpp | 2 + src/gui/game_over/GameOverScreen.cpp | 64 ++++++++++++++++++++++++++-- src/gui/game_over/GameOverScreen.h | 10 +++-- src/player/Player.cpp | 8 ++-- 6 files changed, 84 insertions(+), 21 deletions(-) diff --git a/godot/GUI/GameOver.tscn b/godot/GUI/GameOver.tscn index d9729bf..401d521 100644 --- a/godot/GUI/GameOver.tscn +++ b/godot/GUI/GameOver.tscn @@ -14,18 +14,20 @@ font_data = ExtResource( 2 ) [sub_resource type="StyleBoxFlat" id=3] bg_color = Color( 0.0705882, 0.917647, 0, 1 ) -[node name="GameOver" type="Control"] -anchor_right = 1.0 -anchor_bottom = 1.0 +[node name="GameOver" type="CanvasLayer"] +script = ExtResource( 3 ) -[node name="ColorRect" type="ColorRect" parent="."] +[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 ) -rect_rotation = -0.338293 color = Color( 0, 0, 0, 1 ) -[node name="Label" type="Label" parent="."] +[node name="Label" type="Label" parent="Control"] margin_left = 66.0 margin_top = 17.0 margin_right = 456.0 @@ -34,7 +36,7 @@ custom_fonts/font = SubResource( 1 ) text = "GAME OVER" align = 1 -[node name="botonreiniciar" type="Button" parent="."] +[node name="botonreiniciar" type="Button" parent="Control"] margin_left = 194.0 margin_top = 150.0 margin_right = 338.0 @@ -42,6 +44,5 @@ margin_bottom = 180.0 custom_fonts/font = SubResource( 2 ) custom_styles/hover = SubResource( 3 ) text = "REINICIAR" -script = ExtResource( 3 ) -[connection signal="pressed" from="botonreiniciar" to="botonreiniciar" method="_on_botonreiniciar_pressed"] +[connection signal="pressed" from="Control/botonreiniciar" to="." method="_on_botonreiniciar_pressed"] diff --git a/godot/monitor/Monitor.gd b/godot/monitor/Monitor.gd index a9ff7d7..80ada27 100644 --- a/godot/monitor/Monitor.gd +++ b/godot/monitor/Monitor.gd @@ -53,7 +53,7 @@ func _ready() -> void: Event.connect("object_created", self, "_object_created") Event.connect("object_updated", self, "_object_updated") Event.connect("object_removed", self, "_object_removed") - + Event.connect("coin_collected", self, "_on_coin_update") game_version = get_parent().game_version player["rut"] = "" diff --git a/src/godot.cpp b/src/godot.cpp index 787b6b5..2b06182 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -15,6 +15,7 @@ #include "coin/CoinCounter.h" #include "goal/GoalReached.h" #include "goal/GoalNotReached.h" +#include "gui/game_over/GameOverScreen.h" using namespace godot; @@ -61,4 +62,5 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) register_class(); register_class(); register_class(); + register_class(); } diff --git a/src/gui/game_over/GameOverScreen.cpp b/src/gui/game_over/GameOverScreen.cpp index e497503..203558e 100644 --- a/src/gui/game_over/GameOverScreen.cpp +++ b/src/gui/game_over/GameOverScreen.cpp @@ -1,4 +1,6 @@ #include "gui/game_over/GameOverScreen.h" +#include "Event.h" + #include #include #include @@ -6,11 +8,15 @@ #include #include + using namespace godot; void GameOverScreen::_register_methods() { - register_method("_on_animation_finished", &GameOverScreen::_on_botonreiniciar_pressed); + register_method("_on_botonreiniciar_pressed", &GameOverScreen::_on_botonreiniciar_pressed); + register_method("_ready", &GameOverScreen::_ready); + register_method("connect_signal", &GameOverScreen::connect_signal); + register_method("_on_player_died", &GameOverScreen::_on_player_died); } GameOverScreen::GameOverScreen() @@ -27,16 +33,66 @@ void GameOverScreen::_init() } +void GameOverScreen::_ready() +{ + connect_signal(); +} + void GameOverScreen::_on_botonreiniciar_pressed() { - if (_resource_loader->exists("res://levels/Prototype.tscn")) //CAMBIAR A DINAMICO { Ref level_scene = _resource_loader->load("res://levels/Prototype.tscn"); auto level = level_scene->instance(); - auto level_node = get_tree()->get_root()->find_node("Level"); - level_node->add_child(level); + auto level_node = get_tree()->get_root()->get_node("Main")->find_node("Level"); + + if (level_node != nullptr) + { + + level_node->add_child(level); + set_visible(false); + call_deferred("connect_signal"); + } + else + { + WARN_PRINT("Node level not found!"); + } + } + +} + +void GameOverScreen::_on_player_died() //eliminar nivel +{ + auto event = get_node("/root/Event"); + event->disconnect("player_died", this, "_on_player_died"); + Godot::print("player ded"); + 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 GameOverScreen::connect_signal() +{ + auto event = get_node("/root/Event"); + event->connect("player_died", this, "_on_player_died"); } + diff --git a/src/gui/game_over/GameOverScreen.h b/src/gui/game_over/GameOverScreen.h index a03a2b2..79c5ad9 100644 --- a/src/gui/game_over/GameOverScreen.h +++ b/src/gui/game_over/GameOverScreen.h @@ -2,7 +2,7 @@ #define ALAI_GAME_OVER_SCREEN_H #include -#include +#include #include @@ -12,9 +12,9 @@ namespace godot * @brief This class controls what happens when the Coin is in the collected . * */ - class GameOverScreen : public Control + class GameOverScreen : public CanvasLayer { - GODOT_CLASS(GameOverScreen, Control) + GODOT_CLASS(GameOverScreen, CanvasLayer) private: ResourceLoader *_resource_loader; @@ -50,8 +50,10 @@ namespace godot * @brief Called when the collected of the coin is entered. * */ + void _ready(); + void _on_player_died(); void _on_botonreiniciar_pressed(); - + void connect_signal(); }; diff --git a/src/player/Player.cpp b/src/player/Player.cpp index 17da42d..1ea201b 100644 --- a/src/player/Player.cpp +++ b/src/player/Player.cpp @@ -52,7 +52,7 @@ void Player::_init() set_double_jump(player::double_jump); coins = 0; - + velocity = Vector2(); } @@ -166,14 +166,16 @@ void Player::_physics_process(float delta) { if (!notifier->is_on_screen()) { - if (get_parent()->get_class() == "TileMap") + auto event = get_node("/root/Event"); + event->emit_signal("player_died"); + /*if (get_parent()->get_class() == "TileMap") { auto error = get_tree()->change_scene("res://Main.tscn"); if (error != Error::OK) { ERR_PRINT(String().num((int) error) + " Could not load scene!"); } - } + }*/ } }