ScrollViewScript.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. public class ScrollViewScript : MonoBehaviour, IDropHandler {
  7. public GameObject prefab;
  8. public Transform contentPanel;
  9. private bool placeQuestion;
  10. NewQuestion nq;
  11. private int newQuestionSiblingIndex;
  12. ModalPanel modalPanel;
  13. private bool answeredCorrectly;
  14. // Start is called before the first frame update
  15. void Start() {
  16. SetGiventQuestion();
  17. }
  18. public void SetGiventQuestion() {
  19. // if new game, create one question card to start with
  20. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  21. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  22. questionCard.PrepareQuestion("QuestionText", "AnswerText");
  23. questionCard.GetQuestion(true);
  24. questionCard.SetQuestionSafe();
  25. questionCard.transform.SetSiblingIndex(0);
  26. questionCard.transform.SetParent(contentPanel);
  27. }
  28. public int GetUnlockedQuestionCount() {
  29. int unlockedQuestionCount = 0;
  30. for (int i = 0; i < contentPanel.childCount; i++) {
  31. QuestionCard qc = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  32. if (!qc.IsQuestionSafe()) {
  33. unlockedQuestionCount++;
  34. }
  35. }
  36. return unlockedQuestionCount;
  37. }
  38. private void SetAllQuestionsLocked() {
  39. for (int i = 0; i < contentPanel.childCount; i++) {
  40. QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  41. q.SetQuestionSafe();
  42. }
  43. }
  44. public void OnDrop(PointerEventData eventData) {
  45. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  46. if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
  47. return;
  48. }
  49. // Popup modal asking are you sure here or at onDragEnd?
  50. nq = d.GetComponent<NewQuestion>();
  51. newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex();
  52. GenericDialog dialog = GenericDialog.Instance();
  53. dialog.SetTitle("Är du säker?");
  54. dialog.SetMessage("Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?");
  55. dialog.SetOnAccept("Ja", () => {
  56. YesFunction();
  57. dialog.Hide();
  58. if (answeredCorrectly) {
  59. dialog.SetTitle("Korrekt svarat " + nq.answerString + "!");
  60. dialog.SetMessage("Helt korrekt, vill du ha en ny fråga, du riskerar då " + GetUnlockedQuestionCount() + " om du svarar fel");
  61. dialog.SetOnAccept("Ja", () => {
  62. dialog.Hide();
  63. nq.SetQuestionData();
  64. }); // generera ny fråga
  65. dialog.SetOnDecline("Nej", () => {
  66. dialog.Hide();
  67. SetAllQuestionsLocked();
  68. }); // lås frågor som ej är låsta.
  69. } else {
  70. dialog.SetTitle("Tyvärr fel svar, korrekt svar var " + nq.answerString);
  71. dialog.SetMessage("Du förlorade " + GetUnlockedQuestionCount() + ", bättre lycka nästa gång");
  72. RemoveUnlockedQuestions();
  73. dialog.SetOnAccept("Ok", () => dialog.Hide());
  74. dialog.SetOnDecline("", () => dialog.Hide());
  75. nq.SetQuestionData();
  76. }
  77. dialog.Show();
  78. });
  79. dialog.SetOnDecline("Nej", () => dialog.Hide());
  80. dialog.Show();
  81. }
  82. private void RemoveUnlockedQuestions() {
  83. for (int i = 0; i < contentPanel.childCount; i++) {
  84. QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  85. if (!q.IsQuestionSafe()) {
  86. Destroy(q.gameObject);
  87. }
  88. }
  89. }
  90. // kontrollera om man svarat korrekt, om det är korrekt, visa ny modal med vill du ha ny fråga? om fel, meddela det och avsluta omgång.
  91. void YesFunction() {
  92. int correctAnswer = Int32.Parse(nq.answerString);
  93. Transform questionBefore = null;
  94. Transform questionAfter = null;
  95. try {
  96. questionBefore = contentPanel.GetChild(newQuestionSiblingIndex - 1);
  97. } catch (UnityException e) { }
  98. try {
  99. questionAfter = contentPanel.GetChild(newQuestionSiblingIndex);
  100. } catch (UnityException e) { }
  101. QuestionCard test = contentPanel.GetChild(0).GetComponent<QuestionCard>();
  102. int answerBefore = -1;
  103. int answerAfter = 999999;
  104. if (questionBefore != null) {
  105. answerBefore = Int16.Parse(questionBefore.GetComponent<QuestionCard>().GetAnswerText().text);
  106. }
  107. if (questionAfter != null) {
  108. answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().GetAnswerText().text);
  109. }
  110. if (answerBefore <= correctAnswer && answerAfter >= correctAnswer) {
  111. // korrect svar, spara frågan i tidslinjen och prompta ny modal för "Vill du ha en fråga till?" med meddelande om vad som står på spel (olåsta frågor)
  112. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  113. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  114. questionCard.SetAnswerText(nq.answerString);
  115. questionCard.SetQuestionText(nq.questionString);
  116. questionCard.questionString = nq.questionString;
  117. questionCard.answerString = nq.answerString;
  118. questionCard.categoryString = nq.categoryString;
  119. questionCard.idString = nq.idString;
  120. questionCard.transform.SetParent(contentPanel);
  121. questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
  122. answeredCorrectly = true;
  123. } else {
  124. answeredCorrectly = false;
  125. }
  126. }
  127. void NoFunction() {
  128. }
  129. }