Beispiel #1
0
        private bool CubeHaveAnyPossibilitySided(GameObject cubeToCheck, Vector2Int cubeIndex, Side sideToSearch, GameObject cubeExeption)
        {
            int        x                  = cubeIndex.x;
            int        y                  = cubeIndex.y;
            int        index              = FindGameObjectIndex.IndexGameObject(mapCubes[x, y], cubeToCheck);
            Vector2Int oldIndex           = SideHelp.NextIndex(cubeIndex, sideToSearch);
            bool       foundPossibilities = false;

            if (oldIndex.x <= indexMax.x && oldIndex.x >= 0 && oldIndex.y <= indexMax.y && oldIndex.y >= 0)
            {
                List <GameObject> possibilites = cubeToCheck.GetComponent <CanBeNextTo>().adjoiningCubes[sideToSearch];

                foreach (var aCube in mapCubes[oldIndex.x, oldIndex.y])
                {
                    if (!aCube.name.Contains(cubeExeption.name) && !cubeExeption.name.Contains(aCube.name))
                    {
                        foreach (var possibilite in possibilites)
                        {
                            if (possibilite.name.Contains(aCube.name) || aCube.name.Contains(possibilite.name))
                            {
                                foundPossibilities = true;
                                break;
                            }
                        }

                        if (foundPossibilities)
                        {
                            break;
                        }
                    }
                }
            }

            return(foundPossibilities);
        }
Beispiel #2
0
        private List <GameObject> GetPossibilities(Vector2Int indexOfCube, GameObject theCube, Side sideOfCube)
        {
            //int numberOfPossibilites = 0;
            List <GameObject> possibilities = new List <GameObject>();
            var cubePossibilites            = theCube.GetComponent <CanBeNextTo>().adjoiningCubes[sideOfCube];

            Vector2Int indexToSearch = SideHelp.NextIndex(indexOfCube, sideOfCube);
            //Debug.Log(indexToSearch);
            List <GameObject> actualPossibilites = mapCubes[(int)indexToSearch.x, (int)indexToSearch.y];

            Side sideToSearch = SideHelp.GetInverseSide(sideOfCube);

            if (actualPossibilites.Count > 1)
            {
                for (int i = 0; i < actualPossibilites.Count; i++)
                {
                    var  cube  = actualPossibilites[i];
                    bool found = false;
                    foreach (var item in cubePossibilites)
                    {
                        if (item.name.Contains(cube.name) || cube.name.Contains(item.name))
                        {
                            found = !CubeHaveAnyPossibilitySided(cube, indexToSearch, sideToSearch, theCube);
                            break;
                        }
                    }
                    if (found)
                    {
                        possibilities.Add(cube);
                    }
                }
            }

            return(possibilities);
        }
Beispiel #3
0
 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, LayerMask.GetMask("Parametre")) && this.CarreSelectione == 1)
         {
             Debug.Log("Miam2");
             this.ConteneurCarreSelectione.transform.GetComponent <MeshRenderer>().material = this.PremierCarreSelectioneSelectionAutreCarre;
             this.ParametreSelectione        = hit.transform.gameObject;
             this.ConteneurMaterielParametre = hit.transform.GetComponent <MeshRenderer>().material;
             hit.transform.GetComponent <MeshRenderer>().material = this.PremierCarreSelectione;
             this.CarreSelectione = 2;
         }
         else
         if (Physics.Raycast(ray, out RaycastHit hitt, Mathf.Infinity, LayerMask.GetMask("Carre")))
         {
             Debug.Log(hitt);
             if (this.CarreSelectione == 0)
             {
                 Debug.Log("Miam");
                 this.ConteneurCarreSelectione = hitt.transform.gameObject;
                 foreach (Transform Child in hitt.transform)
                 {
                     Child.gameObject.SetActive(true);
                 }
                 this.ConteneurMaterielCube = hitt.transform.GetComponent <MeshRenderer>().material;
                 hitt.transform.GetComponent <MeshRenderer>().material = this.PremierCarreSelectione;
                 hitt.transform.gameObject.layer = 0;
                 this.CarreSelectione            = 1;
             }
             else if (this.CarreSelectione == 2)
             {
                 Debug.Log("Miam3");
                 hitt.transform.gameObject.layer = 0;
                 this.ListCarreAdd.Add(hitt.transform.gameObject);
                 this.ListMaterielAdd.Add(hitt.transform.GetComponent <MeshRenderer>().material);
                 // rendre les carrés selectionable bleu
                 if (this.ConteneurCarreSelectione.GetComponent <CanBeNextTo>().adjoiningCubes
                     [this.ParametreSelectione.GetComponent <SideParametre>().ParametreChoisie()].Contains(hitt.transform.gameObject))
                 {
                     this.ConteneurCarreSelectione.GetComponent <CanBeNextTo>().adjoiningCubes
                     [this.ParametreSelectione.GetComponent <SideParametre>().ParametreChoisie()].Remove(hitt.transform.gameObject);
                     hitt.transform.GetComponent <CanBeNextTo>().adjoiningCubes
                     [SideHelp.GetInverseSide(this.ParametreSelectione.GetComponent <SideParametre>().ParametreChoisie())].Remove(this.ConteneurCarreSelectione);
                 }
                 else
                 {
                     this.ConteneurCarreSelectione.GetComponent <CanBeNextTo>().adjoiningCubes
                     [this.ParametreSelectione.GetComponent <SideParametre>().ParametreChoisie()].Add(hitt.transform.gameObject);
                     hitt.transform.GetComponent <CanBeNextTo>().adjoiningCubes
                     [SideHelp.GetInverseSide(this.ParametreSelectione.GetComponent <SideParametre>().ParametreChoisie())].Add(this.ConteneurCarreSelectione);
                 }
             }
         }
     }
        public void Check()
        {
            CanBeNextTo       nextTo            = GetComponent <CanBeNextTo>();
            List <GameObject> problematicsCubes = new List <GameObject>();
            List <Side>       problematicsSide  = new List <Side>();

            for (int i = 0; i < nextTo.adjoiningCubes.Count; i++)
            {
                Side side        = (Side)i;
                Side inverseSide = SideHelp.GetInverseSide((side));
                for (int j = 0; j < nextTo.adjoiningCubes[side].Count; j++)
                {
                    GameObject  cubeTest   = nextTo.adjoiningCubes[side][j];
                    CanBeNextTo nextToTest = cubeTest.GetComponent <CanBeNextTo>();
                    bool        found      = false;
                    for (int k = 0; k < nextToTest.adjoiningCubes[inverseSide].Count; k++)
                    {
                        GameObject cubeTestSecond = nextToTest.adjoiningCubes[inverseSide][k];
                        if (cubeTestSecond.name.Contains(gameObject.name) || gameObject.name.Contains(cubeTestSecond.name))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        problematicsCubes.Add(cubeTest);
                        problematicsSide.Add(inverseSide);
                    }
                }
            }
            if (problematicsCubes.Count > 0)
            {
                Debug.LogError("Il y a un problème avec " + name + ", " + problematicsCubes.Count + " cubes ne se comprènent pas, les voici.");
                for (int i = 0; i < problematicsCubes.Count; i++)
                {
                    Debug.LogError(problematicsCubes[i].name + " n'est pas associé " + SideHelp.ShowRealName(problematicsSide[i]));
                }
            }
            else
            {
                Debug.Log(name + " est tout bon");
            }
        }
Beispiel #5
0
        void CollapseMap(Vector2Int cubeIndex, List <Vector2Int> previousPositions, GameObject objectToDestroy)
        {
Begining:
            //Debug.Log("je m'apprete a detruire " + objectToDestroy.name + " en coordonnée "+ cubeIndex);
            int x = cubeIndex.x;
            int y = cubeIndex.y;

            if (objectToDestroy == null || mapCubes[x, y].Count <= 1)
            {
                return;
            }

            List <Side> possibleSides = new List <Side>();

            List <Vector2Int> allPositions = new List <Vector2Int>(previousPositions)
            {
                cubeIndex
            };

            bool[] sideCheck = new bool[4];
            int[]  sideNumberPossibilities       = new int[4];
            List <GameObject>[] sidePossibilites = new List <GameObject> [4];
            for (int i = 0; i < sidePossibilites.Length; i++)
            {
                sidePossibilites[i] = new List <GameObject>();
            }
            Vector2Int[] nextPositions = new Vector2Int[4];



            for (int i = 0; i < sideCheck.Length; i++)
            {
                nextPositions[i] = SideHelp.NextIndex(cubeIndex, (Side)i);

                if (nextPositions[i].x <= indexMax.x && nextPositions[i].y <= indexMax.y && nextPositions[i].x >= 0 && nextPositions[i].y >= 0)
                {
                    sideCheck[i] = true;
                }
                else
                {
                    sideCheck[i] = false;
                }

                foreach (var item in previousPositions)
                {
                    if (item == nextPositions[i])
                    {
                        sideCheck[i] = false;
                        break;
                    }
                }

                sidePossibilites[i]        = (sideCheck[i]) ? GetPossibilities(cubeIndex, objectToDestroy, (Side)i) : new List <GameObject>(0);
                sideNumberPossibilities[i] = sidePossibilites[i].Count;
                if (sideCheck[i])
                {
                    if (sideNumberPossibilities[i] == mapCubes[nextPositions[i].x, nextPositions[i].y].Count)
                    {
                        sideNumberPossibilities[i]--;
                        sidePossibilites[i].RemoveAt(RandomizeIndex(sidePossibilites[i].Count));
                    }
                }
                if (sideCheck[i] && sideNumberPossibilities[i] > 0)
                {
                    possibleSides.Add((Side)i);
                }
            }

            if (possibleSides.Count > 0)
            {
                var anIndex              = RandomizeIndex(possibleSides.Count);
                var theSide              = possibleSides[anIndex];
                int firstOne             = -1;
                int numberOfSidePossible = possibleSides.Count;

                for (int i = 0; i < numberOfSidePossible; i++)
                {
                    /*Debug.Log(SideHelp.ShowRealName(theSide) + " soit au coordonnée " +
                     *        nextPositions[(int) theSide] +
                     *        " " +
                     *        ((sideCheck[(int) theSide] && sideNumberPossibilities[(int) theSide] > 0)
                     *            ? "il y a " + sideNumberPossibilities[i] + "possibilité"
                     *            : "il n'y a pas de possibilité"));*/

                    if ((sideCheck[(int)theSide] && sideNumberPossibilities[(int)theSide] > 0))
                    {
                        ++compteur;
                        //Debug.LogWarning("Le compteur est à " + compteur);
                        //Debug.Log("les possibilités sont :");
                    }


                    if (sideCheck[(int)theSide] && sideNumberPossibilities[(int)theSide] > 0)
                    {
                        firstOne = RandomizeIndex(sideNumberPossibilities[(int)theSide]);
                        for (int j = 0; j < sideNumberPossibilities[(int)theSide]; j++)
                        {
                            if (sidePossibilites[(int)theSide][firstOne] != null)
                            {
                                CollapseMap(nextPositions[(int)theSide], allPositions,
                                            sidePossibilites[(int)theSide][firstOne]);
                                firstOne = IncreaseIndex(firstOne, sideNumberPossibilities[(int)theSide]);
                            }
                        }
                        possibleSides.Remove(theSide);
                        --anIndex;
                    }

                    if (possibleSides.Count > 0)
                    {
                        anIndex = IncreaseIndex(anIndex, possibleSides.Count);
                        theSide = possibleSides[anIndex];
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // List<Side> sidesExeption = new List<Side>();
            // foreach (var position in previousPositions)
            // {
            //     Side side = SideHelp.sideBetweenTwoPoint(cubeIndex, position);
            //     if (side != Side.Null)
            //     {
            //         sidesExeption.Add(side);
            //     }
            // }
            // if (CubeHaveAnyPossibility(objectToDestroy,cubeIndex,sidesExeption))
            if (false)
            {
                goto Begining;
            }
            else
            {
                mapCubes[x, y].Remove(objectToDestroy);
                if (mapCubes[x, y].Count <= 1)
                {
                    positionOfPossibilities[x * squareLenght + y] = Vector2Int.down;
                }
                DestroyImmediate(objectToDestroy, false);
            }
        }
        void CollapseMap(Vector2Int cubeIndex, List <Vector2> previousPositions, GameObject objectToDestroy)
        {
            int x = cubeIndex.x;
            int y = cubeIndex.y;

            if (objectToDestroy == null || mapCubes[x, y].Count <= 1)
            {
                return;
            }


            List <Vector2> allPositions = new List <Vector2>(previousPositions)
            {
                cubeIndex
            };

            bool[] sideCheck = new bool[4];
            int[]  sideNumberPossibilities       = new int[4];
            List <GameObject>[] sidePossibilites = new List <GameObject> [4];
            for (int i = 0; i < sidePossibilites.Length; i++)
            {
                sidePossibilites[i] = new List <GameObject>();
            }
            Vector2Int[] nextPositions = new Vector2Int[4];



            for (int i = 0; i < sideCheck.Length; i++)
            {
                nextPositions[i] = SideHelp.NextIndex(cubeIndex, (Side)i);

                if (nextPositions[i].x <= indexMax.x && nextPositions[i].y <= indexMax.y && nextPositions[i].x >= 0 && nextPositions[i].y >= 0)
                {
                    sideCheck[i] = true;
                }
                else
                {
                    sideCheck[i] = false;
                }

                foreach (var item in previousPositions)
                {
                    if (item == nextPositions[i])
                    {
                        sideCheck[i] = false;
                        break;
                    }
                }

                sidePossibilites[i]        = (sideCheck[i]) ? GetPossibilities(cubeIndex, objectToDestroy, (Side)i) : new List <GameObject>(0);
                sideNumberPossibilities[i] = sidePossibilites[i].Count;
            }

            Side nextSide = (Side)RandomizeIndex(sideCheck.Length);


            int h = 0;

            while (sidePossibilites[(int)nextSide].Count <= 0)
            {
                if (h == 4)
                {
                    goto SwitchTheCollaps;
                }
                nextSide = ((int)nextSide + 1 < 4) ? ++nextSide : 0;
                ++h;
            }


            for (int i = 0; i < sideCheck.Length; i++)
            {
                for (int j = 0; j < sideNumberPossibilities[(int)nextSide]; j++)
                {
                    var index = RandomizeIndex(sidePossibilites[(int)nextSide].Count);

                    if (CubeHaveAnyPossibility(sidePossibilites[(int)nextSide][index], nextPositions[(int)nextSide], SideHelp.GetInverseSide(nextSide), objectToDestroy))
                    {
                        CollapseMap(nextPositions[(int)nextSide], allPositions, sidePossibilites[(int)nextSide][index]);
                    }
                    sidePossibilites[(int)nextSide].RemoveAt(index);
                }
                nextSide = ((int)nextSide + 1 < 4) ? (nextSide + 1) : 0;
            }

SwitchTheCollaps:

            mapCubes[x, y].Remove(objectToDestroy);
            if (mapCubes[x, y].Count <= 1)
            {
                //Debug.Log(positionOfPossibilities[x * squareLenght + y]);
                //positionOfPossibilities.RemoveAt(x * squareLenght + y);
                positionOfPossibilities[x * squareLenght + y] = Vector2Int.down;
            }
        }