You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.8 KiB
83 lines
2.8 KiB
using System.Collections; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
|
|
/// <summary> |
|
/// This Manager is singleton that handles teh display and running of conversations in the game. Other components will |
|
/// use the StartConversation method to launch a pre-defined conversation component. |
|
/// </summary> |
|
public class ConversationManager : Singleton<ConversationManager> |
|
{ |
|
//Is there a converastion going on |
|
bool talking = false; |
|
|
|
//The current line of text being displayed |
|
ConversationEntry currentConversationLine; |
|
|
|
//the Canvas Group for the dialog box |
|
public CanvasGroup dialogBox; |
|
|
|
//the image holder |
|
public Image imageHolder; |
|
|
|
//the text holder |
|
public Text textHolder; |
|
|
|
//Guarantee this will always be a singleton only - can't use the constructor! |
|
protected ConversationManager() { } |
|
|
|
/// <summary> |
|
/// Starts the conversation by getting a reference to the dialog UI and then calling a |
|
/// coroutine to show the content of the conversation. |
|
/// </summary> |
|
/// <param name="conversation">Conversation.</param> |
|
public void StartConversation(Conversation conversation) |
|
{ |
|
dialogBox = GameObject.Find("Dialog Box").GetComponent<CanvasGroup>(); |
|
imageHolder = GameObject.Find("Speaker Image").GetComponent<Image>(); |
|
textHolder = GameObject.Find("Dialog Text").GetComponent<Text>(); |
|
//Start displying the supplied conversation |
|
if (!talking) |
|
{ |
|
StartCoroutine(DisplayConversation(conversation)); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// This coroutine simply iterates through each of the entries in the conversation and displays the content |
|
/// of the entry in the dialog UI. It waits 3 seconds between entries. This is a very simple implementation of |
|
/// a conversation that does not maintain state or allow for player input or phrase options. |
|
/// </summary> |
|
/// <returns>The conversation.</returns> |
|
/// <param name="conversation">Conversation.</param> |
|
IEnumerator DisplayConversation(Conversation conversation) |
|
{ |
|
talking = true; |
|
foreach (var conversationLine in conversation.ConversationLines) |
|
{ |
|
currentConversationLine = conversationLine; |
|
textHolder.text = currentConversationLine.ConversationText; |
|
imageHolder.sprite = currentConversationLine.DisplayPic; |
|
yield return new WaitForSeconds(3); |
|
} |
|
talking = false; |
|
} |
|
|
|
/// <summary> |
|
/// This method handles showing and hiding the dialog box for the conversation. It is a |
|
/// simple alpha switch from visible to invisible. |
|
/// </summary> |
|
void OnGUI() |
|
{ |
|
if (talking) |
|
{ |
|
dialogBox.alpha = 1; |
|
dialogBox.blocksRaycasts = true; |
|
} |
|
else |
|
{ |
|
dialogBox.alpha = 0; |
|
dialogBox.blocksRaycasts = false; |
|
} |
|
} |
|
} |