示例#1
0
        public static bool StartWave(World map, Monster monster, Dictionary <Point, Monster> monstersList)
        {
            int[] stepX     = { 1, 0, -1, 0 };
            int[] stepY     = { 0, 1, 0, -1 };
            int   MapHeight = 9;
            int   MapWidht  = 14;
            bool  add       = true;

            int[,] cMap = new int[MapHeight, MapWidht];
            int x, y, step = 0;

            for (y = 0; y < MapWidht; y++)
            {
                for (x = 0; x < MapHeight; x++)
                {
                    if (map.GetBlock(x, y) != null)
                    {
                        if (!(map.GetBlock(x, y).IsPassThrough) && !monstersList.ContainsKey(new Point(x, y)))
                        {
                            cMap[x, y] = -2;//индикатор стены
                        }
                        else
                        {
                            cMap[x, y] = -1;//индикатор еще не ступали сюда
                        }
                    }
                    if (x == monster.Position.X && y == monster.Position.Y)
                    {
                        cMap[x, y] = -1;
                    }
                }
            }
            cMap[monster.targetPosition.X, monster.targetPosition.Y] = 0;//Начинаем с финиша
            while (add == true)
            {
                add = false;
                for (y = 0; y < MapWidht; y++)
                {
                    for (x = 0; x < MapHeight; x++)
                    {
                        if (cMap[x, y] == step) //Ставим значение шага+1 в соседние ячейки (если они проходимы)
                        {
                            for (int dir = 0; dir < 4; dir++)
                            {
                                Point pos = new Point(x + stepX[dir], y + stepY[dir]);
                                if (IsStepPosible(pos, MapWidht, MapHeight) && IsPositionConsistNesessaryValue(pos, cMap, -1))
                                {
                                    cMap[pos.X, pos.Y] = step + 1;
                                }
                            }
                        }
                    }
                }
                step++;
                add = true;
                if (cMap[monster.Position.X, monster.Position.Y] != -1)//решение найдено
                {
                    add = false;
                    monster.rangeOfView.moveCostMap = cMap;
                    monster.rangeOfView.pathLength  = cMap[monster.Position.X, monster.Position.Y];
                    return(true);
                }
                if (step > MapWidht * MapHeight)//решение не найдено
                {
                    add = false;
                }
            }
            return(false);
        }