Exemple #1
0
    void Update()
    {
        ManageInputState();

        if (!inputLocked)
        {
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                // if you clicked an empty space, activate the camera
                if (!Physics.Raycast(ray, out hit))
                {
                    inputState = InputState.Camera;
                }
                else
                {
                    GameObject cube = hit.transform.gameObject;
                    UpdateSelectedCubeArray(cube);
                    if (selectedCubes.Count == 2)
                    {
                        // are these cubes right next to each other?
                        if (MasterGrid.CubesAreAdjacent(selectedCubes[0], selectedCubes[1]))
                        {
                            StartMove();
                        }
                        else
                        {
                            CancelSelectedCubes();
                        }
                    }
                }
            }
            else if (Input.GetMouseButtonUp(0))
            {
                inputState = InputState.Game;
            }
        }
        else
        {
            if (!AnyCubeIsMoving() && selectedCubes.Count > 0)
            {
                GameObject cube1 = selectedCubes[0];
                GameObject cube2 = selectedCubes[1];
                lastMovedCubes = selectedCubes;
                selectedCubes  = new List <GameObject>();

                MasterGrid.UpdateSelectedCubePositions(cube1, cube2);

                StartCoroutine(TriggerCubeDeletionChain(new List <GameObject> {
                    cube1, cube2
                }, true));
            }
            else if (!AnyCubeIsMoving())
            {
                inputLocked = false;
            }
        }
    }
Exemple #2
0
    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>();
        }
    }