summaryrefslogtreecommitdiff
path: root/Game/Network.gd
diff options
context:
space:
mode:
Diffstat (limited to 'Game/Network.gd')
-rw-r--r--Game/Network.gd44
1 files changed, 35 insertions, 9 deletions
diff --git a/Game/Network.gd b/Game/Network.gd
index 355f7a4..5dcb074 100644
--- a/Game/Network.gd
+++ b/Game/Network.gd
@@ -9,9 +9,9 @@ func _ready():
reset_players()
multiplayer.connected_to_server.connect(_on_connected_to_server)
- multiplayer.server_disconnected.connect(_on_disconneted_from_server)
- multiplayer.peer_connected.connect(_on_peer_connected)
- multiplayer.peer_disconnected.connect(_on_peer_disconnected)
+ multiplayer.server_disconnected.connect(_on_disconnected_from_server)
+ #multiplayer.peer_connected.connect(_on_peer_connected)
+ #multiplayer.peer_disconnected.connect(_on_peer_disconnected)
multiplayer.allow_object_decoding = true
@@ -36,26 +36,33 @@ func join_game(ip, port):
func _on_connected_to_server():
print(multiplayer.get_unique_id(), ": connected to server")
-func _on_disconneted_from_server():
+func _on_disconnected_from_server():
print("disconnected from server")
multiplayer.multiplayer_peer = OfflineMultiplayerPeer.new()
reset_players()
-func _on_peer_connected(id):
+func _on_peer_connected(id: int):
print(multiplayer.get_unique_id(), ": peer connected: ", id)
- add_to_players.rpc_id(id, inst_to_dict(Client.player))
+ ask_game_running.rpc_id(id)
+ is_game_running.connect(func(is_running: bool):
+ if not is_running:
+ add_to_players.rpc_id(id, inst_to_dict(Client.player))
+ elif is_running:
+ get_tree().change_scene_to_file("res://UI/Start.tscn")
+ )
+
# TODO: add existing towers to new peers
-func _on_peer_disconnected(id):
+func _on_peer_disconnected(id: int):
print(multiplayer.get_unique_id(), ": peer disconnected: ", id)
- %Players.remove_child(get_player(id))
+ remove_player(id)
# TODO: move towers owned by peer to host
- if id == 1: # if host disconnected go back to Lobby
+ if id == 1: # if host disconnected go back to Start
get_tree().change_scene_to_file("res://UI/Start.tscn")
@@ -84,6 +91,25 @@ func get_ordered_player_ids(players = get_players()) -> Array:
return keys
+func remove_player(id: int):
+ var player = get_player(id)
+
+ if player:
+ %Players.remove_child(player)
+ players_changed.emit()
+
+
+signal is_game_running(is_running: bool)
+
+@rpc("any_peer")
+func ask_game_running():
+ var is_running = get_tree().current_scene is Stage
+ receive_game_running.rpc_id(multiplayer.get_remote_sender_id(), is_running)
+
+@rpc("any_peer")
+func receive_game_running(is_running: bool):
+ is_game_running.emit(is_running)
+
@rpc("any_peer")
func add_to_players(remote_data: Dictionary):