Exemple #1
0
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit, float.MaxValue, LayerMask.GetMask("PickableItem")))
            {
                PickableItem pickedItem = hit.transform.parent.GetComponent <PickableItem>();
                if (!CheckCoveringItems(pickedItem))
                {
                    _pickedItem = pickedItem;
                    if (_pickedItem != null)
                    {
                        SoundPlayer.Play("tink", 0.5f);
                        _pickedItem.OnPicked();
                        _pickedItem.SetPositionAnimated(GetPosOnPickedPlane(_pickedItem));
                        if (_pickedItem.initialPositionState == PickableItem.InitialPositionState.OnInitialPosition ||
                            _pickedItem.initialPositionState == PickableItem.InitialPositionState.MovingToInitialPosition)
                        {
                            _pickedItem.initialPositionState = PickableItem.InitialPositionState.PickedFromInitialPosition;
                        }
                    }
                }
            }
        }

        if (Input.GetMouseButtonUp(0) && _pickedItem != null)
        {
            SoundPlayer.Play("tink", 0.5f);
            _pickedItem.OnReleased();
            _pickedItem = null;
        }

        if (_pickedItem != null && (!Mathf.Approximately(Input.GetAxis("Mouse X"), 0f) || !Mathf.Approximately(Input.GetAxis("Mouse Y"), 0f)))
        {
            Ray  ray = _camera.ScreenPointToRay(Input.mousePosition);
            bool interactedWithGhost = false;
            if (Physics.Raycast(ray, out RaycastHit hit, float.MaxValue, LayerMask.GetMask("PickableItemGhost")))
            {
                PickableItemGhost ghost = hit.transform.parent.GetComponent <PickableItemGhost>();
                if (!CheckCoveringItems(ghost.transform.GetComponent <PickableItem>()))
                {
                    if (_pickedItem.initialPositionState == PickableItem.InitialPositionState.CanBeReturnedToInitialPosition)
                    {
                        _pickedItem.SetPositionAnimated(ghost.transform.position, pickedItem =>
                        {
                            pickedItem.initialPositionState = PickableItem.InitialPositionState.OnInitialPosition;
                            pickedItem.isBroken             = false;
                        });
                        _pickedItem.initialPositionState = PickableItem.InitialPositionState.MovingToInitialPosition;
                    }
                    else if (_pickedItem.initialPositionState == PickableItem.InitialPositionState.PickedFromInitialPosition)
                    {
                        _pickedItem.SetPosition(GetPosOnPickedPlane(_pickedItem));
                    }

                    interactedWithGhost = true;
                }
            }

            if (!interactedWithGhost)
            {
                if (_pickedItem.initialPositionState == PickableItem.InitialPositionState.PickedFromInitialPosition)
                {
                    _pickedItem.SetPositionAnimated(GetPosOnPickedPlane(_pickedItem));
                    _pickedItem.initialPositionState = PickableItem.InitialPositionState.CanBeReturnedToInitialPosition;
                }
                else
                {
                    _pickedItem.SetPosition(GetPosOnPickedPlane(_pickedItem));
                    if (_pickedItem.initialPositionState == PickableItem.InitialPositionState.OnInitialPosition ||
                        _pickedItem.initialPositionState == PickableItem.InitialPositionState.MovingToInitialPosition)
                    {
                        _pickedItem.initialPositionState = PickableItem.InitialPositionState.CanBeReturnedToInitialPosition;
                    }
                }
            }
        }
    }