// TODO maybe make every cube manage its own moving state. have a static list of gameObjects and when a cube is moving, // it adds itself to that list and when it's done moving it removes itself. then you can just check the list.Count // to see if any blocks are moving private bool AnyCubeIsMoving() { // continually check if everything has stopped falling/moving bool anyBlockIsMoving = false; List <GameObject> allCubes = MasterGrid.GetAllCubes(); // check if any cubes are moving or falling for (int i = 0; i < allCubes.Count; i++) { if (allCubes[i] == null) { continue; } CubeController cc = allCubes[i].GetComponent <CubeController>(); if (cc.isMovingOrFalling()) { anyBlockIsMoving = true; break; } } return(anyBlockIsMoving); }
private IEnumerator TriggerCubeDeletionChain(List <GameObject> cubesToTest, bool isFirstMove) { //grab all of the cube chains we will be checking List <List <GameObject> > cubeChains = new List <List <GameObject> >(); foreach (GameObject cube in cubesToTest) { //if this cube is in an existing chain that we have, skip it. bool cubeAlreadyInChain = false; foreach (var chain in cubeChains) { foreach (var c in chain) { if (c.GetInstanceID() == cube.GetInstanceID()) { cubeAlreadyInChain = true; break; } } if (cubeAlreadyInChain) { break; } } if (cubeAlreadyInChain) { continue; } // add this unique cube chain cubeChains.Add(MasterGrid.GetColorChain(cube)); } Debug.Log(cubeChains.Count); // delete the cubes in each chain that needs it bool chainsWereDeleted = false; foreach (List <GameObject> cubeChain in cubeChains) { if (CubeChainShouldDelete(cubeChain)) { chainsWereDeleted = true; foreach (GameObject c in cubeChain) { // does this need a null check? DestroyImmediate(c.gameObject); } } } // if any cubes were removed, we need to clean the grid of null values // and update "floating" cubes to their new positions and make them fall. if (chainsWereDeleted) { MasterGrid.Clean(); int highestCubeYValue = 0; foreach (var cube in MasterGrid.GetAllCubes()) { if (cube.GetComponent <CubeController>().GetGridPosition().y > highestCubeYValue) { highestCubeYValue = cube.GetComponent <CubeController>().GetGridPosition().y; } } // TODO should be able to start with 1 i think 0 height cubes should never be set to fall right? for (int i = 1; i <= highestCubeYValue; i++) { foreach (var cube in MasterGrid.GetAllCubes()) { if (cube.GetComponent <CubeController>().GetGridPosition().y == i) { int cubesBelow = MasterGrid.GetCubesBelow(cube); if (cube.GetComponent <CubeController>().GetGridPosition().y != cubesBelow) { cube.GetComponent <CubeController>().UpdateGridY(cubesBelow); cube.GetComponent <CubeController>().SetFalling(); } } } } yield return(new WaitUntil(() => AnyCubeIsMoving() == false)); Debug.Log("freedom!"); // recursion! StartCoroutine(TriggerCubeDeletionChain(MasterGrid.GetAllCubes(), false)); } else if (isFirstMove) { MoveCubesBack(); yield return(new WaitUntil(() => AnyCubeIsMoving() == false)); MasterGrid.UpdateSelectedCubePositions(lastMovedCubes[0], lastMovedCubes[1]); lastMovedCubes = new List <GameObject>(); } }