| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public class NewQuestionCardController : MonoBehaviour, IPointerClickHandler {
- public GameObject cardBack;
- public GameObject cardFront;
- public GameObject NewQuestionCard;
- bool rotateDone = true;
- bool backClickable = true;
- [SerializeField] GameObject startParent;
- [SerializeField] GameObject gameManager;
- public bool BackClickable { get => backClickable; set => backClickable = value; }
- public void OnPointerClick(PointerEventData eventData) {
- if (cardBack.activeSelf && rotateDone && BackClickable) {
- RotateCard();
- }
- }
- public void RotateCard() {
- StartCoroutine(Rotate(new Vector3(0, 90f, 0), 0.5f));
- StartCoroutine(SecondRotation());
- StartCoroutine(StartTimer());
- }
- private IEnumerator StartTimer() {
- while (!rotateDone) {
- yield return new WaitForSeconds(0.1f);
- }
- GameObject.Find("GameManager").GetComponent<GameManagerScript>().StartTimer();
- }
- public bool GetRotationDone() {
- return rotateDone;
- }
- private IEnumerator SecondRotation() {
- while (!rotateDone) {
- yield return new WaitForSeconds(0.1f);
- }
- cardBack.SetActive(false);
- cardFront.SetActive(true);
- transform.Rotate(0, 180, 0);
- StartCoroutine(Rotate(new Vector3(0, 90f, 0), 0.5f));
- }
- private IEnumerator Rotate(Vector3 angle, float duration = 1.0f) {
- rotateDone = false;
- Quaternion from = transform.rotation;
- Quaternion to = Quaternion.Euler(angle) * from;
- float elapsed = 0.0f;
- while (elapsed < duration) {
- transform.rotation = Quaternion.Slerp(from, to, elapsed / duration);
- elapsed += Time.deltaTime;
- yield return null;
- }
- transform.rotation = to;
- rotateDone = true;
- }
- private void ShowBackside() {
- cardBack.SetActive(true);
- cardFront.SetActive(false);
- }
- public void GenerateNewQuestion() {
- KeyValuePair<int, string> SignedInUser = Database.Instance.GetSignedInUser();
- string currentPlayer = GameManagerScript.GetCurrentPlayer();
- ResetPosition();
- ShowBackside();
- if (gameManager.GetComponent<GameManagerScript>().GameMode.Equals(Constants.ONLINE)) {
- if (currentPlayer.Equals(SignedInUser.Value, StringComparison.InvariantCultureIgnoreCase)) {
- BackClickable = true;
- } else {
- BackClickable = false;
- }
- } else {
- BackClickable = true;
- }
- transform.parent.GetComponent<NewQuestionsPanel>().GenerateNewQuestion(BackClickable);
- }
- private void ResetPosition() {
- transform.SetParent(startParent.transform);
- transform.localPosition = Vector3.zero;
- }
- }
|