Ejemplo n.º 1
0
    /// <summary>
    /// Throws the grabbed item.
    /// </summary>
    /// <param name="dir">direction of throw</param>
    protected virtual void throwItem()
    {
        if (grabbedItem != null)
        {
            grabbedItem.transform.parent = null;
            grabbedItem.getRigidBody().isKinematic = false;
            grabbedItem.setIsPicked(false);
            grabbedItem.setIsUsed(true);
            grabbedItem.GetComponent <ThrowableItem>().onThrow();

            float throwForce;
            if (chargeAmount > 0)
            {
                throwForce   = grabbedItem.getVelocity() + (chargeAmount * maxThrowForce / 2);
                chargeAmount = 0;
            }
            else
            {
                throwForce = grabbedItem.getVelocity();
            }

            switch (throwDirection)
            {
            case Utilities.direction.Up: grabbedItem.getRigidBody().AddForce(new Vector2(0, throwForce), ForceMode2D.Impulse); break;

            case Utilities.direction.Down: grabbedItem.getRigidBody().AddForce(new Vector2(0, -throwForce), ForceMode2D.Impulse); break;

            case Utilities.direction.Left: grabbedItem.getRigidBody().AddForce(new Vector2(-throwForce, 0), ForceMode2D.Impulse); break;

            case Utilities.direction.Right: grabbedItem.getRigidBody().AddForce(new Vector2(throwForce, 0), ForceMode2D.Impulse); break;

            case Utilities.direction.UpLeft: grabbedItem.getRigidBody().AddForce(new Vector2(-throwForce, throwForce), ForceMode2D.Impulse); break;

            case Utilities.direction.UpRight: grabbedItem.getRigidBody().AddForce(new Vector2(throwForce, throwForce), ForceMode2D.Impulse); break;

            case Utilities.direction.DownLeft: grabbedItem.getRigidBody().AddForce(new Vector2(-throwForce, -throwForce), ForceMode2D.Impulse); break;

            case Utilities.direction.DownRight: grabbedItem.getRigidBody().AddForce(new Vector2(throwForce, -throwForce), ForceMode2D.Impulse); break;
            }
            grabbedItem.GetComponent <ThrowableItem>().setDirection(throwDirection);
            grabbedItem = null;
            hasItem     = false;
        }
        if (isGrounded)
        {
            state = Utilities.state.Idle;
        }
        else
        {
            state = Utilities.state.Air;
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Picks up the closest item in range.
    /// </summary>
    protected void pickupItem()
    {
        Collider2D[] itemsInRange   = Physics2D.OverlapCircleAll(transform.position, 2f, pickableItems);
        int          nearestItem    = 0;
        float        lowestDistance = 1000f;

        if (itemsInRange.Length > 0)
        {
            int totalItems = itemsInRange.Length;
            for (int i = 0; i < totalItems; i++)
            {
                float distance = Vector2.Distance(transform.position, itemsInRange[i].transform.position);
                if (distance < lowestDistance)
                {
                    lowestDistance = distance;
                    nearestItem    = i;
                }
            }
            grabbedItem = itemsInRange[nearestItem].GetComponent <PickableItem>();
            if (!grabbedItem.getIsUsed() && !grabbedItem.getIsPicked())
            {
                hasItem = true;
                grabbedItem.setIsUsed(false);
                grabbedItem.setIsPicked(true);
                grabbedItem.pickUp(pickUpSpot);
            }
        }
    }