//////////////////////////////////////////////////////////////////////// // マップデータを入力 //////////////////////////////////////////////////////////////////////// public static void ImportMapData(TileBlock[,] datas) { try { UpdateMap(datas); } catch (IndexOutOfRangeException ex) { LoggerForm.WriteError("例外エラー: " + ex.Message); } }
//////////////////////////////////////////////////////////////////////// // オーバーロード // typeがTileTypeの範囲を超えた場合InvaildCastExceptionが発生することを考慮 //////////////////////////////////////////////////////////////////////// public void SetTileType(int type) { try { tile.tileType = (TileType)type; } catch (InvalidCastException ex) { LoggerForm.WriteError("例外エラー: " + ex.Message); } }
//////////////////////////////////////////////////////////////////////// // ビットマップを使用して描画するための初期化関数 //////////////////////////////////////////////////////////////////////// public static void CreateContext() { graphics = Graphics.FromImage(GetBitmap()); if (graphics != null) { LoggerForm.WriteSuccess("Context created."); } else { LoggerForm.WriteError("CreateContext() failed."); } }
//////////////////////////////////////////////////////////////////////// // テキストファイルとしてマップを入力 //////////////////////////////////////////////////////////////////////// private void button4_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = Environment.CurrentDirectory; dialog.Filter = "テキストファイル(*.txt) | *.txt"; dialog.Title = "マップを読込"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { var fileName = dialog.FileName; using (StreamReader reader = new StreamReader(fileName, Encoding.UTF8)) { var line = string.Empty; TileBlock[,] datas = World.ExportCurrentMapData(); List <string> list = new List <string>(); while ((line = reader.ReadLine()) != null) { list.Add(line); } int counter = 0; for (int x = 0; x < World.MAX_COORD_X; x++) { for (int y = 0; y < World.MAX_COORD_Y; y++) { counter++; if (World.IsTileBlockExists(new Vector2(x, y))) { int result = 5; if (int.TryParse(list[counter - 1], out result)) { datas[x, y].SetTileType(int.Parse(list[counter - 1])); } else { LoggerForm.WriteError(string.Format("Failed to parse data[{0}, {1}] [{2}]", x, y, list[counter - 1])); } } else { LoggerForm.WriteError(string.Format("Failed to load data[{0}, {1}] [{2}]. Tile is not exists.", x, y, list[counter - 1])); } } } World.ImportMapData(datas); } } }
//////////////////////////////////////////////////////////////////////// // マップを探索 //////////////////////////////////////////////////////////////////////// public static async void AnalyzeMap() { LoggerForm.WriteSuccess("探索開始"); //各タイル座標に属性を付与 SetTileAttributesToAll(); var startTile = GetTileBlockByTileType(TileType.StartTile); var goalTile = GetTileBlockByTileType(TileType.GoalTile); if (startTile == null || goalTile == null) { if (startTile == null) { LoggerForm.WriteError("Start tile not found."); } else if (goalTile == null) { LoggerForm.WriteError("Goal tile not found."); } return; } var startTileCoord = startTile.GetCoordinate(); var goalTileCoord = goalTile.GetCoordinate(); var k = 0; TileBlock bestTile = startTile; while (k < 999) { k++; if (bestTile == null || !IsTileBlockExists(bestTile.GetCoordinate())) { LoggerForm.WriteError("Tile not found."); break; } else if (bestTile.GetTileType() == TileType.GoalTile) { LoggerForm.WriteSuccess("Goal found."); break; } else { bestTile = GetBestTile(bestTile, k); } await Task.Delay(10); } }
//////////////////////////////////////////////////////////////////////// // タイル座標を取得 //////////////////////////////////////////////////////////////////////// public static Vector2 GetTileCoordByTileType(TileType type) { for (int x = 0; x < MAX_COORD_X; x++) { for (int y = 0; y < MAX_COORD_Y; y++) { if (GetTileBlock(new Vector2(x, y)).GetTileType() == type) { return(new Vector2(x, y)); } } } LoggerForm.WriteError("GetTileCoordByTileType() Tile not found!"); return(new Vector2(0, 0)); }
//////////////////////////////////////////////////////////////////////// // タイル属性からタイルクラスを取得 //////////////////////////////////////////////////////////////////////// public static TileBlock GetTileBlockByTileType(TileType type) { for (int x = 0; x < MAX_COORD_X; x++) { for (int y = 0; y < MAX_COORD_Y; y++) { var tile = GetTileBlock(new Vector2(x, y)); if (tile.GetTileType() == type) { return(tile); } } } LoggerForm.WriteError("GetTileBlockByTileType() Tile not found!"); return(null); }