Advertisement
Guest User

Dialogue Manager

a guest
Sep 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5. using System;
  6.  
  7. public class DialogueManager : MonoBehaviour {
  8.     [SerializeField]
  9.     DialogueParser parser;
  10.  
  11.     string dialogue, characterName;
  12.     int lineNum;
  13.     int pose;
  14.     string location;
  15.     int bg;
  16.  
  17.     public Text dialogueBox;
  18.     public Text nameBox;
  19.     public Text log;
  20.    
  21.     // Use this for initialization
  22.     void Start () {
  23.         dialogue = "";
  24.         characterName = "";
  25.         pose = 0;
  26.         location = "N";
  27.         lineNum = 0;
  28.         bg = parser.GetBackground(0);
  29.  
  30.         dialogueBox.text = "";
  31.         ShowDialogue ();
  32.  
  33.         lineNum++;
  34.             }
  35.    
  36.     // Update is called once per frame
  37.     void Update () {
  38.      
  39.         UpdateUI();
  40.         parser.EndScene(lineNum);
  41.     }
  42.  
  43.     public void PlayNovel()
  44.     {
  45.         if (!GameObject.Find("CheckPanel") && (!GameObject.Find("LogPanel")))
  46.         {
  47. //            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended ||
  48. //      Input.GetMouseButtonDown(0))
  49. //            {
  50.                 SetBackground();
  51.                 ShowDialogue();
  52.                 lineNum++;
  53. //            }
  54.         }
  55.     }
  56.  
  57.     public void SetBackground()
  58.     {
  59.         bg = parser.GetBackground(lineNum);
  60.         GameObject Background = GameObject.Find("background");
  61.         SpriteRenderer currSprite = Background.GetComponent<SpriteRenderer>();
  62.         currSprite.sprite = Background.GetComponent<BgController>().background[bg];
  63.  
  64.     }    
  65.  
  66.     public void ShowDialogue()
  67.     {
  68.  
  69.         if (parser.GetLocation(lineNum) != "C" && parser.GetLocation(lineNum) != "N")
  70.         {
  71.             ResetImages();
  72.         }
  73.    
  74.         ParserLine();
  75.  
  76.     }
  77.  
  78.  
  79.     void ResetImages()
  80.     {
  81.         if (characterName != "" && parser.GetName(lineNum) == "Player")
  82.         {
  83.             GameObject character = GameObject.Find(characterName);
  84.             SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
  85.             currSprite.sprite = null;
  86.         }
  87.     }
  88.  
  89.     void ParserLine()
  90.     {
  91.         if (parser.GetName(lineNum) != "Player" && parser.GetName(lineNum) != "Narration")
  92.         {
  93.             characterName = parser.GetName(lineNum);
  94.             dialogue = parser.GetContent(lineNum);
  95.             pose = parser.GetPose(lineNum);
  96.             location = parser.GetLocation(lineNum);
  97.             DisplayImages();
  98.             log.text += characterName + ":" + dialogue + "\n\n";
  99.         }
  100.         else if (parser.GetName(lineNum) == "Player")
  101.         {
  102.             GameObject player = GameObject.Find ("Player");
  103.             characterName = player.GetComponent<PlayerData> ().PlayerName ();
  104.             dialogue = parser.GetContent(lineNum);
  105.             pose = 0;
  106.             log.text += characterName + ":" + dialogue + "\n\n";
  107.         }
  108.         else if(parser.GetName(lineNum) == "Narration")
  109.         {
  110.             characterName = "";
  111.             dialogue = parser.GetContent(lineNum);
  112.             pose = 0;
  113.             log.text += dialogue + "\n\n";
  114.         }
  115.        
  116.  
  117.         if (!parser.IsEnd(lineNum))
  118.         {
  119.             StartCoroutine(TypeText());
  120.             dialogueBox.text = "";
  121.         }        
  122.  
  123.     }
  124.  
  125.    
  126.     void DisplayImages()
  127.     {
  128.         if (characterName != null && parser.GetName(lineNum) == "Player")
  129.         {
  130.             GameObject character = GameObject.Find(characterName);
  131.  
  132.             SetSpritePositions(character);
  133.  
  134.             SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
  135.             currSprite.sprite = character.GetComponent<Character>().CharacterPoses[pose];
  136.         }
  137.     }
  138.  
  139.  
  140.     void SetSpritePositions(GameObject spriteObj)
  141.     {
  142.         if (location == "L")
  143.         {
  144.             spriteObj.transform.position = new Vector3(-6, 0);
  145.         }
  146.         else if (location == "R")
  147.         {
  148.             spriteObj.transform.position = new Vector3(6, 0);
  149.         }
  150.         else if (location == "C")
  151.         {
  152.             spriteObj.transform.position = new Vector3(0, 0);
  153.         }
  154.  
  155.             spriteObj.transform.position = new Vector3 (spriteObj.transform.
  156.             position.x, spriteObj.transform.position.y, 0);
  157.     }
  158.        
  159.     void UpdateUI()
  160.     {
  161.         nameBox.text = characterName;
  162.     }
  163.  
  164.     IEnumerator TypeText()
  165.     {
  166.             foreach (char letter in dialogue.ToCharArray())
  167.             {
  168.                 dialogueBox.text += letter;
  169.                 yield return 0;
  170.  
  171.                 if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended ||
  172.     Input.GetMouseButtonDown(0))
  173.                 {
  174.                     yield break;
  175.  
  176.                 }
  177.             }  
  178.     }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement