Exemplo n.º 1
0
    /**
     * @pre Game has ended
     * @post Add current game's score data to the 'savedGames.gd' file
     * @param int score of the winner, string name of player who won
     * @return None.
     */
    private void WriteScoreToFile(int score, string playerName)
    {
        ScoreData       scoreData = new ScoreData();
        BinaryFormatter bf        = new BinaryFormatter();

        if (File.Exists(Application.persistentDataPath + "/savedGames.gd"))
        {
            FileStream readFile = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
            scoreData = (ScoreData)bf.Deserialize(readFile);
            readFile.Close();
        }
        ScoreEntry currentScoreEntry = new ScoreEntry();

        currentScoreEntry.score = score;
        currentScoreEntry.name  = playerName;
        scoreData.AddScoreByNumShips(Team1.numberOfShips, currentScoreEntry);
        FileStream writeFile = File.Create(Application.persistentDataPath + "/savedGames.gd");

        bf.Serialize(writeFile, scoreData);
        writeFile.Close();

        foreach (ScoreEntry a in scoreData.GetScoresByNumShips(1))
        {
            Debug.Log("1 Ship: " + a.name + ": " + a.score);
        }
    }