@ -16,6 +16,10 @@ public partial class MultiplayerManager : Node
}
public Player GetPlayerByPeerId ( int peerId )
= > Players . GetNode < Player > ( peerId . ToString ( ) ) ;
public void Connect ( string address , ushort port )
{
var peer = new ENetMultiplayerPeer ( ) ;
@ -41,7 +45,7 @@ public partial class MultiplayerManager : Node
void OnMultiplayerReady ( )
{
var localId = Multiplayer . GetUniqueId ( ) ;
LocalPlayer . Name = localId . ToString ( ) ;
LocalPlayer . PeerId = localId ;
LocalPlayer . SetMultiplayerAuthority ( localId ) ;
if ( ! Multiplayer . IsServer ( ) )
@ -53,9 +57,10 @@ public partial class MultiplayerManager : Node
void OnMultiplayerDisconnected ( )
{
LocalPlayer . Name = "Local" ;
foreach ( var player in Players . GetChildren ( ) . Cast < Player > ( ) )
if ( ! player . IsLocal ) OnPeerDisconnected ( player . PeerId ) ;
foreach ( var player in Players . GetChildren ( ) . Cast < Player > ( ) ) {
if ( player . IsLocal ) player . PeerId = 0 ;
else OnPeerDisconnected ( player . PeerId ) ;
}
Multiplayer . MultiplayerPeer . Close ( ) ;
Multiplayer . MultiplayerPeer = null ;
}
@ -65,8 +70,6 @@ public partial class MultiplayerManager : Node
{
var player = PlayerScene . Instantiate < Player > ( ) ;
player . SetMultiplayerAuthority ( ( int ) peerId ) ;
player . Name = peerId . ToString ( ) ;
player . IsLocal = false ;
player . PeerId = ( int ) peerId ;
Players . AddChild ( player ) ;
PlayerJoined ? . Invoke ( player ) ;
@ -74,7 +77,7 @@ public partial class MultiplayerManager : Node
void OnPeerDisconnected ( long peerId )
{
var player = Players . GetNode < Player > ( peerId . ToString ( ) ) ;
var player = GetPlayerByPeerId ( ( int ) peerId ) ;
PlayerLeft ? . Invoke ( player ) ;
player . QueueFree ( ) ;
}