From 68009d1c390d265b7f862353ce652e00d9ad44ad Mon Sep 17 00:00:00 2001 From: John Breaux Date: Mon, 6 Dec 2021 13:16:49 -0600 Subject: [PATCH] Improve interactions between Game, Victory, and Lobby. Game: - Show victory screen on opponent, including ships - Return to Lobby on forfeit/connection error Victory: - Display win/lose status - Request that Game return to lobby, rather than doing it manually - Disconnect from network when returning to main menu --- godot_ship/script/game/Gameplay/Game.gd | 48 ++++++++++++++++--------- godot_ship/script/game/Victory.gd | 20 ++++++++--- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/godot_ship/script/game/Gameplay/Game.gd b/godot_ship/script/game/Gameplay/Game.gd index 72f3b61..32604e9 100644 --- a/godot_ship/script/game/Gameplay/Game.gd +++ b/godot_ship/script/game/Gameplay/Game.gd @@ -25,7 +25,6 @@ var network_id # Called when the node enters the scene tree for the first time. func _ready(): - get_node("Forfeit Confirmation").get_ok().text = "Yes" get_node("Forfeit Confirmation").get_cancel().text = "No" get_node("Forfeit Confirmation").get_ok().rect_min_size.x = 100 @@ -78,7 +77,6 @@ remote func state_check(pos): LOST: # the other player wins rpc("state_win", player.board.ship_data) - victory_screen(null, false) SUNK, HIT: # Hit rpc("state_fire") @@ -87,11 +85,19 @@ remote func state_check(pos): state_fire() pass -# state_win: The winning state. If you reach here, someone's won. +# state_win: The winning state. If you reach here, you've won. # ships: The opponent's ship data, so that their board can be shown remote func state_win(ships): - victory_screen(ships) - pass + # Send ships back to the opponent: + rpc("state_lose", player.board.ship_data) + # Show the victory screen + victory_screen(ships, true) + +# state_lose: The losing state. If you reach here, you've lost. +# ships: The opponent's ship data, so that their board can be shown +remote func state_lose(ships): + # Show the not-victory screen + victory_screen(ships, false) # play_hit_sound: Play a hit sound depending on the severity of the hit # value: Lost/Sunk/Hit/Miss @@ -149,17 +155,22 @@ func _on_player_ready(): # victory_screen: display the victory screen func victory_screen(ships, winner = true): - if winner: - # Hide the buttons - get_node("Buttons").hide() - # Create a new Victory screen - var victory = Victory.instance() - # Give it the ships received from the opponent + # Stop listening for opponent disconnections + Net.disconnect("disconnected", self, "connection_error") + # Hide the buttons + get_node("Buttons").hide() + # Create a new Victory screen + var victory = Victory.instance() + # Allow victory to end the game + victory.connect("end_game", self, "end") + # Tell victory whether we've won or lost + victory.set_win(winner) + # If we were given ships to display + if ships: + # Give victory the ships victory.reveal_ships(ships) - # Add victory to the scene tree - add_child(victory) - else: - end() + # Add victory to the scene tree + add_child(victory) # _on_Forfeit_pressed: Handle forfeit button press func _on_Forfeit_pressed(): @@ -168,6 +179,8 @@ func _on_Forfeit_pressed(): # end: end the Game sync func end(): + # Return to the lobby + MessageBus.emit_signal("change_scene", "Multiplayer") queue_free() @@ -186,9 +199,10 @@ func _on_Forfeit_Confirmation_confirmed(): if Net.connected: # Send forfeit request to all users rpc("end") - end() + else: + end() func _on_Connection_Error_confirmed(): # End the game - queue_free() + end() diff --git a/godot_ship/script/game/Victory.gd b/godot_ship/script/game/Victory.gd index dcf0b43..07283cb 100644 --- a/godot_ship/script/game/Victory.gd +++ b/godot_ship/script/game/Victory.gd @@ -3,6 +3,9 @@ extends Control # Path to Board class, for instantiating new Boards in code var Board = preload("res://scenes/Game/Board.tscn") +# Sidnals +# request to return to lobby +signal end_game # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. @@ -14,17 +17,26 @@ func reveal_ships(ships:Array): for ship in ships: board.callv("place_ship", ship) +func set_win(won:bool): + var Text = find_node("Text") + if won: + Text.text = "You win!" + else: + Text.text = "You lose" + # Called every frame. 'delta' is the elapsed time since the previous frame. #func _process(delta): # pass -func _on_restart_button_down(): +func _on_Restart_pressed(): AudioBus.emit_signal("button_clicked") - MessageBus.emit_signal("change_scene", "Multiplayer") - MessageBus.emit_signal("kill_scene", "Game") + emit_signal("end_game") # returns player(s) back to main menu -func _on_exit_to_main_button_down(): +func _on_Exit_to_Title_pressed(): AudioBus.emit_signal("button_clicked") + # Disconnect from peer + Net.disconnect_host() + # Force return to title MessageBus.emit_signal("return_to_title")