Files
Inter-Face-Off/Assets/Scripts/Windows/TriviaWindow.cs
2026-01-20 12:39:22 +00:00

84 lines
28 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using UnityEngine.UI;
namespace InterfaceOff
{
[System.Serializable] public struct TriviaQuestion
{
/* Disables name warnings because of JSON serialization */
// ReSharper disable once InconsistentNaming
public string question;
// ReSharper disable once InconsistentNaming
public string[] choices;
// ReSharper disable once InconsistentNaming
public string answer;
}
[System.Serializable] public struct TriviaSet
{
/* Disables name warnings because of JSON serialization */
// ReSharper disable once InconsistentNaming
public TriviaQuestion[] questions;
}
public class TriviaWindow : WindowBase
{
private static TriviaSet Trivia;
private static Vector2[] Positions { get; } =
{
new(-110, 30f),
new(110f, 30f),
new(-110, -60),
new(110f, -60)
};
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void LoadTriviaSet()
{
const string json = "{\n \"questions\" : [\n {\n \"question\" : \"2 + 2?\",\n \"choices\" : [\n \"3\",\n \"4\",\n \"5\",\n \"6\"\n ],\n \"answer\" : \"4\"\n },\n {\n \"question\" : \"What is the capital of France?\",\n \"choices\" : [\n \"Paris\",\n \"Rome\",\n \"Berlin\",\n \"Madrid\"\n ],\n \"answer\" : \"Paris\"\n },\n {\n \"question\" : \"What color is the sky?\",\n \"choices\" : [\n \"Blue\",\n \"Green\",\n \"Red\",\n \"Yellow\"\n ],\n \"answer\" : \"Blue\"\n },\n {\n \"question\" : \"What color is grass?\",\n \"choices\" : [\n \"Green\",\n \"Blue\",\n \"Red\",\n \"White\"\n ],\n \"answer\" : \"Green\"\n },\n {\n \"question\" : \"What is the opposite of hot?\",\n \"choices\" : [\n \"Cold\",\n \"Warm\",\n \"Dry\",\n \"Wet\"\n ],\n \"answer\" : \"Cold\"\n },\n {\n \"question\" : \"Which direction does the sun rise?\",\n \"choices\" : [\n \"East\",\n \"West\",\n \"North\",\n \"South\"\n ],\n \"answer\" : \"East\"\n },\n {\n \"question\" : \"How many days are there in a week?\",\n \"choices\" : [\n \"5\",\n \"6\",\n \"7\",\n \"8\"\n ],\n \"answer\" : \"7\"\n },\n {\n \"question\" : \"How many hours are there in a day?\",\n \"choices\" : [\n \"12\",\n \"18\",\n \"24\",\n \"36\"\n ],\n \"answer\" : \"24\"\n },\n {\n \"question\" : \"How many letters are in the alphabet?\",\n \"choices\" : [\n \"24\",\n \"25\",\n \"26\",\n \"27\"\n ],\n \"answer\" : \"26\"\n },\n {\n \"question\" : \"Earth is a?\",\n \"choices\" : [\n \"Star\",\n \"Planet\",\n \"Moon\",\n \"Comet\"\n ],\n \"answer\" : \"Planet\"\n },\n {\n \"question\" : \"What is the largest planet in the solar system?\",\n \"choices\" : [\n \"Earth\",\n \"Mars\",\n \"Jupiter\",\n \"Venus\"\n ],\n \"answer\" : \"Jupiter\"\n },\n {\n \"question\" : \"Which number is the smallest?\",\n \"choices\" : [\n \"1\",\n \"-5\",\n \"10\",\n \"-100\"\n ],\n \"answer\" : \"-100\"\n },\n {\n \"question\" : \"What is the opposite of up?\",\n \"choices\" : [\n \"Down\",\n \"Left\",\n \"Right\",\n \"Around\"\n ],\n \"answer\" : \"Down\"\n },\n {\n \"question\" : \"What temperature does water freeze at?\",\n \"choices\" : [\n \"0°C\",\n \"50°C\",\n \"100°C\",\n \"10°C\"\n ],\n \"answer\" : \"0°C\"\n },\n {\n \"question\" : \"What is the fastest land animal?\",\n \"choices\" : [\n \"Dog\",\n \"Horse\",\n \"Cheetah\",\n \"Cat\"\n ],\n \"answer\" : \"Cheetah\"\n },\n {\n \"question\" : \"What do humans breathe?\",\n \"choices\" : [\n \"Oxygen\",\n \"Carbon\",\n \"Helium\",\n \"Smoke\"\n ],\n \"answer\" : \"Oxygen\"\n },\n {\n \"question\" : \"What shape has 3 sides?\",\n \"choices\" : [\n \"Square\",\n \"Triangle\",\n \"Circle\",\n \"Oval\"\n ],\n \"answer\" : \"Triangle\"\n },\n {\n \"question\" : \"What shape has 4 sides?\",\n \"choices\" : [\n \"Triangle\",\n \"Square\",\n \"Circle\",\n \"Star\"\n ],\n \"answer\" : \"Square\"\n },\n {\n \"question\" : \"Which of these is a Primary color?\",\n \"choices\" : [\n \"Blue\",\n \"Pink\",\n \"Brown\",\n \"Gray\"\n ],\n \"answer\" : \"Blue\"\n },\n {\n \"question\" : \"What is the opposite of big?\",\n \"choices\" : [\n \"Small\",\n \"Tall\",\n \"Wide\",\n \"Long\"\n ],\n \"answer\" : \"Small\"\n },\n {\n \"question\" : \"Fire is?\",\n \"choices\" : [\n \"Hot\",\n \"Cold\",\n \"Wet\",\n \"Soft\"\n ],\n \"answer\" : \"Hot\"\n },\n {\n \"question\" : \"Ice is?\",\n \"choices\" : [\n \"Cold\",\n \"Hot\",\n \"Dry\",\n \"Hard\"\n ],\n \"answer\" : \"Cold\"\n },\n {\n \"question\" : \"Which day is after Monday?\",\n \"choices\" : [\n \"Tuesday\",\n \"Sunday\",\n \"Friday\",\n \"Saturday\"\n ],\n \"answer\" : \"Tuesday\"\n },\n {\n \"question\" : \"Which of these days are on a weekend?\",\n \"choices\" : [\n \"Monday\",\n \"Tuesday\",\n \"Saturday\",\n \"Wednesday\"\n ],\n \"answer\" : \"Saturday\"\n },\n {\n \"question\" : \"What is the color of a banana?\",\n \"choices\" : [\n \"Yellow\",\n \"Blue\",\n \"Red\",\n \"Purple\"\n ],\n \"answer\" : \"Yellow\"\n },\n {\n \"question\" : \"What is the color of an apple?\",\n \"choices\" : [\n \"Red\",\n \"Blue\",\n \"Black\",\n \"Pink\"\n ],\n \"answer\" : \"Red\"\n },\n {\n \"question\" : \"Where do Fish live?\",\n \"choices\" : [\n \"Water\",\n \"Air\",\n \"Sand\",\n \"Fire\"\n ],\n \"answer\" : \"Water\"\n },\n {\n \"question\" : \"What is the opposite of fast?\",\n \"choices\" : [\n \"Slow\",\n \"Quick\",\n \"Sharp\",\n \"Bright\"\n ],\n \"answer\" : \"Slow\"\n },\n {\n \"question\" : \"What is the sun?\",\n \"choices\" : [\n \"Star\",\n \"Planet\",\n \"Moon\",\n \"Rock\"\n ],\n \"answer\" : \"Star\"\n },\n {\n \"question\" : \"What does the moon orbit?\",\n \"choices\" : [\n \"Earth\",\n \"Sun\",\n \"Mars\",\n \"Jupiter\"\n ],\n \"answer\" : \"Earth\"\n },\n {\n \"question\" : \"How many legs does a human have?\",\n \"choices\" : [\n \"2\",\n \"4\",\n \"6\",\n \"8\"\n ],\n \"answer\" : \"2\"\n },\n {\n \"question\" : \"How many eyes does a human have?\",\n \"choices\" : [\n \"1\",\n \"2\",\n \"3\",\n \"4\"\n ],\n \"answer\" : \"2\"\n },\n {\n \"question\" : \"Snow color is snow?\",\n \"choices\" : [\n \"White\",\n \"Black\",\n \"Red\",\n \"Green\"\n ],\n \"answer\" : \"White\"\n },\n {\n \"question\" : \"What is the opposite of open?\",\n \"choices\" : [\n \"Closed\",\n \"Wide\",\n \"Large\",\n \"Empty\"\n ],\n \"answer\" : \"Closed\"\n },\n {\n \"question\" : \"What color is milk?\",\n \"choices\" : [\n \"White\",\n \"Blue\",\n \"Brown\",\n \"Yellow\"\n ],\n \"answer\" : \"White\"\n },\n {\n \"question\" : \"What does fire need to stay alight?\",\n \"choices\" : [\n \"Oxygen\",\n \"Water\",\n \"Ice\",\n \"Sand\"\n ],\n \"answer\" : \"Oxygen\"\n },\n {\n \"question\" : \"What is the opposite of full?\",\n \"choices\" : [\n \"Empty\",\n \"Large\",\n \"Wide\",\n \"Heavy\"\n ],\n \"answer\" : \"Empty\"\n },\n {\n \"question\" : \"What color is the sky during the day?\",\n \"choices\" : [\n \"Blue\",\n \"Black\",\n \"Green\",\n \"Gray\"\n ],\n \"answer\" : \"Blue\"\n },\n {\n \"question\" : \"What color is the sky during the night?\",\n \"choices\" : [\n \"Black\",\n \"Blue\",\n \"Green\",\n \"Red\"\n ],\n \"answer\" : \"Black\"\n },\n {\n \"question\" : \"2 + 3 =\",\n \"choices\" : [\n \"4\",\n \"5\",\n \"6\",\n \"7\"\n ],\n \"answer\" : \"5\"\n },\n {\n \"question\" : \"5 - 2 =\",\n \"choices\" : [\n \"2\",\n \"3\",\n \"4\",\n \"5\"\n ],\n \"answer\" : \"3\"\n },\n {\n \"question\" : \"10 / 2 =\",\n \"choices\" : [\n \"2\",\n \"4\",\n \"5\",\n \"10\"\n ],\n \"answer\" : \"5\"\n },\n {\n \"question\" : \"3 × 3 =\",\n \"choices\" : [\n \"6\",\n \"8\",\n \"9\",\n \"12\"\n ],\n \"answer\" : \"9\"\n },\n {\n \"question\" : \"What is the opposite of left?\",\n \"choices\" : [\n \"Right\",\n \"Up\",\n \"Down\",\n \"Back\"\n ],\n \"answer\" : \"Right\"\n },\n {\n \"question\" : \"What is the color of the sun?\",\n \"choices\" : [\n \"Yellow\",\n \"Blue\",\n \"Green\",\n \"Purple\"\n ],\n \"answer\" : \"Yellow\"\n },\n {\n \"question\" : \"What do fish breathe through?\",\n \"choices\" : [\n \"Gills\",\n \"Lungs\",\n \"Skin\",\n \"Fire\"\n ],\n \"answer\" : \"Gills\"\n },\n {\n \"question\" : \"What is the opposite of loud?\",\n \"choices\" : [\n \"Quiet\",\n \"Fast\",\n \"Sharp\",\n \"Bright\"\n ],\n \"answer\" : \"Quiet\"\n },\n {\n \"question\" : \"What is the color of fire?\",\n \"choices\" : [\n \"Red\",\n \"Blue\",\n \"Green\",\n \"White\"\n ],\n \"answer\" : \"Red\"\n },\n {\n \"question\" : \"What is the capital of Italy?\",\n \"choices\" : [\n \"Rome\",\n \"Paris\",\n \"Berlin\",\n \"Madrid\"\n ],\n \"answer\" : \"Rome\"\n },\n {\n \"question\" : \"What is the largest ocean?\",\n \"choices\" : [\n \"Pacific\",\n \"Atlantic\",\n \"Indian\",\n \"Arctic\"\n ],\n \"answer\" : \"Pacific\"\n },\n {\n \"question\" : \"What is the color of a firetruck?\",\n \"choices\" : [\n \"Red\",\n \"Blue\",\n \"Green\",\n \"Yellow\"\n ],\n \"answer\" : \"Red\"\n },\n {\n \"question\" : \"What is the color of a banana?\",\n \"choices\" : [\n \"Yellow\",\n \"Red\",\n \"Blue\",\n \"Purple\"\n ],\n \"answer\" : \"Yellow\"\n },\n {\n \"question\" : \"What planet is known for its rings?\",\n \"choices\" : [\n \"Saturn\",\n \"Mars\",\n \"Earth\",\n \"Venus\"\n ],\n \"answer\" : \"Saturn\"\n },\n {\n \"question\" : \"What is the largest land animal?\",\n \"choices\" : [\n \"Elephant\",\n \"Lion\",\n \"Horse\",\n \"Bear\"\n ],\n \"answer\" : \"Elephant\"\n },\n {\n \"question\" : \"What is the tallest tree type?\",\n \"choices\" : [\n \"Redwood\",\n \"Pine\",\n \"Oak\",\n \"Birch\"\n ],\n \"answer\" : \"Redwood\"\n },\n {\n \"question\" : \"Which celestial body does the Earth rotate around?\",\n \"choices\" : [\n \"Sun\",\n \"Moon\",\n \"Mars\",\n \"Jupiter\"\n ],\n \"answer\" : \"Sun\"\n },\n {\n \"question\" : \"Which celestial body does the moon orbit?\",\n \"choices\" : [\n \"Earth\",\n \"Sun\",\n \"Mars\",\n \"Venus\"\n ],\n \"answer\" : \"Earth\"\n },\n {\n \"question\" : \"What is the largest desert?\",\n \"choices\" : [\n \"Sahara\",\n \"Gobi\",\n \"Kalahari\",\n \"Arctic\"\n ],\n \"answer\" : \"Sahara\"\n },\n {\n \"question\" : \"What is the smallest continent?\",\n \"choices\" : [\n \"Australia\",\n \"Europe\",\n \"Asia\",\n \"Africa\"\n ],\n \"answer\" : \"Australia\"\n },\n {\n \"question\" : \"What is the largest continent?\",\n \"choices\" : [\n \"Asia\",\n \"Africa\",\n \"Europe\",\n \"Antarctica\"\n ],\n \"answer\" : \"Asia\"\n },\n {\n \"question\" : \"What planet is closest to the Sun?\",\n \"choices\" : [\n \"Mercury\",\n \"Venus\",\n \"Earth\",\n \"Mars\"\n ],\n \"answer\" : \"Mercury\"\n },\n {\n \"question\" : \"What planet is farthest from the Sun?\",\n \"choices\" : [\n \"Neptune\",\n \"Mars\",\n \"Earth\",\n \"Venus\"\n ],\n \"answer\" : \"Neptune\"\n },\n {\n \"question\" : \"What bird cannot fly?\",\n \"choices\" : [\n \"Ostrich\",\n \"Eagle\",\n \"Sparrow\",\n \"Parrot\"\n ],\n \"answer\" : \"Ostrich\"\n },\n {\n \"question\" : \"Which mammal can fly?\",\n \"choices\" : [\n \"Bat\",\n \"Cat\",\n \"Dog\",\n \"Horse\"\n ],\n \"answer\" : \"Bat\"\n },\n {\n \"question\" : \"What animal has a trunk?\",\n \"choices\" : [\n \"Elephant\",\n \"Rhino\",\n \"Horse\",\n \"Lion\"\n ],\n \"answer\" : \"Elephant\"\n },\n {\n \"question\" : \"What animal has stripes?\",\n \"choices\" : [\n \"Tiger\",\n \"Lion\",\n \"Bear\",\n \"Elephant\"\n ],\n \"answer\" : \"Tiger\"\n },\n {\n \"question\" : \"What animal lays eggs?\",\n \"choices\" : [\n \"Chicken\",\n \"Dog\",\n \"Cat\",\n \"Cow\"\n ],\n \"answer\" : \"Chicken\"\n },\n {\n \"question\" : \"Largest planet in solar system?\",\n \"choices\" : [\n \"Jupiter\",\n \"Mars\",\n \"Earth\",\n \"Venus\"\n ],\n \"answer\" : \"Jupiter\"\n },\n {\n \"question\" : \"Smallest planet in the solar system?\",\n \"choices\" : [\n \"Mercury\",\n \"Mars\",\n \"Venus\",\n \"Earth\"\n ],\n \"answer\" : \"Mercury\"\n },\n {\n \"question\" : \"What direction is the opposite of up?\",\n \"choices\" : [\n \"Down\",\n \"Left\",\n \"Right\",\n \"Back\"\n ],\n \"answer\" : \"Down\"\n },\n {\n \"question\" : \"Hot drink from beans?\",\n \"choices\" : [\n \"Coffee\",\n \"Tea\",\n \"Milk\",\n \"Water\"\n ],\n \"answer\" : \"Coffee\"\n },\n {\n \"question\" : \"What is the day after Sunday?\",\n \"choices\" : [\n \"Monday\",\n \"Tuesday\",\n \"Saturday\",\n \"Friday\"\n ],\n \"answer\" : \"Monday\"\n },\n {\n \"question\" : \"What is the longest river?\",\n \"choices\" : [\n \"Nile\",\n \"Amazon\",\n \"Yangtze\",\n \"Mississippi\"\n ],\n \"answer\" : \"Nile\"\n },\n {\n \"question\" : \"What is the highest mountain?\",\n \"choices\" : [\n \"Everest\",\n \"K2\",\n \"Kangchenjunga\",\n \"Denali\"\n ],\n \"answer\" : \"Everest\"\n },\n {\n \"question\" : \"What is the largest country?\",\n \"choices\" : [\n \"Russia\",\n \"Canada\",\n \"China\",\n \"USA\"\n ],\n \"answer\" : \"Russia\"\n },\n {\n \"question\" : \"Language spoken in Spain?\",\n \"choices\" : [\n \"Spanish\",\n \"French\",\n \"German\",\n \"Italian\"\n ],\n \"answer\" : \"Spanish\"\n },\n {\n \"question\" : \"Language spoken in France?\",\n \"choices\" : [\n \"French\",\n \"Spanish\",\n \"English\",\n \"German\"\n ],\n \"answer\" : \"French\"\n },\n {\n \"question\" : \"Language spoken in Germany?\",\n \"choices\" : [\n \"German\",\n \"English\",\n \"Spanish\",\n \"Italian\"\n ],\n \"answer\" : \"German\"\n },\n {\n \"question\" : \"Language spoken in Italy?\",\n \"choices\" : [\n \"Italian\",\n \"French\",\n \"Spanish\",\n \"German\"\n ],\n \"answer\" : \"Italian\"\n },\n {\n \"question\" : \"Hot beverage from leaves?\",\n \"choices\" : [\n \"Tea\",\n \"Coffee\",\n \"Milk\",\n \"Water\"\n ],\n \"answer\" : \"Tea\"\n },\n {\n \"question\" : \"Round fruit, red or green?\",\n \"choices\" : [\n \"Apple\",\n \"Banana\",\n \"Orange\",\n \"Grape\"\n ],\n \"answer\" : \"Apple\"\n },\n {\n \"question\" : \"Long yellow fruit?\",\n \"choices\" : [\n \"Banana\",\n \"Apple\",\n \"Orange\",\n \"Mango\"\n ],\n \"answer\" : \"Banana\"\n },\n {\n \"question\" : \"Orange citrus fruit?\",\n \"choices\" : [\n \"Orange\",\n \"Apple\",\n \"Banana\",\n \"Pear\"\n ],\n \"answer\" : \"Orange\"\n },\n {\n \"question\" : \"Green citrus fruit?\",\n \"choices\" : [\n \"Lime\",\n \"Lemon\",\n \"Orange\",\n \"Grapefruit\"\n ],\n \"answer\" : \"Lime\"\n },\n {\n \"question\" : \"Animal that quacks?\",\n \"choices\" : [\n \"Duck\",\n \"Goose\",\n \"Chicken\",\n \"Turkey\"\n ],\n \"answer\" : \"Duck\"\n },\n {\n \"question\" : \"Animal that oinks?\",\n \"choices\" : [\n \"Pig\",\n \"Cow\",\n \"Dog\",\n \"Sheep\"\n ],\n \"answer\" : \"Pig\"\n },\n {\n \"question\" : \"Animal that moos?\",\n \"choices\" : [\n \"Cow\",\n \"Pig\",\n \"Dog\",\n \"Horse\"\n ],\n \"answer\" : \"Cow\"\n },\n {\n \"question\" : \"What animal has a mane?\",\n \"choices\" : [\n \"Lion\",\n \"Tiger\",\n \"Elephant\",\n \"Horse\"\n ],\n \"answer\" : \"Lion\"\n },\n {\n \"question\" : \"What animal has a hump?\",\n \"choices\" : [\n \"Camel\",\n \"Horse\",\n \"Elephant\",\n \"Donkey\"\n ],\n \"answer\" : \"Camel\"\n },\n {\n \"question\" : \"What animal can hop?\",\n \"choices\" : [\n \"Kangaroo\",\n \"Lion\",\n \"Tiger\",\n \"Elephant\"\n ],\n \"answer\" : \"Kangaroo\"\n },\n {\n \"question\" : \"What animal can swim?\",\n \"choices\" : [\n \"Fish\",\n \"Dog\",\n \"Cat\",\n \"Horse\"\n ],\n \"answer\" : \"Fish\"\n },\n {\n \"question\" : \"Which animal can fly?\",\n \"choices\" : [\n \"Bird\",\n \"Cat\",\n \"Dog\",\n \"Elephant\"\n ],\n \"answer\" : \"Bird\"\n },\n {\n \"question\" : \"What animal has a shell?\",\n \"choices\" : [\n \"Turtle\",\n \"Dog\",\n \"Cat\",\n \"Horse\"\n ],\n \"answer\" : \"Turtle\"\n },\n {\n \"question\" : \"What animal has tusks?\",\n \"choices\" : [\n \"Elephant\",\n \"Lion\",\n \"Horse\",\n \"Tiger\"\n ],\n \"answer\" : \"Elephant\"\n },\n {\n \"question\" : \"Color of coal?\",\n \"choices\" : [\n \"Black\",\n \"White\",\n \"Red\",\n \"Green\"\n ],\n \"answer\" : \"Black\"\n }\n ]\n}\n";
Trivia = JsonUtility.FromJson<TriviaSet>(json);
}
protected override void OnWindowInstantiation()
{
/* Fetches a random question and sets it as the title */
TriviaQuestion question = Trivia.questions[Random.Range(0, Trivia.questions.Length)];
Components.InfoText.text = question.question;
Components.SetWidth(500); // Larger width is needed for this component that others
/* Adds the answer boxes */
for (int i = 0; i < 4; i++)
{
/* Creates the gameobject and fetches the needed components */
GameObject go = Instantiate(PrefabRegistry.Instance.TextButtonPrefab, transform);
Text text = go.GetComponentInChildren<Text>();
Button button = go.GetComponent<Button>();
/* Assigns needed info to the components */
text.text = question.choices[i];
RectTransform buttonTransform = button.GetComponent<RectTransform>();
buttonTransform.anchoredPosition = Positions[i];
buttonTransform.sizeDelta = new Vector2(200, 60);
/* Adds a lambda function to the button press to detect answers */
bool isCorrectButton = string.Equals(question.choices[i], question.answer);
button.onClick.AddListener(() =>
{
if (isCorrectButton)
{
DestroyWindow();
}
});
}
}
public override void OnWindowCloseButtonClicked() { }
}
}