| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class TimerScript : MonoBehaviour
- {
- Image circleImage;
- Color circleColor;
- public Text timerText;
- float time;
- float timeAmount;
- public bool mainTimer;
- bool timerRunning = false;
- Color finalColor = new Color32(130,0,16,220);
- Color startlColor = new Color32(16, 0, 255, 200);
- Database db;
- GameManagerScript gms;
- TimerScript dialogTimer;
- int gameId;
- // Start is called before the first frame update
- void Start()
- {
- dialogTimer = GameObject.Find("DialogTimerCircle").GetComponent<TimerScript>();
- gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
- db = gms.GetDatabase();
- gameId = gms.GameId;
- circleImage = this.GetComponent<Image>(); /*
- time = db.GetQuestionTimer(gameId);
- timeAmount = time; */
-
- circleColor = circleImage.color;
- timerText.text = time.ToString("F0");
- }
- public void StopTimer() {
- timerRunning = false;
- dialogTimer.timerRunning = false;
- }
- public void ResetTimer() {
- gameId = gms.GameId;
- time = db.GetQuestionTimer(gameId);
- timeAmount = time;
- circleColor = Color.Lerp(startlColor, finalColor, 1 - (time / timeAmount));
- dialogTimer.time = time;
- dialogTimer.timeAmount = timeAmount;
- dialogTimer.circleImage.color = circleColor;
- dialogTimer.timerText.text = time.ToString("F0");
- circleImage.color = circleColor;
- timerText.text = time.ToString("F0");
- }
- // Update is called once per frame
- void Update()
- {
- if (timerRunning) {
- circleColor = circleImage.color;
- if (time > 0) {
- time -= Time.deltaTime;
- circleColor = Color.Lerp(startlColor, finalColor, 1 - (time / timeAmount));
- circleImage.color = circleColor;
- timerText.text = time.ToString("F0");
- } else {
- timerRunning = false;
- // send message to fail question
- if (mainTimer) {
- EventManager.TriggerEvent("TimerEvent");
- }
- }
- }
- }
- internal void StartTimer() {
- timerRunning = true;
- dialogTimer.timerRunning = true;
- }
- }
|