summaryrefslogtreecommitdiff
path: root/Network
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2021-01-09 23:30:27 +0100
committerDaniel Weipert <code@drogueronin.de>2021-01-09 23:30:27 +0100
commit485cd463b22e12394cd232812e2c3789ef37ab1c (patch)
tree5c455ec9c80b59a9f71ba5827b9902f2beb6df88 /Network
parent7920e8758506cd46c58ea4889c9f4fb704abfd16 (diff)
Adds a bunch of characters and fixes things
Diffstat (limited to 'Network')
-rw-r--r--Network/Lobby.gd10
-rw-r--r--Network/Lobby.tscn25
-rw-r--r--Network/Network.gd55
-rw-r--r--Network/Remote.gd6
-rw-r--r--Network/Sync.gd6
5 files changed, 84 insertions, 18 deletions
diff --git a/Network/Lobby.gd b/Network/Lobby.gd
index d962081..b30bc1f 100644
--- a/Network/Lobby.gd
+++ b/Network/Lobby.gd
@@ -60,6 +60,7 @@ func _on_Host_pressed():
$Players.show()
$Connect/Error.text = ""
+ $Players/IP.text = IP.get_local_addresses()[6]
var player_name = $Connect/Name.text
Network.host_game(player_name)
self.refresh_lobby()
@@ -74,9 +75,16 @@ func _on_Join_pressed():
$Connect/Error.text = ""
self.disable_connect_buttons(false)
+ $Players/IP.text = IP.get_local_addresses()[6]
var player_name = $Connect/Name.text
Network.join_game(ip, player_name)
func _on_Start_pressed():
- Network.start_game()
+ Global.start_game()
+
+
+func _on_Back_pressed():
+ $Connect.show()
+ $Players.hide()
+ Network.leave_game()
diff --git a/Network/Lobby.tscn b/Network/Lobby.tscn
index 0caf892..7e8c52a 100644
--- a/Network/Lobby.tscn
+++ b/Network/Lobby.tscn
@@ -70,9 +70,23 @@ __meta__ = {
[node name="Label" type="Label" parent="Players"]
margin_left = 16.0
margin_top = 16.0
-margin_right = 240.0
+margin_right = 64.0
margin_bottom = 32.0
text = "Lobby"
+__meta__ = {
+"_edit_use_anchors_": false
+}
+
+[node name="IP" type="Label" parent="Players"]
+margin_left = 128.0
+margin_top = 16.0
+margin_right = 240.0
+margin_bottom = 30.0
+text = "000.000.000.000"
+align = 2
+__meta__ = {
+"_edit_use_anchors_": false
+}
[node name="List" type="ItemList" parent="Players"]
margin_left = 16.0
@@ -93,9 +107,18 @@ __meta__ = {
"_edit_use_anchors_": false
}
+[node name="Back" type="Button" parent="Players"]
+margin_left = 16.0
+margin_top = 416.0
+margin_right = 48.0
+margin_bottom = 448.0
+text = "<"
+
[node name="ErrorDialog" type="AcceptDialog" parent="."]
+visible = true
margin_right = 83.0
margin_bottom = 58.0
[connection signal="pressed" from="Connect/Host" to="." method="_on_Host_pressed"]
[connection signal="pressed" from="Connect/Join" to="." method="_on_Join_pressed"]
[connection signal="pressed" from="Players/Start" to="." method="_on_Start_pressed"]
+[connection signal="pressed" from="Players/Back" to="." method="_on_Back_pressed"]
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("")
diff --git a/Network/Remote.gd b/Network/Remote.gd
new file mode 100644
index 0000000..1b1de57
--- /dev/null
+++ b/Network/Remote.gd
@@ -0,0 +1,6 @@
+extends Node
+
+# rpc remote functions
+
+func _ready():
+ pass
diff --git a/Network/Sync.gd b/Network/Sync.gd
new file mode 100644
index 0000000..cb81ec8
--- /dev/null
+++ b/Network/Sync.gd
@@ -0,0 +1,6 @@
+extends Node
+
+# rpc sync functions
+
+func _ready():
+ pass