Ejemplo n.º 1
0
    /// <summary>
    /// This method is called only when player pressed a button to take out
    /// </summary>
    /// <param name="j">the position of the object in the inventory</param>
    public void takeOut(int j)
    {
        if (!status_script.Hands_available() || inventoryList.Count < j + 1)
        {
            return;
        }

        GameObject itemOut = inventoryList[j]; // take the onject out of the inventory

        inventoryList.RemoveAt(j);

        itemOut.SetActive(true);

        if (identify(itemOut).isGroup("pickUpAble"))
        {
            this.GetComponent <pickUpObject>().takeOut(itemOut);
        }
        else if (identify(itemOut).isGroup("cutted"))
        {
            this.GetComponent <holdCuttedObject>().putCuttedItemToHand(itemOut);
        }

        // update UI
        if (onItemChangedCallback != null)
        {
            onItemChangedCallback.Invoke();
        }
        return;
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        // This part of code is used to track if user currently is holding a object in his hands
        if (picked != null && status_script.Scree_free())
        {
            // if user want to put down the object
            if (Input.GetKeyDown(allKeys.putDown))
            {
                DropItemsOnHand();
                InventoryUI.instance.RemoveFromHandSlot();
            }
            // if user want to put the items into inventory
            if (Input.GetKeyDown(allKeys.putInInventory))
            {
                PutInInventory();
            }
        }

        //pick up an pickupable object if hands are free
        else if (status_script.Hands_available() && Input.GetKeyDown(allKeys.pickUp) && status_script.Scree_free())
        {
            Ray        rayCast = playerCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
            RaycastHit rayHit;
            if (Physics.Raycast(rayCast, out rayHit, rayRange))
            {
                if (identify(rayHit.collider.gameObject) != null && identify(rayHit.collider.gameObject).isGroup("pickUpAble"))
                {
                    // step one: change the hand status
                    if (!status_script.Hands_change(Hands.pickUpAble))
                    {
                        Debug.LogWarning("PickUpObject cannot lock hands status to pickUpAble");
                    }
                    picked = rayHit.collider.gameObject;

                    // step two: find the top of the hierachy tree we want to pick up
                    picked = findTopParentOfTheType("pickUpAble", picked);

                    // step three: disable the collider of all its children
                    Queue <GameObject> originalObjects = new Queue <GameObject>();
                    originalObjects.Enqueue(picked);
                    while (originalObjects.Count != 0)
                    {
                        GameObject currentParent = originalObjects.Dequeue();
                        // print(currentParent);
                        foreach (Transform childs in currentParent.transform)
                        {
                            originalObjects.Enqueue(childs.gameObject);
                        }

                        foreach (Collider c in currentParent.GetComponents <Collider>())
                        {
                            c.enabled = false;
                        }
                    }
                    // step four: store the current parent for future reference
                    identify(picked).putRegedit("originalParent", picked.transform.parent);
                    float pickUpScale = (float)(identify(picked).getRegeditValue("pickUpScale") == null? defaultPickUpScale : identify(picked).getRegeditValue("pickUpScale"));
                    // step five:
                    InventoryUI.instance.AddToHandSlot(picked);

                    if (picked.GetComponent <Rigidbody>() != null)
                    {
                        picked.GetComponent <Rigidbody>().isKinematic = true;
                    }

                    picked.transform.SetParent(displayCanvas.transform);
                    picked.transform.localPosition = placedPosition;
                    picked.transform.localScale    = (float)pickUpScale * picked.transform.localScale;
                    // Scale with screen size?
                    // picked.transform.localScale = (float)pickUpScale * picked.transform.localScale * (Screen.width / 900);
                }
            }
        }
    }