Пример #1
0
    // Update is called once per frame
    void Update()
    {
        UpdateState();
        if (_holdingSlot != null)
        {
            _tileDelta     = _mouseHoldStartPos - Camera.main.ScreenToWorldPoint(Input.mousePosition);
            _dragDirection = Vector2.zero;
            if (_tileDelta.x < -0.5f)
            {
                _dragDirection.x = -1;
            }
            if (_tileDelta.x > 0.5f)
            {
                _dragDirection.x = 1;
            }
            if (_tileDelta.y < -0.5f)
            {
                _dragDirection.y = -1;
            }
            if (_tileDelta.y > 0.5f)
            {
                _dragDirection.y = 1;
            }

            if (_prevDragDirection != _dragDirection)
            {
                if (_dragDirection.x == 0 || _dragDirection.y == 0)
                {
                    Vector2 targetPos = new Vector2(_holdingSlot.gridLocation.x - _dragDirection.x, _holdingSlot.gridLocation.y - _dragDirection.y);
                    if (targetPos.x >= 0 && targetPos.x < _gridSize.x && targetPos.y >= 0 && targetPos.y < _gridSize.y)
                    {
                        _targetPosition = targetPos;
                        _holdingSlot.Animate(targetPos);
                        if (_potentialSlot != null)
                        {
                            _potentialSlot.Animate(_potentialSlot.gridLocation);
                        }
                        _potentialSlot = _grid[(int)targetPos.x, (int)targetPos.y];
                        _potentialSlot.Animate(_holdingSlot.gridLocation);
                    }
                }
                if (_dragDirection == Vector2.zero)
                {
                    Vector2 targetPos = new Vector2(_holdingSlot.gridLocation.x - _prevDragDirection.x, _holdingSlot.gridLocation.y - _prevDragDirection.y);
                    if (targetPos.x >= 0 && targetPos.x < _gridSize.x && targetPos.y >= 0 && targetPos.y < _gridSize.y)
                    {
                        _targetPosition = Vector2.zero;
                        _holdingSlot.Animate(_holdingSlot.gridLocation);
                        _potentialSlot = _grid[(int)targetPos.x, (int)targetPos.y];
                        _potentialSlot.Animate(_grid[(int)targetPos.x, (int)targetPos.y].gridLocation);
                        _potentialSlot = null;
                    }
                }
            }

            _prevDragDirection = _dragDirection;
        }
    }