Redo Player.IsLocal and .PeerId

- IsLocal is now false by default and
  set in the editor for the local player.
- PeerId is calculated from and sets the node's
  Name instead of having its own backing field.
- Add GetPlayerByPeerId to MultiplayerManager.
main
copygirl 11 months ago
parent 585768e48e
commit 647318cc95
  1. 1
      game.tscn
  2. 15
      player/Player.cs
  3. 17
      scripts/MultiplayerManager.cs

@ -30,6 +30,7 @@ PlayerScene = ExtResource("2_iv2f7")
[node name="Players" type="Node3D" parent="."] [node name="Players" type="Node3D" parent="."]
[node name="Local" parent="Players" instance=ExtResource("2_iv2f7")] [node name="Local" parent="Players" instance=ExtResource("2_iv2f7")]
IsLocal = true
[node name="OutlineViewportContainer" type="SubViewportContainer" parent="."] [node name="OutlineViewportContainer" type="SubViewportContainer" parent="."]
material = SubResource("ShaderMaterial_ke1l3") material = SubResource("ShaderMaterial_ke1l3")

@ -1,7 +1,18 @@
public partial class Player : CharacterBody3D public partial class Player : CharacterBody3D
{ {
public bool IsLocal { get; set; } = true; [Export] public bool IsLocal { get; set; }
public int PeerId { get; set; }
public int PeerId {
get {
if (int.TryParse(Name, out var result)) return result;
throw new InvalidOperationException($"'{Name}' could not be parsed to PeerId");
}
set {
if (value > 0) Name = value.ToString();
else if (IsLocal) Name = "Local";
else throw new InvalidOperationException("Non-local player can't have PeerId set to 0");
}
}
public MovementController Movement { get; private set; } public MovementController Movement { get; private set; }
public CameraController Camera { get; private set; } public CameraController Camera { get; private set; }

@ -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();
} }

Loading…
Cancel
Save