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); }
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); }
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; } }