using System; using System.Collections.Generic; using System.IO; using SpellBoundAR.AssetManagement; using SpellBoundAR.MusicalInstruments.SheetMusic; using SpellBoundAR.SavedAssetsSystem; using UnityEngine; namespace SpellBoundAR.MusicalInstruments { [CreateAssetMenu(menuName = "Scriptable Objects/Musical Instruments/Song")] public class Song : IdentifiableScriptableObject { public static event Action OnAnyTimesSelectedChanged; public static event Action OnAnyPlayedChanged; public static event Action OnAnyPlayedPerfectlyWithColorChanged; public static event Action OnAnyPlayedPerfectlyWithoutColorChanged; public event Action OnTimesSelectedChanged; public event Action OnPlayedChanged; public event Action OnPlayedPerfectlyWithColorChanged; public event Action OnPlayedPerfectlyWithoutColorChanged; [SerializeField] private KeySignature keySignature; [SerializeField] private Difficulty difficulty; [SerializeField] private List measures = new (); private List _notes; public KeySignature KeySignature => keySignature; public Difficulty Difficulty => difficulty; public List Measures => measures; public List Notes { get { if (_notes == null) RebuildNotes(); return _notes; } } private SavedInt _timesSelected; private SavedBool _played; private SavedBool _playedPerfectlyWithColor; private SavedBool _playedPerfectlyWithoutColor; public int TimesSelected { get => _timesSelected.Value; set { _timesSelected.Value = value; OnTimesSelectedChanged?.Invoke(); OnAnyTimesSelectedChanged?.Invoke(this); } } public bool Played { get => _played.Value; set { _played.Value = value; OnPlayedChanged?.Invoke(); OnAnyPlayedChanged?.Invoke(this); } } public bool PlayedPerfectlyWithColor { get => _playedPerfectlyWithColor.Value; set { _playedPerfectlyWithColor.Value = value; OnPlayedPerfectlyWithColorChanged?.Invoke(); OnAnyPlayedPerfectlyWithColorChanged?.Invoke(this); } } public bool PlayedPerfectlyWithoutColor { get => _playedPerfectlyWithoutColor.Value; set { _playedPerfectlyWithoutColor.Value = value; OnPlayedPerfectlyWithoutColorChanged?.Invoke(); OnAnyPlayedPerfectlyWithoutColorChanged?.Invoke(this); } } protected virtual string Directory => Path.Combine("Songs", ID); protected virtual void OnEnable() { InitializeSavedInformation(); } protected void InitializeSavedInformation() { string directory = Directory; _timesSelected = new SavedInt(directory, "TimesSelected.txt", 0, null); _played = new SavedBool(directory, "Played.txt", false, null); _playedPerfectlyWithColor = new SavedBool(directory, "PlayedPerfectlyWithColor.txt", false, null); _playedPerfectlyWithoutColor = new SavedBool(directory, "PlayedPerfectlyWithoutColor.txt", false, null); } private void RebuildNotes() { if (_notes == null) _notes = new List(); else _notes.Clear(); foreach (Measure measure in Measures) { foreach (WrittenNote writtenNote in measure.WrittenNotes) { _notes.Add(writtenNote); } } } private void Awake() { RebuildNotes(); } private void OnValidate() { RebuildNotes(); } } }