ScrollViewScript.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 ScrollViewScript : MonoBehaviour, IDropHandler {
  8. public GameObject prefab;
  9. public Transform contentPanel;
  10. private bool placeQuestion;
  11. NewQuestion nq;
  12. private int newQuestionSiblingIndex;
  13. ModalPanel modalPanel;
  14. private bool answeredCorrectly;
  15. // Start is called before the first frame update
  16. void Start() {
  17. SetGiventQuestion();
  18. }
  19. public void SetGiventQuestion() {
  20. // if new game, create one question card to start with
  21. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  22. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  23. questionCard.PrepareQuestion("QuestionText", "AnswerText");
  24. questionCard.GetQuestion(true);
  25. questionCard.SetQuestionSafe();
  26. questionCard.transform.SetSiblingIndex(0);
  27. questionCard.transform.SetParent(contentPanel);
  28. }
  29. public int GetUnlockedQuestionCount() {
  30. int unlockedQuestionCount = 0;
  31. for (int i = 0; i < contentPanel.childCount; i++) {
  32. QuestionCard qc = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  33. if (!qc.IsQuestionSafe()) {
  34. unlockedQuestionCount++;
  35. }
  36. }
  37. return unlockedQuestionCount;
  38. }
  39. private void SetAllQuestionsLocked() {
  40. for (int i = 0; i < contentPanel.childCount; i++) {
  41. QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  42. q.SetQuestionSafe();
  43. }
  44. }
  45. public void OnDrop(PointerEventData eventData) {
  46. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  47. if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
  48. return;
  49. }
  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. SetQuestionsInAnswerLine();
  65. }); // generera ny fråga
  66. dialog.SetOnDecline("Nej", () => {
  67. dialog.Hide();
  68. SetAllQuestionsLocked();
  69. IncreaseRoundValue();
  70. SetQuestionsInAnswerLine();
  71. }); // lås frågor som ej är låsta.
  72. } else {
  73. dialog.SetTitle("Tyvärr fel svar, korrekt svar var " + nq.answerString);
  74. dialog.SetMessage("Du förlorade " + GetUnlockedQuestionCount() + ", bättre lycka nästa gång");
  75. RemoveUnlockedQuestions();
  76. dialog.SetOnAccept("Ok", () => dialog.Hide());
  77. dialog.SetOnDecline("", () => dialog.Hide());
  78. IncreaseRoundValue();
  79. nq.SetQuestionData();
  80. }
  81. dialog.Show();
  82. });
  83. dialog.SetOnDecline("Nej", () => dialog.Hide());
  84. dialog.Show();
  85. }
  86. private void RemoveUnlockedQuestions() {
  87. int lostQuestions = 0;
  88. for (int i = 0; i < contentPanel.childCount; i++) {
  89. QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  90. if (!q.IsQuestionSafe()) {
  91. lostQuestions++;
  92. Destroy(q.gameObject);
  93. }
  94. }
  95. Text lostQuestionsValueText = GameObject.Find("Stats Value Lost").GetComponent<Text>();
  96. lostQuestionsValueText.text = (Int16.Parse(lostQuestionsValueText.text) + lostQuestions).ToString();
  97. SetQuestionsInAnswerLine(contentPanel.childCount - lostQuestions);
  98. }
  99. private void IncreaseRoundValue() {
  100. Text roundTextValue = GameObject.Find("Stats Value Round").GetComponent<Text>();
  101. roundTextValue.text = (Int16.Parse(roundTextValue.text) + 1).ToString();
  102. }
  103. private void SetQuestionsInAnswerLine() {
  104. Text answerlineTextValue = GameObject.Find("Stats Value Inline").GetComponent<Text>();
  105. answerlineTextValue.text = contentPanel.childCount.ToString();
  106. }
  107. private void SetQuestionsInAnswerLine(int answers) {
  108. Text answerlineTextValue = GameObject.Find("Stats Value Inline").GetComponent<Text>();
  109. answerlineTextValue.text = answers.ToString();
  110. }
  111. void YesFunction() {
  112. int correctAnswer = Int32.Parse(nq.answerString);
  113. int currentChildCount = contentPanel.childCount;
  114. Transform questionBefore = null;
  115. Transform questionAfter = null;
  116. if (newQuestionSiblingIndex - 1 > 0) {
  117. questionBefore = contentPanel.GetChild(newQuestionSiblingIndex - 1);
  118. }
  119. if (newQuestionSiblingIndex < currentChildCount) {
  120. questionAfter = contentPanel.GetChild(newQuestionSiblingIndex);
  121. }
  122. QuestionCard test = contentPanel.GetChild(0).GetComponent<QuestionCard>();
  123. int answerBefore = -1;
  124. int answerAfter = 999999;
  125. if (questionBefore != null) {
  126. answerBefore = Int16.Parse(questionBefore.GetComponent<QuestionCard>().GetAnswerText().text);
  127. }
  128. if (questionAfter != null) {
  129. answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().GetAnswerText().text);
  130. }
  131. if (answerBefore <= correctAnswer && answerAfter >= correctAnswer) {
  132. // 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)
  133. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  134. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  135. questionCard.SetAnswerText(nq.answerString);
  136. questionCard.SetQuestionText(nq.questionString);
  137. questionCard.questionString = nq.questionString;
  138. questionCard.answerString = nq.answerString;
  139. questionCard.categoryString = nq.categoryString;
  140. questionCard.idString = nq.idString;
  141. questionCard.transform.SetParent(contentPanel);
  142. questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
  143. answeredCorrectly = true;
  144. } else {
  145. answeredCorrectly = false;
  146. }
  147. }
  148. void NoFunction() {
  149. }
  150. }