123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Xml.Serialization;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class npcScript : MonoBehaviour
- {
- public GameObject npcPanel;
- public GameObject textBtn;
- public TMP_Text npcText;
- // public string[] texts;
- public QuestScriptable [] questData;
- public int activeQuest;
- private int index;
- //public GameObject questUI;
- public float textspeed = 0.10f;
- public bool isPlayerClose;
- void Update()
- {
- if(isPlayerClose){
- // if(npcPanel.activeInHierarchy){
- // ResetTexts();
- // }else{
- // npcPanel.SetActive(true);
- // StartCoroutine(textLoad());
- // }
- }
- if(npcText.text == questData[activeQuest].questLines[index]){
- textBtn.SetActive(true);
- }
- }
- public void ResetTexts(){
- npcPanel.SetActive(false);
- npcText.text = "";
- index = 0;
- }
- bool isLooping = false;
- IEnumerator textLoad(){
- isLooping = true;
- foreach(char letter in questData[activeQuest].questLines[index].ToCharArray()){
- npcText.text += letter;
- yield return new WaitForSecondsRealtime(textspeed);
- }
- isLooping=false;
- }
- Coroutine textLoopAsync ;
- public void NextLine(){
- textBtn.SetActive(false);
- if(index < questData[activeQuest].questLines.Length -1 ){
- index++;
- npcText.text = "";
- LoadText();
- }else{
- ResetTexts();
- //Start quest
- // questUI.SetActive(true);
- playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().QuestFunction(questData[activeQuest]);
- }
- }
- private void OnTriggerEnter2D(Collider2D other) {
- if(other.CompareTag("Player")){
-
- if(other.transform == playerNetwork.localPlayerTransform){
- if(playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().currentQuest == questData[activeQuest]){
- 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;
- }
- }
- isPlayerClose = true;
- if(npcPanel.activeInHierarchy){
- ResetTexts();
- }else{
- npcPanel.SetActive(true);
- LoadText();
- }
- }
-
- }
- }
- void LoadText(){
- npcText.text = "";
- if(textLoopAsync!=null){
- StopCoroutine(textLoopAsync);
- }
- textLoopAsync = StartCoroutine(textLoad());
- }
- private void OnTriggerExit2D(Collider2D other) {
- if(other.CompareTag("Player")){
- if(other.transform == playerNetwork.localPlayerTransform){
- isPlayerClose = false;
- ResetTexts();
- }
- }
- }
- }
|