refactor: Reading all inputs from a single method

This commit is contained in:
Gerard Gascón 2024-04-16 16:47:27 +02:00
parent 3e31ecc943
commit c1a8b06f43
2 changed files with 22 additions and 40 deletions

View file

@ -1,34 +1,34 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Domain.Input; using Domain.Input;
namespace Domain { namespace Domain {
public class CustomInput { public class CustomInput {
private readonly Dictionary<Type, InputReader> _readers = new(); private readonly List<InputReader> _readers = new();
private readonly KeyHistory _history; private readonly KeyHistory _history;
public CustomInput() { public CustomInput() {
_history = new KeyHistory(); _history = new KeyHistory();
_readers.Add(typeof(CedillaReader), new CedillaReader()); _readers.Add(new CedillaReader());
_readers.Add(typeof(GeminadaReader), new GeminadaReader(_history)); _readers.Add(new GeminadaReader(_history));
_readers.Add(typeof(TxReader), new TxReader(_history)); _readers.Add(new TxReader(_history));
_readers.Add(typeof(IxReader), new IxReader(_history)); _readers.Add(new IxReader(_history));
_readers.Add(typeof(NyReader), new NyReader(_history)); _readers.Add(new NyReader(_history));
_readers.Add(typeof(TgReader), new TgReader(_history)); _readers.Add(new TgReader(_history));
_readers.Add(typeof(TjReader), new TjReader(_history)); _readers.Add(new TjReader(_history));
_readers.Add(typeof(LlReader), new LlReader(_history)); _readers.Add(new LlReader(_history));
_readers.Add(typeof(SsReader), new SsReader(_history)); _readers.Add(new SsReader(_history));
_readers.Add(typeof(AObertaReader), new AObertaReader(_history)); _readers.Add(new AObertaReader(_history));
} }
public void UpdateInput() { public void UpdateInput() {
_history.CheckPresses(); _history.CheckPresses();
foreach (KeyValuePair<Type, InputReader> reader in _readers) { foreach (InputReader reader in _readers)
reader.Value.UpdateInput(); reader.UpdateInput();
}
} }
public bool KeyDown(Type key) => _readers[key].KeyDown(); public bool AnyKeyDown() => _readers.Any(reader => reader.KeyDown());
} }
} }

View file

@ -26,34 +26,16 @@ namespace View.UI {
CheckInput(); CheckInput();
} }
private void CheckInput() {
_customInput.UpdateInput();
if (_customInput.KeyDown(typeof(GeminadaReader)))
_click.Execute();
if (_customInput.KeyDown(typeof(CedillaReader)))
_click.Execute();
if (_customInput.KeyDown(typeof(IxReader)))
_click.Execute();
if (_customInput.KeyDown(typeof(LlReader)))
_click.Execute();
if (_customInput.KeyDown(typeof(NyReader)))
_click.Execute();
if (_customInput.KeyDown(typeof(SsReader)))
_click.Execute();
if (_customInput.KeyDown(typeof(TgReader)))
_click.Execute();
if (_customInput.KeyDown(typeof(TjReader)))
_click.Execute();
if (_customInput.KeyDown(typeof(TxReader)))
_click.Execute();
if(_customInput.KeyDown(typeof(AObertaReader)))
_click.Execute();
}
public void UpdateView(int score) { public void UpdateView(int score) {
animator.PlayUntil(_model.GrowPercentage); animator.PlayUntil(_model.GrowPercentage);
text.text = score.ToString(); text.text = score.ToString();
} }
private void CheckInput() {
_customInput.UpdateInput();
if (_customInput.AnyKeyDown())
_click.Execute();
}
} }
} }