Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Copyright (c) 2018-2020 Kai Clavier [kaiclavier.com] Do Not Distribute
- using UnityEngine;
- using System.Collections;
- using UnityEngine.Events;
- /*
- A generic dialogue box that has UnityEvents attached to it,
- demonstrating how to use the drawstring class together with a monobehaviour.
- */
- namespace Fleece
- {
- [AddComponentMenu("Fleece/Generic Drawstring")]
- public class GenericDrawstring : MonoBehaviour
- {
- //define custom unityevents
- [System.Serializable] public class UnityStringArrayEvent : UnityEvent<string[]> {}
- [System.Serializable] public class UnityStringEvent : UnityEvent<string> {}
- //subscribe to events
- void OnEnable()
- {
- drawstring.RunCommandEvent += RunCommand;
- }
- void OnDisable()
- {
- drawstring.RunCommandEvent -= RunCommand;
- }
- void Start()
- {
- if(beginOnStart) Begin(defaultJumper.passage);
- }
- public Jumper defaultJumper;
- //public KeyCode beginKey = KeyCode.Return;
- public KeyCode continueKey = KeyCode.Space;
- public int selectedChoice;
- void Update()
- {
- //debug begin option
- //if(Input.GetKeyDown(beginKey))
- //{
- // Begin(defaultJumper.passage);
- //}
- if(allowContinue)
- {
- if(Input.GetKeyDown(continueKey))
- {
- Continue();
- }
- }
- //change active choice
- if(allowChoices)
- {
- if(Input.GetKeyDown(KeyCode.DownArrow))
- {
- IncreaseSelectedChoice();
- }
- if(Input.GetKeyDown(KeyCode.UpArrow))
- {
- DecreaseSelectedChoice();
- }
- if(Input.GetKeyDown(continueKey))
- {
- MakeChoice(selectedChoice);
- }
- }
- }
- public void IncreaseSelectedChoice()
- {
- selectedChoice = Mathf.Clamp(selectedChoice+1,0,choices.Length-1);
- }
- public void DecreaseSelectedChoice()
- {
- selectedChoice = Mathf.Clamp(selectedChoice-1,0,choices.Length-1);
- }
- private Drawstring drawstring = new Drawstring();
- private bool _allowContinue = false;
- public bool allowContinue
- {
- get
- {
- return _allowContinue;
- }
- }
- private bool _allowChoices = false;
- public bool allowChoices
- {
- get
- {
- return _allowChoices;
- }
- }
- public float initialTextDelay = 0f;
- public bool debugMessages = true;
- //call this to open box and start reading
- public UnityEvent onShowGraphicsEvent;
- public UnityEvent onHideGraphicsEvent;
- public void Begin(string startPassageName)
- {
- Passage foundPassage = Story.settings.activeStory.Find(startPassageName);
- if(foundPassage != null)
- {
- Begin(foundPassage);
- }
- else
- {
- Debug.Log("Starting passage name not found!");
- }
- }
- public void Begin(Passage startPassage)
- {
- if(debugMessages)
- Debug.Log("Starting up text box graphics! Text will display in " + initialTextDelay + " seconds.");
- onShowGraphicsEvent.Invoke();
- //would yield for a delay here
- StartCoroutine(DelayInitialText(startPassage));
- }
- IEnumerator DelayInitialText(Passage startPassage)
- {
- yield return new WaitForSeconds(initialTextDelay);
- SetText(drawstring.Begin(startPassage));
- }
- //if array is less than 2, any integer will just continue.
- public void Continue()
- {
- //if(allowContinue) //this if statement is already called by Update()
- //{
- SetText(drawstring.Continue());
- //}
- }
- public void MakeChoice(int choice)
- {
- SetText(drawstring.MakeChoice(choice));
- }
- //make sure to return false if a matching command wasn't found!!!
- //return true if a command was executed!!
- public bool RunCommand(string command, string[] args, Parser parser, Passage passage)
- {
- return false;
- }
- public bool beginOnStart = true;
- public bool immediatelyAllowContinue = true;
- public string[] choices;
- public UnityStringEvent onSetTextEvent;
- private void SetText(string text)
- {
- //close the proper dialog
- if(allowContinue)
- onHideContinueEvent.Invoke();
- if(allowChoices)
- onHideChoicesEvent.Invoke();
- _allowContinue = false;
- _allowChoices = false;
- if(text.Length > 0)
- {
- if(debugMessages)
- Debug.Log("Displaying the following text: '" + text + "'");
- //send text to a text mesh here
- onSetTextEvent.Invoke(text);
- if(immediatelyAllowContinue)
- {
- TextDoneReading();
- }
- }
- else //if there's an empty string, this is the end of the route
- {
- onHideGraphicsEvent.Invoke();
- }
- }
- public UnityEvent onShowContinueEvent;
- public UnityEvent onHideContinueEvent;
- public UnityStringArrayEvent onShowChoicesEvent;
- public UnityEvent onHideChoicesEvent;
- //called when text is finished reading, or whenever you want to display the continue/choices dialogues
- public void TextDoneReading()
- {
- choices = drawstring.GetChoices();
- if(choices.Length < 2) //it's a continue
- {
- _allowContinue = true;
- if(debugMessages)
- Debug.Log("Text can be continued now.");
- onShowContinueEvent.Invoke();
- }
- else //it's a set of choices!
- {
- selectedChoice = 0; //put cursor on first choice
- _allowChoices = true;
- if(debugMessages)
- {
- string allChoices = string.Join(", ", choices);
- Debug.Log("Choice can be made now. Available choices are: '" + allChoices + "'.");
- }
- onShowChoicesEvent.Invoke(choices);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement