This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewScoreControl.cs
294 lines (272 loc) · 9.14 KB
/
NewScoreControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//Author: Raul Rojas
//CSE 1302
//Project One
//7-13-2017
public class NewScoreControl : MonoBehaviour
{
#region Fields
#region Public
public GameObject CenterPanel;
public InputField nameInputField;
public GameObject noHighScorePanel;
public Button button;
public Text titleText;
#endregion
#region Private
private List<Text> NameTextFields;
private List<Text> ScoreTextFields;
private bool isNewHighScore;
private StreamWriter sw;
private StreamReader sr;
private List<Score> scoreList;
private int playerScore;
private int place;
private bool fromGameScene;
private Text buttonText;
#endregion
#endregion
#region MonoBehaviours
private void Start()
{
button.onClick.AddListener(buttonClicked);
buttonText = button.transform.GetChild(0).GetComponent<Text>();
List<GameObject> placePanels = new List<GameObject>();
NameTextFields = new List<Text>();
ScoreTextFields = new List<Text>();
scoreList = new List<Score>();
for (int i = 0; i < CenterPanel.transform.childCount; i++)
{
placePanels.Add(CenterPanel.transform.GetChild(i).gameObject);
}
foreach(GameObject go in placePanels)
{
NameTextFields.Add(go.transform.GetChild(1).GetComponent<Text>());
ScoreTextFields.Add(go.transform.GetChild(2).GetComponent<Text>());
}
getScores(); //read high Score file, store, and orginize scores.
printScoreList(); //for debugging.
displayScores(); //display to highscore table.
getPrefs(); //retreive player score from PlayerPrefs
titleText.text = "High Scores";
if (fromGameScene)
{
buttonText.text = "Menu";
setFields(); //determine if the player is inputting new high score or just displaying high scores.
}
else
{
buttonText.text = "Back";
noHighScorePanel.SetActive(false);
nameInputField.gameObject.SetActive(false);
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
buttonClicked();
}
}
#endregion
#region Private Methods
void setFields()
{
if (isNewHighScore)
{
titleText.text = "NEW HIGH SCORE!!";
buttonText.text = "Enter";
scoreList.Add(new Score("", playerScore));
scoreList.Sort();
scoreList.RemoveAt(5);
displayScores();
noHighScorePanel.SetActive(false);
nameInputField.gameObject.SetActive(true);
Transform panel = CenterPanel.transform.GetChild(place).transform;
nameInputField.transform.SetParent(panel);
nameInputField.gameObject.GetComponent<RectTransform>().position = panel.GetChild(1).GetComponent<RectTransform>().position;
panel.GetChild(1).gameObject.SetActive(false);
}
else
{
buttonText.text = "Menu";
noHighScorePanel.SetActive(true);
nameInputField.gameObject.SetActive(false);
noHighScorePanel.transform.GetChild(0).GetComponent<Text>().text = "Your Score";
noHighScorePanel.transform.GetChild(1).GetComponent<Text>().text = playerScore.ToString();
}
}
void getPrefs()
{
if (PlayerPrefs.GetInt(GameStructs.PrefKeys.FromGameScene) > 0)
{
fromGameScene = true;
PlayerPrefs.SetInt(GameStructs.PrefKeys.FromGameScene, 0);
if (PlayerPrefs.HasKey(GameStructs.PrefKeys.PlayerScore))
{
playerScore = PlayerPrefs.GetInt(GameStructs.PrefKeys.PlayerScore);
PlayerPrefs.SetInt(GameStructs.PrefKeys.PlayerScore, 0);
for (int i = 0; i < 5; i++)
{
if (playerScore > scoreList[i].ScoreP)
{
isNewHighScore = true;
place = i;
break;
}
else
{
isNewHighScore = false;
}
}
}
else
{
Debug.Log("No Player score found.");
playerScore = 0;
isNewHighScore = false;
}
}
}
void getScores()
{
bool complete = false;
int backUpCount = 0;
do
{
backUpCount++;
if (backUpCount >= 2) //avoid infinate loop if finding file failed and creating default file failed.
{
break;
}
try
{
sr = new StreamReader("HighScores.txt");
string temp = sr.ReadLine();
if (temp == null)
{
//temp = sr.ReadLine();
Debug.Log("Temp is null before loop. ");
}
int count = 0; //only get the first 5 lines to have a total of 5 scores ONLY
while (temp != null && count <= 5)
{
string[] lineData = temp.Split(':');
scoreList.Add(new Score(lineData[0], int.Parse(lineData[1])));
count++;
temp = sr.ReadLine();
}
complete = true;
}
catch (FileNotFoundException fnfe)
{
Debug.Log("A problem finding the Highscores file was encountered: " + fnfe.Message);
StreamWriter swTemp = new StreamWriter("HighScores.txt"); //create missing file.
//swTemp.WriteLine("-----:0");
//swTemp.WriteLine("-----:0");
//swTemp.WriteLine("-----:0");
//swTemp.WriteLine("-----:0");
//swTemp.WriteLine("-----:0");
swTemp.Close();
}
catch(ArgumentException ae)
{
Debug.Log("A problem occured while reading the HighScores file: " + ae.Message);
}
catch(NullReferenceException nre)
{
Debug.Log("A problem occured while reading the HighScores file: " + nre.Message);
}
catch(Exception e)
{
Debug.Log(e.Message);
}
finally
{
if (complete)
{
sr.Close();
}
}
} while (!complete);
//if (complete)
//{
if (scoreList.Count < 5)
{
for (int i = scoreList.Count; i < 5; i++)
{
scoreList.Add(new Score("-----", 0));
}
}
scoreList.Sort();
//}
}
void displayScores()
{
for (int i = 0; i < 5; i++)
{
NameTextFields[i].text = scoreList[i].Name;
ScoreTextFields[i].text = scoreList[i].ScoreP.ToString();
}
}
void printScoreList()
{
foreach(Score s in scoreList)
{
Debug.Log(s.ToString());
}
}
void buttonClicked()
{
AudioManager.instance.Play(GameStructs.ClipName.ButtonClick);
if (isNewHighScore && fromGameScene)
{
string playerInput = nameInputField.transform.GetChild(2).GetComponent<Text>().text; //the text in the inputField
if (playerInput.Length > 0) //check if its place holder text
{
//record score, close file, and change to normal high score scene.
scoreList[place].Name = playerInput;
saveScores();
PlayerPrefs.SetInt(GameStructs.PrefKeys.FromGameScene, 0);
SceneManager.LoadScene("HighScores", LoadSceneMode.Single);
}
else
{
//Tell player to input a name in the text field.
Debug.Log("Please enter your name.");
}
}
else
{
//return to main title menu scene.
SceneManager.LoadScene("Title", LoadSceneMode.Single);
}
}
void saveScores()
{
try
{
sw = new StreamWriter("HighScores.txt");
foreach(Score s in scoreList)
{
sw.WriteLine(s.Name + ":" + s.ScoreP);
}
}
catch (Exception e)
{
Debug.Log(e.Message);
throw;
}
finally
{
sw.Close();
}
}
#endregion
}