TimerScript.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. Database db;
  18. GameManagerScript gms;
  19. TimerScript dialogTimer;
  20. int gameId;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. dialogTimer = GameObject.Find("DialogTimerCircle").GetComponent<TimerScript>();
  25. gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  26. db = gms.GetDatabase();
  27. gameId = gms.GameId;
  28. circleImage = this.GetComponent<Image>(); /*
  29. time = db.GetQuestionTimer(gameId);
  30. timeAmount = time; */
  31. circleColor = circleImage.color;
  32. timerText.text = time.ToString("F0");
  33. }
  34. public void StopTimer() {
  35. timerRunning = false;
  36. dialogTimer.timerRunning = false;
  37. }
  38. public void ResetTimer() {
  39. gameId = gms.GameId;
  40. time = db.GetQuestionTimer(gameId);
  41. timeAmount = time;
  42. circleColor = Color.Lerp(startlColor, finalColor, 1 - (time / timeAmount));
  43. dialogTimer.time = time;
  44. dialogTimer.timeAmount = timeAmount;
  45. dialogTimer.circleImage.color = circleColor;
  46. dialogTimer.timerText.text = time.ToString("F0");
  47. circleImage.color = circleColor;
  48. timerText.text = time.ToString("F0");
  49. }
  50. // Update is called once per frame
  51. void Update()
  52. {
  53. if (timerRunning) {
  54. circleColor = circleImage.color;
  55. if (time > 0) {
  56. time -= Time.deltaTime;
  57. circleColor = Color.Lerp(startlColor, finalColor, 1 - (time / timeAmount));
  58. circleImage.color = circleColor;
  59. timerText.text = time.ToString("F0");
  60. } else {
  61. timerRunning = false;
  62. // send message to fail question
  63. if (mainTimer) {
  64. EventManager.TriggerEvent("TimerEvent");
  65. }
  66. }
  67. }
  68. }
  69. internal void StartTimer() {
  70. timerRunning = true;
  71. dialogTimer.timerRunning = true;
  72. }
  73. }