/// <summary> /// Used by outside scripts to set voicelines of NPC's reaction to an event /// Particularly events such as the player entering a certain area /// </summary> /// <param name="npcvl"></param> public void setTriggerVoiceLine(NPCVoiceLine npcvl) { int index = voiceLines.IndexOf(npcvl); if (currentVoiceLineIndex != index || !source.isPlaying) { setVoiceLine(index); } }
public int getMostRelevantVoiceLineIndex() { for (int i = voiceLines.Count - 1; i > lastPlayedCheckPointLineIndex; i--) { NPCVoiceLine npcvl = voiceLines[i]; if (!npcvl.triggerLine && !npcvl.played && GameEventManager.eventHappened(npcvl.eventReq) && (!npcvl.hasExcludeRequirement() || !GameEventManager.eventHappened(npcvl.eventReqExclude))) { return(i); } else if (npcvl.played && npcvl.checkPointLine) { return(-1); } } return(-1); }
// Update is called once per frame void Update() { source.transform.position = transform.position; //Debug.Log("Number things found: " + thingsFound); if (canGreet()) { if (!source.isPlaying) { int mrvli = getMostRelevantVoiceLineIndex(); if (mrvli >= 0) { setVoiceLine(mrvli); NPCVoiceLine npcvl = voiceLines[mrvli]; npcvl.played = true; lastPlayedCheckPointLineIndex = mrvli; if (npcvl.triggerEvent != null) { GameEventManager.addEvent(npcvl.triggerEvent); } } } } else { if (shouldStop()) { source.Stop(); } } if (source.isPlaying) { string voicelinetext = voiceLines[currentVoiceLineIndex].getVoiceLineText(source.time); NPCManager.speakNPC(gameObject, true, voicelinetext); } else if (currentVoiceLineIndex >= 0) { currentVoiceLineIndex = -1; NPCManager.speakNPC(gameObject, false, ""); } }
// Use this for initialization void Start() { lineToTrigger = controller.voiceLines[lineToTriggerIndex]; }
void refreshVoiceLines() { if (lineFileName != null && lineFileName != "") { voiceLines = new List <NPCVoiceLine>();//2017-09-05 ommitted until text files are filled out int writeIndex = -1; //2017-09-05: copied from an answer by Drakestar: http://answers.unity3d.com/questions/279750/loading-data-from-a-txt-file-c.html try { string line; StreamReader theReader = new StreamReader("Assets/Resources/Dialogue/" + lineFileName, Encoding.Default); using (theReader) { do { line = theReader.ReadLine(); if (line != null) { if (line.StartsWith(":")) { writeIndex++; NPCVoiceLine npcvl = new NPCVoiceLine(); voiceLines.Add(npcvl); } else if (line.StartsWith("audio:")) { string audioPath = line.Substring("audio:".Length).Trim(); voiceLines[writeIndex].voiceLine = Resources.Load <AudioClip>("Dialogue/" + audioPath); } else if (line.StartsWith("text:")) { string text = line.Substring("text:".Length).Trim(); voiceLines[writeIndex].voiceLineText = text; if (voiceLines[writeIndex].lineSegments.Count == 0) { voiceLines[writeIndex].lineSegments.Add(new NPCVoiceLine.Line(text)); } } else if (line.StartsWith("segments:")) { string segmentText = line.Substring("segments:".Length).Trim(); voiceLines[writeIndex].lineSegments.Clear(); string voiceLineText = voiceLines[writeIndex].voiceLineText; foreach (string s in segmentText.Split('>')) { string[] strs = s.Trim().Split(' '); NPCVoiceLine.Line lineSegment = new NPCVoiceLine.Line(strs[0], float.Parse(strs[1])); voiceLines[writeIndex].lineSegments.Add(lineSegment); voiceLineText = lineSegment.bite(voiceLineText); } //Add a dummy line segment for text animation purposes voiceLines[writeIndex].lineSegments.Add(new NPCVoiceLine.Line(null, voiceLines[writeIndex].voiceLine.length)); } else if (line.StartsWith("req:")) { string eventName = line.Substring("req:".Length).Trim(); voiceLines[writeIndex].eventReq = eventName; } else if (line.StartsWith("exclude:")) { string eventName = line.Substring("exclude:".Length).Trim(); voiceLines[writeIndex].eventReqExclude = eventName; } else if (line.StartsWith("event:")) { string eventName = line.Substring("event:".Length).Trim(); voiceLines[writeIndex].triggerEvent = eventName; } else if (line.StartsWith("cpl:")) { bool cpSetting = bool.Parse(line.Substring("cpl:".Length).Trim()); voiceLines[writeIndex].checkPointLine = cpSetting; } else if (line.StartsWith("trigger:")) { bool triggerSetting = bool.Parse(line.Substring("trigger:".Length).Trim()); voiceLines[writeIndex].triggerLine = triggerSetting; } } }while (line != null); theReader.Close(); } } // If anything broke in the try block, we throw an exception with information // on what didn't work catch (System.Exception e) { Debug.LogError("{0} lineFileName: " + lineFileName + "\n>>>" + e.Message + "\n" + e.StackTrace); } } }
void refreshVoiceLines() { if (lineFileName != null && lineFileName != "") { voiceLines = new List <NPCVoiceLine>();//2017-09-05 ommitted until text files are filled out int writeIndex = -1; //2017-09-05: copied from an answer by Drakestar: http://answers.unity3d.com/questions/279750/loading-data-from-a-txt-file-c.html try { string lineFileFSPath = "Assets/Resources/Dialogue/" + lineFileName; // Relative path to linefile on the filesystem. List <string> fileLines; // Array of script file lines. // Attempt to read the data file from the local filesystem. If it doesn't exist fall back to the // packed assets. This is to allow overriding base behaviors with custom client-side ones. if (File.Exists(lineFileFSPath)) { StreamReader theReader = new StreamReader(lineFileFSPath, Encoding.Default); fileLines = new List <string>(theReader.ReadToEnd().Split('\n')); theReader.Close(); } else { TextAsset internalFile = Resources.Load <TextAsset>("Dialogue/" + lineFileName.Split('.')[0]); Debug.Assert(internalFile != null, "Could not load fallback NPC text script assumed to be located at Dialogue/" + lineFileName + "!"); fileLines = new List <string>(internalFile.ToString().Split('\n')); } foreach (string line in fileLines) { if (line.StartsWith(":")) { writeIndex++; NPCVoiceLine npcvl = new NPCVoiceLine(); voiceLines.Add(npcvl); } else if (line.StartsWith("audio:")) { string audioPath = line.Substring("audio:".Length).Trim(); voiceLines[writeIndex].voiceLine = Resources.Load <AudioClip>("Dialogue/" + audioPath); } else if (line.StartsWith("text:")) { string text = line.Substring("text:".Length).Trim(); voiceLines[writeIndex].voiceLineText = text; if (voiceLines[writeIndex].lineSegments.Count == 0) { voiceLines[writeIndex].lineSegments.Add(new NPCVoiceLine.Line(text)); } } else if (line.StartsWith("segments:")) { string segmentText = line.Substring("segments:".Length).Trim(); voiceLines[writeIndex].lineSegments.Clear(); string voiceLineText = voiceLines[writeIndex].voiceLineText; foreach (string s in segmentText.Split('>')) { string[] strs = s.Trim().Split(' '); NPCVoiceLine.Line lineSegment = new NPCVoiceLine.Line(strs[0], float.Parse(strs[1])); voiceLines[writeIndex].lineSegments.Add(lineSegment); voiceLineText = lineSegment.bite(voiceLineText); } //Add a dummy line segment for text animation purposes voiceLines[writeIndex].lineSegments.Add(new NPCVoiceLine.Line(null, voiceLines[writeIndex].voiceLine.length)); } else if (line.StartsWith("req:")) { string eventName = line.Substring("req:".Length).Trim(); voiceLines[writeIndex].eventReq = eventName; } else if (line.StartsWith("exclude:")) { string eventName = line.Substring("exclude:".Length).Trim(); voiceLines[writeIndex].eventReqExclude = eventName; } else if (line.StartsWith("event:")) { string eventName = line.Substring("event:".Length).Trim(); voiceLines[writeIndex].triggerEvent = eventName; } else if (line.StartsWith("cpl:")) { bool cpSetting = bool.Parse(line.Substring("cpl:".Length).Trim()); voiceLines[writeIndex].checkPointLine = cpSetting; } else if (line.StartsWith("trigger:")) { bool triggerSetting = bool.Parse(line.Substring("trigger:".Length).Trim()); voiceLines[writeIndex].triggerLine = triggerSetting; } } } // If anything broke in the try block, we throw an exception with information // on what didn't work catch (System.Exception e) { Debug.LogError("{0} lineFileName: " + lineFileName + "\n>>>" + e.Message + "\n" + e.StackTrace); } } }