/// <summary> /// Conscruct a Song object that holds data regarding the timing of the rhythm, /// and properties like name and artist. Loads in the information from a JSON file /// for the song that is specified by the name and artist arguments. /// </summary> /// <param name="baseFilePath"></param> /// <param name="name"></param> /// <param name="artist"></param> public Song(string baseFilePath, string name, string artist) { filePath = baseFilePath + "\\Assets\\Scripts\\Music\\" + name + " - " + artist; rhythmData = new List <int>(); musicData = new Dictionary <string, dynamic>(); bool isViable = true; // check validity of RhythmData and MusicData, if both are valid then program will proceed. if (!MusicFileHandler.tryRhythmData(filePath, rhythmData)) { Debug.Log("Error in RhythmData.csv, please check error log in " + filePath); isViable = false; } if (!MusicFileHandler.tryMusicData(filePath, musicData)) { Debug.Log("Error in MusicData.csv, please check error log in " + filePath); isViable = false; } if (isViable) { // extract json data if values are viable Name = musicData["Name"]; Artist = musicData["Artist"]; Genre = musicData["Genre"]; Length = musicData["Length"]; NumOfBeats = musicData["Beats"]; Highscore = musicData["Highscore"]; // convert formatted string "Length" to milliseconds string[] lengthArr = Length.Split(':'); LengthMilli = Int32.Parse(lengthArr[0]) * 60000 + Int32.Parse(lengthArr[1]) * 1000; } else { } }
// Start is called before the first frame update /// <summary> /// Before the first frame update, we create multiple variables and set their values /// including the specified cubes, a list of rhythm data to parse through, creating a time and count, /// getting the song from the file, and creating the audio source as well as creating a button manager /// </summary> void Start() { setCubes(); // startTime = Time.time; filePath = Directory.GetCurrentDirectory() + "\\Assets\\Scripts\\Music\\Alive - Mind Vortex"; rhythmData = new List <int>(); MusicFileHandler.tryRhythmData(filePath, rhythmData); time = 0; count = 0; song = new Song(Directory.GetCurrentDirectory(), "Alive", "Mind Vortex"); audioSource.PlayDelayed(delay); lengthMilli = audioSource.clip.length * 1000; manager = new ButtonSceneManager(); }
// Start is called before the first frame update void Start() { musicData = new Dictionary <string, dynamic>(); baseFilePath = Directory.GetCurrentDirectory(); filePath = baseFilePath + "\\Assets\\" + "\\Scripts\\" + "\\Music\\Alive - Mind Vortex\\"; if (MusicFileHandler.tryMusicData(filePath, musicData)) { // set json data to song card if possible title.SetText(musicData["Name"]); artist.SetText(musicData["Artist"]); genre.SetText(musicData["Genre"]); length.SetText(musicData["Length"]); highScore.SetText(musicData["Highscore"].ToString()); } else { Debug.Log("Error in MusicData.json, please check error log in " + filePath); Environment.Exit(-1); } }
/// <summary> /// Update is called once per frame. Updates the song's current playback time. /// If the song is still playing, it will continue with the game methods. When the song /// is over, prepare to move to the next scene and also overwrite the score if the /// current score is greater than the high score. /// </summary> void Update() { if (count < rhythmData.Count && time < lengthMilli) // song is still playing { time = Convert.ToInt32(Math.Floor(audioSource.time * 100 + 0.5) * 10); if (time >= rhythmData[count]) { pickRandomCube(); count++; } } else if (!audioSource.isPlaying) // song is over { // get user's score GameObject gameObject = GameObject.FindGameObjectWithTag("ScoreText"); string scoreStr = gameObject.GetComponent <TextMeshProUGUI>().text; uint userScore; if (!UInt32.TryParse(scoreStr, out userScore)) { Debug.LogError("Could not parse GameObject \"ScoreText\"" + " text component to an unsigned integer"); } // get highscore from json file Dictionary <string, dynamic> dict = new Dictionary <string, dynamic>(); MusicFileHandler.tryMusicData(filePath, dict); uint highscore = dict["Highscore"]; // overwrite previous highscore if user beat it if (userScore > highscore) { dict["Highscore"] = userScore; File.WriteAllText(filePath + "\\MusicData.json", JsonConvert.SerializeObject(dict)); } CurrentScoreManager.setFinalGameScore(); manager.ButtonChangeScene("ScoreboardScene"); } }