0.0.1
This commit is contained in:
commit
fadea22b9d
26 changed files with 741 additions and 0 deletions
81
global/multiplayer.gd
Normal file
81
global/multiplayer.gd
Normal file
|
@ -0,0 +1,81 @@
|
|||
class_name MultiplayerClass
|
||||
extends Node
|
||||
|
||||
const ADDRESS: String = "127.0.0.1"
|
||||
const PORT: int = 4516
|
||||
|
||||
var peer: ENetMultiplayerPeer = null
|
||||
var players: Array = []
|
||||
|
||||
## Host Specific
|
||||
|
||||
# Hosts server
|
||||
func host_server() -> void:
|
||||
peer = ENetMultiplayerPeer.new()
|
||||
if peer.create_server(PORT) != OK:
|
||||
peer = null
|
||||
return
|
||||
multiplayer.multiplayer_peer = peer
|
||||
if multiplayer.peer_connected.connect(_on_player_joined) != OK:
|
||||
print("Failed to connect signal")
|
||||
if multiplayer.peer_disconnected.connect(_on_player_left) != OK:
|
||||
print("Failed to connect signal")
|
||||
_on_player_joined(multiplayer.get_unique_id())
|
||||
|
||||
# Runs when player joins
|
||||
func _on_player_joined(id: int) -> void:
|
||||
players.push_back({
|
||||
"id": id,
|
||||
"loaded": false,
|
||||
"hunter": false,
|
||||
})
|
||||
|
||||
# Runs when player leaves
|
||||
func _on_player_left(id: int) -> void:
|
||||
players = players.filter(func (player: Dictionary) -> bool: return player.id != id)
|
||||
|
||||
# Switches the scene, and waits until everyone is fully loaded.
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func switch_scene(path: String) -> void:
|
||||
if multiplayer.is_server():
|
||||
_scene_loaded.rpc_id(1)
|
||||
while true:
|
||||
var loaded: bool = true
|
||||
for player: Dictionary in players:
|
||||
if player.loaded == false:
|
||||
loaded = false
|
||||
break
|
||||
if loaded:
|
||||
break
|
||||
await get_tree().create_timer(0.1).timeout
|
||||
if get_tree().change_scene_to_file(path) != OK:
|
||||
print("Failed to change to scene.")
|
||||
else:
|
||||
if get_tree().change_scene_to_file(path) != OK:
|
||||
print("Failed to change to scene.")
|
||||
_scene_loaded.rpc_id(1)
|
||||
|
||||
# Callback to switch_scene from the remote peer
|
||||
@rpc("any_peer", "call_local", "reliable")
|
||||
func _scene_loaded() -> void:
|
||||
var id: int = multiplayer.get_remote_sender_id()
|
||||
if multiplayer.is_server():
|
||||
for i: int in range(players.size()):
|
||||
if players[i].id == multiplayer.get_remote_sender_id():
|
||||
players[i].loaded = true
|
||||
|
||||
## Client Specific
|
||||
|
||||
# Joins a server
|
||||
func join_server() -> void:
|
||||
peer = ENetMultiplayerPeer.new()
|
||||
if peer.create_client(ADDRESS, PORT) != OK:
|
||||
peer = null
|
||||
return
|
||||
multiplayer.multiplayer_peer = peer
|
||||
|
||||
## Shared
|
||||
|
||||
# Terminates all Multiplayer functionality.
|
||||
func terminate_multiplayer() -> void:
|
||||
multiplayer.multiplayer_peer = null
|
Loading…
Add table
Add a link
Reference in a new issue