diff options
Diffstat (limited to 'Network/Network.gd')
-rw-r--r-- | Network/Network.gd | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/Network/Network.gd b/Network/Network.gd index 77630a7..fbef800 100644 --- a/Network/Network.gd +++ b/Network/Network.gd @@ -5,6 +5,7 @@ signal connection_succeeded() signal connection_failed() signal player_list_changed() signal game_error() +signal game_started() signal game_ended() @@ -14,10 +15,12 @@ const MAX_PEERS = 2 var peer = null +var ip = null var player = { idx = 0, name = "", + character = "Tux", } # Dictionary { id = { idx: 1, name: "" } } @@ -31,19 +34,22 @@ func _ready(): get_tree().connect("connected_to_server", self, "_connection_succeeded") get_tree().connect("connection_failed", self, "_connection_failed") get_tree().connect("server_disconnected", self, "_server_disconnected") - Global.connect("game_won", self, "end_game") + Global.connect("game_started", self, "_on_Global_game_started") + Global.connect("game_ended", self, "_on_Global_game_ended") + Global.connect("game_won", self, "_on_Global_game_won") remote func register_player(name): var id = get_tree().get_rpc_sender_id() self.players[id] = { - name = name + name = name, + ip = self.peer.get_peer_address(id), } emit_signal("player_list_changed") -func unregister_player(id): - self.player.erase(id) +remote func unregister_player(id): + self.players.erase(id) emit_signal("player_list_changed") @@ -61,7 +67,21 @@ func join_game(ip, player_name): get_tree().set_network_peer(self.peer) -func start_game(): +func leave_game(): + rpc("_leave_game") + get_tree().set_network_peer(null) + + +remote func _leave_game(): + var id = get_tree().get_rpc_sender_id() + self.peer.disconnect_peer(id) + + +func get_player_count(): + return self.players.size() + 1 + + +func _on_Global_game_started(): # preconfigure game and set idx to each player var idx = 1 for id in self.players: @@ -78,30 +98,33 @@ remote func _preconfigure_game(idx): sync func _start_game(): - get_tree().change_scene("res://UI/LevelSelect.tscn") + emit_signal("game_started") -func end_game(winning_player): - emit_signal("game_ended", winning_player) +func _on_Global_game_ended(): + # tell all peers to end the game + rpc("_end_game") + + +func _on_Global_game_won(winning_player): rpc("_end_game", winning_player) sync func _end_game(winning_player): - # TODO: change scene for all to win scene and set winning player id, - # so it can be displayed with self.players - print(winning_player) - get_tree().change_scene("res://Network/Lobby.tscn") + print("winning_player") + emit_signal("game_ended", winning_player) -func _player_connected(id): - rpc_id(id, "register_player", self.player.name) +func _player_connected(_id): + rpc("register_player", self.player.name) func _player_disconnected(id): # if game is in progress: # self.end_game() ? # else - self.unregister_player(id) + #rpc("unregister_player", id) + rpc("unregister_player", id) func _connection_succeeded(): @@ -115,4 +138,4 @@ func _connection_failed(): func _server_disconnected(): emit_signal("game_error", "Server disconnected!") - self.end_game("") + #self.end_game("") |