1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
extends Node
signal connection_succeeded()
signal connection_failed()
signal player_list_changed()
signal game_error()
signal game_started()
signal game_ended()
const PORT = 10567
const MAX_PEERS = 2
var peer = null
var ip = null
var player = {
idx = 0,
name = "",
character = "Tux",
}
# 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_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,
ip = self.peer.get_peer_address(id),
}
emit_signal("player_list_changed")
remote func unregister_player(id):
self.players.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 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:
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():
emit_signal("game_started")
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):
print("winning_player")
emit_signal("game_ended", winning_player)
func _player_connected(_id):
rpc("register_player", self.player.name)
func _player_disconnected(id):
# if game is in progress:
# self.end_game() ?
# else
#rpc("unregister_player", id)
rpc("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("")
|