Esempio n. 1
0
        protected bool CheckForAllowToPlace(Tile[][] map, int x, int y, AdjacencyMatrix matrix, Type type)
        {
            if (map == null || x < 0 || x >= map.Length || y < 0 || y >= map[x].Length || matrix == null || !type.IsSubclassOf(typeof(Tile)))
            {
                throw new ArgumentException();
            }

            int maxDistance = matrix.GetMaxMatrixDistanceForTile(type);

            if (maxDistance == 0)
            {
                return(true);
            }
            else if (maxDistance == -1)
            {
                return(false);
            }

            //проверка на максимальную дистанцию во все стороны
            int xFrom = x - maxDistance >= 0 ? x - maxDistance : 0;
            int xTo   = x + maxDistance < map.Length ? x + maxDistance : map.Length;
            int yFrom = y - maxDistance >= 0 ? y - maxDistance : 0;

            for (int i = xFrom; i < xTo; i++)
            {
                int yTo = y + maxDistance < map[i].Length ? y + maxDistance : map[i].Length;
                for (int j = yFrom; j < yTo; j++)
                {
                    int allowedDistance = matrix.AllowedDistance(type, map[i][j]?.GetType());
                    if (allowedDistance > 0 && (Math.Abs(x - i) < allowedDistance || Math.Abs(y - j) < allowedDistance))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }