对话系统C#

时间:2016-02-22 17:04:26

标签: c# winforms dialog

第一件事 - 我很抱歉我的英语很差,而且我对这个伟大的社区有点新意,所以如果我的问题有些不对劲,我真的很抱歉。 简而言之 - 我不久前开始使用C#(这意味着你在这里找到了很多糟糕的代码),现在我的目标是创建一个对话系统。虽然我有一些工作样本,但问题是:

  • 如何让一切都不蹩脚,改进代码;
  • 关于提高绩效的建议;
  • 总体建议(可能有更合适的工具);

现在我使用有限状态机(FSM)作为一般概念,因此每个状态都是对话场景。最后一个由NPC引用和玩家响应组成。 到现在为止,一切都很基础。在这里我有我的班级响应球员。

using System;
using System.Drawing;

using System.Windows.Forms;
using System.IO;

namespace Testing
{
    public class Player_Qoute : Label
    {
        public string Next_State { get; set; }//is used to tell, where to go next after choosing particular respond
    }

它继承自Label并且还有一个附加字段 - 下一个阶段编号,在下面的函数中添加:

private void NPC_Quote(string path, string specification, RichTextBox info)
{
    StreamReader file = new StreamReader(path);//creating StremReader to read NPC quotes from .txt
    string line = "";//Creating string variable to read from
    try
    {
        while ((line = file.ReadLine()) != null)//readinf file line-by-line until the end
        {
            if (line.Contains(specification))//if line contains specified "tag"
            {
                line = line.Remove(0, specification.Length);//removing "tag" from line
                info.Text += line + "\n";//adding NPC line to the output field 
            }
        }
        file.Close();
    }
    catch (Exception)
    {
        MessageBox.Show("Problem reading file");
    }
}

此函数解析.txt文件,搜索标记为“NPC_stage_n”的行,其中“n” - 是一个阶段的数字。这个数字出现在每个Player响应.txt文件的末尾,我把它放在Player_Quote对象的“Next_Stage”字段中。 这里应用了相同的想法,但现在我动态创建了Player的响应(其数量因阶段而异)。我正面临着在GroupBox上适当放置引号的一些问题 - 有时它们缺少一行或几行,但我正在努力:

void Quotes_Generation(string path, string specification, GroupBox parent)
{
    parent.Controls.Clear();//deleting previous Player qoutes
    int step = 0;//auxilary variable to separate quotes from each other by heigth
    StreamReader file = new StreamReader(path);//StreamReader to read Player responds from .txt
    string line = "";
    while ((line = file.ReadLine()) != null)
    {
        if (line.Contains(specification))
        {
            Player_Qoute quote = new Player_Qoute();//inherited from Label;
            quote.Name = "qoute_" + line.Remove(specification.Length, line.Length - specification.Length);
            quote.Location = new Point(10, 20 + step);
            quote.Size = new Size(360, 10);
            quote.Text = line.Remove(0, specification.Length);//deleting "search tag" from text
            quote.Text = quote.Text.Remove(quote.Text.Length-3, 3); //here we are deleting 3-digit number at the end of the string 
            //this number will show what is the next state of the dialogue if this Player respond is chosen.
            quote.Next_State = line.Remove(0,line.Length - 3);//storing 3-digit number in Player_Quote property
            using (Graphics g = CreateGraphics())//part of code which was borrowed from StackOverFlow and wasn't properly understood by me
            {
                SizeF size = g.MeasureString(quote.Text, quote.Font, 264);
                quote.Height = (int)Math.Ceiling(size.Height);
                quote.Text = quote.Text;
            }
            quote.MouseDown += new MouseEventHandler(this.Quote_Click);//creating event for choosing this respond
            parent.Controls.Add(quote);//adding respond to GroupBox
            step += (quote.Height+3);//increasing step
            if (parent.Height < step)//enlarging GroupBox
            {
                parent.MaximumSize = new System.Drawing.Size(parent.Width, step + 50);
                parent.Size = new System.Drawing.Size(parent.Width, step + 50);
            }
        }
    }
    file.Close();
}

这是Quote_Click事件:

private void Quote_Click(object sender, EventArgs e)
    {
        Player_Qoute current = sender as Player_Qoute;//recognizing the sender
        richTextBox1.Text +=  Player_Name + "   -  " + current.Text + "\n";//adding Player respond with Player name to RichTextBox
        NPC_Quote(Application.StartupPath + "/Readme.txt", "NPC_stage_" + current.Next_State + ":", richTextBox1);//Adding new NPC line according to chosen respond
        Quotes_Generation(Application.StartupPath + "/Readme.txt", "Player_stage_" + current.Next_State + ":", groupBox1);//refreshing responds according to previous actions
    }

我会很感激所有的建议!

0 个答案:

没有答案
相关问题