diff --git a/Assets/Fungus/Scripts/Button.cs b/Assets/Fungus/Scripts/Button.cs index fa0ad2d5..93f77c2c 100644 --- a/Assets/Fungus/Scripts/Button.cs +++ b/Assets/Fungus/Scripts/Button.cs @@ -18,14 +18,14 @@ namespace Fungus public Action buttonAction; /** - * Automatically hide the button when displaying story text/options or waiting. + * Automatically hides the button when displaying story text/options or waiting. */ public bool autoHide; /** - * Hide the button when a value is set. + * Automatically hides the button when the specified game value is set (i.e. not equal to zero). */ - public string hideOnFlag; + public string hideOnSetValue; float targetAlpha; bool showButton; @@ -91,9 +91,9 @@ namespace Fungus } } - // Hide the button if the specified flag has been set to true - if (hideOnFlag.Length > 0 && - Game.GetInstance().state.GetFlag(hideOnFlag)) + // Hide the button if the specified game value is non-zero + if (hideOnSetValue.Length > 0 && + Game.GetInstance().GetValue(hideOnSetValue) != 0) { targetAlpha = 0f; } diff --git a/Assets/Fungus/Scripts/Commands.cs b/Assets/Fungus/Scripts/Commands.cs index 23c6a6ad..8c86de54 100644 --- a/Assets/Fungus/Scripts/Commands.cs +++ b/Assets/Fungus/Scripts/Commands.cs @@ -327,70 +327,22 @@ namespace Fungus } /** - * Sets a global boolean flag value + * Sets a globally accessible game value */ - public class SetFlagCommand : CommandQueue.Command - { - string key; - bool value; - - public SetFlagCommand(string _key, bool _value) - { - key = _key; - value = _value; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - Game.GetInstance().state.SetFlag(key, value); - if (onComplete != null) - { - onComplete(); - } - } - } - - /** - * Sets a global integer counter value - */ - public class SetCounterCommand : CommandQueue.Command + public class SetValueCommand : CommandQueue.Command { string key; int value; - - public SetCounterCommand(string _key, int _value) - { - key = _key; - value = _value; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - Game.GetInstance().state.SetCounter(key, value); - if (onComplete != null) - { - onComplete(); - } - } - } - /** - * Sets a global inventory count value - */ - public class SetInventoryCommand : CommandQueue.Command - { - string key; - int value; - - public SetInventoryCommand(string _key, int _value) + public SetValueCommand(string _key, int _value) { key = _key; value = _value; } - + public override void Execute(CommandQueue commandQueue, Action onComplete) { - Game.GetInstance().state.SetInventory(key, value); + Game.GetInstance().SetValue(key, value); if (onComplete != null) { onComplete(); diff --git a/Assets/Fungus/Scripts/Game.cs b/Assets/Fungus/Scripts/Game.cs index a11cc118..c99e2aff 100644 --- a/Assets/Fungus/Scripts/Game.cs +++ b/Assets/Fungus/Scripts/Game.cs @@ -38,7 +38,7 @@ namespace Fungus public bool showLinks = true; /** - * Text to use on 'Continue' buttons + * Text to use on 'Continue' buttons. */ public string continueText = "Continue"; @@ -58,29 +58,32 @@ namespace Fungus public float roomFadeDuration = 1f; /** - * Time for fade transition to complete when hiding/showing buttons + * Time for fade transition to complete when hiding/showing buttons. */ public float buttonFadeDuration = 0.25f; /** - * Full screen texture used for screen fade effect + * Full screen texture used for screen fade effect. */ public Texture2D fadeTexture; /** - * Sound effect to play when buttons are clicked + * Sound effect to play when buttons are clicked. */ public AudioClip buttonClickClip; + /** + * Global dictionary of integer values for storing game state. + */ + [HideInInspector] + public Dictionary values = new Dictionary(); + [HideInInspector] public View activeView; [HideInInspector] public Page activePage; - - [HideInInspector] - public GameState state = new GameState(); - + [HideInInspector] public StringTable stringTable = new StringTable(); @@ -190,5 +193,29 @@ namespace Fungus return false; } + + /** + * Sets a globally accessible game state value. + * @param key The key of the value. + * @param value The integer value to store. + */ + public void SetValue(string key, int value) + { + values[key] = value; + } + + /** + * Gets a globally accessible game state value. + * @param key The key of the value. + * @return value The integer value for the specified key, or 0 if the key does not exist. + */ + public int GetValue(string key) + { + if (values.ContainsKey(key)) + { + return values[key]; + } + return 0; + } } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/GameController.cs b/Assets/Fungus/Scripts/GameController.cs index 60bdba48..873286bd 100644 --- a/Assets/Fungus/Scripts/GameController.cs +++ b/Assets/Fungus/Scripts/GameController.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System; using System.Collections; @@ -200,114 +200,58 @@ namespace Fungus #endregion #region State Methods - - /** - * Sets a boolean flag value. - * This method returns immediately but it queues an asynchronous command for later execution. - * @param key The name of the flag - * @param value The boolean value to set the flag to - */ - public void SetFlag(string key, bool value) - { - CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.SetFlagCommand(key, value)); - } /** - * Sets a boolean flag value to true. + * Sets a globally accessible integer value. * This method returns immediately but it queues an asynchronous command for later execution. - * @param key The name of the flag + * @param key The name of the value to set + * @param value The value to set */ - public void SetFlag(string key) + public void SetValue(string key, int value) { CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.SetFlagCommand(key, true)); + commandQueue.AddCommand(new Command.SetValueCommand(key, value)); } /** - * Gets the current state of a flag. - * Flag values are set using SetFlag(). - * Returns false if the flag has previously been set to false, or has not yet been set. - * @param key The name of the flag - * @return The boolean state of the flag. - */ - public bool GetFlag(string key) - { - GameState state = Game.GetInstance().state; - return state.GetFlag(key); - } - - /** - * Sets an integer counter value. + * Sets a globally accessible integer value to 1. * This method returns immediately but it queues an asynchronous command for later execution. - * @param key The name of the counter - * @param value The value to set the counter to - */ - public void SetCounter(string key, int value) - { - CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.SetCounterCommand(key, value)); - } - - /** - * Gets the current value of a counter. - * Counter values are set using SetCounter(). - * Returns zero if the counter has not previously been set to a value. - * @param key The name of the counter - * @return The current value of the counter + * @param key The name of the value to set */ - public int GetCounter(string key) + public void SetValue(string key) { - GameState state = Game.GetInstance().state; - return state.GetCounter(key); + SetValue(key, 1); } /** - * Sets an inventory item count to 1. - * This supports the common case where the player can only collect 1 instance of an inventory item. + * Sets a globally accessible integer value to 0. * This method returns immediately but it queues an asynchronous command for later execution. - * @param key The name of the inventory item + * @param key The key of the value. */ - public void SetInventory(string key) + public void ClearValue(string key) { - CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.SetInventoryCommand(key, 1)); + SetValue(key, 0); } - - /** - * Sets the inventory count for an item. - * This method returns immediately but it queues an asynchronous command for later execution. - * @param key The name of the inventory item - * @param value The inventory count for the item - */ - public void SetInventory(string key, int value) - { - CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.SetInventoryCommand(key, value)); - } - + /** - * Gets the current inventory count for an item. - * Inventory counts are set using SetInventory(). - * Returns zero if the inventory count has not previously been set to a value. - * @param key The name of the inventory item - * @return The current inventory count of the item + * Gets a globally accessible integer value. + * Returns zero if the value has not previously been set. + * @param key The name of the value + * @return The integer value for this key, or 0 if not previously set. */ - public int GetInventory(string key) + public int GetValue(string key) { - GameState state = Game.GetInstance().state; - return state.GetInventory(key); + return Game.GetInstance().GetValue(key); } - + /** - * Check if player has an inventory item. - * @param key The name of the inventory item - * @return Returns true if the inventory count for an item is greater than zero + * Checks if a value is non-zero. + * @param key The name of the value to check. + * @return Returns true if the value is not equal to zero. */ - public bool HasInventory(string key) + public bool HasValue(string key) { - GameState state = Game.GetInstance().state; - return (state.GetInventory(key) > 0); + return GetValue(key) != 0; } #endregion diff --git a/Assets/Fungus/Scripts/GameState.cs b/Assets/Fungus/Scripts/GameState.cs deleted file mode 100644 index f6a61af3..00000000 --- a/Assets/Fungus/Scripts/GameState.cs +++ /dev/null @@ -1,90 +0,0 @@ -using UnityEngine; -using System.Collections; -using System.Collections.Generic; - -namespace Fungus -{ - /** - * Manages the global state information for the game. - * Implemented as a separate class from Game to facilitate storing & restoring of game state. - */ - public class GameState - { - protected Dictionary flags = new Dictionary(); - - protected Dictionary counters = new Dictionary(); - - protected Dictionary inventory = new Dictionary(); - - public GameState DeepClone() - { - GameState clone = new GameState(); - - foreach (string key in flags.Keys) - clone.flags[key] = flags[key]; - foreach (string key in counters.Keys) - clone.counters[key] = counters[key]; - foreach (string key in inventory.Keys) - clone.inventory[key] = inventory[key]; - - return clone; - } - - public void ClearFlags() - { - flags.Clear(); - } - - public bool GetFlag(string key) - { - if (flags.ContainsKey(key)) - { - return flags[key]; - } - return false; - } - - public void SetFlag(string key, bool value) - { - flags[key] = value; - } - - public void ClearCounters() - { - counters.Clear(); - } - - public int GetCounter(string key) - { - if (counters.ContainsKey(key)) - { - return counters[key]; - } - return 0; - } - - public void SetCounter(string key, int value) - { - counters[key] = value; - } - - public void ClearInventory() - { - inventory.Clear(); - } - - public int GetInventory(string key) - { - if (inventory.ContainsKey(key)) - { - return inventory[key]; - } - return 0; - } - - public void SetInventory(string key, int value) - { - inventory[key] = value; - } - } -} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/GameState.cs.meta b/Assets/Fungus/Scripts/GameState.cs.meta deleted file mode 100644 index c2ccc75c..00000000 --- a/Assets/Fungus/Scripts/GameState.cs.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 2420b605a4ce1443bba29d71d694429c -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: diff --git a/Assets/FungusExample/Scenes/Example.unity b/Assets/FungusExample/Scenes/Example.unity index c7dda722..32de0143 100644 Binary files a/Assets/FungusExample/Scenes/Example.unity and b/Assets/FungusExample/Scenes/Example.unity differ diff --git a/Assets/FungusExample/Scripts/AudioRoom.cs b/Assets/FungusExample/Scripts/AudioRoom.cs index 0a34248c..0ad0ac56 100644 --- a/Assets/FungusExample/Scripts/AudioRoom.cs +++ b/Assets/FungusExample/Scripts/AudioRoom.cs @@ -10,11 +10,11 @@ public class AudioRoom : Room void OnEnter() { - if (GetFlag("music")) + if (HasValue("music")) { AddOption("Stop the music", StopGameMusic); - if (GetFlag("quiet") == false) + if (HasValue("quiet") == false) { AddOption("Shhh! Make it quieter", MakeQuiet); } @@ -40,15 +40,15 @@ public class AudioRoom : Room { PlayMusic(musicClip); SetMusicVolume(1f); - SetFlag("music", true); + SetValue("music"); Call(OnEnter); } void StopGameMusic() { StopMusic(); - SetFlag("music", false); - SetFlag("quiet", false); + ClearValue("music"); + ClearValue("quiet"); Call(OnEnter); } @@ -60,7 +60,7 @@ public class AudioRoom : Room void MakeQuiet() { - SetFlag("quiet", true); + SetValue("quiet"); SetMusicVolume(0.25f, 1f); Call(OnEnter); } diff --git a/Assets/FungusExample/Scripts/ButtonRoom.cs b/Assets/FungusExample/Scripts/ButtonRoom.cs index 4124a776..ef655407 100644 --- a/Assets/FungusExample/Scripts/ButtonRoom.cs +++ b/Assets/FungusExample/Scripts/ButtonRoom.cs @@ -42,8 +42,8 @@ public class ButtonRoom : Room { PlaySound(effectClip); - // The music button has been set to hide if this flag is set - SetFlag("PlayedSound"); + // The music button has been configured to hide when this value is set + SetValue("PlayedSound"); } void OnQuestionClicked() diff --git a/Assets/FungusExample/Scripts/PageRoom.cs b/Assets/FungusExample/Scripts/PageRoom.cs index 6c78db13..8e1ea621 100644 --- a/Assets/FungusExample/Scripts/PageRoom.cs +++ b/Assets/FungusExample/Scripts/PageRoom.cs @@ -46,9 +46,8 @@ public class PageRoom : Room void GoToSleep() { - // Check to see if a game flag has been set - // You can also use GetCounter & GetInventory to get other types of values. - if (GetFlag("spawned")) + // Check to see if a game value has been set + if (HasValue("spawned")) { Say("I am feeling rather sleepy after all that spawning!"); Say("Yawn! Good night world!"); @@ -78,7 +77,7 @@ public class PageRoom : Room SetPageStyle(defaultStyle); // Sets a game flag which we check above in GoToSleep - SetFlag("spawned", true); + SetValue("spawned"); AddOption("So tired. I sleep now.", GoToSleep); AddOption("No way! More spores!", ProduceSpores);