コード例 #1
0
    public void ItemClicked()
    {
        float minDist = Mathf.Infinity;

        foreach (GameObject O in Overlap)
        {
            float dist = Vector3.Distance(transform.position, O.transform.position);
            if (dist < minDist)
            {
                closest = O;
                minDist = dist;
            }
        }


        //Debug.Log("clicked " + RealObject.name + " in inventory");
        if (selected == false && menu.selected == null)
        {
            transform.parent = menu.transform;
            selected         = true;
            menu.selected    = this;
            Offset           = Input.mousePosition - transform.position;
            foreach (InventoryGridNode G in Location)
            {
                G.Contents = null;
            }
            Location.Clear();
            if (Last.tag == "EquipmentSlot")
            {
                Unequip();
                Last.GetComponent <EquipmentSlot>().Contents = null;
            }
        }
        else if (selected == true && menu.selected == this)
        {
            if (Overlap.Count == 0)
            {
                Debug.Log("dropping " + RealObject.name);
                menu.DropItem(RealObject);
                foreach (InventoryGridNode G in Location)
                {
                    G.Contents = gameObject;
                }
                Destroy(Coll);
                Destroy(gameObject);
                return;
            }

            foreach (GameObject O in Overlap)
            {
                if (O.tag == "EquipmentSlot" &&
                    O.GetComponent <EquipmentSlot>().thisSlot == ThisEquipment &&
                    O.GetComponent <EquipmentSlot>().Contents == null)
                {
                    Last          = O;
                    selected      = false;
                    menu.selected = null;

                    survey.Clear();


                    foreach (InventoryGridNode G in Location)
                    {
                        G.Contents = null;
                    }
                    Location.Clear();

                    Vector3 temp = O.GetComponent <RectTransform>().localPosition;
                    if (dimensions.x == 1)
                    {
                        temp.x += 25;
                    }
                    if (dimensions.y == 2)
                    {
                        temp.y -= 25;
                    }
                    GetComponent <RectTransform>().localPosition = temp;

                    Last.GetComponent <EquipmentSlot>().Contents = RealObject;
                    Equip();
                    return;
                }
            }


            minDist = Mathf.Infinity;

            foreach (GameObject O in Overlap)
            {
                float dist = Vector3.Distance(transform.position, O.transform.position);
                if (dist < minDist)
                {
                    closest = O;
                    minDist = dist;
                }
            }


            for (int r = 0; r < dimensions.y; r++)
            {
                for (int c = 0; c < dimensions.x; c++)
                {
                    if (closest.GetComponent <InventoryGridNode>() != null)
                    {
                        survey.Add(menu.Grid.NodeAtCR(closest.GetComponent <InventoryGridNode>().Column + c, closest.GetComponent <InventoryGridNode>().Row + r));
                    }
                }
            }

            bool openSpot = true;
            if (survey.Count == 0)
            {
                openSpot = false;
            }
            else
            {
                foreach (InventoryGridNode I in survey)
                {
                    if (I.Contents != null)
                    {
                        openSpot = false;
                    }
                }
            }

            if (openSpot == true)
            {
                survey.Clear();
                transform.position = closest.transform.position;

                foreach (InventoryGridNode G in Location)
                {
                    G.Contents = null;
                }
                Location.Clear();


                for (int r = 0; r < dimensions.y; r++)
                {
                    for (int c = 0; c < dimensions.x; c++)
                    {
                        Location.Add(menu.Grid.NodeAtCR(closest.GetComponent <InventoryGridNode>().Column + c, closest.GetComponent <InventoryGridNode>().Row + r));
                    }
                }
                foreach (InventoryGridNode G in Location)
                {
                    G.Contents = gameObject;
                }
                transform.parent = Holder.transform;
                Last             = closest;
                selected         = false;
                menu.selected    = null;
            }
            else
            {
                Debug.Log("does not fit");
                transform.parent   = Holder.transform;
                selected           = false;
                menu.selected      = null;
                transform.position = Last.transform.position;
                survey.Clear();

                if (Last.tag != "EquipmentSlot")
                {
                    for (int r = 0; r < dimensions.y; r++)
                    {
                        for (int c = 0; c < dimensions.x; c++)
                        {
                            Location.Add(menu.Grid.NodeAtCR(Last.GetComponent <InventoryGridNode>().Column + c, Last.GetComponent <InventoryGridNode>().Row + r));
                        }
                    }
                    foreach (InventoryGridNode G in Location)
                    {
                        G.Contents = gameObject;
                    }
                }

                if (Last.tag == "EquipmentSlot")
                {
                    if (dimensions.x == 1)
                    {
                        Vector3 temp = Last.GetComponent <RectTransform>().localPosition;
                        temp.x += 25;
                        GetComponent <RectTransform>().localPosition = temp;
                    }
                    Last.GetComponent <EquipmentSlot>().Contents = RealObject;
                    Equip();
                }

                return;
            }
        }
    }