TimerScript.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class TimerScript : MonoBehaviour
  7. {
  8. Image circleImage;
  9. Color circleColor;
  10. public Text timerText;
  11. float time;
  12. float timeAmount;
  13. public bool mainTimer;
  14. bool timerRunning = false;
  15. Color finalColor = new Color32(130,0,16,220);
  16. Color startlColor = new Color32(16, 0, 255, 200);
  17. GameManagerScript gms;
  18. TimerScript dialogTimer;
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. dialogTimer = GameObject.Find("DialogTimerCircle").GetComponent<TimerScript>();
  23. gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  24. circleImage = this.GetComponent<Image>();
  25. circleColor = circleImage.color;
  26. timerText.text = time.ToString("F0");
  27. }
  28. public void StopTimer() {
  29. timerRunning = false;
  30. dialogTimer.timerRunning = false;
  31. }
  32. public void ResetTimer() {
  33. time = Database.Instance.GetQuestionTimer(gms.GameId);
  34. timeAmount = time;
  35. circleColor = Color.Lerp(startlColor, finalColor, 1 - (time / timeAmount));
  36. dialogTimer.time = time;
  37. dialogTimer.timeAmount = timeAmount;
  38. dialogTimer.circleImage.color = circleColor;
  39. dialogTimer.timerText.text = time.ToString("F0");
  40. circleImage.color = circleColor;
  41. timerText.text = time.ToString("F0");
  42. }
  43. // Update is called once per frame
  44. void Update()
  45. {
  46. if (timerRunning) {
  47. circleColor = circleImage.color;
  48. if (time > 0) {
  49. time -= Time.deltaTime;
  50. circleColor = Color.Lerp(startlColor, finalColor, 1 - (time / timeAmount));
  51. circleImage.color = circleColor;
  52. timerText.text = time.ToString("F0");
  53. } else {
  54. timerRunning = false;
  55. // send message to fail question
  56. if (mainTimer) {
  57. EventManager.TriggerEvent("TimerEvent");
  58. }
  59. }
  60. }
  61. }
  62. internal void StartTimer() {
  63. timerRunning = true;
  64. dialogTimer.timerRunning = true;
  65. }
  66. }