From 3c422f234c14c421f302b57a7228c4cf9dd45611 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Tue, 25 Feb 2014 11:29:51 +0000 Subject: [PATCH] Miscellaneous refactors to hide complexity - 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 changes --- Assets/Fungus/Scripts/AnimationListener.cs | 13 +- Assets/Fungus/Scripts/Button.cs | 20 +- Assets/Fungus/Scripts/CameraController.cs | 22 +- Assets/Fungus/Scripts/CommandQueue.cs | 40 ++- Assets/Fungus/Scripts/Commands.cs | 27 +- Assets/Fungus/Scripts/Game.cs | 129 ++------ Assets/Fungus/Scripts/GameController.cs | 267 ++++++++++++++++ Assets/Fungus/Scripts/GameController.cs.meta | 8 + Assets/Fungus/Scripts/Page.cs | 50 +-- Assets/Fungus/Scripts/Room.cs | 288 +----------------- Assets/Fungus/Scripts/StringTable.cs | 71 +++++ Assets/Fungus/Scripts/StringTable.cs.meta | 8 + Assets/Fungus/Scripts/StringsParser.cs | 4 +- .../Animations/GreenAlienWalk.anim | Bin 11664 -> 11664 bytes Assets/FungusExample/Scenes/Example.unity | Bin 75336 -> 72520 bytes 15 files changed, 484 insertions(+), 463 deletions(-) create mode 100644 Assets/Fungus/Scripts/GameController.cs create mode 100644 Assets/Fungus/Scripts/GameController.cs.meta create mode 100644 Assets/Fungus/Scripts/StringTable.cs create mode 100644 Assets/Fungus/Scripts/StringTable.cs.meta diff --git a/Assets/Fungus/Scripts/AnimationListener.cs b/Assets/Fungus/Scripts/AnimationListener.cs index 3ebdf4ee..bf44c674 100644 --- a/Assets/Fungus/Scripts/AnimationListener.cs +++ b/Assets/Fungus/Scripts/AnimationListener.cs @@ -3,10 +3,16 @@ using System.Collections; using Fungus; // Listens for animation events -// The string parameter specifies a method name on the active room class +// Usage: +// 1. Attach this script to the animated sprite that you want to listen to for events. +// 2. Add an event on the animation timeline +// 3. Edit the event and choose the 'CallRoomMethod' function +// 4. In the string parameters box, enter the name of the method to call in the active Room's script. public class AnimationListener : MonoBehaviour { - void OnAnimationEvent(string methodName) + // Handler method for animation events + // The string event parameter is used to call a named method on the active room class + void CallRoomMethod(string methodName) { Room room = Game.GetInstance().activeRoom; if (room == null) @@ -14,6 +20,7 @@ public class AnimationListener : MonoBehaviour return; } - room.AnimationEvent(methodName); + CommandQueue commandQueue = Game.GetInstance().commandQueue; + commandQueue.CallCommandMethod(room.gameObject, methodName); } } diff --git a/Assets/Fungus/Scripts/Button.cs b/Assets/Fungus/Scripts/Button.cs index ceb15ca6..77752353 100644 --- a/Assets/Fungus/Scripts/Button.cs +++ b/Assets/Fungus/Scripts/Button.cs @@ -8,13 +8,13 @@ namespace Fungus // Simple button handler class. // When the user taps on the button, the named method is called on ancestor game objects (if it exists). [RequireComponent (typeof (SpriteRenderer))] - [RequireComponent (typeof (BoxCollider2D))] + [RequireComponent (typeof (Collider2D))] public class Button : MonoBehaviour { public Action buttonAction; public SpriteRenderer spriteRenderer; - // Makes a sprite into a clickable button + // Makes a sprite clickable by attaching a Button component (and BoxCollider2D if required) public static void MakeButton(SpriteRenderer _spriteRenderer, Action _buttonAction) { if (_spriteRenderer == null) @@ -30,7 +30,12 @@ namespace Fungus Destroy(oldButton); } - // This will automatically add a BoxCollider2d if none currently exists + // Add a BoxCollider2d if none currently exists + if (_spriteRenderer.gameObject.GetComponent() == null) + { + _spriteRenderer.gameObject.AddComponent(); + } + Button button = _spriteRenderer.gameObject.AddComponent