2023-02-16 13:27:34 -07:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
var peer = ENetMultiplayerPeer.new()
|
2023-02-21 13:34:05 -07:00
|
|
|
var connected_peers = []
|
2023-02-21 12:53:13 -07:00
|
|
|
|
2023-02-16 13:27:34 -07:00
|
|
|
func create_server(port, maxPlayers):
|
2023-03-05 20:15:46 -07:00
|
|
|
peer.close()
|
2023-02-16 13:27:34 -07:00
|
|
|
var result = peer.create_server(port, maxPlayers) # Second paramater specifies the player limit. Can be anything between 1 and 4095
|
|
|
|
multiplayer.multiplayer_peer = peer
|
|
|
|
prints("Creating server result:", result)
|
|
|
|
if result == OK:
|
|
|
|
print("Server should be listening on port: ", port)
|
|
|
|
elif result == ERR_ALREADY_IN_USE:
|
|
|
|
printerr("Port is being used by something else!")
|
|
|
|
else:
|
|
|
|
printerr("Server can't start, and idk the problem")
|
|
|
|
|
|
|
|
|
|
|
|
peer.peer_connected.connect(
|
|
|
|
func(new_peer_id):
|
2023-02-21 13:34:05 -07:00
|
|
|
await get_tree().create_timer(0.5).timeout ## REQUIRED, THE RPC IS MADE BEFORE THE CLIENT IS FULLY CONNECTED
|
2023-03-17 12:11:47 -06:00
|
|
|
if connected_peers.has(new_peer_id):
|
|
|
|
print("CONFLICT ID FOUND")
|
|
|
|
return
|
2023-04-04 12:39:23 -06:00
|
|
|
elif Global.roundInSession == true:
|
|
|
|
print("GAME IN PROGRESS ", new_peer_id, " was declined entry")
|
|
|
|
return
|
2023-03-17 12:11:47 -06:00
|
|
|
else:
|
|
|
|
print("NEW CONNECTION FROM ", new_peer_id)
|
2023-03-18 01:11:03 -06:00
|
|
|
rpc_id(new_peer_id, "sync_mapPath", Global.currentMapPath)
|
2023-03-17 12:11:47 -06:00
|
|
|
rpc_id(new_peer_id, "sync_names", Global.connectedPlayers)
|
|
|
|
rpc_id(new_peer_id, "sync_respawn_speed", Global.respawnTimeModifier)
|
|
|
|
rpc_id(new_peer_id, "sync_gamemode", Global.gamemode)
|
|
|
|
rpc_id(new_peer_id, "sync_mapName", Global.mapName)
|
2023-03-19 16:00:06 -06:00
|
|
|
rpc_id(new_peer_id, "sync_roundTimer", Global.roundTimer)
|
2023-03-17 12:11:47 -06:00
|
|
|
rpc("add_newly_connected_player_character", new_peer_id)
|
|
|
|
rpc_id(new_peer_id, "add_previously_connected_player_characters", connected_peers)
|
|
|
|
add_player_node(new_peer_id)
|
2023-02-16 13:27:34 -07:00
|
|
|
)
|
2023-03-16 23:40:33 -06:00
|
|
|
|
2023-03-17 12:11:47 -06:00
|
|
|
peer.peer_disconnected.connect(
|
2023-04-15 15:04:16 -06:00
|
|
|
func():
|
|
|
|
peer_disconnected()
|
2023-03-16 23:40:33 -06:00
|
|
|
)
|
2023-02-16 13:27:34 -07:00
|
|
|
|
|
|
|
func load_map(map):
|
|
|
|
Global.multiplayerCurrent = true
|
|
|
|
get_tree().change_scene_to_file(map)
|
|
|
|
|
2023-02-24 13:43:51 -07:00
|
|
|
@rpc
|
2023-02-22 21:31:22 -07:00
|
|
|
func add_newly_connected_player_character(new_peer_id):
|
2023-02-21 13:34:05 -07:00
|
|
|
print("Connected to server!")
|
2023-02-25 14:31:15 -07:00
|
|
|
if Global.teddyAuthorityID == null:
|
2023-03-18 01:11:03 -06:00
|
|
|
load_map(Global.currentMapPath)
|
2023-02-25 14:31:15 -07:00
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
2023-02-22 21:31:22 -07:00
|
|
|
add_player_node(new_peer_id)
|
|
|
|
|
|
|
|
|
|
|
|
func add_player_node(peer_id):
|
|
|
|
connected_peers.append(peer_id)
|
2023-03-16 13:37:17 -06:00
|
|
|
Global.connectedPlayersByPeerID = connected_peers
|
2023-02-22 21:31:22 -07:00
|
|
|
var player_character = preload("res://scenes/Teddy.tscn").instantiate()
|
|
|
|
player_character.set_multiplayer_authority(peer_id)
|
|
|
|
await get_tree().create_timer(0.5).timeout
|
|
|
|
Global.currentMapNode.add_child(player_character)
|
|
|
|
|
|
|
|
@rpc
|
|
|
|
func add_previously_connected_player_characters(peer_ids):
|
|
|
|
await get_tree().create_timer(0.5).timeout
|
|
|
|
for peer_id in peer_ids:
|
|
|
|
add_player_node(peer_id)
|
|
|
|
|
2023-04-15 15:04:16 -06:00
|
|
|
func peer_disconnected():
|
2023-04-04 12:39:23 -06:00
|
|
|
print("Server connection lost...")
|
|
|
|
Global.HUDStartLabelText = "Connection lost to server!"
|
2023-03-06 00:35:22 -07:00
|
|
|
|
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func sync_names(connectedPlayers):
|
|
|
|
Global.connectedPlayers = connectedPlayers
|
|
|
|
rpc("sync_name_all_peers", Global.playerName)
|
|
|
|
sync_name_all_peers(Global.playerName)
|
|
|
|
|
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func sync_name_all_peers(name):
|
|
|
|
Global.connectedPlayers += [name]
|
|
|
|
print(Global.connectedPlayers)
|
2023-03-06 19:07:22 -07:00
|
|
|
|
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func sync_respawn_speed(speed):
|
|
|
|
Global.respawnTimeModifier = speed
|
2023-05-04 11:29:32 -06:00
|
|
|
Global.playerPleaseRespawn = true
|
2023-03-06 20:24:39 -07:00
|
|
|
|
2023-03-16 13:37:17 -06:00
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func sync_gamemode(gamemode):
|
|
|
|
Global.gamemode = gamemode
|
|
|
|
|
2023-03-16 22:59:05 -06:00
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func sync_mapName(map):
|
|
|
|
Global.mapName = map
|
|
|
|
|
2023-03-18 01:11:03 -06:00
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func sync_mapPath(path):
|
|
|
|
Global.currentMapPath = path
|
|
|
|
|
2023-03-19 16:00:06 -06:00
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func sync_roundTimer(time):
|
|
|
|
Global.roundTimer = time
|
|
|
|
|
2023-03-06 20:24:39 -07:00
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func check_name(name): # Returns true if it finds a matching name
|
|
|
|
for i in range(Global.connectedPlayers.size()):
|
|
|
|
if Global.playerName == Global.connectedPlayers[i]:
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
return false
|
|
|
|
|
|
|
|
@rpc("any_peer", "reliable")
|
|
|
|
func check_round_status(status):
|
|
|
|
pass
|
|
|
|
return true
|