From 396b86cc2ff3174b6ea9ef8e86cd66de2b1e7c84 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 27 Mar 2014 15:56:01 +0000 Subject: [PATCH] Improved command accessibility Made all GameController commands static. Game now also inherits from GameController. Added command for clearing and executing the command queue. --- Assets/Fungus/Scripts/Button.cs | 2 +- Assets/Fungus/Scripts/CommandQueue.cs | 6 +- Assets/Fungus/Scripts/Commands.cs | 33 +++++++-- Assets/Fungus/Scripts/Game.cs | 52 +++----------- Assets/Fungus/Scripts/GameController.cs | 94 +++++++++++++++---------- 5 files changed, 99 insertions(+), 88 deletions(-) diff --git a/Assets/Fungus/Scripts/Button.cs b/Assets/Fungus/Scripts/Button.cs index 93f77c2c..0251fa1f 100644 --- a/Assets/Fungus/Scripts/Button.cs +++ b/Assets/Fungus/Scripts/Button.cs @@ -93,7 +93,7 @@ namespace Fungus // Hide the button if the specified game value is non-zero if (hideOnSetValue.Length > 0 && - Game.GetInstance().GetValue(hideOnSetValue) != 0) + Game.GetInstance().GetGameValue(hideOnSetValue) != 0) { targetAlpha = 0f; } diff --git a/Assets/Fungus/Scripts/CommandQueue.cs b/Assets/Fungus/Scripts/CommandQueue.cs index 85088abb..c11324c6 100644 --- a/Assets/Fungus/Scripts/CommandQueue.cs +++ b/Assets/Fungus/Scripts/CommandQueue.cs @@ -33,7 +33,7 @@ namespace Fungus /** * Clears all queued commands from the list */ - public virtual void Reset() + public virtual void Clear() { commandList.Clear(); } @@ -63,7 +63,7 @@ namespace Fungus */ public void CallCommandMethod(GameObject target, string methodName) { - Reset(); + Clear(); target.SendMessage(methodName, SendMessageOptions.DontRequireReceiver); Execute(); } @@ -74,7 +74,7 @@ namespace Fungus */ public void CallCommandMethod(Action method) { - Reset(); + Clear(); if (method != null) { method(); diff --git a/Assets/Fungus/Scripts/Commands.cs b/Assets/Fungus/Scripts/Commands.cs index 8c86de54..022935f3 100644 --- a/Assets/Fungus/Scripts/Commands.cs +++ b/Assets/Fungus/Scripts/Commands.cs @@ -320,8 +320,24 @@ namespace Fungus public override void Execute(CommandQueue commandQueue, Action onComplete) { - Game.GetInstance().MoveToRoom(room); + Game game = Game.GetInstance(); + game.waiting = true; + + // Fade out screen + game.cameraController.Fade(0f, game.roomFadeDuration / 2f, delegate { + + game.activeRoom = room; + + // Notify room script that the Room is being entered + // Calling private method on Room to hide implementation + game.activeRoom.gameObject.SendMessage("Enter"); + + // Fade in screen + game.cameraController.Fade(1f, game.roomFadeDuration / 2f, delegate { + game.waiting = false; + }); + }); // MoveToRoom always resets the command queue so no need to call onComplete } } @@ -342,7 +358,7 @@ namespace Fungus public override void Execute(CommandQueue commandQueue, Action onComplete) { - Game.GetInstance().SetValue(key, value); + Game.GetInstance().SetGameValue(key, value); if (onComplete != null) { onComplete(); @@ -482,9 +498,12 @@ namespace Fungus { Game game = Game.GetInstance(); + game.waiting = true; + game.cameraController.PanToView(view, duration, delegate { game.activeView = view; + game.waiting = false; // Try to find a page that is a child of the active view. // If there are multiple child pages then it is the client's responsibility @@ -528,12 +547,15 @@ namespace Fungus { Game game = Game.GetInstance(); + game.waiting = true; + game.cameraController.PanToPath(views, duration, delegate { if (views.Length > 0) { game.activeView = views[views.Length - 1]; - + game.waiting = false; + // Try to find a page that is a child of the active view. // If there are multiple child pages then it is the client's responsibility // to set the correct active page in the room script. @@ -577,10 +599,13 @@ namespace Fungus { Game game = Game.GetInstance(); + game.waiting = true; + game.cameraController.FadeToView(view, duration, delegate { game.activeView = view; - + game.waiting = false; + // Try to find a page that is a child of the active view. // If there are multiple child pages then it is the client's responsibility // to set the correct active page in the room script. diff --git a/Assets/Fungus/Scripts/Game.cs b/Assets/Fungus/Scripts/Game.cs index d0bec0a3..182b2c96 100644 --- a/Assets/Fungus/Scripts/Game.cs +++ b/Assets/Fungus/Scripts/Game.cs @@ -2,15 +2,6 @@ using UnityEngine; using System.Collections; using System.Collections.Generic; -/** - * @mainpage notitle - * This is the code documentation for Fungus, a Unity 3D plugin created by Chris Gregan of Snozbot. - * - * @note For a list of all supported scripting commands, please see the Fungus.GameController class documentation. - * - * Refer to http://www.snozbot.com/fungus for more information about Fungus. - */ - /** * @package Fungus An open source library for Unity 3D for creating graphic interactive fiction games. */ @@ -19,7 +10,7 @@ namespace Fungus /** * Manages global game state and movement between rooms. */ - public class Game : MonoBehaviour + public class Game : GameController { /** * The currently active Room. @@ -101,6 +92,9 @@ namespace Fungus static Game instance; + /** + * Returns the singleton instance for the Game class + */ public static Game GetInstance() { if (!instance) @@ -133,39 +127,13 @@ namespace Fungus if (activeRoom != null) { - MoveToRoom(activeRoom); + // Move to the active room + commandQueue.Clear(); + commandQueue.AddCommand(new Command.MoveToRoomCommand(activeRoom)); + commandQueue.Execute(); } } - /** - * Moves player to a different room. - */ - public void MoveToRoom(Room room) - { - if (room == null) - { - Debug.LogError("Failed to move to room. Room must not be null."); - return; - } - - waiting = true; - - // Fade out screen - cameraController.Fade(0f, roomFadeDuration / 2f, delegate { - - activeRoom = room; - - // Notify room script that the Room is being entered - // Calling private method on Room to hide implementation - activeRoom.gameObject.SendMessage("Enter"); - - // Fade in screen - cameraController.Fade(1f, roomFadeDuration / 2f, delegate { - waiting = false; - }); - }); - } - /** * Plays the button clicked sound effect */ @@ -203,7 +171,7 @@ namespace Fungus * @param key The key of the value. * @param value The integer value to store. */ - public void SetValue(string key, int value) + public void SetGameValue(string key, int value) { values[key] = value; } @@ -213,7 +181,7 @@ namespace Fungus * @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) + public int GetGameValue(string key) { if (values.ContainsKey(key)) { diff --git a/Assets/Fungus/Scripts/GameController.cs b/Assets/Fungus/Scripts/GameController.cs index 19bfcd2c..ba272ee1 100644 --- a/Assets/Fungus/Scripts/GameController.cs +++ b/Assets/Fungus/Scripts/GameController.cs @@ -5,18 +5,36 @@ using System.Collections; namespace Fungus { /** - * This class gives easy access to every scripting command available in Fungus. + * This class gives easy static access to every scripting command available in Fungus. */ public class GameController : MonoBehaviour { #region General Methods - + + /** + * Clears the command queue. + */ + public static void Clear() + { + CommandQueue commandQueue = Game.GetInstance().commandQueue; + commandQueue.Clear(); + } + + /** + * Executes the commadns in the command queue. + */ + public static void Execute() + { + CommandQueue commandQueue = Game.GetInstance().commandQueue; + commandQueue.Execute (); + } + /** * Transitions from the current Room to another Room. * This method returns immediately but it queues an asynchronous command for later execution. * @param room The Room to transition to. */ - public void MoveToRoom(Room room) + public static void MoveToRoom(Room room) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.MoveToRoomCommand(room)); @@ -27,7 +45,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param duration The wait duration in seconds */ - public void Wait(float duration) + public static void Wait(float duration) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.WaitCommand(duration)); @@ -37,7 +55,7 @@ namespace Fungus * Wait until player taps, clicks or presses a key before executing the next command. * This method returns immediately but it queues an asynchronous command for later execution. */ - public void WaitForInput() + public static void WaitForInput() { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.WaitForInputCommand()); @@ -49,7 +67,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param callAction The Action delegate method to be called when the command executes. */ - public void Call(Action callAction) + public static void Call(Action callAction) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.CallCommand(callAction)); @@ -64,7 +82,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param view The View object to make active */ - public void SetView(View view) + public static void SetView(View view) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.SetViewCommand(view)); @@ -78,7 +96,7 @@ namespace Fungus * @param targetView The View to pan the camera to. * @param duration The length of time in seconds needed to complete the pan. */ - public void PanToView(View targetView, float duration) + public static void PanToView(View targetView, float duration) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.PanToViewCommand(targetView, duration)); @@ -93,7 +111,7 @@ namespace Fungus * @param duration The length of time in seconds needed to complete the pan. * @param targetViews A parameter list of views to visit during the pan. */ - public void PanToPath(float duration, params View[] targetViews) + public static void PanToPath(float duration, params View[] targetViews) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.PanToPathCommand(targetViews, duration)); @@ -106,7 +124,7 @@ namespace Fungus * @param targetView The View object to fade to. * @param duration The length of time in seconds needed to complete the pan. */ - public void FadeToView(View targetView, float duration) + public static void FadeToView(View targetView, float duration) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.FadeToViewCommand(targetView, duration)); @@ -122,7 +140,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param page The Page object to make active */ - public void SetPage(Page page) + public static void SetPage(Page page) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.SetPageCommand(page)); @@ -134,7 +152,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param pageStyle The style object to make active */ - public void SetPageStyle(PageStyle pageStyle) + public static void SetPageStyle(PageStyle pageStyle) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.SetPageStyleCommand(pageStyle)); @@ -146,7 +164,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param titleText The text to display as the title of the Page. */ - public void Title(string titleText) + public static void Title(string titleText) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.TitleCommand(titleText)); @@ -159,7 +177,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param storyText The text to be written to the currently active Page. */ - public void Say(string storyText) + public static void Say(string storyText) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.SayCommand(storyText)); @@ -172,7 +190,7 @@ namespace Fungus * @param optionText The text to be displayed for this option * @param optionAction The Action delegate method to be called when the player selects the option */ - public void AddOption(string optionText, Action optionAction) + public static void AddOption(string optionText, Action optionAction) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.AddOptionCommand(optionText, optionAction)); @@ -184,7 +202,7 @@ namespace Fungus * @param optionText The text to be displayed for this option * @param optionAction The Action delegate method to be called when the player selects the option */ - public void AddOption(string optionText) + public static void AddOption(string optionText) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.AddOptionCommand(optionText, delegate {})); @@ -194,7 +212,7 @@ namespace Fungus * Display all previously added options as buttons, with no text prompt. * This method returns immediately but it queues an asynchronous command for later execution. */ - public void Choose() + public static void Choose() { Choose(""); } @@ -204,7 +222,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param chooseText The story text to be written above the list of options */ - public void Choose(string chooseText) + public static void Choose(string chooseText) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.ChooseCommand(chooseText)); @@ -219,7 +237,7 @@ namespace Fungus * @param key The name of the value to set * @param value The value to set */ - public void SetValue(string key, int value) + public static void SetValue(string key, int value) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.SetValueCommand(key, value)); @@ -230,7 +248,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param key The name of the value to set */ - public void SetValue(string key) + public static void SetValue(string key) { SetValue(key, 1); } @@ -240,7 +258,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param key The key of the value. */ - public void ClearValue(string key) + public static void ClearValue(string key) { SetValue(key, 0); } @@ -251,9 +269,9 @@ namespace Fungus * @param key The name of the value * @return The integer value for this key, or 0 if not previously set. */ - public int GetValue(string key) + public static int GetValue(string key) { - return Game.GetInstance().GetValue(key); + return Game.GetInstance().GetGameValue(key); } /** @@ -261,7 +279,7 @@ namespace Fungus * @param key The name of the value to check. * @return Returns true if the value is not equal to zero. */ - public bool HasValue(string key) + public static bool HasValue(string key) { return GetValue(key) != 0; } @@ -275,7 +293,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param spriteRenderer The sprite to be modified */ - public void HideSprite(SpriteRenderer spriteRenderer) + public static void HideSprite(SpriteRenderer spriteRenderer) { FadeSprite(spriteRenderer, 0, 0, Vector2.zero); } @@ -286,7 +304,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param spriteRenderer The sprite to be modified */ - public void ShowSprite(SpriteRenderer spriteRenderer) + public static void ShowSprite(SpriteRenderer spriteRenderer) { FadeSprite(spriteRenderer, 1, 0, Vector2.zero); } @@ -297,7 +315,7 @@ namespace Fungus * @param spriteRenderer The sprite to be modified * @param visible Shows the sprite when true, hides the sprite when false. */ - public void ShowSprite(SpriteRenderer spriteRenderer, bool visible) + public static void ShowSprite(SpriteRenderer spriteRenderer, bool visible) { if (visible) { @@ -316,7 +334,7 @@ namespace Fungus * @param targetAlpha The final required transparency value [0..1] * @param duration The duration of the fade transition in seconds */ - public void FadeSprite(SpriteRenderer spriteRenderer, float targetAlpha, float duration) + public static void FadeSprite(SpriteRenderer spriteRenderer, float targetAlpha, float duration) { FadeSprite(spriteRenderer, targetAlpha, duration, Vector2.zero); } @@ -332,7 +350,7 @@ namespace Fungus * @param duration The duration of the fade transition in seconds * @param slideOffset Offset to the starting position for the slide effect. */ - public void FadeSprite(SpriteRenderer spriteRenderer, float targetAlpha, float duration, Vector2 slideOffset) + public static void FadeSprite(SpriteRenderer spriteRenderer, float targetAlpha, float duration, Vector2 slideOffset) { CommandQueue commandQueue = Game.GetInstance().commandQueue; Color color = spriteRenderer.color; @@ -349,7 +367,7 @@ namespace Fungus * @param spriteRenderer The sprite to be made clickable * @param buttonAction The Action delegate method to be called when the player clicks on the button */ - public void ShowButton(Button button, Action buttonAction) + public static void ShowButton(Button button, Action buttonAction) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.ShowButtonCommand(button, true, buttonAction)); @@ -361,7 +379,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param spriteRenderer The sprite to be made non-clickable */ - public void HideButton(Button button) + public static void HideButton(Button button) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.ShowButtonCommand(button, false, null)); @@ -374,7 +392,7 @@ namespace Fungus * @param animator The sprite to be made non-clickable * @param triggerName Name of a trigger parameter in the animator controller */ - public void SetAnimatorTrigger(Animator animator, string triggerName) + public static void SetAnimatorTrigger(Animator animator, string triggerName) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.SetAnimatorTriggerCommand(animator, triggerName)); @@ -388,7 +406,7 @@ namespace Fungus * One music clip may be played at a time. * @param audioClip The music clip to play */ - public void PlayMusic(AudioClip audioClip) + public static void PlayMusic(AudioClip audioClip) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.PlayMusicCommand(audioClip)); @@ -397,7 +415,7 @@ namespace Fungus /** * Stops playing game music. */ - public void StopMusic() + public static void StopMusic() { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.StopMusicCommand()); @@ -407,7 +425,7 @@ namespace Fungus * Sets the game music volume immediately. * @param musicVolume The new music volume value [0..1] */ - public void SetMusicVolume(float musicVolume) + public static void SetMusicVolume(float musicVolume) { SetMusicVolume(musicVolume, 0f); } @@ -417,7 +435,7 @@ namespace Fungus * @param musicVolume The new music volume value [0..1] * @param duration The length of time in seconds needed to complete the volume change. */ - public void SetMusicVolume(float musicVolume, float duration) + public static void SetMusicVolume(float musicVolume, float duration) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.SetMusicVolumeCommand(musicVolume, duration)); @@ -428,7 +446,7 @@ namespace Fungus * Multiple sound effects can be played at the same time. * @param audioClip The sound effect clip to play */ - public void PlaySound(AudioClip audioClip) + public static void PlaySound(AudioClip audioClip) { PlaySound(audioClip, 1f); } @@ -439,7 +457,7 @@ namespace Fungus * @param audioClip The sound effect clip to play * @param volume The volume level of the sound effect */ - public void PlaySound(AudioClip audioClip, float volume) + public static void PlaySound(AudioClip audioClip, float volume) { CommandQueue commandQueue = Game.GetInstance().commandQueue; commandQueue.AddCommand(new Command.PlaySoundCommand(audioClip, volume));