This commit is contained in:
Patrick 2025-07-31 13:57:50 +02:00
parent fadea22b9d
commit d72b5ac57b
14 changed files with 184 additions and 5 deletions

View file

@ -5,7 +5,10 @@ const ADDRESS: String = "127.0.0.1"
const PORT: int = 4516
var peer: ENetMultiplayerPeer = null
var players: Array = []
var players: Array[Dictionary] = []
var fuseboxes: int = 0
var playerlist_label: Label = null
## Host Specific
@ -28,15 +31,31 @@ func _on_player_joined(id: int) -> void:
"id": id,
"loaded": false,
"hunter": false,
"captured": false,
"escaped": false,
})
send_playerlist()
# Runs when player leaves
func _on_player_left(id: int) -> void:
players = players.filter(func (player: Dictionary) -> bool: return player.id != id)
send_playerlist()
# Prepares the label text for the playerlist on the join/host screen
func send_playerlist() -> void:
var playerlist: String = ""
for player: Dictionary in players:
playerlist += str(player.id)
playerlist += "\n"
update_playerlist.rpc(playerlist)
# Switches the scene, and waits until everyone is fully loaded.
@rpc("authority", "call_local", "reliable")
func switch_scene(path: String) -> void:
playerlist_label = null
if multiplayer.is_server():
_scene_loaded.rpc_id(1)
while true:
@ -64,6 +83,15 @@ func _scene_loaded() -> void:
if players[i].id == multiplayer.get_remote_sender_id():
players[i].loaded = true
func _process(_delta: float) -> void:
if multiplayer.multiplayer_peer and multiplayer.is_server() and !players.is_empty():
var lost: bool = true
for player: Dictionary in players:
if (!player.captured or !player.escaped) and !player.hunter:
lost = false
if lost:
terminate_multiplayer.rpc()
## Client Specific
# Joins a server
@ -77,5 +105,17 @@ func join_server() -> void:
## Shared
# Terminates all Multiplayer functionality.
@rpc("authority", "call_local", "reliable")
func terminate_multiplayer() -> void:
multiplayer.multiplayer_peer = null
players = []
fuseboxes = 0
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if get_tree().change_scene_to_file("res://ui/main_menu.tscn") != OK:
print("Failed to change to scene.")
# Updates the player list
@rpc("authority", "call_local", "reliable")
func update_playerlist(list: String) -> void:
if playerlist_label != null:
playerlist_label.text = list