Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
                 }
             }
         }
     }
Ejemplo n.º 3
0
        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");
            }
        }
        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;
            }
        }