diff options
author | Daniel Weipert <code@drogueronin.de> | 2021-01-02 14:54:57 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2021-01-02 14:54:57 +0100 |
commit | ebae6d04ffc95c6648e863d3de71835f6cd4be96 (patch) | |
tree | 58ce9119a65ccd6925d661f12aab98a057dfb53d /Network/Network.gd | |
parent | fb64f15e22acfc1c03c7c5357dd66077454adbcf (diff) |
Initial working commit
Diffstat (limited to 'Network/Network.gd')
-rw-r--r-- | Network/Network.gd | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/Network/Network.gd b/Network/Network.gd new file mode 100644 index 0000000..0f007b4 --- /dev/null +++ b/Network/Network.gd @@ -0,0 +1,114 @@ +extends Node + + +signal connection_succeeded() +signal connection_failed() +signal player_list_changed() +signal game_error() +signal game_ended() + + +const PORT = 10567 + +const MAX_PEERS = 2 + + +var peer = null + +var player = { + idx = 0, + name = "", +} + +# Dictionary { id = { idx: 1, name: "" } } +var players = {} +var players_ready = [] + + +func _ready(): + get_tree().connect("network_peer_connected", self, '_player_connected') + get_tree().connect("network_peer_disconnected", self,"_player_disconnected") + 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") + + +remote func register_player(name): + var id = get_tree().get_rpc_sender_id() + self.players[id] = { + name = name + } + emit_signal("player_list_changed") + + +func unregister_player(id): + self.player.erase(id) + emit_signal("player_list_changed") + + +func host_game(player_name): + self.player.name = player_name + self.peer = NetworkedMultiplayerENet.new() + self.peer.create_server(self.PORT, self.MAX_PEERS) + get_tree().set_network_peer(self.peer) + + +func join_game(ip, player_name): + self.player.name = player_name + self.peer = NetworkedMultiplayerENet.new() + self.peer.create_client(ip, self.PORT) + get_tree().set_network_peer(self.peer) + + +func start_game(): + # preconfigure game and set idx to each player + var idx = 1 + for id in self.players: + self.players[id].idx = idx + rpc_id(id, "_preconfigure_game", idx) + idx += 1 + + # start game for everyone + rpc("_start_game") + + +remote func _preconfigure_game(idx): + self.player.idx = idx + + +sync func _start_game(): + get_tree().change_scene("res://UI/LevelSelect.tscn") + + +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") + pass + + +func _player_connected(id): + rpc_id(id, "register_player", self.player.name) + + +func _player_disconnected(id): + # if game is in progress: + # self.end_game() ? + # else + self.unregister_player(id) + + +func _connection_succeeded(): + emit_signal("connection_succeeded") + + +func _connection_failed(): + get_tree().set_network_peer(null) # remove peer + emit_signal("connection_failed") + + +func _server_disconnected(): + emit_signal("game_error", "Server disconnected!") + self.end_game("") |