Пример #1
0
        ////////////////////////////////////////////////////////////////////////
        // 該当タイルを更新
        ////////////////////////////////////////////////////////////////////////
        public static bool UpdateTile(Vector2 coord, TileType type)
        {
            if (!VaildCoord(coord))
            {
                return(false);
            }

            var block = GetTileBlock(new Vector2(coord.x, coord.y));

            if (block == null)
            {
                return(false);
            }

            if (block.GetTileType() != type)
            {
                block.SetTileType(type);
                var rect = GetRectangle(coord);
                DrawRectWithTileType(TileType.NullTile, rect);
                LoggerForm.WriteInfo(string.Format("Tile Updated | X: {0}, Y: {1}", coord.x, coord.y));
                return(DrawRectWithTileType(type, rect));
            }
            else
            {
                return(false);
            }
        }
Пример #2
0
        ////////////////////////////////////////////////////////////////////////
        // 4方向から進むに最適なタイルを算出
        ////////////////////////////////////////////////////////////////////////
        private static TileBlock GetBestTile(TileBlock origin, int cost)
        {
            if (origin.GetTileType() == TileType.GoalTile || origin.GetAnalyzed())
            {
                return(origin);
            }

            var goalCoord   = GetTileBlockByTileType(TileType.GoalTile).GetCoordinate();
            var originCoord = origin.GetCoordinate();

            var up     = GetTileBlock(new Vector2(originCoord.x, originCoord.y - 1));
            var bottom = GetTileBlock(new Vector2(originCoord.x, originCoord.y + 1));
            var right  = GetTileBlock(new Vector2(originCoord.x + 1, originCoord.y));
            var left   = GetTileBlock(new Vector2(originCoord.x - 1, originCoord.y));

            if (up != null && up.GetTileType() != TileType.Walkable && up.GetTileType() != TileType.GoalTile)
            {
                up = null;
            }
            if (bottom != null && bottom.GetTileType() != TileType.Walkable && bottom.GetTileType() != TileType.GoalTile)
            {
                bottom = null;
            }
            if (right != null && right.GetTileType() != TileType.Walkable && right.GetTileType() != TileType.GoalTile)
            {
                right = null;
            }
            if (left != null && left.GetTileType() != TileType.Walkable && left.GetTileType() != TileType.GoalTile)
            {
                left = null;
            }

            if (up != null && up.GetAnalyzed())
            {
                up = null;
            }
            if (bottom != null && bottom.GetAnalyzed())
            {
                bottom = null;
            }
            if (right != null && right.GetAnalyzed())
            {
                right = null;
            }
            if (left != null && left.GetAnalyzed())
            {
                left = null;
            }

            if (up != null && up.GetTileType() == TileType.GoalTile)
            {
                DrawLineCenterTileToTile(origin.GetCoordinate(), up.GetCoordinate());
            }
            if (bottom != null && bottom.GetTileType() == TileType.GoalTile)
            {
                DrawLineCenterTileToTile(origin.GetCoordinate(), bottom.GetCoordinate());
            }
            if (right != null && right.GetTileType() == TileType.GoalTile)
            {
                DrawLineCenterTileToTile(origin.GetCoordinate(), right.GetCoordinate());
            }
            if (left != null && left.GetTileType() == TileType.GoalTile)
            {
                DrawLineCenterTileToTile(origin.GetCoordinate(), left.GetCoordinate());
            }

            var up_hcost     = 0;
            var bottom_hcost = 0;
            var right_hcost  = 0;
            var left_hcost   = 0;

            if (up != null)
            {
                up_hcost = CalculateHeuristic(up.GetCoordinate(), goalCoord);
            }
            if (bottom != null)
            {
                bottom_hcost = CalculateHeuristic(bottom.GetCoordinate(), goalCoord);
            }
            if (right != null)
            {
                right_hcost = CalculateHeuristic(right.GetCoordinate(), goalCoord);
            }
            if (left != null)
            {
                left_hcost = CalculateHeuristic(left.GetCoordinate(), goalCoord);
            }

            if (up != null)
            {
                up.SetAnalyzeData(cost, up_hcost);
            }
            if (bottom != null)
            {
                bottom.SetAnalyzeData(cost, bottom_hcost);
            }
            if (right != null)
            {
                right.SetAnalyzeData(cost, right_hcost);
            }
            if (left != null)
            {
                left.SetAnalyzeData(cost, left_hcost);
            }

            var up_score     = 0;
            var bottom_score = 0;
            var right_score  = 0;
            var left_score   = 0;

            if (up != null)
            {
                up_score = up.GetScore();
            }
            if (bottom != null)
            {
                bottom_score = bottom.GetScore();
            }
            if (right != null)
            {
                right_score = right.GetScore();
            }
            if (left != null)
            {
                left_score = left.GetScore();
            }

            var scores = new int[4];

            scores[0] = up_score;
            scores[1] = bottom_score;
            scores[2] = right_score;
            scores[3] = left_score;

            var hcosts = new int[4];

            hcosts[0] = up_hcost;
            hcosts[1] = bottom_hcost;
            hcosts[2] = right_hcost;
            hcosts[3] = left_hcost;

            var tiles = new TileBlock[4];

            tiles[0] = up;
            tiles[1] = bottom;
            tiles[2] = right;
            tiles[3] = left;

            var min_score = int.MaxValue;
            var min_cost  = int.MaxValue;
            var min_hcost = int.MaxValue;
            var min_tile  = origin;

            for (int m = 0; m < 4; m++)
            {
                if (scores[m] == 0)
                {
                    continue;
                }
                if (scores[m] > min_score)
                {
                    continue;
                }
                if (scores[m] == min_score && cost >= min_cost)
                {
                    continue;
                }

                min_score = scores[m];
                min_cost  = cost;
                min_tile  = tiles[m];
                min_hcost = hcosts[m];
            }

            if (origin != null)
            {
                origin.Close();
            }

            if (min_tile.GetTileType() == TileType.Walkable)
            {
                origin.SetAnalyzed(true);
                DrawLineCenterTileToTile(origin.GetCoordinate(), min_tile.GetCoordinate());

                bool showScore = f.checkBox1.Checked;

                if (up != null && showScore)
                {
                    DrawText((up_hcost + cost).ToString(), Brushes.Black, PointToVector2(GetRednerTileLocation(up.GetCoordinate())));
                    LoggerForm.WriteWarn("UP HCOST: " + up_hcost.ToString());
                    LoggerForm.WriteWarn("UP  COST: " + cost.ToString());
                    LoggerForm.WriteWarn("UP SCORE: " + up_score.ToString());
                    LoggerForm.WriteInfo("-------------");
                }
                if (bottom != null && showScore)
                {
                    DrawText((bottom_hcost + cost).ToString(), Brushes.Black, PointToVector2(GetRednerTileLocation(bottom.GetCoordinate())));
                    LoggerForm.WriteWarn("BOTTOM HCOST: " + bottom_hcost.ToString());
                    LoggerForm.WriteWarn("BOTTOM  COST: " + cost.ToString());
                    LoggerForm.WriteWarn("BOTTOM SCORE: " + bottom_score.ToString());
                    LoggerForm.WriteInfo("-------------");
                }
                if (right != null && showScore)
                {
                    DrawText((right_hcost + cost).ToString(), Brushes.Black, PointToVector2(GetRednerTileLocation(right.GetCoordinate())));
                    LoggerForm.WriteWarn("RIGHT HCOST: " + right_hcost.ToString());
                    LoggerForm.WriteWarn("RIGHT  COST: " + cost.ToString());
                    LoggerForm.WriteWarn("RIGHT SCORE: " + right_score.ToString());
                    LoggerForm.WriteInfo("-------------");
                }
                if (left != null && showScore)
                {
                    DrawText((left_hcost + cost).ToString(), Brushes.Black, PointToVector2(GetRednerTileLocation(left.GetCoordinate())));
                    LoggerForm.WriteWarn("LEFT HCOST: " + left_hcost.ToString());
                    LoggerForm.WriteWarn("LEFT  COST: " + cost.ToString());
                    LoggerForm.WriteWarn("LEFT SCORE: " + left_score.ToString());
                    LoggerForm.WriteInfo("-------------");
                }
            }
            return(min_tile);
        }