NewQuestionCardController.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class NewQuestionCardController : MonoBehaviour, IPointerClickHandler {
  8. public GameObject cardBack;
  9. public GameObject cardFront;
  10. public GameObject NewQuestionCard;
  11. bool rotateDone = true;
  12. bool backClickable = true;
  13. [SerializeField] GameObject startParent;
  14. [SerializeField] GameObject gameManager;
  15. public void OnPointerClick(PointerEventData eventData) {
  16. if (cardBack.activeSelf && rotateDone && backClickable)
  17. {
  18. RotateCard();
  19. }
  20. }
  21. public void RotateCard()
  22. {
  23. StartCoroutine(Rotate(new Vector3(0, 90f, 0), 0.5f));
  24. StartCoroutine(secondRotation());
  25. StartCoroutine(startTimer());
  26. }
  27. private IEnumerator startTimer()
  28. {
  29. while (!rotateDone) {
  30. yield return new WaitForSeconds(0.1f);
  31. }
  32. GameObject.Find("GameManager").GetComponent<GameManagerScript>().StartTimer();
  33. }
  34. public bool getRotationDone() {
  35. return rotateDone;
  36. }
  37. private IEnumerator secondRotation() {
  38. while (!rotateDone) {
  39. yield return new WaitForSeconds(0.1f);
  40. }
  41. cardBack.SetActive(false);
  42. cardFront.SetActive(true);
  43. transform.Rotate(0, 180, 0);
  44. StartCoroutine(Rotate(new Vector3(0, 90f,0 ), 0.5f));
  45. }
  46. private IEnumerator Rotate(Vector3 angle, float duration = 1.0f) {
  47. rotateDone = false;
  48. Quaternion from = transform.rotation;
  49. Quaternion to = Quaternion.Euler(angle) * from;
  50. float elapsed = 0.0f;
  51. while (elapsed < duration) {
  52. transform.rotation = Quaternion.Slerp(from, to, elapsed / duration);
  53. elapsed += Time.deltaTime;
  54. yield return null;
  55. }
  56. transform.rotation = to;
  57. rotateDone = true;
  58. }
  59. private void ShowBackside() {
  60. cardBack.SetActive(true);
  61. cardFront.SetActive(false);
  62. }
  63. public void GenerateNewQuestion() {
  64. KeyValuePair<int, string> SignedInUser = Database.Instance.GetSignedInUser();
  65. string currentPlayer = GameManagerScript.GetCurrentPlayer();
  66. resetPosition();
  67. ShowBackside();
  68. string gameMode = gameManager.GetComponent<GameManagerScript>().GetGameMode();
  69. if (gameMode.Equals("Online")) {
  70. if (currentPlayer.Equals(SignedInUser.Value)) {
  71. backClickable = true;
  72. } else {
  73. backClickable = false;
  74. }
  75. } else {
  76. backClickable = true;
  77. }
  78. transform.parent.GetComponent<NewQuestionsPanel>().generateNewQuestion(backClickable);
  79. }
  80. private void resetPosition() {
  81. transform.SetParent(startParent.transform);
  82. transform.localPosition = Vector3.zero;
  83. }
  84. }