all repos — RPG @ 4e93c03aca30d5f05be0515cea98d392c0f07583

Fully functional 3D turn based role playing game coded in C# and base Unity.

Assets/Scripts/Menu/EndGameManager.cs (view raw)

 1using System.IO;
 2using System.Runtime.Serialization.Formatters.Binary;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6public class EndGameManager : MonoBehaviour
 7{
 8    [Header("UI")]
 9    [SerializeField] private GameObject inputPanel = null;
10    [SerializeField] private GameObject highscoresPanel = null;
11    [SerializeField] private Text highscoreName = null;
12    [SerializeField] private Text[] nameObjects = null;
13    [SerializeField] private Text[] scoreObjects = null;
14    [SerializeField] private Text scoreText = null;
15    
16    [Header("Good Guys")]
17    [SerializeField] private GameObject[] goodGuys = null;
18
19    private Leaderboard leaderboard;
20    private int userScore;
21    const int maxRecordsNumber = 4;
22
23    void Start(){
24        GameMaster gm = GameMaster.Instance;
25        userScore = gm.gold;
26        string timeString = gm.GetTimerString();
27        //TODO: set text
28        scoreText.text = "You finished the game in " + timeString + "\nand you collected " + userScore + "g";
29    }
30
31    public void saveHighScore()
32    {
33        inputPanel.SetActive(false);
34
35        BinaryFormatter bf = new BinaryFormatter();
36        FileStream file;
37        //check if leaderboard file exists
38        if (File.Exists(Application.persistentDataPath + "/leaderboard.bin"))
39        {
40           
41            file = File.Open(Application.persistentDataPath + "/leaderboard.bin", FileMode.Open);
42            leaderboard = (Leaderboard) bf.Deserialize(file);
43            file.Close();
44        } else {
45            leaderboard = new Leaderboard(maxRecordsNumber);
46        }
47
48        //add new record
49        leaderboard.AddRecord(new Record(highscoreName.text, userScore));
50        file = File.Create(Application.persistentDataPath + "/leaderboard.bin");
51        bf.Serialize(file, leaderboard);
52        file.Close();
53
54        //show records list
55        int i = 0;
56        foreach (Record record in leaderboard.Records)
57        {
58            nameObjects[i].enabled = true;
59            nameObjects[i].text = (i + 1) + ". " + record.Name;
60            scoreObjects[i].enabled = true;
61            scoreObjects[i].text = record.ScoreToString();
62            i++;
63        }
64        for(; i < maxRecordsNumber; i++)
65        {
66            nameObjects[i].enabled = false;
67            scoreObjects[i].enabled = false;
68        }
69
70        highscoresPanel.SetActive(true);
71    }
72
73    public void BackToMainMenu()
74    {
75        UnityEngine.SceneManagement.SceneManager.LoadScene("Menu");
76    }
77}