NewQuestionCardController.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 bool BackClickable { get => backClickable; set => backClickable = value; }
  16. public void OnPointerClick(PointerEventData eventData) {
  17. if (cardBack.activeSelf && rotateDone && BackClickable)
  18. {
  19. RotateCard();
  20. }
  21. }
  22. public void RotateCard()
  23. {
  24. StartCoroutine(Rotate(new Vector3(0, 90f, 0), 0.5f));
  25. StartCoroutine(secondRotation());
  26. StartCoroutine(startTimer());
  27. }
  28. private IEnumerator startTimer()
  29. {
  30. while (!rotateDone) {
  31. yield return new WaitForSeconds(0.1f);
  32. }
  33. GameObject.Find("GameManager").GetComponent<GameManagerScript>().StartTimer();
  34. }
  35. public bool getRotationDone() {
  36. return rotateDone;
  37. }
  38. private IEnumerator secondRotation() {
  39. while (!rotateDone) {
  40. yield return new WaitForSeconds(0.1f);
  41. }
  42. cardBack.SetActive(false);
  43. cardFront.SetActive(true);
  44. transform.Rotate(0, 180, 0);
  45. StartCoroutine(Rotate(new Vector3(0, 90f,0 ), 0.5f));
  46. }
  47. private IEnumerator Rotate(Vector3 angle, float duration = 1.0f) {
  48. rotateDone = false;
  49. Quaternion from = transform.rotation;
  50. Quaternion to = Quaternion.Euler(angle) * from;
  51. float elapsed = 0.0f;
  52. while (elapsed < duration) {
  53. transform.rotation = Quaternion.Slerp(from, to, elapsed / duration);
  54. elapsed += Time.deltaTime;
  55. yield return null;
  56. }
  57. transform.rotation = to;
  58. rotateDone = true;
  59. }
  60. private void ShowBackside() {
  61. cardBack.SetActive(true);
  62. cardFront.SetActive(false);
  63. }
  64. public void GenerateNewQuestion() {
  65. KeyValuePair<int, string> SignedInUser = Database.Instance.GetSignedInUser();
  66. string currentPlayer = GameManagerScript.GetCurrentPlayer();
  67. resetPosition();
  68. ShowBackside();
  69. string gameMode = gameManager.GetComponent<GameManagerScript>().GetGameMode();
  70. if (gameMode.Equals("Online")) {
  71. if (currentPlayer.Equals(SignedInUser.Value, StringComparison.InvariantCultureIgnoreCase)) {
  72. BackClickable = true;
  73. } else {
  74. BackClickable = false;
  75. }
  76. } else {
  77. BackClickable = true;
  78. }
  79. transform.parent.GetComponent<NewQuestionsPanel>().generateNewQuestion(BackClickable);
  80. }
  81. private void resetPosition() {
  82. transform.SetParent(startParent.transform);
  83. transform.localPosition = Vector3.zero;
  84. }
  85. }