using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class ScrollViewScript : MonoBehaviour, IDropHandler { public GameObject prefab; public Transform contentPanel; private bool placeQuestion; NewQuestion nq; private int newQuestionSiblingIndex; ModalPanel modalPanel; private bool answeredCorrectly; // Start is called before the first frame update void Start() { SetGiventQuestion(); } public void SetGiventQuestion() { // if new game, create one question card to start with GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject; QuestionCard questionCard = question.GetComponent(); questionCard.PrepareQuestion("QuestionText", "AnswerText"); questionCard.GetQuestion(true); questionCard.SetQuestionSafe(); questionCard.transform.SetSiblingIndex(0); questionCard.transform.SetParent(contentPanel); } public int GetUnlockedQuestionCount() { int unlockedQuestionCount = 0; for (int i = 0; i < contentPanel.childCount; i++) { QuestionCard qc = contentPanel.GetChild(i).GetComponent(); if (!qc.IsQuestionSafe()) { unlockedQuestionCount++; } } return unlockedQuestionCount; } private void SetAllQuestionsLocked() { for (int i = 0; i < contentPanel.childCount; i++) { QuestionCard q = contentPanel.GetChild(i).GetComponent(); q.SetQuestionSafe(); } } public void OnDrop(PointerEventData eventData) { Draggable d = eventData.pointerDrag.GetComponent(); if (d == null || !d.gameObject.name.Contains("NewQuestion")) { return; } nq = d.GetComponent(); newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex(); GenericDialog dialog = GenericDialog.Instance(); dialog.SetTitle("Är du säker?"); dialog.SetMessage("Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?"); dialog.SetOnAccept("Ja", () => { YesFunction(); dialog.Hide(); if (answeredCorrectly) { dialog.SetTitle("Korrekt svarat " + nq.answerString + "!"); dialog.SetMessage("Helt korrekt, vill du ha en ny fråga, du riskerar då " + GetUnlockedQuestionCount() + " om du svarar fel"); dialog.SetOnAccept("Ja", () => { dialog.Hide(); nq.SetQuestionData(); SetQuestionsInAnswerLine(); }); // generera ny fråga dialog.SetOnDecline("Nej", () => { dialog.Hide(); SetAllQuestionsLocked(); IncreaseRoundValue(); SetQuestionsInAnswerLine(); }); // lås frågor som ej är låsta. } else { dialog.SetTitle("Tyvärr fel svar, korrekt svar var " + nq.answerString); dialog.SetMessage("Du förlorade " + GetUnlockedQuestionCount() + ", bättre lycka nästa gång"); RemoveUnlockedQuestions(); dialog.SetOnAccept("Ok", () => dialog.Hide()); dialog.SetOnDecline("", () => dialog.Hide()); IncreaseRoundValue(); nq.SetQuestionData(); } dialog.Show(); }); dialog.SetOnDecline("Nej", () => dialog.Hide()); dialog.Show(); } private void RemoveUnlockedQuestions() { int lostQuestions = 0; for (int i = 0; i < contentPanel.childCount; i++) { QuestionCard q = contentPanel.GetChild(i).GetComponent(); if (!q.IsQuestionSafe()) { lostQuestions++; Destroy(q.gameObject); } } Text lostQuestionsValueText = GameObject.Find("Stats Value Lost").GetComponent(); lostQuestionsValueText.text = (Int16.Parse(lostQuestionsValueText.text) + lostQuestions).ToString(); SetQuestionsInAnswerLine(contentPanel.childCount - lostQuestions); } private void IncreaseRoundValue() { Text roundTextValue = GameObject.Find("Stats Value Round").GetComponent(); roundTextValue.text = (Int16.Parse(roundTextValue.text) + 1).ToString(); } private void SetQuestionsInAnswerLine() { Text answerlineTextValue = GameObject.Find("Stats Value Inline").GetComponent(); answerlineTextValue.text = contentPanel.childCount.ToString(); } private void SetQuestionsInAnswerLine(int answers) { Text answerlineTextValue = GameObject.Find("Stats Value Inline").GetComponent(); answerlineTextValue.text = answers.ToString(); } void YesFunction() { int correctAnswer = Int32.Parse(nq.answerString); int currentChildCount = contentPanel.childCount; Transform questionBefore = null; Transform questionAfter = null; if (newQuestionSiblingIndex - 1 > 0) { questionBefore = contentPanel.GetChild(newQuestionSiblingIndex - 1); } if (newQuestionSiblingIndex < currentChildCount) { questionAfter = contentPanel.GetChild(newQuestionSiblingIndex); } QuestionCard test = contentPanel.GetChild(0).GetComponent(); int answerBefore = -1; int answerAfter = 999999; if (questionBefore != null) { answerBefore = Int16.Parse(questionBefore.GetComponent().GetAnswerText().text); } if (questionAfter != null) { answerAfter = Int16.Parse(questionAfter.GetComponent().GetAnswerText().text); } if (answerBefore <= correctAnswer && answerAfter >= correctAnswer) { // 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) GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject; QuestionCard questionCard = question.GetComponent(); questionCard.SetAnswerText(nq.answerString); questionCard.SetQuestionText(nq.questionString); questionCard.questionString = nq.questionString; questionCard.answerString = nq.answerString; questionCard.categoryString = nq.categoryString; questionCard.idString = nq.idString; questionCard.transform.SetParent(contentPanel); questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex); answeredCorrectly = true; } else { answeredCorrectly = false; } } void NoFunction() { } }