首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

WPF/Silverlight 遊戲開發教程(三十四之四):地圖編輯器障礙物導出入與總結

     編輯:關於C語言

4) 障礙物陣列導出與導入

預製 XML 模板:<Item ID="Obstruction" Value="" />。繪製滿意並通過 A* 測試後導出:

private void outputMatrix_FileOk(object sender, CancelEventArgs e)
{
    SaveFileDialog outputMatrix = sender as SaveFileDialog;
    string result = "";
    for (int y = 0; y <= ObstructionMatrix.GetUpperBound(1); y++)
        for (int x = 0; x <= ObstructionMatrix.GetUpperBound(0); x++)
            if (ObstructionMatrix[x, y] == 0)
                result = string.Format("{0}{1}", result, string.Format("{0}_{1},", x, y));
    SetXMLValue(Data, "Item", "ID", "Obstruction", "Value", result.TrimEnd(','));
    Data.Save(outputMatrix.FileName);
    MessageBox.Show("導出成功!");
}

導出內容範例:

資料格式 x_y 逗號分隔,導入輕鬆:

private void loadMatrix_FileOk(object sender, CancelEventArgs e)
{
    OpenFileDialog loadMatrix = sender as OpenFileDialog;
    try
    {
        XElement XML = XElement.Load(loadMatrix.FileName);
        if (XML.HasAttributes)
        {
            ClearGrid(); RemoveRect();
            string[] matrix = GetXmlValue(XML, "Item", "ID", "Obstruction", "Value").Split(',');
            for (int i = 0; i < matrix.Count(); i++)
                SetRect(string.Format("Rect_{0}", matrix[i]), new SolidColorBrush(Colors.Yellow), new SolidColorBrush(Colors.Black), GridWidthSlider.Value, GridHeightSlider.Value, 0, 0, Convert.ToInt32(matrix[i].Split('_')[0]), Convert.ToInt32(matrix[i].Split('_')[1]));
        }
    }
    catch { MessageBox.Show("導入失敗!請檢文件是否匹配"); e.Cancel = true; }
}

地圖編輯器初具雛形,後續將持續添加新功能,使遊戲設計更輕鬆快速。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved