· 8 years ago · Jun 05, 2018, 07:16 AM
1using System.Collections;
2using System.Collections.Generic;
3using System.IO;
4using UnityEngine;
5using UnityEngine.UI;
6using VRKeyboard.Utils;
7public class GameManager : MonoBehaviour {
8
9 public static GameManager instance = null;
10
11
12 User user;
13 List<User> users = new List<User>();
14 private bool startGame;
15 private int score;
16
17
18 public GameObject resultPanel;
19 public Text[] playersList;
20 public Text[] scoreList;
21
22
23 //testing
24 float timertest;
25 float gameTimer = 10;
26 public Text timertxt;
27
28 private KeyboardManager keyboardManagerScript;
29
30
31 private void Awake()
32 {
33 if (instance == null)
34 instance = this;
35 if (!File.Exists("leaderList.csv"))
36 {
37 File.Create("leaderList.csv");
38 }
39 StreamReader sw = new StreamReader(Application.persistentDataPath + "/leaderboard.csv");
40 while (!sw.EndOfStream)
41 {
42 var line = sw.ReadLine();
43 var values = line.Split(';');
44 users.Add(new User(values[0], values[1], int.Parse(values[2])));
45 }
46 }
47
48 // Use this for initialization
49 void Start () {
50
51 keyboardManagerScript = FindObjectOfType<KeyboardManager>();
52
53
54 StartGame();
55
56 //TODO: Start CountDown + Instuctions
57
58 //TODO: Start Logo Spawn
59
60
61 //TODO: End Game and show score
62
63 //TODO Reset
64
65 }
66
67 // Update is called once per frame
68 void Update () {
69
70 timertest += Time.deltaTime;
71 gameTimer -= Time.deltaTime;
72
73 timertxt.text = gameTimer.ToString();
74 if (timertest > 1)
75 {
76 score += Random.Range(2, 10);
77 Debug.Log(score);
78 timertest = 0;
79 }
80
81
82 if (gameTimer <= 0)
83 {
84 StopGame();
85 StartGame();
86 }
87
88
89
90
91 }
92
93
94 // Methods
95
96
97 public static string GenerateName(int len)
98 {
99 System.Random r = new System.Random();
100 string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "l", "n", "p", "q", "r", "s", "sh", "zh", "t", "v", "w", "x" };
101 string[] vowels = { "a", "e", "i", "o", "u", "ae", "y" };
102 string Name = "";
103 Name += consonants[r.Next(consonants.Length)].ToUpper();
104 Name += vowels[r.Next(vowels.Length)];
105 int b = 2; //b tells how many times a new letter has been added. It's 2 right now because the first two letters are already in the name.
106 while (b < len)
107 {
108 Name += consonants[r.Next(consonants.Length)];
109 b++;
110 Name += vowels[r.Next(vowels.Length)];
111 b++;
112 }
113
114 return Name;
115
116
117 }
118
119
120 public void StartGame()
121 {
122 keyboardManagerScript.nameInputText.text = GenerateName(6);
123 keyboardManagerScript.emailInputText.text = GenerateName(10) + "@gmail.com";
124
125 startGame = true;
126
127 user = new User(keyboardManagerScript.nameInputText.text, keyboardManagerScript.emailInputText.text, 0);
128 // KeyBoardManagerScript.instance.ClearInputsField();
129 // readyWindow.SetActive(true);
130
131
132 // timer = 4;
133 // ready = true;
134 }
135
136
137 public void StopGame()
138 {
139 gameTimer = 10;
140
141 startGame = false;
142
143 user.SetScore(score);
144 score = 0;
145
146
147 // scoreVal.text = score.ToString();
148
149 users.Add(user);
150
151 users.Sort(delegate (User us1, User us2)
152 {
153 return us2.GetScore().CompareTo(us1.GetScore());
154
155 });
156
157
158 ShowResult();
159 }
160
161 public void ShowResult()
162 {
163 int max = users.Count <= 10 ? users.Count : 9;
164
165 for (int i = 0; i < max; i++)
166 {
167 playersList[i].text = users[i].GetName();
168 scoreList[i].text = users[i].GetScore().ToString();
169 }
170
171 resultPanel.SetActive(true);
172 Invoke("CloseResultPanel", 10f);
173 }
174
175 private void CloseResultPanel() { resultPanel.SetActive(false); }
176
177 private void OnDestroy()
178 {
179 File.WriteAllText(@"leaderList.csv", string.Empty);
180 StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/leaderboard.csv");
181 foreach (User us in users)
182 {
183 sw.WriteLine(us.GetName() + ";" + us.GetEmail() + ";" + us.GetScore());
184 }
185 sw.Close();
186 }
187
188
189}
190
191
192class User
193{
194 string name;
195 string email;
196 int score = 0;
197 public User(string _name, string _email, int _score)
198 {
199 name = _name;
200 email = _email;
201 score = _score;
202 }
203 public string GetName() { return name; }
204 public string GetEmail() { return email; }
205 public int GetScore() { return score; }
206 public void SetScore(int _score) { score = _score; }
207}