Пример #1
0
    private void Update()
    {
        _mousePos   = _mainCamera.ScreenToWorldPoint(Input.mousePosition);
        _clampedPos =
            Vector3.ClampMagnitude(new Vector3(_mousePos.x, _mousePos.y, LocalPlayerTransform.position.z) - LocalPlayerTransform.position,
                                   MaxInteractRange) + LocalPlayerTransform.position;
        RaycastHit2D hit = Physics2D.Raycast(_clampedPos, Vector2.zero);

        if (hit.collider != null && hit.transform.CompareTag("Moveable") && !_exploded)
        {
            if (Input.GetMouseButtonDown(0))
            {
                _grabbyHand.CloseHand();
            }
            else if (Input.GetMouseButtonUp(0))
            {
                _grabbyHand.OpenHand();
            }

            if (_previousCollider == null || !_active)
            {
                _grabbyHand.OpenHand();

                _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 if it wasn't clicked
                if (!_highlightedItem.Picked)
                {
                    _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)
        {
            _grabbyHand.Pointer();

            _active = false;
            // Only remove borders on non picked items
            if (!_highlightedItem.Picked)
            {
                _renderer.material.SetFloat("Vector1_5D8044E5", 0);
            }
            _highlightedItem = null;
        }

        if (Input.GetMouseButtonDown(0) && _highlightedItem && !_highlightedItem.Picked && !_exploded)
        {
            _highlightedItem.Picked = true;
            if (_client.IsMyGuessCorrect(_highlightedItem.Id))
            {
                _renderer.material.SetColor("Color_BC0A261F", Color.green);
                CorrectItems += 1;

                _audioSource.PlayOneShot(DingSound);
            }
            else
            {
                _renderer.material.SetColor("Color_BC0A261F", Color.red);
                WrongItems += 1;

                ExplodePlayer();
            }

            UpdateStatusText();
        }
    }
Пример #2
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;
            }
        }
    }