Пример #1
0
 public void UndoSwap()
 {
     if (lastSwipedItem != null && dragDirection != default(Vector2))
     {
         lastSwipedItem.Move(-dragDirection, true);
     }
 }
Пример #2
0
    private void OnDrag()
    {
        //Wait for end of swap
        if (swiping || swipedItem == null || GameplayController.Gameplay.InputBlocked || MovementAnimationSystem.IsAnimatingSomeObject())
        {
            return;
        }

        mouseCurrentPosition          = Input.mousePosition;
        mouseDistanceBetweenPositions = mouseCurrentPosition - mouseBeginPositon;

        //Mouse move must overcome a certain distance to allow swap
        if (Mathf.Abs(mouseDistanceBetweenPositions.x) < deadZone && Mathf.Abs(mouseDistanceBetweenPositions.y) < deadZone)
        {
            return;
        }

        dragDirection = GetDirtection(mouseDistanceBetweenPositions);
        swipedItem.Move(dragDirection);

        swiping = true;
    }
Пример #3
0
    // Update is called once per frame
    void Update()
    {
        _mousePos = _mainCamera.ScreenToWorldPoint(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(_mousePos, Vector2.zero);

        if (!_movingItem)
        {
            if (hit.collider != null && hit.transform.CompareTag("Moveable"))
            {
                // Open grabby hand
                _grabbyHand.OpenHand();

                if (_previousCollider == null || !_active)
                {
                    _active = true;

                    _previousCollider = hit.collider;

                    _renderer = hit.collider.GetComponent <SpriteRenderer>();
                    _renderer.material.SetFloat("Vector1_5D8044E5", 1);

                    _highlightedItem = hit.collider.GetComponent <GridItem>();
                }
                else if (_previousCollider != hit.collider)
                {
                    _active           = true;
                    _previousCollider = hit.collider;

                    // Remove outline from previously selected item
                    _renderer.material.SetFloat("Vector1_5D8044E5", 0);

                    _renderer = hit.collider.GetComponent <SpriteRenderer>();
                    _renderer.material.SetFloat("Vector1_5D8044E5", 1);

                    _highlightedItem = hit.collider.GetComponent <GridItem>();
                }
            }
            else if (_active)
            {
                _active = false;
                _renderer.material.SetFloat("Vector1_5D8044E5", 0);
                _highlightedItem = null;

                _grabbyHand.Pointer();
            }
        }
        else
        {
            _highlightedItem.Move(_mousePos);
            _currentlyInValidPosition = IsPositionValid(_grid, _highlightedItem.X, _highlightedItem.Y, _highlightedItem.Width, _highlightedItem.Height) &&
                                        (_highlightedItem.X != _highlightedItem.InitialX || _highlightedItem.Y != _highlightedItem.InitialY);
            if (_currentlyInValidPosition)
            {
                _renderer.material.SetColor("Color_BC0A261F", Color.green);
            }
            else
            {
                _renderer.material.SetColor("Color_BC0A261F", Color.red);
            }
        }


        if (Input.GetMouseButtonDown(0))
        {
            // Check if a object is being hovered
            if (_highlightedItem != null)
            {
                // Check if we are currently holding something
                if (_movingItem)
                {
                    // Reset the object's position is placed in an invalid location
                    if (!_currentlyInValidPosition || OverMovingLimit())
                    {
                        _highlightedItem.ResetPosition();
                    }
                    else
                    {
                        // Update the collision grid then update the objects initial positions
                        // so that it will reset to this spot next time and can update the grid
                        // correctly if moved again
                        UpdateGrid(_grid, _highlightedItem);
                        _highlightedItem.RoundX = _highlightedItem.X;
                        _highlightedItem.RoundY = _highlightedItem.Y;

                        _grabbyHand.OpenHand();

                        _movedObjects.Add(_highlightedItem.Id);
                        _audioSource.PlayOneShot(PlaceSound);

                        ObjectMoved?.Invoke(_highlightedItem.Id, _highlightedItem.X, _highlightedItem.Y);
                    }

                    // Either way, drop it
                    _highlightedItem = null;
                    _renderer.material.SetColor("Color_BC0A261F", Color.white);
                }
                else
                {
                    // Close/Grab with grabby hand
                    _grabbyHand.CloseHand();

                    _audioSource.PlayOneShot(PickupSound);
                }

                _movingItem = !_movingItem;
            }
        }
    }