Browse Source
- Added GameController facade class to hide complexity and provide a single place to access all Fungus functionality - Removed existing facade methods on Game (redundant) - Removed Room.OnLeave (not necessary, and possibly dangerous) - Renamed AnimationListener.OnAnimationEvent() to CallRoomMethod (more descriptive). - Button component will now use existing Collider2D if one exists. - Game component now manages CameraController configuration - Game now adds CameraController and CommandQueue at runtime instead of in editor (less user configuration) - CommandQueue now owns the methods for executing command methods - Added StringTable class to manage key/string lookups - Moved SubstituteStrings() and FormatLinkText() to new StringTable class - Room.Enter() method is now private to hide implementation details - Updated example project to match changesmaster
chrisgregan
11 years ago
15 changed files with 484 additions and 463 deletions
@ -0,0 +1,267 @@ |
|||||||
|
using UnityEngine; |
||||||
|
using System; |
||||||
|
using System.Collections; |
||||||
|
using Fungus; |
||||||
|
|
||||||
|
// This facade class gives easy access to all game control |
||||||
|
// functionality available in Fungus |
||||||
|
public class GameController : MonoBehaviour |
||||||
|
{ |
||||||
|
// |
||||||
|
// Synchronous methods |
||||||
|
// The following methods all execute immediately |
||||||
|
// |
||||||
|
|
||||||
|
// Return true if the boolean flag for the key has been set to true |
||||||
|
public bool GetFlag(string key) |
||||||
|
{ |
||||||
|
GameState state = Game.GetInstance().state; |
||||||
|
return state.GetFlag(key); |
||||||
|
} |
||||||
|
|
||||||
|
// Returns the count value for the key |
||||||
|
// Returns zero if no value has been set. |
||||||
|
public int GetCounter(string key) |
||||||
|
{ |
||||||
|
GameState state = Game.GetInstance().state; |
||||||
|
return state.GetCounter(key); |
||||||
|
} |
||||||
|
|
||||||
|
// Returns the inventory count value for the key |
||||||
|
// Returns zero if no inventory count has been set. |
||||||
|
public int GetInventory(string key) |
||||||
|
{ |
||||||
|
GameState state = Game.GetInstance().state; |
||||||
|
return state.GetInventory(key); |
||||||
|
} |
||||||
|
|
||||||
|
// Returns true if the inventory count for the key is greater than zero |
||||||
|
public bool HasInventory(string key) |
||||||
|
{ |
||||||
|
GameState state = Game.GetInstance().state; |
||||||
|
return (state.GetInventory(key) > 0); |
||||||
|
} |
||||||
|
|
||||||
|
// |
||||||
|
// Asynchronous methods |
||||||
|
// The following methods all queue commands for later execution in strict serial order |
||||||
|
// |
||||||
|
|
||||||
|
// Wait for a period of time before executing the next command |
||||||
|
public void Wait(float duration) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new WaitCommand(duration)); |
||||||
|
} |
||||||
|
|
||||||
|
// Call a delegate method provided by the client |
||||||
|
// Used to queue the execution of arbitrary code. |
||||||
|
public void Call(Action callAction) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new CallCommand(callAction)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets the currently active view immediately. |
||||||
|
// The main camera snaps to the active view. |
||||||
|
public void SetView(View view) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SetViewCommand(view)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets the currently active page for text rendering |
||||||
|
public void SetPage(Page page) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SetPageCommand(page)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets the title text displayed at the top of the active page |
||||||
|
public void Title(string titleText) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new TitleCommand(titleText)); |
||||||
|
} |
||||||
|
|
||||||
|
// Writes story text to the currently active page. |
||||||
|
// A 'continue' button is displayed when the text has fully appeared. |
||||||
|
public void Say(string storyText) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SayCommand(storyText)); |
||||||
|
} |
||||||
|
|
||||||
|
// Adds an option button to the current list of options. |
||||||
|
// Use the Choose command to display added options. |
||||||
|
public void AddOption(string optionText, Action optionAction) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new AddOptionCommand(optionText, optionAction)); |
||||||
|
} |
||||||
|
|
||||||
|
// Display all previously added options as buttons, with no text prompt |
||||||
|
public void Choose() |
||||||
|
{ |
||||||
|
Choose(""); |
||||||
|
} |
||||||
|
|
||||||
|
// Displays a text prompt, followed by all previously added options as buttons. |
||||||
|
public void Choose(string chooseText) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new ChooseCommand(chooseText)); |
||||||
|
} |
||||||
|
|
||||||
|
// Changes the active room to a different room |
||||||
|
public void MoveToRoom(Room room) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new MoveToRoomCommand(room)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets a global boolean flag value |
||||||
|
public void SetFlag(string key, bool value) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SetFlagCommand(key, value)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets a global integer counter value |
||||||
|
public void SetCounter(string key, int value) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SetCounterCommand(key, value)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets a global inventory count value |
||||||
|
// Assumes that the count value is 1 (common case) |
||||||
|
public void SetInventory(string key) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SetInventoryCommand(key, 1)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets a global inventory count value |
||||||
|
public void SetInventory(string key, int value) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SetInventoryCommand(key, value)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets sprite alpha to 0 immediately |
||||||
|
public void HideSprite(SpriteRenderer spriteRenderer) |
||||||
|
{ |
||||||
|
FadeSprite(spriteRenderer, 0, 0, Vector2.zero); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets sprite alpha to 1 immediately |
||||||
|
public void ShowSprite(SpriteRenderer spriteRenderer) |
||||||
|
{ |
||||||
|
FadeSprite(spriteRenderer, 1, 0, Vector2.zero); |
||||||
|
} |
||||||
|
|
||||||
|
// Fades a sprite to a given alpha value over a period of time |
||||||
|
public void FadeSprite(SpriteRenderer spriteRenderer, float targetAlpha, float duration) |
||||||
|
{ |
||||||
|
FadeSprite(spriteRenderer, targetAlpha, duration, Vector2.zero); |
||||||
|
} |
||||||
|
|
||||||
|
// Fades a sprite to a given alpha value over a period of time, and applies a sliding motion to the sprite transform |
||||||
|
public void FadeSprite(SpriteRenderer spriteRenderer, float targetAlpha, float duration, Vector2 slideOffset) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
Color color = spriteRenderer.color; |
||||||
|
color.a = targetAlpha; |
||||||
|
commandQueue.AddCommand(new FadeSpriteCommand(spriteRenderer, color, duration, slideOffset)); |
||||||
|
} |
||||||
|
|
||||||
|
// Makes a sprite behave as a clickable button |
||||||
|
public void AddButton(SpriteRenderer buttonSprite, Action buttonAction) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new AddButtonCommand(buttonSprite, buttonAction)); |
||||||
|
} |
||||||
|
|
||||||
|
// Makes a sprite stop behaving as a clickable button |
||||||
|
public void RemoveButton(SpriteRenderer buttonSprite) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new RemoveButtonCommand(buttonSprite)); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets an animator trigger to change the animation state for an animated sprite |
||||||
|
public void SetAnimatorTrigger(Animator animator, string triggerName) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SetAnimatorTriggerCommand(animator, triggerName)); |
||||||
|
} |
||||||
|
|
||||||
|
// Pans the camera to the target view over a period of time |
||||||
|
public void PanToView(View targetView, float duration) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new PanToViewCommand(targetView, duration)); |
||||||
|
} |
||||||
|
|
||||||
|
// Pans the camera through a sequence of target views over a period of time |
||||||
|
public void PanToPath(float duration, params View[] targetViews) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new PanToPathCommand(targetViews, duration)); |
||||||
|
} |
||||||
|
|
||||||
|
// Snaps the camera to the target view immediately |
||||||
|
public void SnapToView(View targetView) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new PanToViewCommand(targetView, 0f)); |
||||||
|
} |
||||||
|
|
||||||
|
// Fades out the current camera view, and fades in again using the target view. |
||||||
|
public void FadeToView(View targetView, float duration) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new FadeToViewCommand(targetView, duration)); |
||||||
|
} |
||||||
|
|
||||||
|
// Plays game music using an audio clip |
||||||
|
public void PlayGameMusic(AudioClip audioClip) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new PlayMusicCommand(audioClip)); |
||||||
|
} |
||||||
|
|
||||||
|
// Stops playing game music |
||||||
|
public void StopGameMusic() |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new StopMusicCommand()); |
||||||
|
} |
||||||
|
|
||||||
|
// Sets music volume immediately |
||||||
|
public void SetMusicVolume(float musicVolume) |
||||||
|
{ |
||||||
|
SetMusicVolume(musicVolume, 0f); |
||||||
|
} |
||||||
|
|
||||||
|
// Fades music volume to required level over a period of time |
||||||
|
public void SetMusicVolume(float musicVolume, float duration) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new SetMusicVolumeCommand(musicVolume, duration)); |
||||||
|
} |
||||||
|
|
||||||
|
// Plays a sound effect once |
||||||
|
public void PlaySound(AudioClip audioClip) |
||||||
|
{ |
||||||
|
PlaySound(audioClip, 1f); |
||||||
|
} |
||||||
|
|
||||||
|
// Plays a sound effect once, at the specified volume |
||||||
|
public void PlaySound(AudioClip audioClip, float volume) |
||||||
|
{ |
||||||
|
CommandQueue commandQueue = Game.GetInstance().commandQueue; |
||||||
|
commandQueue.AddCommand(new PlaySoundCommand(audioClip, volume)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: 4341f88125d5c4fe1b941bd614ae342d |
||||||
|
MonoImporter: |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
@ -0,0 +1,71 @@ |
|||||||
|
using UnityEngine; |
||||||
|
using System.Collections; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Text.RegularExpressions; |
||||||
|
|
||||||
|
// Stores long or frequently repeated strings in a dictionary. |
||||||
|
// Strings can then be retrieved using a short key string. |
||||||
|
public class StringTable |
||||||
|
{ |
||||||
|
Dictionary<string, string> stringTable = new Dictionary<string, string>(); |
||||||
|
|
||||||
|
public void ClearStringTable() |
||||||
|
{ |
||||||
|
stringTable.Clear(); |
||||||
|
} |
||||||
|
|
||||||
|
// Retrieves a string from the table |
||||||
|
public string GetString(string key) |
||||||
|
{ |
||||||
|
if (stringTable.ContainsKey(key)) |
||||||
|
{ |
||||||
|
return stringTable[key]; |
||||||
|
} |
||||||
|
return ""; |
||||||
|
} |
||||||
|
|
||||||
|
// Adds or updates a string in the table |
||||||
|
public void SetString(string key, string value) |
||||||
|
{ |
||||||
|
stringTable[key] = value; |
||||||
|
} |
||||||
|
|
||||||
|
// Replace keys in the input string with the string table entry |
||||||
|
// Example format: "This {string_key} string" |
||||||
|
public string SubstituteStrings(string text) |
||||||
|
{ |
||||||
|
string subbedText = text; |
||||||
|
|
||||||
|
// Instantiate the regular expression object. |
||||||
|
Regex r = new Regex("{.*?}"); |
||||||
|
|
||||||
|
// Match the regular expression pattern against a text string. |
||||||
|
var results = r.Matches(text); |
||||||
|
foreach (Match match in results) |
||||||
|
{ |
||||||
|
string stringKey = match.Value.Substring(1, match.Value.Length - 2); |
||||||
|
string stringValue = GetString(stringKey); |
||||||
|
|
||||||
|
subbedText = subbedText.Replace(match.Value, stringValue); |
||||||
|
} |
||||||
|
|
||||||
|
return subbedText; |
||||||
|
} |
||||||
|
|
||||||
|
// Chops a string at the first new line character |
||||||
|
// Useful for link / button strings that must fit on a single line |
||||||
|
public string FormatLinkText(string text) |
||||||
|
{ |
||||||
|
string trimmed; |
||||||
|
if (text.Contains("\n")) |
||||||
|
{ |
||||||
|
trimmed = text.Substring(0, text.IndexOf("\n")); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
trimmed = text; |
||||||
|
} |
||||||
|
|
||||||
|
return trimmed; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: 43c44334e66c44af1a071056e4fde1f9 |
||||||
|
MonoImporter: |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue