From cafbc55780dc239521a7bc5a29b02a43cfb7ae77 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 13 Feb 2024 11:16:43 +0300 Subject: [PATCH] feat: Networked Keyboard Inputs Finished --- Game/Behaviours/NetworkedKeyboardInputs.cs | 44 +++++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/Game/Behaviours/NetworkedKeyboardInputs.cs b/Game/Behaviours/NetworkedKeyboardInputs.cs index 8169cde..03153d9 100644 --- a/Game/Behaviours/NetworkedKeyboardInputs.cs +++ b/Game/Behaviours/NetworkedKeyboardInputs.cs @@ -9,8 +9,8 @@ namespace Pong.Behaviours; public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs { - private readonly Dictionary, Keys>> OnPressed = new(256); - private readonly Dictionary, Keys>> OnReleased = new(256); + private readonly Dictionary, Keys>?> OnPressed = new(256); + private readonly Dictionary, Keys>?> OnReleased = new(256); private int cachePressedCurrentlyCount = 0; private readonly Keys[] cachePressedCurrently = new Keys[256]; @@ -23,6 +23,8 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs if (OnPressed.TryGetValue(key, out var action)) { action += callback; + OnPressed.Remove(key); + OnPressed.Add(key, action); return; } @@ -40,6 +42,8 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs if (OnReleased.TryGetValue(key, out var action)) { action += callback; + OnReleased.Remove(key); + OnReleased.Add(key, action); return; } @@ -71,7 +75,7 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs if (WasPressed(currentlyPressedKey)) continue; - action.Invoke(this, currentlyPressedKey); + action?.Invoke(this, currentlyPressedKey); } for (int i = 0; i < cachePressedPreviouslyCount; i++) @@ -84,7 +88,7 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs if (IsPressed(previouslyPressedKey)) continue; - action.Invoke(this, previouslyPressedKey); + action?.Invoke(this, previouslyPressedKey); } Array.Copy(cachePressedCurrently, cachePressedPreviously, cachePressedCurrentlyCount); @@ -107,6 +111,34 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs return false; } + protected override void OnInitialize() + { + base.OnInitialize(); + foreach (Keys key in Enum.GetValues(typeof(Keys))) + { + RegisterOnPress(key, (keys, keyVal) => + { + if (!LocalAssigned && !IsServer) + return; + + var netDataWriter = NetworkCommunicator.GetMessageWriter(this); + netDataWriter.Put(true); + netDataWriter.Put((int)key); + NetworkCommunicator.Manager.SendToAll(netDataWriter, DeliveryMethod.ReliableOrdered); + }); + RegisterOnRelease(key, (keys, keyVal) => + { + if (!LocalAssigned && !IsServer) + return; + + var netDataWriter = NetworkCommunicator.GetMessageWriter(this); + netDataWriter.Put(false); + netDataWriter.Put((int)key); + NetworkCommunicator.Manager.SendToAll(netDataWriter, DeliveryMethod.ReliableOrdered); + }); + } + } + protected override void OnMessageReceived(NetPacketReader reader, NetPeer peer) { bool isPressed = reader.GetBool(); @@ -114,9 +146,9 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs if (isPressed) if (OnPressed.TryGetValue(key, out var action)) - action.Invoke(this, key); + action?.Invoke(this, key); if (!isPressed) if (OnReleased.TryGetValue(key, out var action)) - action.Invoke(this, key); + action?.Invoke(this, key); } }