Пример #1
0
    // Update is called once per frame
    void Update()
    {
        Vector2Int position = VectorTransformer.Vector3ToVector2Int(transform.position);

        if (Input.GetAxis("Horizontal") >= 1f)
        {
            Vector2Int positionRight = position + Vector2Int.right;

            if (CanBePushed(positionRight, Direction.Right))
            //if(_gridController.HasGridObjectAt(positionRight) & _gridController.CellIsEmpty(positionRight + Vector2Int.right))
            {
                if (VectorTransformer.IsAlmostEqualVector3Int(transform.position))
                {
                    if (_pushFrom.Item2 != Direction.Right || !_pushFrom.Item1.Equals(position))
                    {
                        //Save the values of the push
                        _pushFrom = new Tuple <Vector2Int, Direction>(position, Direction.Right);

                        if (!_coroutineIsRunning)
                        {
                            _coroutineIsRunning = true;
                            //chama a corrotina
                            StartCoroutine(WaitPush(position, positionRight, Direction.Right));
                        }
                    }
                }
            }
        }
        if (Input.GetAxis("Horizontal") <= -1f)
        {
            Vector2Int positionLeft = position + Vector2Int.left;

            if (CanBePushed(positionLeft, Direction.Left))
            //if(_gridController.HasGridObjectAt(positionLeft) & _gridController.CellIsEmpty(positionLeft + Vector2Int.left) )
            {
                if (VectorTransformer.IsAlmostEqualVector3Int(transform.position))
                {
                    //Verify if the last push was from the same position and direction to prevent mutiple pushes from the same place
                    if (_pushFrom.Item2 != Direction.Left || !_pushFrom.Item1.Equals(position))
                    {
                        //Save the values of the push
                        _pushFrom = new Tuple <Vector2Int, Direction>(position, Direction.Left);

                        if (!_coroutineIsRunning)
                        {
                            _coroutineIsRunning = true;
                            //chama a corrotina
                            StartCoroutine(WaitPush(position, positionLeft, Direction.Left));
                        }
                    }
                }
            }
        }
    }