コード例 #1
0
    public void Drop()
    {
        IPickable pickable = _grabing.GetComponent(typeof(IPickable)) as IPickable;

        pickable.Drop();

        _grabing = null;
    }
コード例 #2
0
    void dropItem()
    {
        if (isHolding)
        {
            IPickable pickable = _heldItem.GetComponent <IPickable>();
            if (pickable != null)
            {
                pickable.Drop();
            }

            _heldItem = null;
        }
    }
コード例 #3
0
        private void HandlePickUp(InputAction.CallbackContext context)
        {
            var interactable = _interactableController.CurrentInteractable;

            // empty hands, try to pick
            if (_currentPickable == null)
            {
                _currentPickable = interactable as IPickable;
                if (_currentPickable != null)
                {
                    animator.SetBool(_hasPickupHash, true);
                    this.PlaySoundTransition(pickupAudio);
                    _currentPickable.Pick();
                    _interactableController.Remove(_currentPickable as Interactable);
                    _currentPickable.gameObject.transform.SetPositionAndRotation(slot.transform.position,
                                                                                 Quaternion.identity);
                    _currentPickable.gameObject.transform.SetParent(slot);
                    return;
                }

                // Interactable only (not a IPickable)
                _currentPickable = interactable?.TryToPickUpFromSlot(_currentPickable);
                if (_currentPickable != null)
                {
                    animator.SetBool(_hasPickupHash, true);
                    this.PlaySoundTransition(pickupAudio);
                }

                _currentPickable?.gameObject.transform.SetPositionAndRotation(
                    slot.position, Quaternion.identity);
                _currentPickable?.gameObject.transform.SetParent(slot);
                return;
            }

            // we carry a pickable, let's try to drop it (we may fail)

            // no interactable in range or at most a Pickable in range (we ignore it)
            if (interactable == null || interactable is IPickable)
            {
                animator.SetBool(_hasPickupHash, false);
                this.PlaySoundTransition(dropAudio);
                _currentPickable.Drop();
                _currentPickable = null;
                return;
            }

            // we carry a pickable and we have an interactable in range
            // we may drop into the interactable

            // Try to drop on the interactable. It may refuse it, e.g. dropping a plate into the CuttingBoard,
            // or simply it already have something on it
            //Debug.Log($"[PlayerController] {_currentPickable.gameObject.name} trying to drop into {interactable.gameObject.name} ");

            bool dropSuccess = interactable.TryToDropIntoSlot(_currentPickable);

            if (!dropSuccess)
            {
                return;
            }

            animator.SetBool(_hasPickupHash, false);
            this.PlaySoundTransition(dropAudio);
            _currentPickable = null;
        }
コード例 #4
0
    void Update()
    {
        ray = playerCamera.ScreenPointToRay(Input.mousePosition);
        IUserInteraction hitInteraction = Physics.Raycast(ray, out hit, interactRadio) ? hit.transform.GetComponent <IUserInteraction>() : null;
        bool             realizedHit    = hitInteraction != null;

        if (pointer != null)
        {
            if (realizedHit)
            {
                pointer.sprite = objectInHand != null ? ocupiedPointer : hitInteraction.canInteract ? activePointer : passivePointer;
            }
            else
            {
                pointer.sprite = objectInHand != null ? ocupiedPointer : passivePointer;
            }
        }

        if (InteractTimer >= 0.6f && objectInHand == null)
        {
            if (realizedHit)
            {
                IPickable objectToPick = hit.transform.GetComponent <IPickable>();
                if (objectToPick != null && objectToPick.canInteract)
                {
                    objectInHand = objectToPick;
                    objectToPick.Pick(hit.point);
                }
            }
        }

        if (objectInHand != null)
        {
            if (Vector3.Distance(playerCamera.transform.position, objectInHand.transform.position) > interactRadio * 2)
            {
                objectInHand.Drop();
                InteractTimer = 0;
                objectInHand  = null;
            }
            else
            {
                objectInHand.Pick(transform.position + playerCamera.transform.forward * interactRadio);
            }
        }

        if (Input.GetButtonDown(interactionInput))
        {
            if (selectedGridObject)
            {
                placeObject = placeObject == null?Instantiate(selectedGridObject.gameObject) : placeObject;
            }
            else if (realizedHit && hitInteraction.canInteract && hitInteraction.gameObject.GetComponent <GridObject>())
            {
                hitInteraction.Interact(hit.point, gameObject);
            }
            if (objectInHand != null)
            {
                InteractTimer = 0;
                objectInHand.Drop();
                objectInHand = null;
            }
        }

        if (Input.GetButton(interactionInput) && objectInHand == null)
        {
            if (realizedHit && hitInteraction.canInteract && placeObject)
            {
                hitInteraction.Interact(hit.point, placeObject.gameObject, null);
            }
            InteractTimer += Time.deltaTime;
        }


        if (Input.GetButtonUp(interactionInput) && objectInHand == null)
        {
            InteractTimer = 0;
            if (realizedHit && InteractTimer < 0.6f)
            {
                if (hitInteraction.gameObject.GetComponent <ITrigger>() != null)
                {
                    hitInteraction.gameObject.GetComponent <ITrigger>().delegates[0].Invoke();
                }
                if (hitInteraction.canInteract && placeObject)
                {
                    hitInteraction.Interact(hit.point, placeObject.gameObject);
                }
            }
            else
            {
                Destroy(placeObject);
            }
        }
    }