Go to main menu on disconnect

This commit is contained in:
Sofia 2026-07-23 19:23:11 +03:00
parent 0cd1f12493
commit 1508b42797
3 changed files with 28 additions and 5 deletions

View File

@ -4,10 +4,12 @@
[ext_resource type="MapResource" uid="uid://c7n1tacc7a73t" path="res://maps/default_map.tres" id="1_glms7"]
[ext_resource type="PackedScene" uid="uid://df84sctteh4kw" path="res://scenes/game_finished.tscn" id="1_qf2qp"]
[ext_resource type="MapResource" uid="uid://sc3507f75002" path="res://maps/diner.tres" id="2_glms7"]
[ext_resource type="PackedScene" uid="uid://lahirbqfppvw" path="res://scenes/main.tscn" id="3_7ele1"]
[ext_resource type="MapResource" uid="uid://bsjoandgu476y" path="res://maps/two_towers.tres" id="5_rotn8"]
[node name="game_manager" type="GameManager" unique_id=1442044769]
respawn_timer = 10.0
lobby_screen = ExtResource("1_3ama4")
game_finished_screen = ExtResource("1_qf2qp")
main_menu = ExtResource("3_7ele1")
maps = Array[MapResource]([ExtResource("1_glms7"), ExtResource("2_glms7"), ExtResource("5_rotn8")])

View File

@ -31,10 +31,14 @@ pub struct GameManager {
pub lobby_screen: Option<Gd<PackedScene>>,
#[export]
pub game_finished_screen: Option<Gd<PackedScene>>,
#[export]
pub main_menu: Option<Gd<PackedScene>>,
pub game: Option<Gd<Game>>,
base: Base<Node>,
exiting: bool,
#[export]
pub maps: Array<Gd<MapResource>>,
}
@ -77,9 +81,11 @@ impl GameManager {
game.signals()
.on_map_changed()
.connect_other(s, |s, id| s.signals().on_map_changed().emit(id));
game.signals()
.on_game_finished()
.connect_other(s, |s| s.go_to_finish_screen());
game.signals().on_game_finished().connect_other(s, |s| {
if !s.exiting {
s.go_to_finish_screen()
}
});
}
});
@ -88,6 +94,15 @@ impl GameManager {
self.game = Some(game);
}
pub fn exit_game(&mut self) {
self.exiting = true;
if let Some(mut game) = self.game.take() {
game.bind_mut().finish_game();
game.queue_free();
}
self.go_to_main_menu();
}
pub fn go_to_finish_screen(&self) {
if let Some(scene) = &self.game_finished_screen {
self.base().get_tree().change_scene_to_packed(scene);
@ -99,6 +114,12 @@ impl GameManager {
self.base().get_tree().change_scene_to_packed(lobby);
}
}
pub fn go_to_main_menu(&self) {
if let Some(scene) = &self.main_menu {
self.base().get_tree().change_scene_to_packed(scene);
}
}
}
pub struct GameOptions {

View File

@ -37,7 +37,7 @@ impl NetworkManager {
}
teanet::PeerMessage::Closed => {
self.peer = None;
GameManager::singleton().bind_mut().game = None;
GameManager::singleton().bind_mut().exit_game();
}
teanet::PeerMessage::Message(_, msg) => match msg {
Package::Hello => {
@ -244,7 +244,7 @@ impl NetworkManager {
cli.bind_mut()
.publish_message(format!("Server closed"), CliColor::Info);
self.peer = None;
GameManager::singleton().bind_mut().game = None;
GameManager::singleton().bind_mut().exit_game();
}
teanet::PeerMessage::Message(conn, msg) => match msg {
Package::SetSelfNick(nick) => {