project_teddy/scripts/networking.gd

90 lines
2.7 KiB
GDScript3
Raw Normal View History

extends Node
var peer = ENetMultiplayerPeer.new()
2023-02-22 21:31:22 -07:00
var SCENE = "res://scenes/toyland.tscn"
2023-02-21 13:34:05 -07:00
var connected_peers = []
2023-02-21 12:53:13 -07:00
func create_server(port, maxPlayers):
2023-03-05 20:15:46 -07:00
peer.close()
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):
print("NEW CONNECTION FROM ", 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-06 00:35:22 -07:00
rpc_id(new_peer_id, "sync_names", Global.connectedPlayers)
2023-03-06 19:07:22 -07:00
rpc_id(new_peer_id, "sync_respawn_speed", Global.respawnTimeModifier)
rpc("add_newly_connected_player_character", new_peer_id)
rpc_id(new_peer_id, "add_previously_connected_player_characters", connected_peers)
2023-02-22 21:31:22 -07:00
add_player_node(new_peer_id)
)
2023-02-28 11:48:41 -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:
load_map(SCENE)
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)
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)
player_character.position.y = 10
@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-02-28 11:48:41 -07:00
@rpc
func disconnect_peer(peer_id):
peer_id.queue_free()
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
@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