123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Xml.Serialization;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class npcScript : MonoBehaviour
- {
- public static npcScript activeNpc;
- public GameObject npcPanel;
- public GameObject textBtn;
- public TMP_Text npcText;
- // public string[] texts;
- public QuestScriptable[] questData;
- public int activeQuest=0;
- public int questLineIndex;
- //public GameObject questUI;
- public float textspeed = 0.10f;
- public bool isPlayerClose;
- void Update()
- {
- if (isPlayerClose)
- {
- }
- if (npcText.text == questData[activeQuest].questLines[questLineIndex])
- {
- textBtn.SetActive(true);
- }
- }
- bool isLoadingText = false;
- IEnumerator textLoad()
- {
- isLoadingText = true;
- Debug.Log($"Reading line {questLineIndex} from quest {activeQuest}", gameObject);
- foreach (char letter in questData[activeQuest].questLines[questLineIndex].ToCharArray())
- {
- npcText.text += letter;
- yield return new WaitForSecondsRealtime(textspeed);
- }
- isLoadingText = false;
- }
- Coroutine textLoopAsync;
- public void NextLine()
- {
- textBtn.SetActive(false);
- if (questLineIndex < questData[activeQuest].questLines.Length - 1)
- {
- questLineIndex++;
- npcText.text = "";
- LoadText();
- }
- else
- {
- ResetTexts();
- //Start quest
- // questUI.SetActive(true);
- playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().SetActiveQuest(questData[activeQuest]);
- }
- }
- void ResetTexts(){
- npcText.text = "";
- npcPanel.SetActive(false);
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.CompareTag("Player"))
- {
- Debug.Log($"Player entered into {gameObject.name}",gameObject);
- if (other.transform == playerNetwork.localPlayerTransform)
- {
- playerNetwork pnet = playerNetwork.localPlayerTransform.GetComponent<playerNetwork>();
- if (pnet.currentQuest == questData[activeQuest]) //Already doing this quest
- {
- return;
- }
- // for (int i = 0; i < questData.Length; i++)
- // {
- // bool isFound = false;
- // foreach (string questName in playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().completedQuests)
- // {
- // if (questName == questData[i].name)
- // {
- // isFound = true;
- // }
- // }
- // if (!isFound)
- // {
- // activeQuest = i;
- // break;
- // }
- // }
- if(pnet.completedQuests.Contains(questData[activeQuest].questName)){ //Set next quest if current one is completed
- for(int i=0; i < questData.Length; i++){
- if(pnet.completedQuests.Contains(questData[i].questName)){
- continue;
- }
- activeQuest = i;
- break;
- }
- }
- isPlayerClose = true;
- activeNpc= this;
- if (npcPanel.activeInHierarchy)
- {
- ResetTexts();
- }
- else
- {
- questLineIndex=0;
- npcPanel.SetActive(true);
- LoadText();
- }
- }
- }
- }
- void LoadText()
- {
- if(!isPlayerClose){
- Debug.Log("This player aint in my area", gameObject);
- return;
- }
- npcText.text = "";
- if (textLoopAsync != null)
- {
- StopCoroutine(textLoopAsync);
- }
- Debug.Log("request quest line text load",gameObject);
- textLoopAsync = StartCoroutine(textLoad());
- }
- private void OnTriggerExit2D(Collider2D other)
- {
- if (other.CompareTag("Player"))
- {
- if (other.transform == playerNetwork.localPlayerTransform)
- {
- isPlayerClose = false;
- ResetTexts();
- }
- }
- }
- }
|