Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- using UnityEngine.UI;
- public class QuestionData : MonoBehaviour
- {
- public static QuestionData instance { get; private set; }
- private string[] questionFiles = { "rank_0.json", "rank_1.json", "rank_2.json" }; // All question JSON files
- private string currentQuestionsFile = "current.json"; // Current empty JSON file
- private string questionJsonFilePath; // Path of question files
- private string emptyJsonFilePath; // Path of empty file
- private bool answerCheck; // check current question's answer
- private int randomizedIndex;
- public ListQuestions listQuestions;
- [SerializeField] Text questionDisplayText = default;
- [SerializeField] Button yesButton = default;
- [SerializeField] Button noButton = default;
- [SerializeField] private int totalQuestionsLeft;
- private void Awake()
- {
- if(instance == null)
- {
- instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- private void Start()
- {
- }
- public void LoadQuestion(int playerRank)
- {
- // Load Questions according on player rank
- questionJsonFilePath = Path.Combine(Application.streamingAssetsPath, questionFiles[playerRank]);
- emptyJsonFilePath = Path.Combine(Application.streamingAssetsPath, currentQuestionsFile);
- ValidateJson(questionJsonFilePath, emptyJsonFilePath);
- }
- private void ValidateJson(string fullOfQuestionJson, string emptyJson)
- {
- if (File.Exists(fullOfQuestionJson))
- {
- string dataAsJson = File.ReadAllText(fullOfQuestionJson);
- listQuestions = JsonUtility.FromJson<ListQuestions>(dataAsJson);
- if (File.Exists(emptyJson))
- {
- string toJson = JsonUtility.ToJson(listQuestions);
- File.WriteAllText(emptyJson, toJson);
- }
- totalQuestionsLeft = listQuestions.questions.Count;
- DispalyQuestion();
- }
- }
- // Display questions randomly
- private void DispalyQuestion()
- {
- if (totalQuestionsLeft > 0)
- {
- int questionIndex = Random.Range(0, listQuestions.questions.Count);
- randomizedIndex = questionIndex;
- questionDisplayText.text = listQuestions.questions[questionIndex].question;
- }
- else if (totalQuestionsLeft <= 0)
- {
- GamePanel.instance.SuccessPanel();
- Debug.Log("Level Has Finished");
- Player.instance.playerData.rankNumber += 1;
- Player.instance.SavePlayerData();
- }
- }
- // Check True Answers
- public void YesAnswer(bool isTrue)
- {
- if (totalQuestionsLeft > 0)
- {
- if (listQuestions.questions[randomizedIndex].isTrue == isTrue)
- {
- print("Yes answer is correct");
- UpdateQuestion(randomizedIndex);
- }
- else if (listQuestions.questions[randomizedIndex].isTrue != isTrue)
- {
- print("Yes answer is incorrect");
- GamePanel.instance.FailPanel();
- }
- }
- }
- // Check False Answers
- public void NoAnswer(bool isFalse)
- {
- if (totalQuestionsLeft > 0)
- {
- if (listQuestions.questions[randomizedIndex].isTrue == isFalse)
- {
- print("No answer is correct");
- UpdateQuestion(randomizedIndex);
- }
- else if (listQuestions.questions[randomizedIndex].isTrue != isFalse)
- {
- print("No answer is incorrect");
- GamePanel.instance.FailPanel();
- }
- }
- }
- private void UpdateQuestion(int removalElement)
- {
- questionDisplayText.text = listQuestions.questions[removalElement].question;
- listQuestions.questions.RemoveAt(removalElement);
- totalQuestionsLeft = listQuestions.questions.Count;
- string newJson = JsonUtility.ToJson(listQuestions);
- File.WriteAllText(emptyJsonFilePath, newJson);
- DispalyQuestion();
- GamePanel.instance.Rotate();
- }
- }
- [System.Serializable]
- public class Questions
- {
- public string question;
- public bool isTrue;
- }
- [System.Serializable]
- public class ListQuestions
- {
- public List<Questions> questions;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement