Advertisement
SimonDarksideJ

MU2D - Updated conversation manager - setting completed flag

Aug 29th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. public class ConversationManager : Singleton<ConversationManager > {
  4.     protected ConversationManager() { } // guarantee this will be always a singleton only - can't use the constructor!
  5.  
  6.     //Is there a converastion going on
  7.     bool talking = false;
  8.  
  9.     //The current line of text being displayed
  10.     ConversationEntry currentConversationLine;
  11.  
  12.     //Estimated width of characters in the font
  13.     int fontSpacing = 7;
  14.  
  15.     //How wide does the dialog window need to be
  16.     int conversationTextWidth;
  17.  
  18.     //How high does the dialog window need to be
  19.     int dialogHeight = 70;
  20.  
  21.     //Offset space needed for character image
  22.     public int displayTextureOffset = 70;
  23.  
  24.     //Scaled image rectangle for displaying character image
  25.     Rect scaledTextureRect;
  26.  
  27.     public void StartConversation(Conversation conversation)
  28.     {
  29.         //Start displying the supplied conversation
  30.         if (!talking)
  31.         {
  32.             StartCoroutine(DisplayConversation(conversation));
  33.         }
  34.     }
  35.  
  36.     IEnumerator DisplayConversation(Conversation conversation)
  37.     {
  38.         talking = true;
  39.         foreach (var conversationLine in conversation.ConversationLines)
  40.         {
  41.             currentConversationLine = conversationLine;
  42.             conversationTextWidth = currentConversationLine.ConversationText.Length * fontSpacing;
  43.             scaledTextureRect = new Rect(
  44.                 currentConversationLine.DisplayPic.textureRect.x / currentConversationLine.DisplayPic.texture.width,
  45.                 currentConversationLine.DisplayPic.textureRect.y / currentConversationLine.DisplayPic.texture.height,
  46.                 currentConversationLine.DisplayPic.textureRect.width / currentConversationLine.DisplayPic.texture.width,
  47.                 currentConversationLine.DisplayPic.textureRect.height / currentConversationLine.DisplayPic.texture.height);
  48.             yield return new WaitForSeconds(3);
  49.         }
  50.         talking = false;
  51.         conversation.Complete = true;
  52.         yield return null;
  53.     }
  54.  
  55.  
  56.     void OnGUI()
  57.     {
  58.         if (talking)
  59.         {
  60.             //layout start
  61.             GUI.BeginGroup(new Rect(Screen.width / 2 - conversationTextWidth / 2, 50, conversationTextWidth + displayTextureOffset + 10, dialogHeight));
  62.  
  63.             //the background box
  64.             GUI.Box(new Rect(0, 0, conversationTextWidth + displayTextureOffset + 10, dialogHeight), "");
  65.  
  66.             //the character name
  67.             GUI.Label(new Rect(displayTextureOffset, 10, conversationTextWidth + 30, 20), currentConversationLine.SpeakingCharacterName);
  68.  
  69.             //the conversation text
  70.             GUI.Label(new Rect(displayTextureOffset, 30, conversationTextWidth + 30, 20), currentConversationLine.ConversationText);
  71.  
  72.             //the character image
  73.             GUI.DrawTextureWithTexCoords(new Rect(10, 10, 50, 50), currentConversationLine.DisplayPic.texture, scaledTextureRect);
  74.  
  75.             //layout end
  76.             GUI.EndGroup();
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement