Exemple #1
0
    private bool TryApplyPattern(GrammarPattern pattern, int nbRotation, Vector2 firstPosition)
    {
        List <ContentType> graphBeforeRotated = RotatePattern(pattern.GraphBefore, pattern.Width, nbRotation);
        List <ContentType> graphAfterRotated  = RotatePattern(pattern.GraphAfter, pattern.Width, nbRotation);

        // On récupère toutes les cases qui sont couvertes par le pattern à partir de la position précisée
        List <RoomContent> equivalentToPatternGraph = allContents.Where(c => c.GetPos().x >= firstPosition.x &&
                                                                        c.GetPos().y >= firstPosition.y &&
                                                                        c.GetPos().x < firstPosition.x + pattern.Width &&
                                                                        c.GetPos().y < firstPosition.y + pattern.Width).ToList();

        // Si la taille du pattern et la tailles des cases qu'il couvre  est différentes, alors c'est que l'on est dans un coin
        // -> on ne pourra pas appliquer le pattern
        if (equivalentToPatternGraph.Count != graphBeforeRotated.Count)
        {
            return(false);
        }

        // On vérifie si le pattern 'avant' colle bien au pattern récupéré
        for (int i = 0; i < equivalentToPatternGraph.Count; i++)
        {
            if (graphBeforeRotated[i] == ContentType.Anything)
            {
                continue;
            }

            if (equivalentToPatternGraph[i].GetContentType() != graphBeforeRotated[i])
            {
                return(false);
            }
        }

        // Si on arrive ici c'est qu'on peut appliquer le pattern
        for (int i = 0; i < equivalentToPatternGraph.Count; i++)
        {
            // Anything veut dire que l'on laisse la case dans son état, mais qu'on avait bien besoin d'une case (par un mur)
            if (graphAfterRotated[i] == ContentType.Anything)
            {
                continue;
            }

            equivalentToPatternGraph[i].SetContentType(graphAfterRotated[i]);
        }


        return(true);
    }
Exemple #2
0
    private bool TryApplyPattern(GrammarPattern pattern, bool randomizePositions = true)
    {
        List <Vector2> pos = new List <Vector2>();

        for (int y = 0; y < graphHeight; y++)
        {
            for (int x = 0; x < graphWidth; x++)
            {
                pos.Add(new Vector2(x, y));
            }
        }

        if (randomizePositions)
        {
            pos.Shuffle();
        }

        foreach (var position in pos)
        {
            List <int> nbRotation = new List <int> {
                UnityEngine.Random.Range(0, 4)
            };
            if (pattern.ApplyAtAnyCost)
            {
                nbRotation = new List <int> {
                    0, 1, 2, 3
                };
                nbRotation.Shuffle();
            }

            Queue <int> nbRotationQueue = new Queue <int>(nbRotation);
            do
            {
                if (TryApplyPattern(pattern, nbRotationQueue.Dequeue(), position))
                {
                    return(true);
                }
            } while (nbRotationQueue.Count > 0);
        }

        return(false);
    }
Exemple #3
0
 void OnEnable()
 {
     pattern = target as GrammarPattern;
 }