-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionManager.gd
79 lines (64 loc) · 2.6 KB
/
ConnectionManager.gd
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
extends Node
const DEFAULT_IP = '127.0.0.1'
const DEFAULT_PORT = 31400
const MAX_PLAYERS = 5
var players = { }
var self_data = { name = '', position = Vector2(200, 100) }
signal player_disconnected
signal server_disconnected
func _ready():
print("\n\n\n<3 <3 <3 THANKS FOR HELPING ME TEST <3 <3 <3\n\n\n")
get_tree().connect('network_peer_disconnected', self, '_on_player_disconnected')
get_tree().connect('network_peer_connected', self, '_on_player_connected')
func create_server(player_nickname, character):
# host a game
print("Hosting game...")
self_data.name = player_nickname
self_data.character = character
players[1] = self_data
var peer = NetworkedMultiplayerENet.new()
peer.create_server(DEFAULT_PORT, MAX_PLAYERS)
get_tree().set_network_peer(peer)
get_tree().change_scene("res://Lobby.tscn")
func connect_to_server(ip, player_nickname, character):
# join a game
print("Joining Game...")
self_data.name = player_nickname
self_data.character = character
get_tree().connect('connected_to_server', self, '_connected_to_server')
var peer = NetworkedMultiplayerENet.new()
peer.create_client(ip, DEFAULT_PORT)
get_tree().set_network_peer(peer)
func _connected_to_server():
# joined a game
var local_player_id = get_tree().get_network_unique_id()
print(str(local_player_id) + " connected.")
players[local_player_id] = self_data
rpc('_send_player_info', local_player_id, self_data)
func _on_player_disconnected(id):
# disconnected
players.erase(id)
func _on_player_connected(connected_player_id):
print("Connected: " + str(connected_player_id))
var local_player_id = get_tree().get_network_unique_id()
if not(get_tree().is_network_server()):
rpc_id(1, '_request_player_info', local_player_id, connected_player_id)
remote func _request_player_info(request_from_id, player_id):
if get_tree().is_network_server():
rpc_id(request_from_id, '_send_player_info', player_id, players[player_id])
# A function to be used if needed. The purpose is to request all players in the current session.
remote func _request_players(request_from_id):
if get_tree().is_network_server():
for peer_id in players:
if( peer_id != request_from_id):
rpc_id(request_from_id, '_send_player_info', peer_id, players[peer_id])
remote func _send_player_info(id, info):
players[id] = info
var new_player = load('res://Player.tscn').instance()
new_player.name = str(id)
new_player.set_network_master(id)
get_tree().change_scene("res://Lobby.tscn")
#$'/root/Level1/YSort/Players'.add_child(new_player)
#new_player.init(info.name, info.position, info.character)
func update_position(id, position):
players[id].position = position