This repository has been archived on 2020-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
odyssey-old/Scenes/Global/Multiplayer.gd

70 lines
2 KiB
GDScript

extends Node
const SERVER_PORT = 5513
const MAX_PLAYERS = 30
export var player_name = ""
var player_info = {}
func _ready():
player_name = "tider-" + str(randi() % 1000)
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, "_connected_ok")
get_tree().connect("connection_failed", self, "_connected_fail")
get_tree().connect("server_disconnected", self, "_server_disconnected")
func host():
var peer = NetworkedMultiplayerENet.new()
peer.create_server(SERVER_PORT, MAX_PLAYERS)
get_tree().set_network_peer(peer)
print("Hosting")
player_info[1] = { name=player_name }
#mapLoader.map.spawn_player(peer.get_unique_id())
func join(addr: String):
var peer = NetworkedMultiplayerENet.new()
peer.create_client(addr, SERVER_PORT)
get_tree().network_peer = peer
print("Connecting to", addr)
func leave():
var peer = get_tree().network_peer
if get_tree().is_network_server():
#TODO Do something if server
pass
elif peer != null:
#TODO Send leave message
pass
get_tree().network_peer = null
func _player_connected(id):
rpc_id(id, "register_player", player_name)
if get_tree().is_network_server():
pass
#rpc_id(id, "spawn_players", mapLoader.map.all_players())
func _player_disconnected(id):
print(player_info[id].name, " (", id, ") disconnected")
player_info.erase(id)
func _connected_ok():
print("Connected to server")
func _server_disconnected():
print("Disconnected from server")
func _connected_fail():
print("Connection failed")
remote func register_player(username: String):
var id = get_tree().get_rpc_sender_id()
player_info[id] = { name=username }
print(player_info[id].name, " (", id, ") connected")
remote func spawn_players(players):
for player_id in players:
pass
#mapLoader.map.spawn_player(player_id)
# Spawn myself as well
#mapLoader.map.rpc("spawn_player", get_tree().network_peer.get_unique_id())