Merge branch 'Pasha'
This commit is contained in:
@@ -7,9 +7,6 @@ namespace InterfaceOff
|
||||
[field: SerializeField] public RectTransform CanvasRect { get; private set; }
|
||||
[field: SerializeField] public Canvas GameCanvas { get; private set; }
|
||||
|
||||
[field: SerializeField] public GameObject ImagePrefab { get; private set; }
|
||||
[field: SerializeField] public ImageRegistry Images { get; set; }
|
||||
|
||||
public static CanvasManager Instance { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
|
||||
3
Assets/Scripts/Ext.meta
Normal file
3
Assets/Scripts/Ext.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c4ac8eadd314129ac24f2035e462dc0
|
||||
timeCreated: 1768482210
|
||||
45
Assets/Scripts/Ext/MonoScriptAttribute.cs
Normal file
45
Assets/Scripts/Ext/MonoScriptAttribute.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
/*****
|
||||
* This is part of a simple PropertyDrawer for string variables to allow drag
|
||||
* and drop of MonoScripts in the inspector of the Unity3d editor.
|
||||
*
|
||||
* NOTE: This is a runtime script and MUST NOT be placed in a folder named "editor".
|
||||
* It also requires another editor file named "MonoScriptPropertyDrawer.cs"
|
||||
*
|
||||
* Copyright (c) 2016 Bunny83
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
*****/
|
||||
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace Ext.B83.Unity.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class MonoScriptAttribute : PropertyAttribute
|
||||
{
|
||||
public Type type;
|
||||
public MonoScriptAttribute() { }
|
||||
public MonoScriptAttribute(System.Type aType)
|
||||
{
|
||||
type = aType;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Ext/MonoScriptAttribute.cs.meta
Normal file
11
Assets/Scripts/Ext/MonoScriptAttribute.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 433a7d4d13adbbc43bd8ef66708150c1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
103
Assets/Scripts/Ext/MonoScriptPropertyDrawer.cs
Normal file
103
Assets/Scripts/Ext/MonoScriptPropertyDrawer.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
/*****
|
||||
* This is a simple PropertyDrawer for string variables to allow drag and drop
|
||||
* of MonoScripts in the inspector of the Unity3d editor.
|
||||
*
|
||||
* NOTE: This is an editor script and need to be placed in a folder named "editor".
|
||||
* It also requires another runtime file named "MonoScriptAttribute.cs"
|
||||
*
|
||||
* Copyright (c) 2016 Bunny83
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
*****/
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections.Generic;
|
||||
using Ext.B83.Unity.Attributes;
|
||||
|
||||
namespace Ext.B83.Unity.Editor.PropertyDrawers
|
||||
{
|
||||
|
||||
[CustomPropertyDrawer(typeof(MonoScriptAttribute), false)]
|
||||
public class MonoScriptPropertyDrawer : PropertyDrawer
|
||||
{
|
||||
static Dictionary<string, MonoScript> m_ScriptCache;
|
||||
static MonoScriptPropertyDrawer()
|
||||
{
|
||||
m_ScriptCache = new Dictionary<string, MonoScript>();
|
||||
var scripts = Resources.FindObjectsOfTypeAll<MonoScript>();
|
||||
for (int i = 0; i < scripts.Length; i++)
|
||||
{
|
||||
var type = scripts[i].GetClass();
|
||||
if (type != null && !m_ScriptCache.ContainsKey(type.FullName))
|
||||
{
|
||||
m_ScriptCache.Add(type.FullName, scripts[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool m_ViewString = false;
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (property.propertyType == SerializedPropertyType.String)
|
||||
{
|
||||
Rect r = EditorGUI.PrefixLabel(position, label);
|
||||
Rect labelRect = position;
|
||||
labelRect.xMax = r.xMin;
|
||||
position = r;
|
||||
m_ViewString = GUI.Toggle(labelRect, m_ViewString, "", "label");
|
||||
if (m_ViewString)
|
||||
{
|
||||
property.stringValue = EditorGUI.TextField(position, property.stringValue);
|
||||
return;
|
||||
}
|
||||
MonoScript script = null;
|
||||
string typeName = property.stringValue;
|
||||
if (!string.IsNullOrEmpty(typeName))
|
||||
{
|
||||
m_ScriptCache.TryGetValue(typeName, out script);
|
||||
if (script == null)
|
||||
GUI.color = Color.red;
|
||||
}
|
||||
|
||||
script = (MonoScript)EditorGUI.ObjectField(position, script, typeof(MonoScript), false);
|
||||
if (GUI.changed)
|
||||
{
|
||||
if (script != null)
|
||||
{
|
||||
var type = script.GetClass();
|
||||
MonoScriptAttribute attr = (MonoScriptAttribute)attribute;
|
||||
if (attr.type != null && !attr.type.IsAssignableFrom(type))
|
||||
type = null;
|
||||
if (type != null)
|
||||
property.stringValue = type.FullName;
|
||||
else
|
||||
Debug.LogWarning("The script file " + script.name + " doesn't contain an assignable class");
|
||||
}
|
||||
else
|
||||
property.stringValue = "";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GUI.Label(position, "The MonoScript attribute can only be used on string variables");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Ext/MonoScriptPropertyDrawer.cs.meta
Normal file
11
Assets/Scripts/Ext/MonoScriptPropertyDrawer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 52d258da1716b584badd0297eae9bfbf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,21 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
[System.Serializable] public struct ImageSet
|
||||
{
|
||||
[field: SerializeField] public Sprite[] Images { get; set; }
|
||||
}
|
||||
|
||||
[CreateAssetMenu(fileName = "Image Registry", menuName = "Custom/Create Image Registry")]
|
||||
public class ImageRegistry : ScriptableObject
|
||||
{
|
||||
[field: SerializeField] public ImageSet[] Registered { get; set; }
|
||||
|
||||
public Sprite[] GetRandomSpriteSet()
|
||||
{
|
||||
int index = Random.Range(0, Registered.Length);
|
||||
return Registered[index].Images;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Registeries.meta
Normal file
3
Assets/Scripts/Registeries.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88f50186ae5949a69b1a09bdbf679e2b
|
||||
timeCreated: 1768472737
|
||||
27
Assets/Scripts/Registeries/AdvertRegistry.cs
Normal file
27
Assets/Scripts/Registeries/AdvertRegistry.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
[System.Serializable]
|
||||
public struct Advert
|
||||
{
|
||||
[field: SerializeField] public string Name { get; private set; }
|
||||
[field: SerializeField] public Sprite Image { get; private set; }
|
||||
}
|
||||
|
||||
[CreateAssetMenu(fileName = "Advert Registry", menuName = "Custom/Create Advert Registry")]
|
||||
public class AdvertRegistry : ScriptableObject
|
||||
{
|
||||
[field: SerializeField] public Advert[] m_Adverts;
|
||||
private static AdvertRegistry Instance;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void LoadRegistry() => Instance = Resources.Load<AdvertRegistry>("AdvertRegistry");
|
||||
|
||||
public static Advert GetRandomAdvert()
|
||||
{
|
||||
int index = Random.Range(0, Instance.m_Adverts.Length);
|
||||
return Instance.m_Adverts[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Registeries/AdvertRegistry.cs.meta
Normal file
3
Assets/Scripts/Registeries/AdvertRegistry.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a072103fe40445691e28d3eb4509d1b
|
||||
timeCreated: 1768472783
|
||||
25
Assets/Scripts/Registeries/ImageRegistry.cs
Normal file
25
Assets/Scripts/Registeries/ImageRegistry.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
[System.Serializable] public struct ImageSet
|
||||
{
|
||||
[field: SerializeField] public Sprite[] Images { get; set; }
|
||||
}
|
||||
|
||||
[CreateAssetMenu(fileName = "Image Registry", menuName = "Custom/Create Image Registry")]
|
||||
public class ImageRegistry : ScriptableObject
|
||||
{
|
||||
[field: SerializeField] private ImageSet[] m_Registered;
|
||||
private static ImageRegistry Instance;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void LoadRegistry() => Instance = Resources.Load<ImageRegistry>("ImageRegistry");
|
||||
|
||||
public static Sprite[] GetRandomSpriteSet()
|
||||
{
|
||||
int index = Random.Range(0, Instance.m_Registered.Length);
|
||||
return Instance.m_Registered[index].Images;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Assets/Scripts/Registeries/PrefabRegistry.cs
Normal file
16
Assets/Scripts/Registeries/PrefabRegistry.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
[CreateAssetMenu(fileName = nameof(PrefabRegistry), menuName = "Custom/Create Prefab Registry")]
|
||||
public class PrefabRegistry : ScriptableObject
|
||||
{
|
||||
[field: SerializeField] public GameObject ImagePrefab { get; private set; }
|
||||
[field: SerializeField] public GameObject TextButtonPrefab { get; private set; }
|
||||
|
||||
public static PrefabRegistry Instance { get; private set; }
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void LoadRegistry() => Instance = Resources.Load<PrefabRegistry>(nameof(PrefabRegistry));
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Registeries/PrefabRegistry.cs.meta
Normal file
3
Assets/Scripts/Registeries/PrefabRegistry.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9631aba76ad0408d8819b9f979caae5e
|
||||
timeCreated: 1768474019
|
||||
@@ -54,6 +54,13 @@ namespace InterfaceOff
|
||||
}
|
||||
|
||||
public virtual void OnWindowInstantiation() { }
|
||||
public virtual void OnWindowClicked() { }
|
||||
|
||||
/* Default close button closes the window */
|
||||
public virtual void OnWindowCloseButtonClicked() => DestroyWindow();
|
||||
|
||||
protected void DestroyWindow()
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
public class WindowComponents : MonoBehaviour
|
||||
{
|
||||
[field: SerializeField] public Image WindowImage { get; private set; }
|
||||
[field: SerializeField] public RectTransform TopBarRectTransform { get; private set; }
|
||||
[field: SerializeField] public RectTransform RectTransform { get; private set; }
|
||||
[field: SerializeField] public RectTransform CloseButtonRectTransform { get; private set; }
|
||||
[field: SerializeField] public Text InfoText { get; private set; }
|
||||
@@ -18,5 +18,16 @@ namespace InterfaceOff
|
||||
RectTransform.rect.width,
|
||||
RectTransform.rect.height
|
||||
);
|
||||
|
||||
public void SetWidth(float width)
|
||||
{
|
||||
RectTransform.sizeDelta = new Vector2(width, RectTransform.sizeDelta.y);
|
||||
|
||||
WindowImage.rectTransform.sizeDelta = new Vector2(width, 200);
|
||||
TopBarRectTransform.sizeDelta = new Vector2(width, 40);
|
||||
|
||||
CloseButtonRectTransform.anchoredPosition = new Vector2(width / 2 - 20, 0);
|
||||
InfoText.rectTransform.sizeDelta = new Vector2(width - 40, 30);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,6 @@ namespace InterfaceOff
|
||||
private WindowBase AttachedWindow { get; set; }
|
||||
|
||||
public void SetAttachedTo(WindowBase window) => AttachedWindow = window;
|
||||
public void WindowClicked() => AttachedWindow.OnWindowClicked();
|
||||
public void WindowClicked() => AttachedWindow.OnWindowCloseButtonClicked();
|
||||
}
|
||||
}
|
||||
@@ -1,37 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Ext.B83.Unity.Attributes;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
[Serializable] public struct SpawnableWindowType
|
||||
{
|
||||
[field: SerializeField, MonoScript] private string Typename { get; set; }
|
||||
public Type Type => Type.GetType(Typename);
|
||||
|
||||
[field: SerializeField] public int SpawnWeight { get; private set; }
|
||||
}
|
||||
|
||||
public class WindowSpawner : MonoBehaviour
|
||||
{
|
||||
private List<Type> WindowTypes { get; } = new();
|
||||
|
||||
[field: SerializeField] private GameObject SampleChild { get; set; }
|
||||
[field: SerializeField] private Canvas GameCanvas { get; set; }
|
||||
|
||||
[field: SerializeField] private SpawnableWindowType[] WindowTypes { get; set; }
|
||||
private int TotalSpawnWeight { get; set; }
|
||||
|
||||
[field: SerializeField] public bool AutoSpawn { get; private set; } = true;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
/* Fetches all window types created */
|
||||
Type[] types = typeof(WindowBase).Assembly.GetTypes();
|
||||
foreach (Type t in types)
|
||||
/* Logs the amount of types found and errors if there is none */
|
||||
Debug.Log($"Found [{WindowTypes.Length}] different window types ");
|
||||
if (WindowTypes.Length == 0)
|
||||
{
|
||||
if (t.IsSubclassOf(typeof(WindowBase)))
|
||||
Debug.LogError("Could not find any window types");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Calculates the total spawn weight */
|
||||
TotalSpawnWeight = 0;
|
||||
foreach (SpawnableWindowType type in WindowTypes)
|
||||
{
|
||||
TotalSpawnWeight += type.SpawnWeight;
|
||||
}
|
||||
}
|
||||
|
||||
private Type GetRandomWindowType()
|
||||
{
|
||||
int currentTypeWeight = Random.Range(0, TotalSpawnWeight);
|
||||
foreach (SpawnableWindowType type in WindowTypes)
|
||||
{
|
||||
currentTypeWeight -= type.SpawnWeight;
|
||||
if (currentTypeWeight <= 0)
|
||||
{
|
||||
WindowTypes.Add(t);
|
||||
return type.Type;
|
||||
}
|
||||
}
|
||||
|
||||
/* Logs the amount of types found and errors if there is none */
|
||||
Debug.Log($"Found [{WindowTypes.Count}] different window types ");
|
||||
if (WindowTypes.Count == 0)
|
||||
{
|
||||
Debug.LogError("Could not find any window types");
|
||||
}
|
||||
return WindowTypes[0].Type;
|
||||
}
|
||||
|
||||
private void SpawnNewRandomWindow()
|
||||
@@ -40,7 +63,9 @@ namespace InterfaceOff
|
||||
GameObject go = Instantiate(SampleChild, GameCanvas.transform);
|
||||
go.SetActive(true);
|
||||
|
||||
Type type = WindowTypes.GetRandom();
|
||||
Type type = GetRandomWindowType();
|
||||
go.name = type.Name;
|
||||
|
||||
WindowBase windowBase = go.AddComponent(type) as WindowBase;
|
||||
|
||||
/* Checks it created correctly before instantiating further */
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
namespace InterfaceOff
|
||||
using UnityEngine;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
public class BasicWindow : WindowBase
|
||||
{
|
||||
public override void OnWindowInstantiation()
|
||||
{
|
||||
Components.InfoText.text = "Close";
|
||||
/* Adds a random advert to the display */
|
||||
Advert advert = AdvertRegistry.GetRandomAdvert();
|
||||
Components.WindowImage.sprite = advert.Image;
|
||||
Components.InfoText.text = advert.Name;
|
||||
Components.WindowImage.color = new Color(1, 1, 1, 1);
|
||||
}
|
||||
|
||||
public override void OnWindowClicked()
|
||||
public override void OnWindowCloseButtonClicked()
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@ namespace InterfaceOff
|
||||
Components.InfoText.text = "Rotate";
|
||||
|
||||
/* Creates the images to rotate */
|
||||
Sprite[] sprites = CanvasManager.Instance.Images.GetRandomSpriteSet();
|
||||
Sprite[] sprites = ImageRegistry.GetRandomSpriteSet();
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
/* Fetches/Creates needed components */
|
||||
GameObject go = Instantiate(CanvasManager.Instance.ImagePrefab, transform);
|
||||
GameObject go = Instantiate(PrefabRegistry.Instance.ImagePrefab, transform);
|
||||
RectTransform t = go.GetComponent<RectTransform>();
|
||||
|
||||
t.sizeDelta = new Vector2(80, 80);
|
||||
@@ -59,7 +59,7 @@ namespace InterfaceOff
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnWindowClicked()
|
||||
public override void OnWindowCloseButtonClicked()
|
||||
{
|
||||
if (m_TilesRotatedCorrectly == 4)
|
||||
{
|
||||
|
||||
@@ -4,28 +4,22 @@ namespace InterfaceOff
|
||||
{
|
||||
public class MovingWindow : WindowBase
|
||||
{
|
||||
private int m_Health = int.MaxValue;
|
||||
|
||||
public override void OnWindowInstantiation()
|
||||
{
|
||||
/* Creates a random health value */
|
||||
m_Health = Random.Range(2, 6);
|
||||
Components.InfoText.text = $"{m_Health}";
|
||||
|
||||
/* Adds a random advert to the display */
|
||||
Advert advert = AdvertRegistry.GetRandomAdvert();
|
||||
Components.WindowImage.sprite = advert.Image;
|
||||
Components.InfoText.text = advert.Name;
|
||||
Components.WindowImage.color = new Color(1, 1, 1, 1);
|
||||
|
||||
/* Calculates a random velocity direction */
|
||||
float angle = Random.Range(0, Mathf.PI * 2);
|
||||
Velocity = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * 100;
|
||||
}
|
||||
|
||||
public override void OnWindowClicked()
|
||||
public override void OnWindowCloseButtonClicked()
|
||||
{
|
||||
/* Decreases health and destroys if at 0 */
|
||||
m_Health--;
|
||||
Components.InfoText.text = $"{m_Health}";
|
||||
|
||||
if (m_Health <= 0)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
84
Assets/Scripts/Windows/TriviaWindow.cs
Normal file
84
Assets/Scripts/Windows/TriviaWindow.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System.IO;
|
||||
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()
|
||||
{
|
||||
string path = Application.dataPath + "/Resources/Trivia.json";
|
||||
string json = File.ReadAllText(path);
|
||||
|
||||
Trivia = JsonUtility.FromJson<TriviaSet>(json);
|
||||
}
|
||||
|
||||
public 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Windows/TriviaWindow.cs.meta
Normal file
11
Assets/Scripts/Windows/TriviaWindow.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3c11be2f2ebd10b4dafb50bb17c06112
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -5,25 +5,37 @@ namespace InterfaceOff
|
||||
public class TrollWindow : WindowBase
|
||||
{
|
||||
private int m_Health = int.MaxValue;
|
||||
private bool m_SpawnOnRight;
|
||||
|
||||
public override void OnWindowInstantiation()
|
||||
{
|
||||
/* Adds a random advert to the display */
|
||||
Advert advert = AdvertRegistry.GetRandomAdvert();
|
||||
Components.WindowImage.sprite = advert.Image;
|
||||
Components.InfoText.text = advert.Name;
|
||||
Components.WindowImage.color = new Color(1, 1, 1, 1);
|
||||
|
||||
/* Calculates a random health value */
|
||||
m_Health = Random.Range(2, 6);
|
||||
Components.InfoText.text = "TROLL";
|
||||
}
|
||||
|
||||
public override void OnWindowClicked()
|
||||
public override void OnWindowCloseButtonClicked()
|
||||
{
|
||||
/* Removes info text as it can overlap with the close button */
|
||||
Components.InfoText.text = string.Empty;
|
||||
|
||||
/* Decreases health and destroys if at 0 */
|
||||
m_Health--;
|
||||
if (m_Health <= 0)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
DestroyWindow();
|
||||
return;
|
||||
}
|
||||
|
||||
/* If not at zero randomly moves the close button */
|
||||
float randX = Random.Range(-80, 80);
|
||||
float randX = m_SpawnOnRight ? Random.Range(20, 80) : Random.Range(-80, -20);
|
||||
m_SpawnOnRight = !m_SpawnOnRight; // Makes sure the close button is semi far away
|
||||
|
||||
Components.CloseButtonRectTransform.anchoredPosition = new Vector2(randX, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user