feat: Networked Keyboard Inputs Finished

This commit is contained in:
Syntriax 2024-02-13 11:16:43 +03:00
parent ea3c4a2d2a
commit cafbc55780
1 changed files with 38 additions and 6 deletions

View File

@ -9,8 +9,8 @@ namespace Pong.Behaviours;
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>> OnReleased = 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 int cachePressedCurrentlyCount = 0;
private readonly Keys[] cachePressedCurrently = new Keys[256];
@ -23,6 +23,8 @@ public class NetworkedKeyboardInputs : NetworkBehaviour, IButtonInputs<Keys>
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<Keys>
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<Keys>
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<Keys>
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<Keys>
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<Keys>
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);
}
}