feat: Networked Keyboard Inputs Finished
This commit is contained in:
		| @@ -9,8 +9,8 @@ namespace Pong.Behaviours; | |||||||
|  |  | ||||||
| public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys> | public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys> | ||||||
| { | { | ||||||
|     private readonly Dictionary<Keys, Action<IButtonInputs<Keys>, Keys>> OnPressed = new(256); |     private readonly Dictionary<Keys, Action<IButtonInputs<Keys>, Keys>?> OnPressed = new(256); | ||||||
|     private readonly Dictionary<Keys, Action<IButtonInputs<Keys>, Keys>> OnReleased = new(256); |     private readonly Dictionary<Keys, Action<IButtonInputs<Keys>, Keys>?> OnReleased = new(256); | ||||||
|  |  | ||||||
|     private int cachePressedCurrentlyCount = 0; |     private int cachePressedCurrentlyCount = 0; | ||||||
|     private readonly Keys[] cachePressedCurrently = new Keys[256]; |     private readonly Keys[] cachePressedCurrently = new Keys[256]; | ||||||
| @@ -23,6 +23,8 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys> | |||||||
|         if (OnPressed.TryGetValue(key, out var action)) |         if (OnPressed.TryGetValue(key, out var action)) | ||||||
|         { |         { | ||||||
|             action += callback; |             action += callback; | ||||||
|  |             OnPressed.Remove(key); | ||||||
|  |             OnPressed.Add(key, action); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -40,6 +42,8 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys> | |||||||
|         if (OnReleased.TryGetValue(key, out var action)) |         if (OnReleased.TryGetValue(key, out var action)) | ||||||
|         { |         { | ||||||
|             action += callback; |             action += callback; | ||||||
|  |             OnReleased.Remove(key); | ||||||
|  |             OnReleased.Add(key, action); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -71,7 +75,7 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys> | |||||||
|             if (WasPressed(currentlyPressedKey)) |             if (WasPressed(currentlyPressedKey)) | ||||||
|                 continue; |                 continue; | ||||||
|  |  | ||||||
|             action.Invoke(this, currentlyPressedKey); |             action?.Invoke(this, currentlyPressedKey); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         for (int i = 0; i < cachePressedPreviouslyCount; i++) |         for (int i = 0; i < cachePressedPreviouslyCount; i++) | ||||||
| @@ -84,7 +88,7 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys> | |||||||
|             if (IsPressed(previouslyPressedKey)) |             if (IsPressed(previouslyPressedKey)) | ||||||
|                 continue; |                 continue; | ||||||
|  |  | ||||||
|             action.Invoke(this, previouslyPressedKey); |             action?.Invoke(this, previouslyPressedKey); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         Array.Copy(cachePressedCurrently, cachePressedPreviously, cachePressedCurrentlyCount); |         Array.Copy(cachePressedCurrently, cachePressedPreviously, cachePressedCurrentlyCount); | ||||||
| @@ -107,6 +111,34 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys> | |||||||
|         return false; |         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) |     protected override void OnMessageReceived(NetPacketReader reader, NetPeer peer) | ||||||
|     { |     { | ||||||
|         bool isPressed = reader.GetBool(); |         bool isPressed = reader.GetBool(); | ||||||
| @@ -114,9 +146,9 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys> | |||||||
|  |  | ||||||
|         if (isPressed) |         if (isPressed) | ||||||
|             if (OnPressed.TryGetValue(key, out var action)) |             if (OnPressed.TryGetValue(key, out var action)) | ||||||
|                 action.Invoke(this, key); |                 action?.Invoke(this, key); | ||||||
|         if (!isPressed) |         if (!isPressed) | ||||||
|             if (OnReleased.TryGetValue(key, out var action)) |             if (OnReleased.TryGetValue(key, out var action)) | ||||||
|                 action.Invoke(this, key); |                 action?.Invoke(this, key); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user