diff --git a/Assets/Fungus/Scripts/Commands/Menu.cs b/Assets/Fungus/Scripts/Commands/Menu.cs index 458fd195..a64578dc 100644 --- a/Assets/Fungus/Scripts/Commands/Menu.cs +++ b/Assets/Fungus/Scripts/Commands/Menu.cs @@ -35,6 +35,9 @@ namespace Fungus [Tooltip("A custom Menu Dialog to use to display this menu. All subsequent Menu commands will use this dialog.")] [SerializeField] protected MenuDialog setMenuDialog; + [Tooltip("If true, this option will be passed to the Menu Dialogue but marked as hidden, this can be used to hide options while maintaining a Menu Shuffle.")] + [SerializeField] protected BooleanData hideThisOption = new BooleanData(false); + #region Public members public MenuDialog SetMenuDialog { get { return setMenuDialog; } set { setMenuDialog = value; } } @@ -47,11 +50,9 @@ namespace Fungus MenuDialog.ActiveMenuDialog = setMenuDialog; } - bool hideOption = (hideIfVisited && targetBlock != null && targetBlock.GetExecutionCount() > 0); + bool hideOption = (hideIfVisited && targetBlock != null && targetBlock.GetExecutionCount() > 0) || hideThisOption.Value; - if (!hideOption) - { - var menuDialog = MenuDialog.GetMenuDialog(); + var menuDialog = MenuDialog.GetMenuDialog(); if (menuDialog != null) { menuDialog.SetActive(true); @@ -59,10 +60,9 @@ namespace Fungus var flowchart = GetFlowchart(); string displayText = flowchart.SubstituteVariables(text); - menuDialog.AddOption(displayText, interactable, targetBlock); + menuDialog.AddOption(displayText, interactable, hideOption, targetBlock); } - } - + Continue(); } diff --git a/Assets/Fungus/Scripts/Commands/MenuShuffle.cs b/Assets/Fungus/Scripts/Commands/MenuShuffle.cs new file mode 100644 index 00000000..fcaf0082 --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/MenuShuffle.cs @@ -0,0 +1,59 @@ +// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). +// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) + +using UnityEngine; +using UnityEngine.Serialization; +using System.Collections.Generic; + +namespace Fungus +{ + /// + /// Shuffle the order of the items in a Fungus Menu + /// + [CommandInfo("Narrative", + "Menu Shuffle", + "Shuffle the order of the items in a Fungus Menu")] + [AddComponentMenu("")] + [ExecuteInEditMode] + public class MenuShuffle : Command + { + public enum Mode + { + Every, + Once + } + [SerializeField] + [Tooltip("Determines if the order is shuffled everytime this command is it (Every) or if it is consistent when returned to but random (Once)")] + protected Mode shuffleMode = Mode.Once; + + private int seed = -1; + + public override void OnEnter() + { + var menuDialog = MenuDialog.GetMenuDialog(); + + //if we shuffle every time or we haven't shuffled yet + if(shuffleMode == Mode.Every || seed == -1) + { + seed = Random.Range(0,1000000); + } + + if (menuDialog != null) + { + menuDialog.Shuffle(new System.Random(seed)); + } + + Continue(); + } + + public override string GetSummary() + { + return shuffleMode.ToString(); + } + + public override Color GetButtonColor() + { + return new Color32(184, 210, 235, 255); + } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Commands/MenuShuffle.cs.meta b/Assets/Fungus/Scripts/Commands/MenuShuffle.cs.meta new file mode 100644 index 00000000..7e4ba734 --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/MenuShuffle.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4d88125c88dab4a44851835e94ff4d90 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/Scripts/Components/MenuDialog.cs b/Assets/Fungus/Scripts/Components/MenuDialog.cs index 44d76085..9462276c 100644 --- a/Assets/Fungus/Scripts/Components/MenuDialog.cs +++ b/Assets/Fungus/Scripts/Components/MenuDialog.cs @@ -21,6 +21,67 @@ namespace Fungus protected Button[] cachedButtons; protected Slider cachedSlider; + private int nextOptionIndex; + + #region Public members + + /// + /// Currently active Menu Dialog used to display Menu options + /// + public static MenuDialog ActiveMenuDialog { get; set; } + + /// + /// A cached list of button objects in the menu dialog. + /// + /// The cached buttons. + public virtual Button[] CachedButtons { get { return cachedButtons; } } + + /// + /// A cached slider object used for the timer in the menu dialog. + /// + /// The cached slider. + public virtual Slider CachedSlider { get { return cachedSlider; } } + + /// + /// Sets the active state of the Menu Dialog gameobject. + /// + public virtual void SetActive(bool state) + { + gameObject.SetActive(state); + } + + + + /// + /// Returns a menu dialog by searching for one in the scene or creating one if none exists. + /// + public static MenuDialog GetMenuDialog() + { + if (ActiveMenuDialog == null) + { + // Use first Menu Dialog found in the scene (if any) + var md = GameObject.FindObjectOfType(); + if (md != null) + { + ActiveMenuDialog = md; + } + + if (ActiveMenuDialog == null) + { + // Auto spawn a menu dialog object from the prefab + GameObject prefab = Resources.Load("Prefabs/MenuDialog"); + if (prefab != null) + { + GameObject go = Instantiate(prefab) as GameObject; + go.SetActive(false); + go.name = "MenuDialog"; + ActiveMenuDialog = go.GetComponent(); + } + } + } + + return ActiveMenuDialog; + } protected virtual void Awake() { @@ -66,9 +127,9 @@ namespace Fungus protected virtual IEnumerator WaitForTimeout(float timeoutDuration, Block targetBlock) { float elapsedTime = 0; - - Slider timeoutSlider = GetComponentInChildren(); - + + Slider timeoutSlider = CachedSlider; + while (elapsedTime < timeoutDuration) { if (timeoutSlider != null) @@ -76,12 +137,12 @@ namespace Fungus float t = 1f - elapsedTime / timeoutDuration; timeoutSlider.value = t; } - + elapsedTime += Time.deltaTime; - + yield return null; } - + Clear(); gameObject.SetActive(false); @@ -108,64 +169,6 @@ namespace Fungus } } - #region Public members - - /// - /// Currently active Menu Dialog used to display Menu options - /// - public static MenuDialog ActiveMenuDialog { get; set; } - - /// - /// Returns a menu dialog by searching for one in the scene or creating one if none exists. - /// - public static MenuDialog GetMenuDialog() - { - if (ActiveMenuDialog == null) - { - // Use first Menu Dialog found in the scene (if any) - var md = GameObject.FindObjectOfType(); - if (md != null) - { - ActiveMenuDialog = md; - } - - if (ActiveMenuDialog == null) - { - // Auto spawn a menu dialog object from the prefab - GameObject prefab = Resources.Load("Prefabs/MenuDialog"); - if (prefab != null) - { - GameObject go = Instantiate(prefab) as GameObject; - go.SetActive(false); - go.name = "MenuDialog"; - ActiveMenuDialog = go.GetComponent(); - } - } - } - - return ActiveMenuDialog; - } - - /// - /// A cached list of button objects in the menu dialog. - /// - /// The cached buttons. - public virtual Button[] CachedButtons { get { return cachedButtons; } } - - /// - /// A cached slider object used for the timer in the menu dialog. - /// - /// The cached slider. - public virtual Slider CachedSlider { get { return cachedSlider; } } - - /// - /// Sets the active state of the Menu Dialog gameobject. - /// - public virtual void SetActive(bool state) - { - gameObject.SetActive(state); - } - /// /// Clear all displayed options in the Menu Dialog. /// @@ -173,7 +176,9 @@ namespace Fungus { StopAllCoroutines(); - var optionButtons = GetComponentsInChildren