using SpellBoundAR.MusicalInstruments.Autoplay; using SpellBoundAR.MusicalInstruments.Piano; using UnityEngine; namespace SpellBoundAR.MusicalInstruments.PlaybackVerifiers { public class PerfectPlaybackVerifier : MonoBehaviour { public static Song Song => MusicalInstrumentsManager.Song; [Header("Cache")] private bool _playedWithColor; private int _noteIndex; private void OnEnable() { MusicalInstrumentsManager.OnSongChanged += RestartVerifier; Autoplayer.OnAnyPlayingChanged += OnAnyPlayingChanged; PianoKey.OnPianoKeyPlayed += OnPianoKeyPlayed; } private void OnDisable() { MusicalInstrumentsManager.OnSongChanged -= RestartVerifier; Autoplayer.OnAnyPlayingChanged -= OnAnyPlayingChanged; PianoKey.OnPianoKeyPlayed -= OnPianoKeyPlayed; } private void OnAnyPlayingChanged(Autoplayer automaticPlayer) => RestartVerifier(); public void RestartVerifier() { _noteIndex = 0; _playedWithColor = false; } 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) { if (MusicalInstrumentsManager.Colors) _playedWithColor = true; do { _noteIndex++; if (_noteIndex >= Song.Notes.Count) { Song.PlayedPerfectlyWithColor = true; if (_playedWithColor) continue; Song.PlayedPerfectlyWithoutColor = true; RestartVerifier(); } } while (_noteIndex < Song.Notes.Count && Song.Notes[_noteIndex].Note == null); } else RestartVerifier(); } } }