Browse Source

Replaced Flags, Counters & Inventory with Values

- Removed GameState class
- Added Game.GetValue() and Game.SetValue()
- Added GetValue(), SetValue(), HasValue() & ClearValue() to
GameController class.
master
chrisgregan 11 years ago
parent
commit
be32f264a6
  1. 12
      Assets/Fungus/Scripts/Button.cs
  2. 56
      Assets/Fungus/Scripts/Commands.cs
  3. 41
      Assets/Fungus/Scripts/Game.cs
  4. 106
      Assets/Fungus/Scripts/GameController.cs
  5. 90
      Assets/Fungus/Scripts/GameState.cs
  6. 8
      Assets/Fungus/Scripts/GameState.cs.meta
  7. BIN
      Assets/FungusExample/Scenes/Example.unity
  8. 12
      Assets/FungusExample/Scripts/AudioRoom.cs
  9. 4
      Assets/FungusExample/Scripts/ButtonRoom.cs
  10. 7
      Assets/FungusExample/Scripts/PageRoom.cs

12
Assets/Fungus/Scripts/Button.cs

@ -18,14 +18,14 @@ namespace Fungus
public Action buttonAction; 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; 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; float targetAlpha;
bool showButton; bool showButton;
@ -91,9 +91,9 @@ namespace Fungus
} }
} }
// Hide the button if the specified flag has been set to true // Hide the button if the specified game value is non-zero
if (hideOnFlag.Length > 0 && if (hideOnSetValue.Length > 0 &&
Game.GetInstance().state.GetFlag(hideOnFlag)) Game.GetInstance().GetValue(hideOnSetValue) != 0)
{ {
targetAlpha = 0f; targetAlpha = 0f;
} }

56
Assets/Fungus/Scripts/Commands.cs

@ -327,62 +327,14 @@ namespace Fungus
} }
/** /**
* Sets a global boolean flag value * Sets a globally accessible game value
*/ */
public class SetFlagCommand : CommandQueue.Command public class SetValueCommand : 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
{
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; string key;
int value; int value;
public SetInventoryCommand(string _key, int _value) public SetValueCommand(string _key, int _value)
{ {
key = _key; key = _key;
value = _value; value = _value;
@ -390,7 +342,7 @@ namespace Fungus
public override void Execute(CommandQueue commandQueue, Action onComplete) public override void Execute(CommandQueue commandQueue, Action onComplete)
{ {
Game.GetInstance().state.SetInventory(key, value); Game.GetInstance().SetValue(key, value);
if (onComplete != null) if (onComplete != null)
{ {
onComplete(); onComplete();

41
Assets/Fungus/Scripts/Game.cs

@ -38,7 +38,7 @@ namespace Fungus
public bool showLinks = true; public bool showLinks = true;
/** /**
* Text to use on 'Continue' buttons * Text to use on 'Continue' buttons.
*/ */
public string continueText = "Continue"; public string continueText = "Continue";
@ -58,28 +58,31 @@ namespace Fungus
public float roomFadeDuration = 1f; 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; public float buttonFadeDuration = 0.25f;
/** /**
* Full screen texture used for screen fade effect * Full screen texture used for screen fade effect.
*/ */
public Texture2D fadeTexture; public Texture2D fadeTexture;
/** /**
* Sound effect to play when buttons are clicked * Sound effect to play when buttons are clicked.
*/ */
public AudioClip buttonClickClip; public AudioClip buttonClickClip;
/**
* Global dictionary of integer values for storing game state.
*/
[HideInInspector] [HideInInspector]
public View activeView; public Dictionary<string, int> values = new Dictionary<string, int>();
[HideInInspector] [HideInInspector]
public Page activePage; public View activeView;
[HideInInspector] [HideInInspector]
public GameState state = new GameState(); public Page activePage;
[HideInInspector] [HideInInspector]
public StringTable stringTable = new StringTable(); public StringTable stringTable = new StringTable();
@ -190,5 +193,29 @@ namespace Fungus
return false; 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;
}
} }
} }

106
Assets/Fungus/Scripts/GameController.cs

@ -1,4 +1,4 @@
using UnityEngine; using UnityEngine;
using System; using System;
using System.Collections; using System.Collections;
@ -202,112 +202,56 @@ namespace Fungus
#region State Methods #region State Methods
/** /**
* Sets a boolean flag value. * Sets a globally accessible integer value.
* This method returns immediately but it queues an asynchronous command for later execution. * 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 boolean value to set the flag to * @param value The value to set
*/ */
public void SetFlag(string key, bool value) public void SetValue(string key, int value)
{ {
CommandQueue commandQueue = Game.GetInstance().commandQueue; CommandQueue commandQueue = Game.GetInstance().commandQueue;
commandQueue.AddCommand(new Command.SetFlagCommand(key, value)); commandQueue.AddCommand(new Command.SetValueCommand(key, value));
} }
/** /**
* Sets a boolean flag value to true. * Sets a globally accessible integer value to 1.
* This method returns immediately but it queues an asynchronous command for later execution. * 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
*/ */
public void SetFlag(string key) public void SetValue(string key)
{ {
CommandQueue commandQueue = Game.GetInstance().commandQueue; SetValue(key, 1);
commandQueue.AddCommand(new Command.SetFlagCommand(key, true));
}
/**
* 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.
* 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. * Sets a globally accessible integer value to 0.
* 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
*/
public int GetCounter(string key)
{
GameState state = Game.GetInstance().state;
return state.GetCounter(key);
}
/**
* Sets an inventory item count to 1.
* This supports the common case where the player can only collect 1 instance of an inventory item.
* This method returns immediately but it queues an asynchronous command for later execution. * 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; SetValue(key, 0);
commandQueue.AddCommand(new Command.SetInventoryCommand(key, 1));
}
/**
* 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. * Gets a globally accessible integer value.
* Inventory counts are set using SetInventory(). * Returns zero if the value has not previously been set.
* Returns zero if the inventory count has not previously been set to a value. * @param key The name of the value
* @param key The name of the inventory item * @return The integer value for this key, or 0 if not previously set.
* @return The current inventory count of the item
*/ */
public int GetInventory(string key) public int GetValue(string key)
{ {
GameState state = Game.GetInstance().state; return Game.GetInstance().GetValue(key);
return state.GetInventory(key);
} }
/** /**
* Check if player has an inventory item. * Checks if a value is non-zero.
* @param key The name of the inventory item * @param key The name of the value to check.
* @return Returns true if the inventory count for an item is greater than zero * @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 GetValue(key) != 0;
return (state.GetInventory(key) > 0);
} }
#endregion #endregion

90
Assets/Fungus/Scripts/GameState.cs

@ -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<string, bool> flags = new Dictionary<string, bool>();
protected Dictionary<string, int> counters = new Dictionary<string, int>();
protected Dictionary<string, int> inventory = new Dictionary<string, int>();
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;
}
}
}

8
Assets/Fungus/Scripts/GameState.cs.meta

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 2420b605a4ce1443bba29d71d694429c
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.

12
Assets/FungusExample/Scripts/AudioRoom.cs

@ -10,11 +10,11 @@ public class AudioRoom : Room
void OnEnter() void OnEnter()
{ {
if (GetFlag("music")) if (HasValue("music"))
{ {
AddOption("Stop the music", StopGameMusic); AddOption("Stop the music", StopGameMusic);
if (GetFlag("quiet") == false) if (HasValue("quiet") == false)
{ {
AddOption("Shhh! Make it quieter", MakeQuiet); AddOption("Shhh! Make it quieter", MakeQuiet);
} }
@ -40,15 +40,15 @@ public class AudioRoom : Room
{ {
PlayMusic(musicClip); PlayMusic(musicClip);
SetMusicVolume(1f); SetMusicVolume(1f);
SetFlag("music", true); SetValue("music");
Call(OnEnter); Call(OnEnter);
} }
void StopGameMusic() void StopGameMusic()
{ {
StopMusic(); StopMusic();
SetFlag("music", false); ClearValue("music");
SetFlag("quiet", false); ClearValue("quiet");
Call(OnEnter); Call(OnEnter);
} }
@ -60,7 +60,7 @@ public class AudioRoom : Room
void MakeQuiet() void MakeQuiet()
{ {
SetFlag("quiet", true); SetValue("quiet");
SetMusicVolume(0.25f, 1f); SetMusicVolume(0.25f, 1f);
Call(OnEnter); Call(OnEnter);
} }

4
Assets/FungusExample/Scripts/ButtonRoom.cs

@ -42,8 +42,8 @@ public class ButtonRoom : Room
{ {
PlaySound(effectClip); PlaySound(effectClip);
// The music button has been set to hide if this flag is set // The music button has been configured to hide when this value is set
SetFlag("PlayedSound"); SetValue("PlayedSound");
} }
void OnQuestionClicked() void OnQuestionClicked()

7
Assets/FungusExample/Scripts/PageRoom.cs

@ -46,9 +46,8 @@ public class PageRoom : Room
void GoToSleep() void GoToSleep()
{ {
// Check to see if a game flag has been set // Check to see if a game value has been set
// You can also use GetCounter & GetInventory to get other types of values. if (HasValue("spawned"))
if (GetFlag("spawned"))
{ {
Say("I am feeling rather sleepy after all that spawning!"); Say("I am feeling rather sleepy after all that spawning!");
Say("Yawn! Good night world!"); Say("Yawn! Good night world!");
@ -78,7 +77,7 @@ public class PageRoom : Room
SetPageStyle(defaultStyle); SetPageStyle(defaultStyle);
// Sets a game flag which we check above in GoToSleep // Sets a game flag which we check above in GoToSleep
SetFlag("spawned", true); SetValue("spawned");
AddOption("So tired. I sleep now.", GoToSleep); AddOption("So tired. I sleep now.", GoToSleep);
AddOption("No way! More spores!", ProduceSpores); AddOption("No way! More spores!", ProduceSpores);

Loading…
Cancel
Save