Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using System.Collections.Generic;
- using System;
- public class DialogueManager : MonoBehaviour {
- [SerializeField]
- DialogueParser parser;
- string dialogue, characterName;
- int lineNum;
- int pose;
- string location;
- int bg;
- public Text dialogueBox;
- public Text nameBox;
- public Text log;
- // Use this for initialization
- void Start () {
- dialogue = "";
- characterName = "";
- pose = 0;
- location = "N";
- lineNum = 0;
- bg = parser.GetBackground(0);
- dialogueBox.text = "";
- ShowDialogue ();
- lineNum++;
- }
- // Update is called once per frame
- void Update () {
- UpdateUI();
- parser.EndScene(lineNum);
- }
- public void PlayNovel()
- {
- if (!GameObject.Find("CheckPanel") && (!GameObject.Find("LogPanel")))
- {
- // if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended ||
- // Input.GetMouseButtonDown(0))
- // {
- SetBackground();
- ShowDialogue();
- lineNum++;
- // }
- }
- }
- public void SetBackground()
- {
- bg = parser.GetBackground(lineNum);
- GameObject Background = GameObject.Find("background");
- SpriteRenderer currSprite = Background.GetComponent<SpriteRenderer>();
- currSprite.sprite = Background.GetComponent<BgController>().background[bg];
- }
- public void ShowDialogue()
- {
- if (parser.GetLocation(lineNum) != "C" && parser.GetLocation(lineNum) != "N")
- {
- ResetImages();
- }
- ParserLine();
- }
- void ResetImages()
- {
- if (characterName != "" && parser.GetName(lineNum) == "Player")
- {
- GameObject character = GameObject.Find(characterName);
- SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
- currSprite.sprite = null;
- }
- }
- void ParserLine()
- {
- if (parser.GetName(lineNum) != "Player" && parser.GetName(lineNum) != "Narration")
- {
- characterName = parser.GetName(lineNum);
- dialogue = parser.GetContent(lineNum);
- pose = parser.GetPose(lineNum);
- location = parser.GetLocation(lineNum);
- DisplayImages();
- log.text += characterName + ":" + dialogue + "\n\n";
- }
- else if (parser.GetName(lineNum) == "Player")
- {
- GameObject player = GameObject.Find ("Player");
- characterName = player.GetComponent<PlayerData> ().PlayerName ();
- dialogue = parser.GetContent(lineNum);
- pose = 0;
- log.text += characterName + ":" + dialogue + "\n\n";
- }
- else if(parser.GetName(lineNum) == "Narration")
- {
- characterName = "";
- dialogue = parser.GetContent(lineNum);
- pose = 0;
- log.text += dialogue + "\n\n";
- }
- if (!parser.IsEnd(lineNum))
- {
- StartCoroutine(TypeText());
- dialogueBox.text = "";
- }
- }
- void DisplayImages()
- {
- if (characterName != null && parser.GetName(lineNum) == "Player")
- {
- GameObject character = GameObject.Find(characterName);
- SetSpritePositions(character);
- SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
- currSprite.sprite = character.GetComponent<Character>().CharacterPoses[pose];
- }
- }
- void SetSpritePositions(GameObject spriteObj)
- {
- if (location == "L")
- {
- spriteObj.transform.position = new Vector3(-6, 0);
- }
- else if (location == "R")
- {
- spriteObj.transform.position = new Vector3(6, 0);
- }
- else if (location == "C")
- {
- spriteObj.transform.position = new Vector3(0, 0);
- }
- spriteObj.transform.position = new Vector3 (spriteObj.transform.
- position.x, spriteObj.transform.position.y, 0);
- }
- void UpdateUI()
- {
- nameBox.text = characterName;
- }
- IEnumerator TypeText()
- {
- foreach (char letter in dialogue.ToCharArray())
- {
- dialogueBox.text += letter;
- yield return 0;
- if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended ||
- Input.GetMouseButtonDown(0))
- {
- yield break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement