-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayUI.cs
More file actions
122 lines (103 loc) · 3.82 KB
/
DisplayUI.cs
File metadata and controls
122 lines (103 loc) · 3.82 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.VisualScripting;
using UnityEngine.SceneManagement;
public class DisplayUI : MonoBehaviour
{
//instanciranje skripte
public static DisplayUI Instance;
//text polja za unos sira, timer i bodove
public TextMeshProUGUI cheeseCount;
public TextMeshProUGUI timerText;
public TextMeshProUGUI scoreText;
//varijable potrebne za timer
public float timeLeft;
public bool timerOn = false;
private float minute;
private float sec;
//varijable potrebne za izracunavanje bodova
public float remainTime;
private float score = 0;
public int valueOfCheese;
public int valueOfSec;
int roundedValue;
public int livesValue = 6;
//varijabla za spremanje build indeksa trenutne scene
int currentSceneIndex;
//instanciranje skripte
void Awake()
{
Instance = this;
}
//pokretanje timera
void Start()
{
scoreText.text = "Score: " + score.ToString();
timerOn = true;
}
//dohvaca broj sireva u Inventory skripti i povecava score za rijednost sira(100)
public void UpdateCheeseCount(Inventory playerInventory)
{
cheeseCount.text = playerInventory.NumberOfCheese.ToString();
score += valueOfCheese;
scoreText.text = "Score: " + score.ToString();
}
//logika timera
void Update()
{
if (timerOn)
{
if (timeLeft > 0)
{
timeLeft -= Time.deltaTime;
UpdateTimer(timeLeft);
}
else
{
//Debug.Log("Z timer ");
timeLeft = 0;
timerOn = false;
}
}
}
//pretvaranje broj sekundi u minute i sekunde prema formatu
void UpdateTimer(float currentTime)
{
currentTime += 1;
minute = Mathf.FloorToInt(currentTime / 60);
sec = Mathf.FloorToInt(currentTime % 60);
timerText.text = string.Format("{0:00} : {1:00}", minute, sec);
remainTime = minute * 60 + sec;
}
//azuriranje finalnih bodova prema dolje navedenoj logici
public void UpdateFinishScore()
{
int lives = PlayerPrefs.GetInt("Lives");
//Debug.Log("Broj zivota: " + lives);
if (lives == 0) {
//ne uzimamo u obzir preostalo vrijeme i ne dodajemo bodove za preostale živote
roundedValue = Mathf.RoundToInt(score);
}
else if (lives > 0)
{
//uzimamo u obzir preostalo vrijeme i dodajemo bodove za preostale živote
score += (timeLeft * valueOfSec) * (lives*livesValue);
roundedValue = Mathf.RoundToInt(score);
}
//Debug.Log("TimeLeft " + timeLeft);
scoreText.text = "Score: " + roundedValue.ToString();
SaveLoadData();
//Debug.Log("Broj zivota2: " + lives);
timerOn = false;
}
//spremanje bodova u PlayerPrefs za svaki level koristeci buildindex scene
public void SaveLoadData()
{
currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("Score" + currentSceneIndex, roundedValue);
Debug.Log("Score" + currentSceneIndex + ": " + roundedValue);
PlayerPrefs.Save();
}
}