feat: l geminada working
This commit is contained in:
parent
1869a92580
commit
30ca50fb35
15 changed files with 189 additions and 29 deletions
|
@ -5,12 +5,17 @@ using Domain.Input;
|
|||
namespace Domain {
|
||||
public class CustomInput {
|
||||
private readonly Dictionary<Type, InputReader> _readers = new();
|
||||
private readonly KeyHistory _history;
|
||||
|
||||
public CustomInput() {
|
||||
_history = new KeyHistory();
|
||||
|
||||
_readers.Add(typeof(CedillaReader), new CedillaReader());
|
||||
_readers.Add(typeof(GeminadaReader), new GeminadaReader(_history));
|
||||
}
|
||||
|
||||
public void UpdateInput() {
|
||||
_history.CheckPresses();
|
||||
foreach (KeyValuePair<Type, InputReader> reader in _readers) {
|
||||
reader.Value.UpdateInput();
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ using System.Linq;
|
|||
|
||||
namespace Domain.Input {
|
||||
public class GeminadaReader : InputReader {
|
||||
protected override int Key { get; } = 0x33;
|
||||
protected sealed override int Key { get; } = 0x33;
|
||||
private int LKey { get; } = 76;
|
||||
|
||||
private bool _lPressed;
|
||||
|
@ -11,22 +11,24 @@ namespace Domain.Input {
|
|||
private bool _dotPressed;
|
||||
private bool _dotWasPressed;
|
||||
|
||||
private List<int> _lastPresses;
|
||||
private readonly KeyHistory _history;
|
||||
private readonly List<int> _desiredSequence;
|
||||
|
||||
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();
|
||||
public GeminadaReader(KeyHistory history) {
|
||||
_history = history;
|
||||
_desiredSequence = new List<int> { LKey, Key, LKey };
|
||||
}
|
||||
|
||||
private bool UpdateDotInput() {
|
||||
public override void UpdateInput() {
|
||||
if (UpdateDotInputDown()) {
|
||||
_history.KeyPressed(Key);
|
||||
}
|
||||
|
||||
WasPressed = IsPressed;
|
||||
IsPressed = _history.ContainsSequence(_desiredSequence);
|
||||
}
|
||||
|
||||
private bool UpdateDotInputDown() {
|
||||
_dotWasPressed = _dotPressed;
|
||||
short dotState = Win32API.GetAsyncKeyState(Key);
|
||||
|
||||
|
@ -40,7 +42,7 @@ namespace Domain.Input {
|
|||
return false;
|
||||
}
|
||||
|
||||
private bool UpdateLInput() {
|
||||
private bool UpdateLInputDown() {
|
||||
_lWasPressed = _lPressed;
|
||||
short lState = Win32API.GetAsyncKeyState(LKey);
|
||||
|
||||
|
|
|
@ -1,27 +1,42 @@
|
|||
using System.Collections.Generic;
|
||||
using Extensions;
|
||||
|
||||
namespace Domain {
|
||||
public class KeyHistory {
|
||||
private readonly List<int> _lastPresses = new();
|
||||
private readonly List<int> _desiredSequence;
|
||||
private readonly LimitedSizeList<int> _lastPresses = new(10);
|
||||
|
||||
public KeyHistory(List<int> desiredSequence) {
|
||||
_desiredSequence = desiredSequence;
|
||||
}
|
||||
private readonly bool[] _isPressed = new bool[26];
|
||||
private readonly bool[] _wasPressed = new bool[26];
|
||||
|
||||
public void KeyPressed(int key) => _lastPresses.Add(key);
|
||||
public void ClearPressed() => _lastPresses.Clear();
|
||||
|
||||
public bool ContainsSequence() {
|
||||
if (_lastPresses.Count < _desiredSequence.Count)
|
||||
public void CheckPresses() {
|
||||
const int aIndex = 0x41;
|
||||
const int zIndex = 0x5A;
|
||||
|
||||
for (int i = aIndex, j = 0; i <= zIndex; i++, j++) {
|
||||
_wasPressed[j] = _isPressed[j];
|
||||
short state = Win32API.GetAsyncKeyState(i);
|
||||
|
||||
if (!_wasPressed[j] && state != 0) {
|
||||
_isPressed[j] = true;
|
||||
KeyPressed(i);
|
||||
}else if (_isPressed[j] && state == 0) {
|
||||
_isPressed[j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool ContainsSequence(List<int> sequence) {
|
||||
if (_lastPresses.List.Count < sequence.Count)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < _lastPresses.Count; i++) {
|
||||
if (i >= _desiredSequence.Count)
|
||||
for (int i = 0; i < _lastPresses.List.Count; i++) {
|
||||
if (i >= sequence.Count)
|
||||
break;
|
||||
|
||||
int keyPressed = _lastPresses[_lastPresses.Count - 1 - i];
|
||||
int sequenceKey = _desiredSequence[_desiredSequence.Count - 1 - i];
|
||||
int keyPressed = _lastPresses.List[_lastPresses.List.Count - 1 - i];
|
||||
int sequenceKey = sequence[sequence.Count - 1 - i];
|
||||
|
||||
if (keyPressed != sequenceKey) {
|
||||
return false;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
"name": "SantJordi.Domain"
|
||||
"name": "SantJordi.Domain",
|
||||
"references":[ "GUID:b347d0ff97f738846abb5625028d64db" ]
|
||||
}
|
||||
|
|
3
Assets/Scripts/Extensions.meta
Normal file
3
Assets/Scripts/Extensions.meta
Normal file
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bfb91da35c9749c980ab26f8e6b48dbe
|
||||
timeCreated: 1713127616
|
21
Assets/Scripts/Extensions/LimitedSizeList.cs
Normal file
21
Assets/Scripts/Extensions/LimitedSizeList.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extensions {
|
||||
public class LimitedSizeList<T> {
|
||||
public readonly List<T> List;
|
||||
private readonly int _maxSize;
|
||||
|
||||
public LimitedSizeList(int maxSize) {
|
||||
_maxSize = maxSize;
|
||||
List = new List<T>(maxSize);
|
||||
}
|
||||
|
||||
public void Add(T item) {
|
||||
List.Add(item);
|
||||
if (List.Count > _maxSize) {
|
||||
List.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Extensions/LimitedSizeList.cs.meta
Normal file
3
Assets/Scripts/Extensions/LimitedSizeList.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bd4db2e2399447bdaa6b28d73a4fc7f9
|
||||
timeCreated: 1713127625
|
3
Assets/Scripts/Extensions/SantJordi.Extensions.asmdef
Normal file
3
Assets/Scripts/Extensions/SantJordi.Extensions.asmdef
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"name": "SantJordi.Extensions"
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b347d0ff97f738846abb5625028d64db
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Assets/Scripts/Tests.meta
Normal file
8
Assets/Scripts/Tests.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dede9b43c4aed9843a524ea11cb932a8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
50
Assets/Scripts/Tests/KeySequenceTests.cs
Normal file
50
Assets/Scripts/Tests/KeySequenceTests.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using System.Collections.Generic;
|
||||
using Domain;
|
||||
using NUnit.Framework;
|
||||
|
||||
public class KeySequenceTests {
|
||||
[Test]
|
||||
public void NoPresses_DontContain() {
|
||||
List<int> sequence = new() { 0x1, 0x2, 0x3 };
|
||||
KeyHistory history = new();
|
||||
|
||||
Assert.IsFalse(history.ContainsSequence(sequence));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CorrectPresses_Contains() {
|
||||
List<int> sequence = new() { 0x1, 0x2, 0x3 };
|
||||
KeyHistory history = new();
|
||||
|
||||
history.KeyPressed(0x1);
|
||||
history.KeyPressed(0x2);
|
||||
history.KeyPressed(0x3);
|
||||
|
||||
Assert.IsTrue(history.ContainsSequence(sequence));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CorrectPresses_BeforeLast_DontContain() {
|
||||
List<int> sequence = new() { 0x1, 0x2, 0x3 };
|
||||
KeyHistory history = new();
|
||||
|
||||
history.KeyPressed(0x1);
|
||||
history.KeyPressed(0x2);
|
||||
history.KeyPressed(0x3);
|
||||
history.KeyPressed(0x1);
|
||||
|
||||
Assert.IsFalse(history.ContainsSequence(sequence));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IncorrectPresses_DontContain() {
|
||||
List<int> sequence = new() { 0x1, 0x2, 0x3 };
|
||||
KeyHistory history = new();
|
||||
|
||||
history.KeyPressed(0x1);
|
||||
history.KeyPressed(0x3);
|
||||
history.KeyPressed(0x2);
|
||||
|
||||
Assert.IsFalse(history.ContainsSequence(sequence));
|
||||
}
|
||||
}
|
11
Assets/Scripts/Tests/KeySequenceTests.cs.meta
Normal file
11
Assets/Scripts/Tests/KeySequenceTests.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d8ab5fe36e022dd4d9dbbb15c6198fb3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
24
Assets/Scripts/Tests/Tests.asmdef
Normal file
24
Assets/Scripts/Tests/Tests.asmdef
Normal file
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"name": "Tests",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"UnityEngine.TestRunner",
|
||||
"UnityEditor.TestRunner",
|
||||
"SantJordi.Domain"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": true,
|
||||
"precompiledReferences": [
|
||||
"nunit.framework.dll"
|
||||
],
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": [
|
||||
"UNITY_INCLUDE_TESTS"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
7
Assets/Scripts/Tests/Tests.asmdef.meta
Normal file
7
Assets/Scripts/Tests/Tests.asmdef.meta
Normal file
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e70aee605542b224b8274efcc111c230
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -21,7 +21,7 @@ namespace View {
|
|||
|
||||
private void CheckInput() {
|
||||
_customInput.UpdateInput();
|
||||
if (_customInput.KeyDown(typeof(CedillaReader)))
|
||||
if (_customInput.KeyDown(typeof(GeminadaReader)))
|
||||
_click.Execute();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue