NewDropZoneScript.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class NewDropZoneScript : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
  9. {
  10. public bool RightmostSpacer;
  11. public bool LeftmostSpacer;
  12. public GameObject AnswerLineQuestionCardPrefab;
  13. public float preferedWidth = 30;
  14. private AnswerLineQuestionCard leftSibling;
  15. private AnswerLineQuestionCard rightSibling;
  16. private NewQuestionData questionData;
  17. private GameObject draggedQuestion;
  18. private bool answeredCorrectly = false;
  19. public void OnDrop(PointerEventData eventData)
  20. {
  21. if (eventData != null && draggedQuestion != null)
  22. {
  23. questionData = draggedQuestion.GetComponentInParent<NewQuestionDragController>().NewQuestion.GetComponentInParent<NewQuestionsPanel>()
  24. .QuestionData;
  25. GenericDialog confirmDialog = GenericDialog.Instance();
  26. string message = string.Format(LocalizationManager.Instance.GetText("DROPPED_QUESTION_DIALOG_MESSAGE_TEXT"),
  27. questionData.Question,
  28. draggedQuestion.GetComponent<NewQuestionDragController>().AnswerText.text);
  29. confirmDialog.SetMessage(message);
  30. confirmDialog.SetOnAccept(LocalizationManager.Instance.GetText("YES"), () => {
  31. YesFunction();
  32. confirmDialog.Hide();
  33. CheckAnswerFunction(); // Generera bara ny fråga här?? Vid rätt??
  34. });
  35. confirmDialog.SetOnDecline(LocalizationManager.Instance.GetText("NO"), () =>
  36. {
  37. NewQuestionDragController.setAnswerText("???? - ????");
  38. confirmDialog.Hide();
  39. });
  40. confirmDialog.Show();
  41. }
  42. }
  43. private void CheckAnswerFunction()
  44. {
  45. if (answeredCorrectly) {
  46. GameObject go = Instantiate(AnswerLineQuestionCardPrefab, Vector3.zero, Quaternion.identity);
  47. AnswerLineQuestionCard newAnsweredQuestion = go.GetComponentInParent<AnswerLineQuestionCard>();
  48. newAnsweredQuestion.answerText.text = questionData.Answer;
  49. newAnsweredQuestion.questionText.text = questionData.Question;
  50. newAnsweredQuestion.SetId(questionData.Id);
  51. GameObject AnswerLine = GameObject.FindGameObjectWithTag("AnswerLine");
  52. int newAnswerPos = transform.GetSiblingIndex();
  53. newAnsweredQuestion.transform.SetParent(AnswerLine.transform);
  54. newAnsweredQuestion.transform.SetSiblingIndex(newAnswerPos);
  55. newAnsweredQuestion.transform.localScale = new Vector3(1,1,1);
  56. if (LeftmostSpacer)
  57. {
  58. CreateNewSpacer(AnswerLine, newAnswerPos + 2);
  59. CreateLeftSpacer(AnswerLine);
  60. }
  61. else if (RightmostSpacer)
  62. {
  63. CreateNewSpacer(AnswerLine, newAnswerPos);
  64. CreateRightSpacer(AnswerLine);
  65. }
  66. else
  67. {
  68. CreateNewSpacer(AnswerLine, newAnswerPos);
  69. CreateNewSpacer(AnswerLine, newAnswerPos + 2);
  70. }
  71. Destroy(gameObject);
  72. GameObject.Find("GameManager").GetComponent<GameManagerScript>().StopTimer();
  73. // TODO Check win condition, if met show Congratulations and send loss message to others
  74. draggedQuestion.GetComponentInParent<NewQuestionCardController>().GenerateNewQuestion();
  75. } else { // Wrong answer
  76. // TODO Visa fel svar dialog
  77. GameObject.Find("GameManager").GetComponent<GameManagerScript>().RemoveUnlockedQuestions();
  78. GameObject.Find("GameManager").GetComponent<GameManagerScript>().NextRound();
  79. }
  80. }
  81. private void CreateLeftSpacer(GameObject AnswerLine)
  82. {
  83. GameObject newSpacerLeft = Instantiate(gameObject);
  84. newSpacerLeft.GetComponent<LayoutElement>().preferredWidth = preferedWidth;
  85. newSpacerLeft.name = "LeftDropZone";
  86. newSpacerLeft.GetComponent<NewDropZoneScript>().LeftmostSpacer = true;
  87. newSpacerLeft.transform.SetParent(AnswerLine.transform);
  88. newSpacerLeft.transform.SetAsFirstSibling();
  89. newSpacerLeft.transform.localScale = new Vector3(1,1,1);
  90. }
  91. private void CreateRightSpacer(GameObject AnswerLine)
  92. {
  93. GameObject newSpacerRight = Instantiate(gameObject);
  94. newSpacerRight.GetComponent<LayoutElement>().preferredWidth = preferedWidth;
  95. newSpacerRight.name = "RightDropZone";
  96. newSpacerRight.GetComponent<NewDropZoneScript>().RightmostSpacer = true;
  97. newSpacerRight.transform.SetParent(AnswerLine.transform);
  98. newSpacerRight.transform.SetAsLastSibling();
  99. newSpacerRight.transform.localScale = new Vector3(1,1,1);
  100. }
  101. private void CreateNewSpacer(GameObject AnswerLine, int newAnswerPos)
  102. {
  103. GameObject newSpacer = Instantiate(gameObject);
  104. newSpacer.GetComponent<NewDropZoneScript>().LeftmostSpacer = false;
  105. newSpacer.GetComponent<NewDropZoneScript>().RightmostSpacer = false;
  106. newSpacer.GetComponent<LayoutElement>().preferredWidth = preferedWidth;
  107. newSpacer.name = "DropZone";
  108. newSpacer.transform.SetParent(AnswerLine.transform);
  109. newSpacer.transform.SetSiblingIndex(newAnswerPos);
  110. newSpacer.transform.localScale = new Vector3(1,1,1);
  111. }
  112. public void OnPointerEnter(PointerEventData eventData)
  113. {
  114. draggedQuestion = NewQuestionDragController.itemBeeingDragged;
  115. if (draggedQuestion != null) {
  116. // Expandera aktuell drop yta
  117. LayoutElement le = transform.GetComponent<LayoutElement>();
  118. le.preferredWidth = le.preferredWidth + preferedWidth;
  119. if (LeftmostSpacer) {
  120. rightSibling = transform.parent.GetChild(1).GetComponent<AnswerLineQuestionCard>();
  121. NewQuestionDragController.setAnswerText(LocalizationManager.Instance.GetText("BEFORE") + rightSibling.answerText.text);
  122. leftSibling = null;
  123. } else if (RightmostSpacer) {
  124. leftSibling = transform.parent.GetChild(transform.GetSiblingIndex() - 1).GetComponent<AnswerLineQuestionCard>();
  125. NewQuestionDragController.setAnswerText(LocalizationManager.Instance.GetText("AFTER") + leftSibling.answerText.text);
  126. rightSibling = null;
  127. } else {
  128. leftSibling = transform.parent.GetChild(transform.GetSiblingIndex() - 1).GetComponent<AnswerLineQuestionCard>();
  129. rightSibling = transform.parent.GetChild(transform.GetSiblingIndex() + 1).GetComponent<AnswerLineQuestionCard>();
  130. NewQuestionDragController.setAnswerText(leftSibling.answerText.text + " - " + rightSibling.answerText.text);
  131. }
  132. }
  133. }
  134. public void OnPointerExit(PointerEventData eventData)
  135. {
  136. if (draggedQuestion != null) {
  137. NewQuestionDragController.setAnswerText("???? - ????");
  138. LayoutElement le = transform.GetComponent<LayoutElement>();
  139. le.preferredWidth = le.preferredWidth - preferedWidth;
  140. }
  141. }
  142. void YesFunction() {
  143. String correctYear = questionData.Answer;
  144. String answeredYear = draggedQuestion.GetComponent<NewQuestionDragController>().AnswerText.text;
  145. String yearBefore = leftSibling != null?leftSibling.answerText.text:"-99999";
  146. String yearAfter = rightSibling != null?rightSibling.answerText.text:"99999";
  147. Int32.TryParse(yearBefore, out int answerBeforeInt);
  148. Int32.TryParse(yearAfter, out int answerAfterInt);
  149. Int32.TryParse(correctYear, out int correctYearInt);
  150. if (answerBeforeInt <= correctYearInt && answerAfterInt >= correctYearInt) {
  151. answeredCorrectly = true;
  152. } else {
  153. answeredCorrectly = false;
  154. }
  155. }
  156. }