From be32f264a6b5c7793ad587d0a1d50a38bc3513d1 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 13 Mar 2014 13:22:01 +0000 Subject: [PATCH] Replaced Flags, Counters & Inventory with Values - Removed GameState class - Added Game.GetValue() and Game.SetValue() - Added GetValue(), SetValue(), HasValue() & ClearValue() to GameController class. --- Assets/Fungus/Scripts/Button.cs | 12 +-- Assets/Fungus/Scripts/Commands.cs | 58 +---------- Assets/Fungus/Scripts/Game.cs | 43 ++++++-- Assets/Fungus/Scripts/GameController.cs | 110 +++++---------------- Assets/Fungus/Scripts/GameState.cs | 90 ----------------- Assets/Fungus/Scripts/GameState.cs.meta | 8 -- Assets/FungusExample/Scenes/Example.unity | Bin 54504 -> 54616 bytes Assets/FungusExample/Scripts/AudioRoom.cs | 12 +-- Assets/FungusExample/Scripts/ButtonRoom.cs | 4 +- Assets/FungusExample/Scripts/PageRoom.cs | 7 +- 10 files changed, 84 insertions(+), 260 deletions(-) delete mode 100644 Assets/Fungus/Scripts/GameState.cs delete mode 100644 Assets/Fungus/Scripts/GameState.cs.meta 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 c7dda7228247b49d465e63692510d88077ae8e27..32de0143d009cdc81031f36f1b5dd34963af663f 100644 GIT binary patch delta 1858 zcmY+_e{54#6u|KVg)zmDX%?yTM-Wh90#-C@)q#y{OM#SiLdZbJO19#b$TEqOvLUaD zfHN9c(tjF)NmnG9B;c^9#Q4VohIDFl3P%1gW-*#nBCtiqhD7K0t?$nBlHRoM^X@(8 z-g9r?%YW#(e8N*AawskGW4(yn!S8|7CEoJljj`7^Z-2ETmR(kUVXHMTQ{)d%p&jh) zUmPtJS?pZfeq(*ZithO$TZl2kX=7L(i*P;5X*aQeS7Z~{wEmYyhjo{&xC(fXM^y z2_DBHSe$;BoR$k)eu&N;1s`=MNhb$uQkbqxFE)Yc3TBdn zR!!(JkraV$IeT~U%G}T}c809x(1e%1wJx<(q(e<+16ERANIc<;t;Jff0R8@TEP|ym zz3+9H_i>RHvHm}Br;6@bp8!QwVME|zII&!0k4jvN$%;baagzkA$J*&<4saco!}Mfc zw8uW7CPRLy?`?Z24(f7k!7gLETwAdvKKH>qGq!G90^6x3_z59L<;!6!vR^fUjG z&oHYS)TO!9dZ1BOGFVmN#OS?Ek*7s|Q(Mxs|5j6=!o4NUSU+YbzUfpZhKb*OK6YUB z7?rW6_vsa|2h@}K_QEeais_}x{~gwiwUfk@dI+1q_)N$99`ilpj`suBhehc(HZ?)& zzyH8{t;NrZ{H#{@=jVZ)`R7Eo(-EF(SFs3BnK;+x0dft?VY<|Nq_o|;O5`0iwiD~a z7GQNw>%wxDgL}b3D{ap^_A+L-H|bcsTBMENrB2(2#W9_D?eT^29QKV`$d%Zx&Qwsu zr?%4*%5~;mC{)!ltD6Oe*0=-C#!?u&B8^Tf!iF$ig*)xBRU#Y_x#8^l^{*?s5?BLf za*SZZn40t&83ldS?uW|kaO2({O#dT_un}w%0nH0$4i>7(_Ptx?N%{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