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

252 lines
6.2 KiB
C#

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 const string DUMP = @"{
""questions"": [
{
""question"": ""2 + 2 ="",
""choices"": [
""3"",
""4"",
""5"",
""6""
],
""answer"": ""4""
},
{
""question"": ""How many sides does an octagon have?"",
""choices"": [
""45"",
""8"",
""4"",
""5""
],
""answer"": ""8""
},
{
""question"": ""How many stripes are on the US Flag?"",
""choices"": [
""0"",
""12"",
""13"",
""14""
],
""answer"": ""13""
},
{
""question"": ""What city do The Beatles come from?"",
""choices"": [
""London"",
""Manchester"",
""Liverpool"",
""Bristol""
],
""answer"": ""Liverpool""
},
{
""question"": ""Who is the Roman God of love?"",
""choices"": [
""Athena"",
""Zeus"",
""Cupid"",
""Hermes""
],
""answer"": ""Cupid""
},
{
""question"": ""What is the largest lake in Scotland?"",
""choices"": [
""Michigan"",
""Loch Lomond"",
""Lake Superior"",
""Loot Lake""
],
""answer"": ""Loch Lomond""
},
{
""question"": ""What is the capital of Peru?"",
""choices"": [
""Lima"",
""Argentina"",
""Mozambique"",
""Mozart""
],
""answer"": ""Lima""
},
{
""question"": ""How many Queens have ruled France?"",
""choices"": [
""53 1/2"",
""Unknown"",
""None"",
""1""
],
""answer"": ""None""
},
{
""question"": ""What colour is a banana?"",
""choices"": [
""Yellow"",
""Chimera"",
""Green"",
""Purple""
],
""answer"":""Yellow""
},
{
""question"": ""How many oceans are there?"",
""choices"": [
""None"",
""Three"",
""Disputed"",
""Twenty-two""
],
""answer"": ""Disputed""
},
{
""question"": ""What colour is grass?"",
""choices"": [
""Banana"",
""Purple"",
""Green"",
""Sky Blue""
],
""answer"": ""Green""
},
{
""question"": ""What number is after 8?"",
""choices"": [
""69"",
""9"",
""0b0001101"",
""7""
],
""answer"": ""9""
},
{
""question"": ""Main Ingredient of Bread?"",
""choices"": [
""Flour / Wheat"",
""Uranium"",
""Cyanide"",
""Ricin""
],
""answer"": ""Flour / Wheat""
},
{
""question"": ""How many pyramids are there in Egypt?"",
""choices"": [
""None they are all a lie"",
""138"",
""420"",
""Over 9000!!!!""
],
""answer"": ""138""
},
{
""question"": ""How many eyes do a Hornet have?"",
""choices"": [
""5"",
""No Eyes"",
""21"",
""8""
],
""answer"": ""5""
},
{
""question"": ""What kind of drink is soda?"",
""choices"": [
""Undrinkable"",
""Lave"",
""Soft Drink"",
""Food""
],
""answer"": ""Soft Drink""
}
]
}
";
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()
{
Trivia = JsonUtility.FromJson<TriviaSet>(DUMP);
}
protected override void OnWindowInstantiation()
{
/* Destroys the Window Close button to stop accidental clicking */
Destroy(Components.CloseButtonRectTransform.gameObject);
/* 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() { }
}
}