8000 rewrite DataController. no longer accepting None pagetype in PageCont… · coderDarren/UnityCore@ec6044c · GitHub
[go: up one dir, main page]

Skip to content

Commit ec6044c

Browse files
committed
rewrite DataController. no longer accepting None pagetype in PageController
1 parent e1cebb2 commit ec6044c

File tree

2 files changed

+63
-35
lines changed

2 files changed

+63
-35
lines changed

Data/DataController.cs

+61-35
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,90 @@ namespace Data {
77

88
public class DataController : MonoBehaviour
99
{
10-
public static readonly string DATA_SCORE = "score";
11-
public static readonly string DATA_HIGHSCORE = "highscore";
12-
10+
private static readonly string DATA_SCORE = "score";
11+
private static readonly string DATA_HIGHSCORE = "highscore";
1312
private static readonly int DEFAULT_INT = 0;
14-
private static readonly float DEFAULT_FLOAT = 0.0F;
15-
private static readonly string DEFAULT_STRING = "";
1613

1714
public bool debug;
1815

19-
#region Public Functions
20-
public T GetData<T>(string _data, bool _createIfNull=true) {
21-
if (!DataExists(_data)) {
22-
string _warning = "Data ["+_data+"] does not exist.";
23-
if (_createIfNull) {
24-
_warning += "Creating..";
25-
LogWarning(_warning);
26-
} else {
27-
_warning += "Returning default..";
28-
LogWarning(_warning);
29-
return default(T);
30-
}
16+
#region Properties
17+
public int Highscore {
18+
get {
19+
return GetInt(DATA_HIGHSCORE);
20+
}
21+
private set {
22+
SaveInt(DATA_HIGHSCORE, value);
3123
}
24+
}
3225

33-
if (typeof(T) == typeof(int)) {
34-
return PlayerPrefs.GetInt(_data, DEFAULT_INT);
35-
} else if (typeof(T) == typeof(float)) {
36-
return PlayerPrefs.GetFloat(_data, DEFAULT_FLOAT);
37-
} else if (typeof(T) == typeof(string)) {
38-
return PlayerPrefs.GetString(_data, DEFAULT_STRING);
26+
public int Score {
27+
get {
28+
return GetInt(DATA_SCORE);
29+
}
30+
set {
31+
SaveInt(DATA_SCORE, value);
32+
int _score = this.Score;
33+
if (_score > this.Highscore) {
34+
this.Highscore = _score;
35+
}
3936
}
40-
41-
return default(T);
4237
}
38+
#endregion
39+
40+
#region Unity Functions
41+
#if UNITY_EDITOR
42+
private void Update() {
43+
if (Input.GetKeyUp(KeyCode.R)) {
44+
TestAddScore(1);
45+
Log("[Test] Score = "+this.Score+" | Highscore = "+this.Highscore);
46+
}
47+
48+
if (Input.GetKeyUp(KeyCode.T)) {
49+
TestAddScore(-1);
50+
Log("[Test] Score = "+this.Score+" | Highscore = "+this.Highscore);
51+
}
4352

44-
public void SaveData<T>(string _data, T _val) {
45-
if (typeof(T) == typeof(int)) {
46-
return PlayerPrefs.SetInt(_data, _val);
47-
} else if (typeof(T) == typeof(float)) {
48-
return PlayerPrefs.SetFloat(_data, _val);
49-
} else if (typeof(T) == typeof(string)) {
50-
return PlayerPrefs.SetString(_data, _val);
53+
if (Input.GetKeyUp(KeyCode.Space)) {
54+
TestResetHighscore();
55+
TestResetScore();
56+
Log("[Test] Score = "+this.Score+" | Highscore = "+this.Highscore);
5157
}
5258
}
5359
#endif
60+
#endregion
5461

5562
#region Private Functions
56-
private bool DataExists(string _data) {
57-
return PlayerPrefs.HasKey(_data);
63+
private void SaveInt(string _data, int _value) {
64+
PlayerPrefs.SetInt(_data, _value);
65+
}
66+
67+
private int GetInt(string _data) {
68+
return PlayerPrefs.GetInt(_data, DEFAULT_INT);
69+
}
70+
71+
private void Log(string _msg) {
72+
if (!debug) return;
73+
Debug.Log("[DataController]: "+_msg);
5874
}
5975

6076
private void LogWarning(string _msg) {
6177
if (!debug) return;
6278
Debug.LogWarning("[DataController]: "+_msg);
6379
}
64-
#endif
80+
#endregion
6581

6682
#region Tests
83+
private void TestAddScore(int _delta) {
84+
this.Score += _delta;
85+
}
6786

87+
private void TestResetScore() {
88+
this.Score = 0;
89+
}
90+
91+
private void TestResetHighscore() {
92+
this.Highscore = 0;
93+
}
6894
#endregion
6995
}
7096
}

Menu/PageController.cs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private void Update() {
5353

5454
#region Public Functions
5555
public void TurnPageOn(PageType _type) {
56+
if (_type == PageType.None) return;
5657
if (!PageExists(_type)) {
5758
LogWarning("You are trying to turn a page on ["+_type+"] that has not been registered.");
5859
return;
@@ -64,6 +65,7 @@ public void TurnPageOn(PageType _type) {
6465
}
6566

6667
public void TurnPageOff(PageType _off, PageType _on=PageType.None, bool _waitForExit=false) {
68+
if (_off == PageType.None) return;
6769
if (!PageExists(_off)) {
6870
LogWarning("You are trying to turn a page off ["+_on+"] that has not been registered.");
6971
return;

0 commit comments

Comments
 (0)
0