From 1869a92580189cc32059c0b69b51ae599a9180de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Sun, 14 Apr 2024 20:02:15 +0200 Subject: [PATCH] feat: key history algorithm --- Assets/Scripts/Domain/CustomInput.cs | 2 - Assets/Scripts/Domain/Input/GeminadaReader.cs | 57 +++++++++++++++++++ .../Domain/Input/GeminadaReader.cs.meta | 3 + Assets/Scripts/Domain/Input/InputReader.cs | 14 ++--- Assets/Scripts/Domain/KeyHistory.cs | 34 +++++++++++ Assets/Scripts/Domain/KeyHistory.cs.meta | 3 + Packages/manifest.json | 1 + Packages/packages-lock.json | 11 ++++ 8 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 Assets/Scripts/Domain/Input/GeminadaReader.cs create mode 100644 Assets/Scripts/Domain/Input/GeminadaReader.cs.meta create mode 100644 Assets/Scripts/Domain/KeyHistory.cs create mode 100644 Assets/Scripts/Domain/KeyHistory.cs.meta diff --git a/Assets/Scripts/Domain/CustomInput.cs b/Assets/Scripts/Domain/CustomInput.cs index b3754a3..8354277 100644 --- a/Assets/Scripts/Domain/CustomInput.cs +++ b/Assets/Scripts/Domain/CustomInput.cs @@ -17,7 +17,5 @@ namespace Domain { } public bool KeyDown(Type key) => _readers[key].KeyDown(); - public bool KeyPressed(Type key) => _readers[key].KeyPressed(); - public bool KeyUp(Type key) => _readers[key].KeyUp(); } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/GeminadaReader.cs b/Assets/Scripts/Domain/Input/GeminadaReader.cs new file mode 100644 index 0000000..2eafe68 --- /dev/null +++ b/Assets/Scripts/Domain/Input/GeminadaReader.cs @@ -0,0 +1,57 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Domain.Input { + public class GeminadaReader : InputReader { + protected override int Key { get; } = 0x33; + private int LKey { get; } = 76; + + private bool _lPressed; + private bool _lWasPressed; + private bool _dotPressed; + private bool _dotWasPressed; + + private List _lastPresses; + + public override void UpdateInput() { + if (UpdateLInput()) { + if (_lastPresses.Count == 0) { + _lastPresses.Add(LKey); + } else if (_lastPresses.Count >= 2) { + if (_lastPresses[^1] == Key && _lastPresses[^2] == LKey) { + IsPressed = true; + } + } + } + UpdateDotInput(); + } + + private bool UpdateDotInput() { + _dotWasPressed = _dotPressed; + short dotState = Win32API.GetAsyncKeyState(Key); + + if (!_dotWasPressed && dotState != 0) { + _dotPressed = true; + return true; + } + if (_dotPressed && dotState == 0) { + _dotPressed = false; + } + return false; + } + + private bool UpdateLInput() { + _lWasPressed = _lPressed; + short lState = Win32API.GetAsyncKeyState(LKey); + + if (!_lWasPressed && lState != 0) { + _lPressed = true; + return true; + } + if (_lPressed && lState == 0) { + _lPressed = false; + } + return false; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/GeminadaReader.cs.meta b/Assets/Scripts/Domain/Input/GeminadaReader.cs.meta new file mode 100644 index 0000000..4f282a0 --- /dev/null +++ b/Assets/Scripts/Domain/Input/GeminadaReader.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 057b144441c443ccb0a24de75397b551 +timeCreated: 1713114678 \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/InputReader.cs b/Assets/Scripts/Domain/Input/InputReader.cs index 82323d0..2f27dab 100644 --- a/Assets/Scripts/Domain/Input/InputReader.cs +++ b/Assets/Scripts/Domain/Input/InputReader.cs @@ -1,17 +1,15 @@ namespace Domain.Input { public abstract class InputReader { - private bool _wasPressed; - private bool _isPressed; + protected bool WasPressed; + protected bool IsPressed; protected abstract int Key { get; } - public void UpdateInput() { - _wasPressed = _isPressed; - _isPressed = Win32API.GetAsyncKeyState(Key) != 0; + public virtual void UpdateInput() { + WasPressed = IsPressed; + IsPressed = Win32API.GetAsyncKeyState(Key) != 0; } - public bool KeyDown() => _isPressed && !_wasPressed; - public bool KeyPressed() => _isPressed; - public bool KeyUp() => !_isPressed && _wasPressed; + public bool KeyDown() => IsPressed && !WasPressed; } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/KeyHistory.cs b/Assets/Scripts/Domain/KeyHistory.cs new file mode 100644 index 0000000..3f60e8d --- /dev/null +++ b/Assets/Scripts/Domain/KeyHistory.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; + +namespace Domain { + public class KeyHistory { + private readonly List _lastPresses = new(); + private readonly List _desiredSequence; + + public KeyHistory(List desiredSequence) { + _desiredSequence = desiredSequence; + } + + public void KeyPressed(int key) => _lastPresses.Add(key); + public void ClearPressed() => _lastPresses.Clear(); + + public bool ContainsSequence() { + if (_lastPresses.Count < _desiredSequence.Count) + return false; + + for (int i = 0; i < _lastPresses.Count; i++) { + if (i >= _desiredSequence.Count) + break; + + int keyPressed = _lastPresses[_lastPresses.Count - 1 - i]; + int sequenceKey = _desiredSequence[_desiredSequence.Count - 1 - i]; + + if (keyPressed != sequenceKey) { + return false; + } + } + + return true; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Domain/KeyHistory.cs.meta b/Assets/Scripts/Domain/KeyHistory.cs.meta new file mode 100644 index 0000000..c1ddd53 --- /dev/null +++ b/Assets/Scripts/Domain/KeyHistory.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a63e60d016a84434ac8ae1855004239e +timeCreated: 1713116240 \ No newline at end of file diff --git a/Packages/manifest.json b/Packages/manifest.json index 5cdff06..6651716 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -3,6 +3,7 @@ "com.sator-imaging.app-window-utility": "https://github.com/sator-imaging/AppWindowUtility.git", "com.unity.2d.sprite": "1.0.0", "com.unity.ide.rider": "3.0.28", + "com.unity.test-framework": "1.1.33", "com.unity.textmeshpro": "3.0.8", "com.unity.ugui": "1.0.0", "com.unity.modules.ai": "1.0.0", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 4bac257..d01d75b 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -29,6 +29,17 @@ }, "url": "https://packages.unity.com" }, + "com.unity.test-framework": { + "version": "1.1.33", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.ext.nunit": "1.0.6", + "com.unity.modules.imgui": "1.0.0", + "com.unity.modules.jsonserialize": "1.0.0" + }, + "url": "https://packages.unity.com" + }, "com.unity.textmeshpro": { "version": "3.0.8", "depth": 0,