using SpellBoundAR.MusicalInstruments.Piano; using UnityEngine; namespace SpellBoundAR.MusicalInstruments.PlaybackVerifiers { public class PlaybackVerifier : MonoBehaviour { public static Song Song => MusicalInstrumentsManager.Song; [Header("Cache")] private int _noteIndex; private void OnEnable() { MusicalInstrumentsManager.OnSongChanged += RestartVerifier; PianoKey.OnPianoKeyPlayed += OnPianoKeyPlayed; } private void OnDisable() { MusicalInstrumentsManager.OnSongChanged -= RestartVerifier; PianoKey.OnPianoKeyPlayed -= OnPianoKeyPlayed; } private void RestartVerifier() => _noteIndex = 0; private void OnPianoKeyPlayed(PlayerType playerType, Tone tone) { if (!Song || playerType != PlayerType.Human) return; while (_noteIndex < Song.Notes.Count && !Song.Notes[_noteIndex].Note) { _noteIndex++; } if (_noteIndex >= Song.Notes.Count) { RestartVerifier(); } if (Song.Notes[_noteIndex].Note.Tone == tone) { do { _noteIndex++; if (Song.Notes.Count == _noteIndex) Song.Played = true; } while (_noteIndex < Song.Notes.Count && Song.Notes[_noteIndex].Note == null); } } } }