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