NewQuestionCardController.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. [SerializeField] GameObject startParent;
  13. public void OnPointerClick(PointerEventData eventData) {
  14. if (cardBack.activeSelf && rotateDone)
  15. {
  16. RotateCard();
  17. }
  18. }
  19. public void RotateCard()
  20. {
  21. StartCoroutine(Rotate(new Vector3(0, 90f, 0), 0.5f));
  22. StartCoroutine(secondRotation());
  23. StartCoroutine(startTimer());
  24. }
  25. private IEnumerator startTimer()
  26. {
  27. while (!rotateDone) {
  28. yield return new WaitForSeconds(0.1f);
  29. }
  30. GameObject.Find("GameManager").GetComponent<GameManagerScript>().StartTimer();
  31. }
  32. public bool getRotationDone() {
  33. return rotateDone;
  34. }
  35. private IEnumerator secondRotation() {
  36. while (!rotateDone) {
  37. yield return new WaitForSeconds(0.1f);
  38. }
  39. cardBack.SetActive(false);
  40. cardFront.SetActive(true);
  41. transform.Rotate(0, 180, 0);
  42. StartCoroutine(Rotate(new Vector3(0, 90f,0 ), 0.5f));
  43. }
  44. private IEnumerator Rotate(Vector3 angle, float duration = 1.0f) {
  45. rotateDone = false;
  46. Quaternion from = transform.rotation;
  47. Quaternion to = Quaternion.Euler(angle) * from;
  48. float elapsed = 0.0f;
  49. while (elapsed < duration) {
  50. transform.rotation = Quaternion.Slerp(from, to, elapsed / duration);
  51. elapsed += Time.deltaTime;
  52. yield return null;
  53. }
  54. transform.rotation = to;
  55. rotateDone = true;
  56. }
  57. private void ShowBackside() {
  58. cardBack.SetActive(true);
  59. cardFront.SetActive(false);
  60. }
  61. public void GenerateNewQuestion() {
  62. resetPosition();
  63. ShowBackside();
  64. transform.parent.GetComponent<NewQuestionsPanel>().generateNewQuestion();
  65. }
  66. private void resetPosition() {
  67. transform.SetParent(startParent.transform);
  68. transform.localPosition = Vector3.zero;
  69. }
  70. }