private void comboCharacters_SelectedIndexChanged(object sender, EventArgs e) { Character curChar = (Character)comboCharacters.SelectedItem; curFile = Speaker.GetXmlSpeaks(curChar.pathSpeaksFile); Speak newSpeak = Speaker.GetSpeak(0, curFile); //получаем первую фразу, которую говорит персонаж npcText.Text = newSpeak.npcText; listAnswers.DataSource = newSpeak.answers; listAnswers.DisplayMember = "textUser"; listAnswers.ValueMember = "idNextSpeak"; }
//Выбираем ответ private void btnAnswer_Click(object sender, EventArgs e) { Answer nextAnswer = (Answer)listAnswers.SelectedItem; if (nextAnswer.isExit != true) { Speak newSpeak = Speaker.GetSpeak(nextAnswer.idNextSpeak, curFile); npcText.Text = newSpeak.npcText; listAnswers.DataSource = newSpeak.answers; } else { MessageBox.Show("Диалог окончен"); } }
//получить рандомно реплику из списка /// <summary> /// Получить фразу с ответами по идентификатору /// </summary> /// <param name="spId">Идентификатор диалога</param> /// <param name="doc">Файл с фразами</param> /// <returns></returns> public static Speak GetSpeak(int spId, XmlDocument doc) { Speak res = new Speak(); XmlElement curSpeak = doc.DocumentElement; string xpath = "//Speak[@id=" + spId + "]"; XmlNodeList speakNodes = curSpeak.SelectNodes(xpath); XmlNode speakNode; //если несколько вариантов диалога, то выбираем рандомно if (speakNodes.Count > 1) { Random rnd = new Random(); double p = 1.0 / speakNodes.Count; //вероятность попадания в один из вариантов double pElem = rnd.NextDouble(); int ind = int.Parse(Math.Round(pElem / p).ToString()); speakNode = speakNodes.Item(ind); } else { speakNode = speakNodes.Item(0); } res.npcText = speakNode.Attributes["npcText"].Value; if (speakNode.SelectNodes("answer").Count != 0) { XmlNodeList answersXml = speakNode.SelectNodes("answer"); res.answers = new List <Answer>(); for (int i = 0; i < answersXml.Count; i++) { Answer a = new Answer(); a.textUser = answersXml.Item(i).Attributes["text"].Value; a.idNextSpeak = int.Parse(answersXml.Item(i).Attributes["speakId"].Value == ""?"0": answersXml.Item(i).Attributes["speakId"].Value); a.isExit = bool.Parse(answersXml.Item(i).Attributes["exit"].Value); res.answers.Add(a); } } return(res); }