2024-02-07 23:25:08 +03:00
|
|
|
extends Node
|
2024-02-12 00:11:50 +03:00
|
|
|
var player_script := preload("res://scripts/Player.gd")
|
2024-02-07 23:25:08 +03:00
|
|
|
|
2024-02-14 22:18:05 +03:00
|
|
|
var server_map
|
|
|
|
|
2024-02-07 23:25:08 +03:00
|
|
|
var player_model = preload("res://models/player.tscn")
|
|
|
|
var peer = ENetMultiplayerPeer.new()
|
|
|
|
var settings
|
|
|
|
var clients = {}
|
2024-02-14 22:18:05 +03:00
|
|
|
var last_client_id = 1
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
func _ready():
|
|
|
|
if "--server" in OS.get_cmdline_args():
|
|
|
|
await get_tree().create_timer(0.01).timeout #костыль пиздец но мне похую как-то
|
|
|
|
get_tree().change_scene_to_file("res://scenes/test_map.tscn")
|
2024-02-14 22:18:05 +03:00
|
|
|
await get_tree().create_timer(0.05).timeout #костыль
|
|
|
|
server_map = get_tree().root.get_node("test_map")
|
2024-02-07 23:25:08 +03:00
|
|
|
StartServer()
|
|
|
|
|
|
|
|
#######################################SERVER####################################
|
|
|
|
func StartServer():
|
|
|
|
settings = GameData.server_settings
|
|
|
|
var port = int(settings["port"])
|
|
|
|
var maxclients = int(settings["maxclients"])
|
|
|
|
if (peer.create_server(port, maxclients) != OK):
|
|
|
|
print("Couldn't create server. Check if another proccess binds port " + str(port))
|
|
|
|
return
|
|
|
|
|
|
|
|
multiplayer.multiplayer_peer = peer
|
|
|
|
print("Server started")
|
|
|
|
|
|
|
|
peer.connect("peer_connected", _Peer_Connected)
|
|
|
|
peer.connect("peer_disconnected", _Peer_Disconnected)
|
|
|
|
|
|
|
|
func send_everyone_except(client_id, args):
|
2024-02-08 21:16:31 +03:00
|
|
|
if (typeof(args[0]) == 4): #string
|
|
|
|
for current_client_id in clients.keys():
|
|
|
|
if (str(current_client_id) == str(client_id)): continue
|
|
|
|
if (args.size() == 1): rpc_id(int(current_client_id), args[0])
|
|
|
|
elif (args.size() == 2): rpc_id(int(current_client_id), args[0], args[1])
|
2024-02-15 13:10:01 +03:00
|
|
|
elif (args.size() == 3): rpc_id(int(current_client_id), args[0], args[1], args[2])
|
|
|
|
else: rpc_id(int(current_client_id), args[0], args[1], args[2], args[3])
|
2024-02-08 21:16:31 +03:00
|
|
|
else:
|
|
|
|
for current_client_id in clients.keys():
|
|
|
|
if (str(current_client_id) == str(client_id)): continue
|
|
|
|
if (args.size() == 1): args[0].rpc_id(int(current_client_id))
|
|
|
|
elif (args.size() == 2): args[0].rpc_id(int(current_client_id), args[1])
|
2024-02-15 13:10:01 +03:00
|
|
|
elif (args.size() == 3): args[0].rpc_id(int(current_client_id), args[1], args[2])
|
|
|
|
else: args[0].rpc_id(int(current_client_id), args[1], args[2], args[3])
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
func _Peer_Connected(client_id):
|
2024-02-08 21:16:31 +03:00
|
|
|
print("User " + str(client_id) + " has conected")
|
2024-02-07 23:25:08 +03:00
|
|
|
var puppet = player_model.instantiate()
|
2024-02-14 22:18:05 +03:00
|
|
|
var internal_id = last_client_id + 1
|
|
|
|
last_client_id += 1
|
2024-02-08 21:58:36 +03:00
|
|
|
var CB3D = puppet.find_child("CharacterBody3D")
|
|
|
|
CB3D.set_is_playable(false)
|
|
|
|
CB3D.set_internal_id(internal_id)
|
|
|
|
CB3D.set_player_name("player" + str(internal_id))
|
|
|
|
|
2024-02-08 21:16:31 +03:00
|
|
|
clients[str(client_id)] = player_script.new().properties.duplicate()
|
2024-02-07 23:25:08 +03:00
|
|
|
clients[str(client_id)]["internal_id"] = internal_id
|
|
|
|
|
2024-02-14 22:18:05 +03:00
|
|
|
server_map.add_child(puppet)
|
2024-02-15 15:14:21 +03:00
|
|
|
|
|
|
|
@rpc("any_peer", "reliable", "call_remote")
|
|
|
|
func client_ready(client_id):
|
|
|
|
var client = clients[str(client_id)]
|
|
|
|
var internal_id = client["internal_id"]
|
|
|
|
print("Spawning new puppet: " + str(clients[str(client_id)]))
|
|
|
|
send_everyone_except(client_id, ["spawn_puppet", clients[str(client_id)]])
|
2024-02-07 23:25:08 +03:00
|
|
|
|
2024-02-08 21:16:31 +03:00
|
|
|
|
2024-02-07 23:25:08 +03:00
|
|
|
func _Peer_Disconnected(client_id):
|
2024-02-14 22:18:05 +03:00
|
|
|
print("User " + str(client_id) + " has disconnected")
|
|
|
|
var client = clients[str(client_id)]
|
|
|
|
var internal_id = client["internal_id"]
|
|
|
|
rpc("despawn_puppet", internal_id)
|
|
|
|
var puppet = server_map.get_node("player" + str(internal_id))
|
|
|
|
server_map.remove_child(puppet)
|
|
|
|
clients.erase(str(client_id))
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
@rpc ("any_peer", "reliable", "call_remote")
|
|
|
|
func get_character_properties(client_id):
|
|
|
|
if !clients.has(str(client_id)):
|
|
|
|
return
|
|
|
|
var to_send = clients[str(client_id)]
|
2024-02-08 21:16:31 +03:00
|
|
|
rpc_id(client_id, "set_character_properties", to_send.duplicate())
|
2024-02-07 23:25:08 +03:00
|
|
|
|
2024-02-08 21:20:49 +03:00
|
|
|
@rpc("any_peer", "call_remote", "unreliable")
|
2024-02-15 13:10:01 +03:00
|
|
|
func sync_client(client_id, position, rotation):
|
2024-02-07 23:25:08 +03:00
|
|
|
#if(settings["enableAntiCheat"]): if(position > 10): return # нормально допилить надо
|
|
|
|
var client = clients[str(client_id)]
|
2024-02-08 21:58:36 +03:00
|
|
|
var internal_id = client["internal_id"]
|
2024-02-08 21:16:31 +03:00
|
|
|
client["position"] = position
|
2024-02-15 13:10:01 +03:00
|
|
|
client["rotation"] = rotation
|
|
|
|
send_everyone_except(client_id, [server_map.get_node("player" + str(internal_id)).find_child("CharacterBody3D").sync_puppet, internal_id, position, rotation])
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
@rpc ("any_peer", "call_remote", "reliable")
|
|
|
|
func get_client_list(client_id):
|
|
|
|
for current_client_id in clients.keys():
|
|
|
|
if (current_client_id == str(client_id)): continue
|
2024-02-15 15:14:21 +03:00
|
|
|
rpc_id(client_id, "spawn_puppet", clients[str(current_client_id)])
|
2024-02-07 23:25:08 +03:00
|
|
|
|
2024-02-12 00:11:50 +03:00
|
|
|
@rpc ("any_peer", "call_remote", "reliable")
|
|
|
|
func get_server_settings(client_id):
|
2024-02-14 22:18:05 +03:00
|
|
|
print("Sending game settings")
|
|
|
|
var client = clients[str(client_id)]
|
|
|
|
var internal_id = client["internal_id"]
|
|
|
|
server_map.get_node("player" + str(internal_id)).find_child("CharacterBody3D").set_game_settings.rpc_id(int(client_id), settings["game"])
|
|
|
|
|
2024-02-15 15:14:21 +03:00
|
|
|
@rpc ("any_peer", "call_remote", "reliable")
|
|
|
|
func set_nickname(client_id, nickname):
|
|
|
|
clients[str(client_id)]["nickname"] = nickname
|
2024-02-14 22:18:05 +03:00
|
|
|
|
2024-02-07 23:25:08 +03:00
|
|
|
##########################################CLIENT#######################
|
|
|
|
|
|
|
|
var player
|
2024-02-14 22:18:05 +03:00
|
|
|
var menu = preload("res://scenes/menu.tscn")
|
|
|
|
var test_map = preload("res://scenes/test_map.tscn")
|
|
|
|
var current_map_instance
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
@rpc ("reliable", "call_remote")
|
|
|
|
func set_character_properties(p):
|
2024-02-08 21:16:31 +03:00
|
|
|
print("Setting player properties to " + str(p))
|
2024-02-07 23:25:08 +03:00
|
|
|
player = p
|
|
|
|
|
|
|
|
@rpc("authority", "reliable", "call_remote")
|
2024-02-15 15:14:21 +03:00
|
|
|
func spawn_puppet(properties):
|
2024-02-07 23:25:08 +03:00
|
|
|
var puppet = player_model.instantiate()
|
2024-02-15 15:14:21 +03:00
|
|
|
var internal_id = int(properties["internal_id"])
|
2024-02-07 23:25:08 +03:00
|
|
|
puppet.find_child("CharacterBody3D").set_is_playable(false)
|
|
|
|
puppet.find_child("CharacterBody3D").set_internal_id(internal_id)
|
2024-02-08 21:16:31 +03:00
|
|
|
puppet.find_child("CharacterBody3D").set_player_name("player" + str(internal_id))
|
2024-02-15 15:14:21 +03:00
|
|
|
print("Spawning puppet with properties: " + str(properties))
|
|
|
|
puppet.find_child("CharacterBody3D").set_nickname(properties["nickname"])
|
2024-02-08 21:16:31 +03:00
|
|
|
|
2024-02-07 23:25:08 +03:00
|
|
|
puppet.name = "player" + str(internal_id)
|
|
|
|
|
2024-02-14 22:18:05 +03:00
|
|
|
current_map_instance.add_child(puppet)
|
|
|
|
|
|
|
|
@rpc("authority", "reliable", "call_remote")
|
|
|
|
func despawn_puppet(internal_id):
|
|
|
|
var puppet = current_map_instance.get_node("player" + str(internal_id))
|
|
|
|
current_map_instance.remove_child(puppet)
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
func ConnectToServer(ip, port):
|
|
|
|
peer.create_client(ip, port)
|
|
|
|
multiplayer.multiplayer_peer = peer
|
|
|
|
|
|
|
|
if not multiplayer.connected_to_server.is_connected(_Connection_Succseeded):
|
|
|
|
multiplayer.connected_to_server.connect(_Connection_Succseeded)
|
|
|
|
if not multiplayer.connection_failed.is_connected(_Connection_Failed):
|
|
|
|
multiplayer.connection_failed.connect(_Connection_Failed)
|
|
|
|
if not multiplayer.server_disconnected.is_connected(_Server_Disconnected):
|
|
|
|
multiplayer.server_disconnected.connect(_Server_Disconnected)
|
|
|
|
|
|
|
|
func _Connection_Failed():
|
|
|
|
print("Failed to connect to server")
|
|
|
|
multiplayer.multiplayer_peer = null
|
2024-02-14 22:18:05 +03:00
|
|
|
peer = ENetMultiplayerPeer.new()
|
|
|
|
multiplayer.multiplayer_peer = peer
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
func _Connection_Succseeded():
|
|
|
|
print("Succsessfully connected to the server")
|
2024-02-15 15:14:21 +03:00
|
|
|
var nickname = GameData.client_settings["nickname"]
|
|
|
|
rpc_id(1, "set_nickname", multiplayer.get_unique_id(), nickname)
|
2024-02-07 23:25:08 +03:00
|
|
|
rpc_id(1, "get_character_properties", multiplayer.get_unique_id())
|
|
|
|
|
|
|
|
get_tree().change_scene_to_file("res://scenes/test_map.tscn")
|
2024-02-14 22:18:05 +03:00
|
|
|
await get_tree().create_timer(1).timeout # костыль
|
|
|
|
current_map_instance = get_tree().root.get_node("test_map")
|
|
|
|
rpc_id(1, "get_client_list", multiplayer.get_unique_id())
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
var player_node = player_model.instantiate()
|
|
|
|
player_node.find_child("CharacterBody3D").set_is_playable(true)
|
|
|
|
player_node.find_child("CharacterBody3D").set_internal_id(player["internal_id"])
|
2024-02-08 21:16:31 +03:00
|
|
|
player_node.find_child("CharacterBody3D").set_player_name("player" + str(player["internal_id"]))
|
2024-02-15 15:14:21 +03:00
|
|
|
player_node.find_child("CharacterBody3D").set_nickname(nickname)
|
|
|
|
|
2024-02-14 22:18:05 +03:00
|
|
|
current_map_instance.add_child(player_node)
|
|
|
|
await get_tree().create_timer(0.05).timeout # костыль
|
|
|
|
rpc_id(1, "get_server_settings", multiplayer.get_unique_id())
|
|
|
|
|
2024-02-15 15:14:21 +03:00
|
|
|
rpc_id(1, "client_ready", multiplayer.get_unique_id())
|
2024-02-07 23:25:08 +03:00
|
|
|
|
|
|
|
func _Server_Disconnected():
|
|
|
|
print("Server has disconnected")
|
2024-02-14 22:18:05 +03:00
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
get_tree().change_scene_to_file("res://scenes/menu.tscn")
|
|
|
|
multiplayer.multiplayer_peer = null
|
|
|
|
peer = ENetMultiplayerPeer.new()
|
|
|
|
multiplayer.multiplayer_peer = peer
|