Commit c1a8b06f authored by Gerard Gascón's avatar Gerard Gascón
Browse files

refactor: Reading all inputs from a single method

parent 3e31ecc9
Loading
Loading
Loading
Loading
+15 −15
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using System.Linq;
using Domain.Input;

namespace Domain {
	public class CustomInput {
		private readonly Dictionary<Type, InputReader> _readers = new();
		private readonly List<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));
			_readers.Add(typeof(TxReader), new TxReader(_history));
			_readers.Add(typeof(IxReader), new IxReader(_history));
			_readers.Add(typeof(NyReader), new NyReader(_history));
			_readers.Add(typeof(TgReader), new TgReader(_history));
			_readers.Add(typeof(TjReader), new TjReader(_history));
			_readers.Add(typeof(LlReader), new LlReader(_history));
			_readers.Add(typeof(SsReader), new SsReader(_history));
			_readers.Add(typeof(AObertaReader), new AObertaReader(_history));
			_readers.Add(new CedillaReader());
			_readers.Add(new GeminadaReader(_history));
			_readers.Add(new TxReader(_history));
			_readers.Add(new IxReader(_history));
			_readers.Add(new NyReader(_history));
			_readers.Add(new TgReader(_history));
			_readers.Add(new TjReader(_history));
			_readers.Add(new LlReader(_history));
			_readers.Add(new SsReader(_history));
			_readers.Add(new AObertaReader(_history));
		}

		public void UpdateInput() {
			_history.CheckPresses();
			foreach (KeyValuePair<Type, InputReader> reader in _readers) {
				reader.Value.UpdateInput();
			}
			foreach (InputReader reader in _readers)
				reader.UpdateInput();
		}

		public bool KeyDown(Type key) => _readers[key].KeyDown();
		public bool AnyKeyDown() => _readers.Any(reader => reader.KeyDown());
	}
}
 No newline at end of file
+6 −24
Original line number Diff line number Diff line
@@ -26,34 +26,16 @@ namespace View.UI {
			CheckInput();
		}

		public void UpdateView(int score) {
			animator.PlayUntil(_model.GrowPercentage);
			text.text = score.ToString();
		}

		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)))
			if (_customInput.AnyKeyDown())
				_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) {
			animator.PlayUntil(_model.GrowPercentage);
			text.text = score.ToString();
		}
	}
}
 No newline at end of file