Beispiel #1
0
    /// <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
        {
        }
    }
Beispiel #2
0
    // 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);
        }
    }
Beispiel #3
0
    /// <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");
        }
    }