-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cs
More file actions
76 lines (57 loc) · 2.01 KB
/
Timer.cs
File metadata and controls
76 lines (57 loc) · 2.01 KB
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
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public Text timerText;
public Text bestTime;
public Text newBest;
public float startTimeCount;
public float pointsPerSec;
public bool scoreIncreasing = true;
public static float bestTimeCount = -1;
void Start()
{
newBest.text = "";
ReadFromFile();
if (bestTimeCount != -1)
bestTime.text = "Best time: " + Mathf.Round(Timer.bestTimeCount);
}
void Update()
{
if (scoreIncreasing)
{
startTimeCount += pointsPerSec + Time.deltaTime;
}
timerText.text = "Time: " + Mathf.Round(startTimeCount);
}
public void GameEnd()
{
if ((startTimeCount < Timer.bestTimeCount) || (Timer.bestTimeCount == -1))
{
Timer.bestTimeCount = startTimeCount;
PlayerPrefs.SetFloat("BestTime", (Timer.bestTimeCount));
newBest.text = "!!New best time!!";
}
bestTime.text = "Best time: " + Mathf.Round(Timer.bestTimeCount);
StoreToFile();
}
private void StoreToFile()
{
string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
string specificFolder = Path.Combine(folder, "Soko");
Directory.CreateDirectory(specificFolder);
File.WriteAllText(Path.Combine(specificFolder, "besttime"), Timer.bestTimeCount.ToString());
}
private void ReadFromFile()
{
string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
string specificFolder = Path.Combine(folder, "Soko");
if (File.Exists(Path.Combine(specificFolder, "besttime")))
{
Timer.bestTimeCount = float.Parse(File.ReadAllText(Path.Combine(specificFolder, "besttime")));
}
}
}