2024-02-27 02:54:47 +00:00
|
|
|
extends Node
|
|
|
|
class_name Game
|
|
|
|
|
|
|
|
|
2024-02-28 05:30:45 +00:00
|
|
|
signal trackers_changed(trackers: Dictionary)
|
|
|
|
|
|
|
|
func _on_trackers_changed(trackers: Dictionary):
|
|
|
|
trackers_changed.emit(trackers)
|
|
|
|
|
|
|
|
|
|
|
|
@onready var mp_tracker: MultiplePlayersTracker = $"MultiplePlayersTracker"
|
|
|
|
|
|
|
|
|
2024-02-27 02:54:47 +00:00
|
|
|
# This cannot be called in _ready because the multiplayer node is set *after* adding the node to the tree.
|
|
|
|
func init_signals():
|
|
|
|
print("[Game] Initializing signals...")
|
|
|
|
multiplayer.connected_to_server.connect(_on_connected_to_server)
|
|
|
|
multiplayer.server_disconnected.connect(_on_server_disconnected)
|
|
|
|
multiplayer.connection_failed.connect(_on_connection_failed)
|
|
|
|
multiplayer.peer_connected.connect(_on_peer_connected)
|
|
|
|
multiplayer.peer_disconnected.connect(_on_peer_disconnected)
|
|
|
|
|
|
|
|
|
2024-02-28 05:30:45 +00:00
|
|
|
var local_player_name: String = "Player"
|
|
|
|
var local_player_color: Color = Color.WHITE
|
|
|
|
|
|
|
|
func init_identity(player_name: String, player_color: Color):
|
|
|
|
local_player_name = player_name
|
|
|
|
local_player_color = player_color
|
|
|
|
|
|
|
|
|
2024-02-27 02:54:47 +00:00
|
|
|
func _on_connected_to_server():
|
2024-02-28 05:30:45 +00:00
|
|
|
Log.peer(self, "Connected to server!")
|
2024-02-27 02:54:47 +00:00
|
|
|
|
|
|
|
func _on_server_disconnected():
|
2024-02-28 05:30:45 +00:00
|
|
|
Log.peer(self, "Server disconnected...")
|
2024-02-27 02:54:47 +00:00
|
|
|
|
|
|
|
func _on_connection_failed():
|
2024-02-28 05:30:45 +00:00
|
|
|
Log.peer(self, "Connection failed...")
|
2024-02-27 02:54:47 +00:00
|
|
|
|
|
|
|
func _on_peer_connected(peer_id: int):
|
2024-02-28 05:30:45 +00:00
|
|
|
Log.peer(self, "Peer connected: " + str(peer_id))
|
|
|
|
if multiplayer.is_server():
|
|
|
|
assert(peer_id != 1, "This is a server, but the remote peer_id is 1.")
|
|
|
|
Log.peer(self, "Initializing tracker for: " + str(peer_id))
|
2024-03-07 04:06:37 +00:00
|
|
|
mp_tracker.create(peer_id)
|
|
|
|
# Log.peer(self, "Setting multiplayer authority: " + str(peer_id))
|
|
|
|
# var sp_tracker = mp_tracker.find(peer_id)
|
|
|
|
# sp_tracker.set_multiplayer_authority(peer_id)
|
2024-02-27 02:54:47 +00:00
|
|
|
|
|
|
|
func _on_peer_disconnected(peer_id: int):
|
2024-02-28 05:30:45 +00:00
|
|
|
Log.peer(self, "Peer disconnected: " + str(peer_id))
|
|
|
|
if multiplayer.is_server():
|
|
|
|
assert(peer_id != 1, "This is a server, but the remote peer_id is 1.")
|
|
|
|
Log.peer(self, "Deinitializing tracker for: " + str(peer_id))
|
|
|
|
mp_tracker.mark_disconnected.rpc(peer_id)
|
2024-03-07 04:06:37 +00:00
|
|
|
# Log.peer(self, "Unsetting multiplayer authority: " + str(peer_id))
|
|
|
|
# var sp_tracker = mp_tracker.find(peer_id)
|
|
|
|
# sp_tracker.set_multiplayer_authority(1)
|
2024-02-28 05:30:45 +00:00
|
|
|
|
2024-03-07 04:06:37 +00:00
|
|
|
func _on_single_player_tracker_created(spawned_tracker: SinglePlayerTracker) -> void:
|
2024-02-28 05:30:45 +00:00
|
|
|
# If we spawned ourselves, set our own identity
|
|
|
|
if spawned_tracker.get_multiplayer_authority() == multiplayer.multiplayer_peer.get_unique_id():
|
2024-03-07 04:06:37 +00:00
|
|
|
Log.peer(self, "Checking if we should notify our identity to everybody else...")
|
2024-02-28 05:30:45 +00:00
|
|
|
spawned_tracker.update_identity.rpc(local_player_name, local_player_color)
|
|
|
|
# If another player spawned, and they connected before us, send them our identity
|
2024-03-07 04:06:37 +00:00
|
|
|
elif not multiplayer.is_server():
|
|
|
|
Log.peer(self, "Checking if we should notify our identity to: " + str(spawned_tracker.get_multiplayer_authority()))
|
2024-02-28 05:30:45 +00:00
|
|
|
var self_tracker = mp_tracker.find(multiplayer.multiplayer_peer.get_unique_id())
|
|
|
|
if self_tracker != null:
|
|
|
|
self_tracker.notify_identity(spawned_tracker.get_multiplayer_authority())
|
2024-03-07 04:06:37 +00:00
|
|
|
# If we're the server, broadcast information about disconnected players
|
|
|
|
else:
|
|
|
|
Log.peer(self, "Checking if we should notify identity for disconnected peers...")
|
|
|
|
for tracker in mp_tracker.find_children("", "SinglePlayerTracker", true, false):
|
|
|
|
Log.peer(self, str(tracker))
|
|
|
|
if tracker.get_multiplayer_authority() == 1:
|
|
|
|
Log.peer(self, "I'm the authority, do notify.")
|
|
|
|
tracker.notify_identity(spawned_tracker.get_multiplayer_authority())
|
|
|
|
else:
|
|
|
|
Log.peer(self, "I'm not the authority, do not notify.")
|