using System; using System.Collections; using SpellBoundAR.MusicalInstruments.Piano; using UnityEngine; using UnityEngine.UI; namespace SpellBoundAR.MusicalInstruments.SheetMusic { [RequireComponent(typeof(SheetMusic))] public class SheetMusicScroller : MonoBehaviour { public event Action OnComboChanged; [Header("Settings")] [SerializeField] private float offset = 0f; [SerializeField] private float highlighterPadding = 5f; [Header("References")] [SerializeField] private ScrollRect scrollRect; [SerializeField] private RectTransform content; [SerializeField] private SheetMusicHighlighter highlighter; [Header("Cache")] private SheetMusic _sheetMusic; [Header("States")] private int _previousNoteIndex; private int _currentNoteIndex; private int _combo; public int Combo { get => _combo; private set { if (_combo == value) return; _combo = value; OnComboChanged?.Invoke(); } } public bool AtStart => content.anchoredPosition == Vector2.zero && _currentNoteIndex == 0; private void Awake() { _sheetMusic = GetComponent(); MusicalInstrumentsManager.OnSongChanged += Reset; UIPianoKey.OnPianoKeyPlayed += OnPianoKeyPlayed; } private void OnDestroy() { MusicalInstrumentsManager.OnSongChanged -= Reset; UIPianoKey.OnPianoKeyPlayed -= OnPianoKeyPlayed; } private void Start() => Reset(); private void Reset() { StopAllCoroutines(); ScrollToStart(); } public void ScrollToStart() { StopAllCoroutines(); scrollRect.velocity = Vector2.zero; content.pivot = new Vector2(0f, .5f); content.anchoredPosition = Vector2.zero; _previousNoteIndex = 0; _currentNoteIndex = 0; UpdateHighlighter(); } private void OnPianoKeyPlayed(PlayerType player, Tone tone) { if (_currentNoteIndex >= _sheetMusic.UI_WrittenNotes.Count) { _previousNoteIndex = _currentNoteIndex; _currentNoteIndex = 0; } if (_currentNoteIndex < _sheetMusic.UI_WrittenNotes.Count) { if (_sheetMusic.UI_WrittenNotes[_currentNoteIndex].WrittenNote.Note.Tone == tone) { _sheetMusic.UI_WrittenNotes[_currentNoteIndex].PulseScale(); _previousNoteIndex = _currentNoteIndex; _currentNoteIndex++; if (player == PlayerType.Human) Combo++; UpdateHighlighter(); if (_previousNoteIndex < _currentNoteIndex) { StopAllCoroutines(); StartCoroutine(MoveContentTo(-_sheetMusic.UI_WrittenNotes[_currentNoteIndex].GetComponent().offsetMin.x + offset)); } } else { if (player == PlayerType.Human) Combo = 0; highlighter.AnimateMiss(); } } } private void UpdateHighlighter() { if (_currentNoteIndex >= _sheetMusic.UI_WrittenNotes.Count) { _previousNoteIndex = _currentNoteIndex; _currentNoteIndex = 0; } if (_currentNoteIndex < _sheetMusic.UI_WrittenNotes.Count) { highlighter.gameObject.SetActive(true); RectTransform currentNote = _sheetMusic.UI_WrittenNotes[_currentNoteIndex].GetComponent(); highlighter.RectTransform.offsetMin = new Vector2(currentNote.offsetMin.x - highlighterPadding, 0f); highlighter.RectTransform.offsetMax = new Vector2(currentNote.offsetMax.x + highlighterPadding, 0f); } else highlighter.gameObject.SetActive(false); } private IEnumerator MoveContentTo(float endXPosition) { float seconds = .2f; float startXPosition = content.anchoredPosition.x; for (float i = 0f; i < seconds; i += Time.unscaledDeltaTime) { float progress = i / seconds; float xPosition = Mathf.Lerp(startXPosition, endXPosition, progress); content.anchoredPosition = new Vector2(xPosition, 0f); yield return null; } content.anchoredPosition = new Vector2(endXPosition, 0f); } } }