Resolve WES-76 "87 themeselection"

This commit is contained in:
Lukas Van Rossem
2023-03-13 21:49:40 +00:00
parent 94ec7e0359
commit a267301ab6
23 changed files with 141 additions and 893 deletions

View File

@@ -91,8 +91,12 @@ public class InfoMinigame : MonoBehaviour
gameImage.sprite = minigame.thumbnail;
controls.text = minigame.controls;
// Add click functionality
button.onClick.AddListener(() => SceneManager.LoadScene(minigame.minigameEntryPoint));
// Add click
if (minigame.needsTheme) {
button.onClick.AddListener(() => SceneManager.LoadScene("Common/Scenes/ThemeSelection"));
} else {
button.onClick.AddListener(() => SceneManager.LoadScene(minigame.minigameEntryPoint));
}
}
/// <summary>

View File

@@ -21,6 +21,11 @@ public class Minigame : ScriptableObject
/// </summary>
public string description;
/// <summary>
/// Whether or not the game needs a theme to be selected
/// </summary>
public bool needsTheme;
/// <summary>
/// Reference to the minigame thumbnail
/// </summary>

View File

@@ -2,12 +2,13 @@ using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections.Generic;
/// <summary>
/// Handles the display of minigames in the ListMinigameScreen scene
/// </summary>
public class MinigameItem : MonoBehaviour
{
{
/// <summary>
/// Reference to the minigame object
/// </summary>
@@ -45,8 +46,11 @@ public class MinigameItem : MonoBehaviour
// Set appearance
thumbnail.sprite = minigame.thumbnail;
title.text = minigame.title;
// Add click functionality
button.onClick.AddListener(() => SceneManager.LoadScene("Common/Scenes/InfoMinigame"));
button.onClick.AddListener(() => {
PlayerPrefs.SetString("gamePath", minigame.minigameEntryPoint);
SceneManager.LoadScene("Common/Scenes/InfoMinigame");
});
}
}

View File

@@ -0,0 +1,65 @@
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// Handles the display of themes in the ThemeSelectionController scene
/// </summary>
public class ThemeItem : MonoBehaviour
{
/// <summary>
/// The theme title
/// </summary>
public string themeTitle;
/// <summary>
/// A short description of the theme
/// </summary>
public string themeDescription;
/// <summary>
/// The callback function to start the game with the correct theme loaded
/// </summary>
public UnityAction startGameCallback;
/// <summary>
/// UI reference to the gameobject for displaying the theme title
/// </summary>
public TMP_Text title;
/// <summary>
/// UI reference to the gameobject for displaying the description
/// </summary>
public TMP_Text description;
/// <summary>
/// UI reference to the button so the correct callback can be trigger on click
/// </summary>
public Button button;
/// <summary>
/// Start is called before the first frame update
/// </summary>
void Start()
{
// Use public function so that this component can get Instantiated
GenerateContent();
}
/// <summary>
/// (Re)generate the ThemeItem object and update its appearance
/// </summary>
public void GenerateContent()
{
// Set appearance
title.text = themeTitle;
// TODO: make description only visible when hovering
description.text = themeDescription;
// Add click functionality
button.onClick.AddListener(startGameCallback);
}
}

View File

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

View File

@@ -0,0 +1,36 @@
using UnityEngine;
/// <summary>
/// JSON structure containing all themes/words
/// </summary>
[System.Serializable]
public class ThemeList
{
public Theme[] themes;
}
/// <summary>
/// Object representing part of the JSON containing word data
/// </summary>
[System.Serializable]
public class Theme
{
public string name;
public string description;
public string[] words;
}
/// <summary>
/// Loader of the themes-JSON
/// </summary>
public class ThemeLoader : MonoBehaviour
{
/// <summary>
/// Loads the JSON file containing all of the themes
/// </summary>
public static ThemeList LoadJson()
{
TextAsset themeJson = Resources.Load<TextAsset>("Common/words");
return JsonUtility.FromJson<ThemeList>(themeJson.text);
}
}

View File

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

View File

@@ -0,0 +1,51 @@
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// Controller for the theme selection screen
/// </summary>
public class ThemeSelectionController : MonoBehaviour
{
[Header("Theme Selection")]
/// <summary>
/// Theme prefab
/// </summary>
public GameObject themePrefab;
/// <summary>
/// Reference to container holding all theme-buttons
/// </summary>
public Transform themesContainer;
/// <summary>
/// Function that is called upon loading the scene
/// </summary>
public void Start()
{
// TODO: change to ScriptableObject
ThemeList themeList = ThemeLoader.LoadJson();
for (int i = 0; i < themeList.themes.Length; i++)
{
Theme theme = themeList.themes[i];
// First, you need to create a new button game object
GameObject instance = GameObject.Instantiate(themePrefab, themesContainer);
// Dynamically load appearance
ThemeItem item = instance.GetComponent<ThemeItem>();
item.themeTitle = theme.name;
item.themeDescription = theme.description;
item.startGameCallback = () => OnButtonClick(theme.name);
}
}
/// <summary>
/// Function that is called upon a button click
/// </summary>
public void OnButtonClick(string clickedTheme)
{
PlayerPrefs.SetString("themeName", clickedTheme);
SceneManager.LoadScene(PlayerPrefs.GetString("gamePath"));
}
}

View File

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