From 1f001c03b7d7d4fa7b4a2206a8d2fd5abfceae98 Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Sat, 8 Aug 2020 01:15:33 +0300 Subject: [PATCH] Commit the last half of the previous commit --- Assets/Prefabs/LocalPlayer.prefab | 1 + Assets/Scripts/Networking/Instances/Client.cs | 10 +++++----- Assets/Scripts/Networking/Instances/Server.cs | 8 ++++++-- Assets/Scripts/Players/LocalPlayer.cs | 4 +++- Assets/Scripts/Players/Player.cs | 18 +++++++++++++----- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Assets/Prefabs/LocalPlayer.prefab b/Assets/Prefabs/LocalPlayer.prefab index 35108ca..42a8735 100644 --- a/Assets/Prefabs/LocalPlayer.prefab +++ b/Assets/Prefabs/LocalPlayer.prefab @@ -237,6 +237,7 @@ MonoBehaviour: m_EditorClassIdentifier: Camera: {fileID: 9173392441446035453} DisableInput: 0 + FullSyncFrequency: 2 --- !u!114 &3508289943927587728 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Networking/Instances/Client.cs b/Assets/Scripts/Networking/Instances/Client.cs index d809eeb..edf85e2 100644 --- a/Assets/Scripts/Networking/Instances/Client.cs +++ b/Assets/Scripts/Networking/Instances/Client.cs @@ -80,8 +80,8 @@ namespace NeonTea.Quakeball.Networking.Instances { PlayerJumpPckt jump = (PlayerJumpPckt)packet; Players[jump.PlayerId].Controlled.Jump(); } else if (packet is PlayerSyncPacket) { - PlayerSyncPacket sync = (PlayerSyncPacket)packet; - + PlayerSyncPacket syncPckt = (PlayerSyncPacket)packet; + Players[syncPckt.PlayerId].Controlled.ProcessSyncPacket(syncPckt); } } @@ -89,7 +89,7 @@ namespace NeonTea.Quakeball.Networking.Instances { if (pckt.PlayerId == LocalPlayer.Id) { return; // Ignore, again. } - Players[pckt.PlayerId].Controlled.ProcessUpdatePacket(ref pckt); + Players[pckt.PlayerId].Controlled.ProcessUpdatePacket(pckt); } public override void UpdateLocalPlayer() { @@ -108,8 +108,8 @@ namespace NeonTea.Quakeball.Networking.Instances { public override void SendPlayerSync() { if (SelfIdentified && Server != null) { - PlayerSyncPacket packet; // Get the sync packet for LocalPlayer - // SendUnreliable(Server.uid, packet); + PlayerSyncPacket packet = LocalPlayer.Controlled.CreateSyncPacket(); + Peer.SendUnreliable(Server.uid, packet); } } } diff --git a/Assets/Scripts/Networking/Instances/Server.cs b/Assets/Scripts/Networking/Instances/Server.cs index 14b5f75..0b1bdc2 100644 --- a/Assets/Scripts/Networking/Instances/Server.cs +++ b/Assets/Scripts/Networking/Instances/Server.cs @@ -78,7 +78,7 @@ namespace NeonTea.Quakeball.Networking.Instances { PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet; if (Players[conn.uid].Controlled != null) { updatePckt.PlayerId = conn.uid; - Players[conn.uid].Controlled.ProcessUpdatePacket(ref updatePckt); + Players[conn.uid].Controlled.ProcessUpdatePacket(updatePckt); } } else if (packet is PlayerJumpPckt) { PlayerJumpPckt jump = (PlayerJumpPckt)packet; @@ -89,7 +89,11 @@ namespace NeonTea.Quakeball.Networking.Instances { } } } else if (packet is PlayerSyncPacket) { - PlayerSyncPacket sync = (PlayerSyncPacket)packet; + PlayerSyncPacket syncPckt = (PlayerSyncPacket)packet; + if (Players[conn.uid].Controlled != null) { + syncPckt.PlayerId = conn.uid; + Players[conn.uid].Controlled.ProcessSyncPacket(syncPckt); + } } } diff --git a/Assets/Scripts/Players/LocalPlayer.cs b/Assets/Scripts/Players/LocalPlayer.cs index a4fc0c0..f445490 100644 --- a/Assets/Scripts/Players/LocalPlayer.cs +++ b/Assets/Scripts/Players/LocalPlayer.cs @@ -92,7 +92,9 @@ namespace NeonTea.Quakeball.Players { if (Time.time - PreviousPlayerFullSync >= 1f / FullSyncFrequency) { FullSyncFrequency = Time.time; - // TODO: Create and send the packet + if (Networking.Net.Singleton.Instance != null) { + Networking.Net.Singleton.Instance.SendPlayerSync(); + } } } } diff --git a/Assets/Scripts/Players/Player.cs b/Assets/Scripts/Players/Player.cs index 0199749..b6ed5c4 100644 --- a/Assets/Scripts/Players/Player.cs +++ b/Assets/Scripts/Players/Player.cs @@ -65,8 +65,8 @@ namespace NeonTea.Quakeball.Players { return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Pitch, Yaw, id); } - /// Updates this Player with the given packet, and sets it to null. No reusing packets. - public void ProcessUpdatePacket(ref PlayerUpdatePckt packet) { + /// Updates this Player with the given packet. + public void ProcessUpdatePacket(PlayerUpdatePckt packet) { Pitch = packet.Pitch; Yaw = packet.Yaw; MoveDirection = packet.MoveDirection; @@ -74,13 +74,21 @@ namespace NeonTea.Quakeball.Players { packet = null; } - /// Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server + /// Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server. public PlayerSyncPacket CreateSyncPacket(ulong id = 0) { return new PlayerSyncPacket(id, transform.position, GroundVelocity); } - /// Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server - public void ProcessSyncPacket(PlayerSyncPacket syncPckt) { + /// Applies the sync packet, checking it for cheatiness if shouldApply is false. + public void ProcessSyncPacket(PlayerSyncPacket syncPckt, bool shouldApplyWithoutInspection = true) { + bool ShouldApply = shouldApplyWithoutInspection; + if (!shouldApplyWithoutInspection) { + // TODO: Gaze into the crystal ball to determine the nefariousness level of the packet and update ShouldApply accordingly. + } + if (ShouldApply) { + transform.position = syncPckt.Location; + GroundVelocity = syncPckt.Location; + } } /// The normal of the ground below the player. If there is no ground, it's Vector3.up by default.