Jelajahi Sumber

Ny dialog lösning vid svar (ej klar)

Axel Nordh 7 tahun lalu
induk
melakukan
d6562725b4

+ 8 - 1
Assets/Scripts/Draggable.cs

@@ -96,7 +96,14 @@ public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDra
         GetComponent<CanvasGroup>().blocksRaycasts = true;
 
         Destroy(placeholder);
-        //Destroy(this);
+        // Destroy(this); fungerar ej här om man ångrar vart man släpper frågan, då går den ej att dra igen.
         // Show Modal and get yes/no confirm on placement, create new QuestionCard at newSiblingIndex.
+
+        //ModalPanel modalPanel = ModalPanel.Instance();
+        //ModalPanelDetails wrongAnswerDetail = new ModalPanelDetails { question = "Tyvärr fel svar, du förlorade olåsta frågor", iconImage = null };
+        //wrongAnswerDetail.button1Details = new EventButtonDetails { buttonTitle = "Ok" };
+        //wrongAnswerDetail.button2Details = null;
+        //wrongAnswerDetail.button3Details = null;
+        //modalPanel.NewChoice(wrongAnswerDetail);
     }
 }

+ 8 - 0
Assets/Scripts/GeneralDialog.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 9eccd71bd0c3de04f8857ea1b45bf85a
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 67 - 0
Assets/Scripts/GeneralDialog/GenericDialog.cs

@@ -0,0 +1,67 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+using UnityEngine.Events;
+
+[RequireComponent(typeof(CanvasGroup))]
+public class GenericDialog : MonoBehaviour {
+    public Text title;
+    public Text message;
+    public Text accept, decline;
+    public Button acceptButton, declineButton;
+
+    private CanvasGroup cg;
+
+    void Awake() {
+        cg = GetComponent<CanvasGroup>();
+    }
+
+    public GenericDialog SetOnAccept(string text, UnityAction action) {
+        accept.text = text;
+        acceptButton.onClick.RemoveAllListeners();
+        acceptButton.onClick.AddListener(action);
+        return this;
+    }
+
+    public GenericDialog SetOnDecline(string text, UnityAction action) {
+        decline.text = text;
+        declineButton.onClick.RemoveAllListeners();
+        declineButton.onClick.AddListener(action);
+        return this;
+    }
+
+    public GenericDialog SetTitle(string title) {
+        this.title.text = title;
+        return this;
+    }
+
+    public GenericDialog SetMessage(string message) {
+        this.message.text = message;
+        return this;
+    }
+
+    public void Show() {
+        this.transform.SetAsLastSibling();
+        cg.blocksRaycasts = true;
+        cg.alpha = 1f;
+        cg.interactable = true;
+    }
+
+    public void Hide() {
+        cg.interactable = false;
+        cg.alpha = 0f;
+        cg.blocksRaycasts = false;
+    }
+
+    private static GenericDialog instance;
+    public static GenericDialog Instance() {
+        if (!instance) {
+            instance = FindObjectOfType(typeof(GenericDialog)) as GenericDialog;
+            if (!instance) {
+                Debug.Log("There need to be at least one active GenericDialog on the screen");
+            }
+        }
+        return instance;
+    }
+}

+ 11 - 0
Assets/Scripts/GeneralDialog/GenericDialog.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 39d09746ab85ce4418cf16140664dca3
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 0 - 112
Assets/Scripts/ModalPanel/ModalPanel.cs

@@ -52,28 +52,6 @@ public class ModalPanel : MonoBehaviour {
         return modalPanel;
     }
 
-
-    //  //  Announcement with Image:  A string, a Sprite and Cancel event;
-    //  public void Choice (string question, UnityAction cancelEvent, Sprite iconImage = null) {
-    //      modalPanelObject.SetActive (true);
-    //      
-    //      button3.onClick.RemoveAllListeners();
-    //      button3.onClick.AddListener (cancelEvent);
-    //      button3.onClick.AddListener (ClosePanel);
-    //      
-    //      this.question.text = question;
-    //      if (iconImage)
-    //          this.iconImage.sprite = iconImage;
-    //      
-    //      if (iconImage)
-    //          this.iconImage.gameObject.SetActive(true);
-    //      else
-    //          this.iconImage.gameObject.SetActive(false);
-    //      button1.gameObject.SetActive(false);
-    //      button2.gameObject.SetActive(false);
-    //      button3.gameObject.SetActive(true);
-    //  }
-
     public void NewChoice(ModalPanelDetails details) {
         modalPanelObject.SetActive(true);
 
@@ -112,96 +90,6 @@ public class ModalPanel : MonoBehaviour {
         }
     }
 
-    //  //  Yes/No: A string, a Yes event, a No event (No Cancel Button);
-    //  public void Choice (string question, UnityAction yesEvent, UnityAction noEvent) {
-    //      modalPanelObject.SetActive (true);
-    //
-    //      button1.onClick.RemoveAllListeners();
-    //      button1.onClick.AddListener (yesEvent);
-    //      button1.onClick.AddListener (ClosePanel);
-    //
-    //      button2.onClick.RemoveAllListeners();
-    //      button2.onClick.AddListener (noEvent);
-    //      button2.onClick.AddListener (ClosePanel);
-    //
-    //      this.question.text = question;
-    //      
-    //      this.iconImage.gameObject.SetActive(false);
-    //      button1.gameObject.SetActive(true);
-    //      button2.gameObject.SetActive(true);
-    //      button3.gameObject.SetActive(false);
-    //  }
-    //  
-    //  //  Yes/No/Cancel: A string, a Yes event, a No event and Cancel event;
-    //  public void Choice (string question, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) {
-    //      modalPanelObject.SetActive (true);
-    //      
-    //      button1.onClick.RemoveAllListeners();
-    //      button1.onClick.AddListener (yesEvent);
-    //      button1.onClick.AddListener (ClosePanel);
-    //      
-    //      button2.onClick.RemoveAllListeners();
-    //      button2.onClick.AddListener (noEvent);
-    //      button2.onClick.AddListener (ClosePanel);
-    //      
-    //      button3.onClick.RemoveAllListeners();
-    //      button3.onClick.AddListener (cancelEvent);
-    //      button3.onClick.AddListener (ClosePanel);
-    //      
-    //      this.question.text = question;
-    //      
-    //      this.iconImage.gameObject.SetActive(false);
-    //      button1.gameObject.SetActive(true);
-    //      button2.gameObject.SetActive(true);
-    //      button3.gameObject.SetActive(true);
-    //  }
-    //  
-    //  //  Yes/No with Image: A string, a Sprite, a Yes event, a No event (No Cancel Button);
-    //  public void Choice (string question, Sprite iconImage, UnityAction yesEvent, UnityAction noEvent) {
-    //      modalPanelObject.SetActive (true);
-    //      
-    //      button1.onClick.RemoveAllListeners();
-    //      button1.onClick.AddListener (yesEvent);
-    //      button1.onClick.AddListener (ClosePanel);
-    //      
-    //      button2.onClick.RemoveAllListeners();
-    //      button2.onClick.AddListener (noEvent);
-    //      button2.onClick.AddListener (ClosePanel);
-    //      
-    //      this.question.text = question;
-    //      this.iconImage.sprite = iconImage;
-    //      
-    //      this.iconImage.gameObject.SetActive(true);
-    //      button1.gameObject.SetActive(true);
-    //      button2.gameObject.SetActive(true);
-    //      button3.gameObject.SetActive(false);
-    //  }
-    //  
-    //  //  Yes/No/Cancel with Image: A string, a Sprite, a Yes event, a No event and Cancel event;
-    //  public void Choice (string question, Sprite iconImage, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) {
-    //      modalPanelObject.SetActive (true);
-    //      
-    //      button1.onClick.RemoveAllListeners();
-    //      button1.onClick.AddListener (yesEvent);
-    //      button1.onClick.AddListener (ClosePanel);
-    //      
-    //      button2.onClick.RemoveAllListeners();
-    //      button2.onClick.AddListener (noEvent);
-    //      button2.onClick.AddListener (ClosePanel);
-    //      
-    //      button3.onClick.RemoveAllListeners();
-    //      button3.onClick.AddListener (cancelEvent);
-    //      button3.onClick.AddListener (ClosePanel);
-    //      
-    //      this.question.text = question;
-    //      this.iconImage.sprite = iconImage;
-    //      
-    //      this.iconImage.gameObject.SetActive(true);
-    //      button1.gameObject.SetActive(true);
-    //      button2.gameObject.SetActive(true);
-    //      button3.gameObject.SetActive(true);
-    //  }
-
     void ClosePanel() {
         modalPanelObject.SetActive(false);
     }

+ 0 - 1
Assets/Scripts/NewQuestion.cs

@@ -3,7 +3,6 @@ using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class NewQuestion : QuestionCard {
-
     // Start is called before the first frame update
     void Start()
     {

+ 7 - 0
Assets/Scripts/QuestionCard.cs

@@ -101,6 +101,13 @@ public class QuestionCard : MonoBehaviour
         this.GetComponent<Image>().color = safeColor;
     }
 
+    public bool IsQuestionSafe() {
+        if (this.GetComponent<Image>().color == safeColor) {
+            return true;
+        }
+        return false;
+    }
+
     public void SetQuestionText(string text) {
         this.questionText.text = text;
     }

+ 49 - 7
Assets/Scripts/ScrollViewScript.cs

@@ -10,6 +10,8 @@ public class ScrollViewScript : MonoBehaviour, IDropHandler {
     private bool placeQuestion;
     NewQuestion nq;
     private int newQuestionSiblingIndex;
+    ModalPanel modalPanel;
+    private bool answeredCorrectly;
 
     // Start is called before the first frame update
     void Start() {
@@ -28,6 +30,17 @@ public class ScrollViewScript : MonoBehaviour, IDropHandler {
         questionCard.transform.SetParent(contentPanel);
     }
 
+    public int GetUnlockedQuestionCount() {
+        int unlockedQuestionCount = 0;
+        for (int i = 0; i < contentPanel.childCount; i++) {
+            QuestionCard qc = contentPanel.GetChild(i).GetComponent<QuestionCard>();
+            if (!qc.IsQuestionSafe()) {
+                unlockedQuestionCount++;
+            }
+        }
+        return unlockedQuestionCount;
+    }
+
     public void OnDrop(PointerEventData eventData) {
         Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
         if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
@@ -37,15 +50,41 @@ public class ScrollViewScript : MonoBehaviour, IDropHandler {
         nq = d.GetComponent<NewQuestion>();
         newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex();
 
-        ModalPanel modalPanel = ModalPanel.Instance();
-        ModalPanelDetails modalDetails = new ModalPanelDetails { question = "Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?", iconImage = null };
-        modalDetails.button1Details = new EventButtonDetails { buttonTitle = "Ja", action = YesFunction };
-        modalDetails.button2Details = new EventButtonDetails { buttonTitle = "Nej", action = NoFunction };
-        modalPanel.NewChoice(modalDetails);
+        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(); }); // generera ny fråga
+                dialog.SetOnDecline("Nej", () => { dialog.Hide(); }); // 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");
+                dialog.SetOnAccept("Ok", () => dialog.Hide());
+                dialog.SetOnDecline("", () => dialog.Hide());
+            }
+            dialog.Show();
+
+        });
+        dialog.SetOnDecline("Nej", () => dialog.Hide());
+        dialog.Show();
+
+
+        //modalPanel = ModalPanel.Instance();
+        //ModalPanelDetails modalDetails = new ModalPanelDetails { question = "Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?", iconImage = null };
+        //modalDetails.button1Details = new EventButtonDetails { buttonTitle = "Ja", action = YesFunction };
+        //modalDetails.button2Details = new EventButtonDetails { buttonTitle = "Nej", action = NoFunction };
+        //modalPanel.NewChoice(modalDetails);
     }
 
     // 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.
     void YesFunction() {
+        
         int correctAnswer = Int32.Parse(nq.answerString);
         Transform questionBefore = null;
         Transform questionAfter = null;
@@ -65,7 +104,8 @@ public class ScrollViewScript : MonoBehaviour, IDropHandler {
         if (questionAfter != null) {
             answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().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)
+        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>();
@@ -79,8 +119,10 @@ public class ScrollViewScript : MonoBehaviour, IDropHandler {
 
             questionCard.transform.SetParent(contentPanel);
             questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
-        } else { // Visa modal för fel svar och meddela att man svarat fel och förlorat x olåsta frågor.
 
+            answeredCorrectly = true;
+        } else {
+            answeredCorrectly = false;
         }
     }
 

File diff ditekan karena terlalu besar
+ 1016 - 22
Assets/narKampen.unity


Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini