Advertisement
KaiClavier

GenericDrawstring.cs

Mar 2nd, 2020
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. //Copyright (c) 2018-2020 Kai Clavier [kaiclavier.com] Do Not Distribute
  2. using UnityEngine;
  3. using System.Collections;
  4. using UnityEngine.Events;
  5. /*
  6. A generic dialogue box that has UnityEvents attached to it,
  7. demonstrating how to use the drawstring class together with a monobehaviour.
  8. */
  9. namespace Fleece
  10. {
  11.     [AddComponentMenu("Fleece/Generic Drawstring")]
  12.     public class GenericDrawstring : MonoBehaviour
  13.     {
  14.         //define custom unityevents
  15.         [System.Serializable] public class UnityStringArrayEvent : UnityEvent<string[]> {}
  16.         [System.Serializable] public class UnityStringEvent : UnityEvent<string> {}
  17.  
  18.         //subscribe to events
  19.         void OnEnable()
  20.         {
  21.             drawstring.RunCommandEvent += RunCommand;
  22.         }
  23.         void OnDisable()
  24.         {
  25.             drawstring.RunCommandEvent -= RunCommand;
  26.         }
  27.  
  28.         void Start()
  29.         {
  30.             if(beginOnStart) Begin(defaultJumper.passage);
  31.         }
  32.  
  33.         public Jumper defaultJumper;
  34.         //public KeyCode beginKey = KeyCode.Return;
  35.         public KeyCode continueKey = KeyCode.Space;
  36.         public int selectedChoice;
  37.         void Update()
  38.         {
  39.             //debug begin option
  40.             //if(Input.GetKeyDown(beginKey))
  41.             //{
  42.             //  Begin(defaultJumper.passage);
  43.             //}
  44.             if(allowContinue)
  45.             {
  46.                 if(Input.GetKeyDown(continueKey))
  47.                 {
  48.                     Continue();
  49.                 }
  50.             }
  51.             //change active choice
  52.             if(allowChoices)
  53.             {
  54.                 if(Input.GetKeyDown(KeyCode.DownArrow))
  55.                 {
  56.                     IncreaseSelectedChoice();
  57.                 }
  58.                 if(Input.GetKeyDown(KeyCode.UpArrow))
  59.                 {
  60.                     DecreaseSelectedChoice();
  61.                 }
  62.                 if(Input.GetKeyDown(continueKey))
  63.                 {
  64.                     MakeChoice(selectedChoice);
  65.                 }
  66.             }
  67.         }
  68.         public void IncreaseSelectedChoice()
  69.         {
  70.             selectedChoice = Mathf.Clamp(selectedChoice+1,0,choices.Length-1);
  71.         }
  72.         public void DecreaseSelectedChoice()
  73.         {
  74.             selectedChoice = Mathf.Clamp(selectedChoice-1,0,choices.Length-1);
  75.         }
  76.  
  77.         private Drawstring drawstring = new Drawstring();
  78.         private bool _allowContinue = false;
  79.         public bool allowContinue
  80.         {
  81.             get
  82.             {
  83.                 return _allowContinue;
  84.             }
  85.         }
  86.         private bool _allowChoices = false;
  87.         public bool allowChoices
  88.         {
  89.             get
  90.             {
  91.                 return _allowChoices;
  92.             }
  93.         }
  94.         public float initialTextDelay = 0f;
  95.         public bool debugMessages = true;
  96.  
  97.         //call this to open box and start reading
  98.         public UnityEvent onShowGraphicsEvent;
  99.         public UnityEvent onHideGraphicsEvent;
  100.         public void Begin(string startPassageName)
  101.         {
  102.             Passage foundPassage = Story.settings.activeStory.Find(startPassageName);
  103.             if(foundPassage != null)
  104.             {
  105.                 Begin(foundPassage);
  106.             }
  107.             else
  108.             {
  109.                 Debug.Log("Starting passage name not found!");
  110.             }
  111.         }
  112.         public void Begin(Passage startPassage)
  113.         {
  114.             if(debugMessages)
  115.                 Debug.Log("Starting up text box graphics! Text will display in " + initialTextDelay + " seconds.");
  116.             onShowGraphicsEvent.Invoke();
  117.             //would yield for a delay here
  118.             StartCoroutine(DelayInitialText(startPassage));
  119.         }
  120.         IEnumerator DelayInitialText(Passage startPassage)
  121.         {
  122.             yield return new WaitForSeconds(initialTextDelay);
  123.             SetText(drawstring.Begin(startPassage));
  124.         }
  125.         //if array is less than 2, any integer will just continue.
  126.         public void Continue()
  127.         {
  128.             //if(allowContinue) //this if statement is already called by Update()
  129.             //{
  130.             SetText(drawstring.Continue());
  131.             //}
  132.         }
  133.         public void MakeChoice(int choice)
  134.         {
  135.             SetText(drawstring.MakeChoice(choice));
  136.         }
  137.        
  138.        
  139.  
  140.         //make sure to return false if a matching command wasn't found!!!
  141.         //return true if a command was executed!!
  142.         public bool RunCommand(string command, string[] args, Parser parser, Passage passage)
  143.         {
  144.             return false;
  145.         }
  146.  
  147.        
  148.         public bool beginOnStart = true;
  149.         public bool immediatelyAllowContinue = true;
  150.         public string[] choices;
  151.         public UnityStringEvent onSetTextEvent;
  152.         private void SetText(string text)
  153.         {
  154.             //close the proper dialog
  155.             if(allowContinue)
  156.                 onHideContinueEvent.Invoke();
  157.             if(allowChoices)
  158.                 onHideChoicesEvent.Invoke();
  159.  
  160.             _allowContinue = false;
  161.             _allowChoices = false;
  162.            
  163.             if(text.Length > 0)
  164.             {
  165.                 if(debugMessages)
  166.                     Debug.Log("Displaying the following text: '" + text + "'");
  167.                 //send text to a text mesh here
  168.                 onSetTextEvent.Invoke(text);
  169.                 if(immediatelyAllowContinue)
  170.                 {
  171.                     TextDoneReading();
  172.                 }
  173.             }
  174.             else //if there's an empty string, this is the end of the route
  175.             {
  176.                 onHideGraphicsEvent.Invoke();
  177.             }
  178.            
  179.         }
  180.  
  181.         public UnityEvent onShowContinueEvent;
  182.         public UnityEvent onHideContinueEvent;
  183.  
  184.         public UnityStringArrayEvent onShowChoicesEvent;
  185.         public UnityEvent onHideChoicesEvent;  
  186.         //called when text is finished reading, or whenever you want to display the continue/choices dialogues
  187.         public void TextDoneReading()
  188.         {
  189.            
  190.  
  191.             choices = drawstring.GetChoices();
  192.             if(choices.Length < 2) //it's a continue
  193.             {
  194.                 _allowContinue = true;
  195.                 if(debugMessages)
  196.                     Debug.Log("Text can be continued now.");
  197.                 onShowContinueEvent.Invoke();
  198.             }
  199.             else //it's a set of choices!
  200.             {
  201.                 selectedChoice = 0; //put cursor on first choice
  202.                 _allowChoices = true;
  203.                 if(debugMessages)
  204.                 {
  205.                     string allChoices = string.Join(", ", choices);
  206.                     Debug.Log("Choice can be made now. Available choices are: '" + allChoices + "'.");
  207.                 }
  208.                 onShowChoicesEvent.Invoke(choices);
  209.             }
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement