Advertisement
AndrewRosyaev

Dialogue.cs

Aug 30th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Dialogue : MonoBehaviour
  6. {
  7.     public Transform NpcTransform;
  8.  
  9.     bool active;
  10.     public Transform DialogueInterface;
  11.     public float offset;
  12.     bool uiActiveFollow;
  13.     int uistate;
  14.     private Player player;
  15.     public string DialogID;
  16.     public TextAsset textRU;
  17.     public TextAsset textEN;
  18.     float DialTimeout;
  19.     DialogUI dialui;
  20.     public DialogueAnimSync[] SfxTr;
  21.     public Runner runner;
  22.  
  23.     public bool Deactive;
  24.     public PressE DialogueUItr;
  25.  
  26.     void Start ()
  27.     {
  28.         dialui = DialogueInterface.GetComponent<DialogUI> ();
  29.         DialTimeout = 2;
  30.     }
  31.  
  32.     void Update ()
  33.     {
  34.         if (uiActiveFollow)
  35.         {
  36.             DialogueInterface.position = Camera.main.WorldToScreenPoint (transform.position + new Vector3 (0, offset, 0));
  37.         }
  38.         if (DialTimeout < 2)
  39.         {
  40.             DialTimeout += Time.deltaTime;
  41.         }
  42.     }
  43.  
  44.     public void OnTriggerEnter2D(Collider2D col)
  45.     {
  46.         if (col.transform.CompareTag ("Player") && active == false&&DialTimeout>=2&&Deactive==false)
  47.         {
  48.             player = col.GetComponent<Player> ();
  49.             if (NpcTransform.GetComponent<BackgroundNpcPatrol> ())
  50.             {
  51.                 BackgroundNpcPatrol NpcScript = NpcTransform.GetComponent<BackgroundNpcPatrol> ();
  52.                 NpcScript.Walk = false;
  53.                 NpcScript.anim.SetBool ("Run", false);
  54.  
  55.                 if (NpcScript.transform.position.x < player.transform.position.x)
  56.                 {
  57.                     NpcScript.transform.localScale = new Vector3 (1, 1, 1);
  58.                 } else
  59.                 {
  60.                     NpcScript.transform.localScale = new Vector3 (-1, 1, 1);
  61.                 }
  62.             }
  63.             if (DialogueUItr == null)
  64.             {
  65.                 EnableDialogue ();
  66.             }
  67.             else
  68.             {
  69.                 DialogueUItr.active = true;
  70.                 DialogueUItr.uiActiveFollow = true;
  71.                 DialogueUItr.uistate = 1;
  72.                 DialogueUItr.target = NpcTransform;
  73.                 DialogueUItr.dialogueTr = this;
  74.             }
  75.         }
  76.     }
  77.  
  78.     public void EnableDialogue()
  79.     {
  80.         active = true;
  81.         dialui.uistate = 1;
  82.  
  83.         //Say (1);
  84.         uiActiveFollow = true;
  85.         CancelInvoke ("DeactivatePosUiFollow");
  86.         player.DisableControl = true;
  87.         GameObject.FindObjectOfType<MainMenu> ().MenuCursorEnable ();
  88.         if (SfxTr != null)
  89.         {
  90.             //Instantiate (SfxTr[PlayerPrefs.GetInt(DialogID+"Replica")], transform.position, transform.rotation);
  91.             Instantiate (SfxTr[PlayerPrefs.GetInt(DialogID+"Replica")].ReplicaSFX, transform.position, transform.rotation);
  92.             NpcTransform.GetComponent<Animator>().Play(SfxTr[PlayerPrefs.GetInt(DialogID+"Replica")].ReplicaAnim);
  93.         }
  94.  
  95.         if (PlayerPrefs.GetInt ("SelectionLanguage") == 0)
  96.         {
  97.             dialui.currentTextFile = textEN;
  98.             if (textEN == null)
  99.             {
  100.                 dialui.currentTextFile = textRU;
  101.             }
  102.         }
  103.         if (PlayerPrefs.GetInt ("SelectionLanguage") == 1)
  104.         {
  105.             dialui.currentTextFile = textRU;
  106.         }
  107.  
  108.         dialui.DialogID = DialogID;
  109.         dialui.dialogueNpc = this;
  110.         dialui.LoadText ();
  111.  
  112.         if (runner != null)
  113.         {
  114.             runner.MovementEventState = 0;
  115.         }
  116.     }
  117.  
  118.     public void Say(int SayNumber)
  119.     {
  120.         if (SayNumber == 1)
  121.         {
  122.             StartCoroutine ("AnimOnce", "Say");
  123.         }
  124.     }
  125.  
  126.     public IEnumerator AnimOnce(string anim)
  127.     {
  128.         NpcTransform.GetComponent<Animator>().SetBool (anim, true);
  129.         yield return new WaitForSeconds (0.2f);
  130.         NpcTransform.GetComponent<Animator>().SetBool (anim, false);
  131.     }  
  132.  
  133.     public void OnTriggerExit2D(Collider2D col)
  134.     {
  135.         if (col.transform.CompareTag ("Player") )
  136.         {
  137.             if (active)
  138.             {
  139.                 DeactivateDialog ();
  140.             }
  141.             if (DialogueUItr != null)
  142.             {
  143.                 DialogueUItr.Deactivate ();
  144.             }
  145.         }
  146.     }
  147.  
  148.     public void DeactivateDialog()
  149.     {
  150.         active = false;
  151.         dialui.uistate = 2;
  152.  
  153.         if (NpcTransform.GetComponent<BackgroundNpcPatrol> ())
  154.         {
  155.             BackgroundNpcPatrol NpcScript = NpcTransform.GetComponent<BackgroundNpcPatrol> ();
  156.             NpcScript.Walk = true;
  157.             NpcScript.GetPatrolPoint ();
  158.         }
  159.         Invoke ("DeactivatePosUiFollow", 1);
  160.         player.DisableControl = false;
  161.         GameObject.FindObjectOfType<MainMenu> ().MenuCursorDisable ();
  162.         DialTimeout = 0;
  163.    
  164.     }
  165.  
  166.     public void DeactivatePosUiFollow()
  167.     {
  168.         uiActiveFollow = false;
  169.     }
  170. }
  171.  
  172. [System.Serializable]
  173. public class DialogueAnimSync
  174. {
  175.     public Transform ReplicaSFX;
  176.     public string ReplicaAnim;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement