Ejemplo n.º 1
0
    void Update()
    {
        Vector2Int objectPosition = VectorTransformer.Vector3ToVector2Int(transform.position);
        Vector2Int downPosition   = VectorTransformer.Vector2IntDown(objectPosition);

        if (!_fallControl)
        {
            _fallControl = true;
            StartCoroutine(WaitFall(objectPosition, downPosition));
        }

        if (!_gridController.CellIsEmptyWithoutCharacter(downPosition))
        {
            _startFallFrom = objectPosition;
        }

        //Se o objeto abaixo deste é Slider e na posição do lado e lado baixo estão vazias a pedra escorrega
        if (_gridController.HasGridObjectAt(downPosition))
        {
            Transform downObject = _gridController.GetObject(downPosition);
            if (downObject.CompareTag("SlideObject") || downObject.CompareTag("Rock"))//verifica se o de baixo é slider
            {
                if (!_gridController.HasNextFreeSpace(objectPosition, Direction.Down))
                {
                    Vector2Int leftPosition = VectorTransformer.Vector2IntLeft(objectPosition);
                    if (_gridController.CellIsEmpty(leftPosition))                                       //Verifica lado esquerdo
                    {
                        if (_gridController.CellIsEmpty(VectorTransformer.Vector2IntDown(leftPosition))) //Verifica esquerdo baixo
                        {
                            //Desliza
                            StartCoroutine(WaitSlide(objectPosition, leftPosition));
                            return;
                        }
                    }
                    Vector2Int rightPosition = VectorTransformer.Vector2IntRight(objectPosition); //Verifica lado direito
                    if (_gridController.CellIsEmpty(rightPosition))
                    {
                        if (_gridController.CellIsEmpty(VectorTransformer.Vector2IntDown(rightPosition))) //Verifica direita baixo
                        {
                            //Desliza
                            StartCoroutine(WaitSlide(objectPosition, rightPosition));
                            return;
                        }
                    }
                }
            }
        }
        if (_gridController.characterPosition.Equals(downPosition))
        {
            if (!_startFallFrom.Equals(VectorTransformer.NullPoint) & _startFallFrom.y > _gridController.characterPosition.y + 1)
            {
                _gridController.character.GetComponent <Lose>().StageLose();
            }
        }
    }
    IEnumerator Walk()
    {
        Vector2Int position = VectorTransformer.Vector3ToVector2Int(transform.position);

        IsCharacterNeighbor(position);

        Direction nextDirection      = Direction.None;
        int       nextNeighborPoints = -200;
        int       actualNeighborPoints;

        List <Vector2Int> isGluedAt = VerifyWhereIsGlued(position);
        Vector2Int        actualNeighborPosition = position + Vector2Int.left;

        if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition))
        {
            actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition);
            if (_directionFrom == Direction.Right)
            {
                actualNeighborPoints = actualNeighborPoints - 100;
            }


            int isGluedAtPoints  = IsInSameWall(actualNeighborPosition, isGluedAt);
            int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt);

            actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints;

            if (actualNeighborPoints > nextNeighborPoints)
            {
                nextNeighborPoints = actualNeighborPoints;
                nextDirection      = Direction.Left;
            }
        }


        actualNeighborPosition = position + Vector2Int.up;
        if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition))
        {
            actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition);
            if (_directionFrom == Direction.Down)
            {
                actualNeighborPoints = actualNeighborPoints - 100;
            }



            int isGluedAtPoints  = IsInSameWall(actualNeighborPosition, isGluedAt);
            int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt);

            actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints;

            if (actualNeighborPoints > nextNeighborPoints)
            {
                nextNeighborPoints = actualNeighborPoints;
                nextDirection      = Direction.Up;
            }
        }

        actualNeighborPosition = position + Vector2Int.right;
        if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition))
        {
            actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition);
            if (_directionFrom == Direction.Left)
            {
                actualNeighborPoints = actualNeighborPoints - 100;
            }


            int isGluedAtPoints  = IsInSameWall(actualNeighborPosition, isGluedAt);
            int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt);

            actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints;

            if (actualNeighborPoints > nextNeighborPoints)
            {
                nextNeighborPoints = actualNeighborPoints;
                nextDirection      = Direction.Right;
            }
        }

        actualNeighborPosition = position + Vector2Int.down;
        if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition))
        {
            actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition);
            if (_directionFrom == Direction.Up)
            {
                actualNeighborPoints = actualNeighborPoints - 100;
            }

            int isGluedAtPoints  = IsInSameWall(actualNeighborPosition, isGluedAt);
            int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt);

            actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints;

            if (actualNeighborPoints > nextNeighborPoints)
            {
                nextNeighborPoints = actualNeighborPoints;
                nextDirection      = Direction.Down;
            }
        }



        Vector2Int toPosition = VectorTransformer.DirectionToVector2Int(position, nextDirection);

        _gridController.MoveObject(position, toPosition);
        transform.position = VectorTransformer.Vector2IntToVector3Int(toPosition);
        _directionFrom     = nextDirection;

        _wasGluedAt = isGluedAt;

        yield return(new WaitForSeconds(0.5f));

        _coroutineControl = true;
    }