NewQuestionCardController.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. RotateCard();
  19. }
  20. }
  21. public void RotateCard() {
  22. StartCoroutine(Rotate(new Vector3(0, 90f, 0), 0.5f));
  23. StartCoroutine(SecondRotation());
  24. StartCoroutine(StartTimer());
  25. }
  26. private IEnumerator StartTimer() {
  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. KeyValuePair<int, string> SignedInUser = Database.Instance.GetSignedInUser();
  63. string currentPlayer = GameManagerScript.GetCurrentPlayer();
  64. ResetPosition();
  65. ShowBackside();
  66. if (gameManager.GetComponent<GameManagerScript>().GameMode.Equals(Constants.ONLINE)) {
  67. if (currentPlayer.Equals(SignedInUser.Value, StringComparison.InvariantCultureIgnoreCase)) {
  68. BackClickable = true;
  69. } else {
  70. BackClickable = false;
  71. }
  72. } else {
  73. BackClickable = true;
  74. }
  75. transform.parent.GetComponent<NewQuestionsPanel>().GenerateNewQuestion(BackClickable);
  76. }
  77. private void ResetPosition() {
  78. transform.SetParent(startParent.transform);
  79. transform.localPosition = Vector3.zero;
  80. }
  81. }