public void ImportNodes() { StreamReader sr = new StreamReader(fileDir + fileNodes); string dataLine = sr.ReadLine(); //initial gets headers and throw them string[] readData; string[] storyGlobals; string[] storyGlobal; string[] continuationPairs; string[] continuationPair; while ((dataLine = sr.ReadLine()) != null) { if (dataLine == "") { continue; } readData = dataLine.Split(";"); NodeStory Node = new NodeStory() { ID = readData[0], text = readData[1], storyGlobals = new Dictionary <string, string>(), continuations = new Dictionary <string, float>() }; //keep going with more complex ones; if (readData[2] != "") { storyGlobals = readData[2].Split(","); foreach (string s in storyGlobals) { storyGlobal = s.Split(":"); Node.storyGlobals[storyGlobal[0]] = storyGlobal[1]; } } if (readData[3] != "") { continuationPairs = readData[3].Split(","); foreach (string s in continuationPairs) { continuationPair = s.Split(":"); Node.continuations[continuationPair[0]] = float.Parse(continuationPair[1], CultureInfo.InvariantCulture); } } Nodes[Node.ID] = Node; } }
void Traverse(NodeStory node) { //Set new globals foreach (KeyValuePair <string, string> kvp in node.storyGlobals) { Globals[kvp.Key] = kvp.Value; } //Time to handle text; string toWrite = node.text.Replace(@"\N", "\n"); //for some reason imported escape characters are not recognised, so we upCase and work around. foreach (KeyValuePair <string, string> kvp in Globals) { toWrite = toWrite.Replace(kvp.Key, kvp.Value); //not very memoryfriendly I know, not in-place } Console.Write(toWrite); //check continuations if (node.continuations.Count == 0) { Console.WriteLine("\n\n\n"); return; } else { string next = ""; double rndNext = rnd.NextDouble(); double sum = 0; foreach (KeyValuePair <string, float> kvp in node.continuations) { sum += kvp.Value; if (rndNext < sum) { next = kvp.Key; break; } } NodeStory nextNode = repository.GetNodeByID(next); Traverse(nextNode); } }