Esempio n. 1
0
 /// <summary>
 /// Set the current item to display
 /// </summary>
 /// <param name="item"></param>
 public void setItem(Item item)
 {
     this.item = item;
     if (item != null && item.type != null)
     {
         itemModel = Instantiate(ItemDataMapper.GetModelFor(item), transform);
         // add a rigidbody for gravity and enable the colliders which are disabled by default.
         itemModel.AddComponent(typeof(Rigidbody));
         Collider[] itemColliders = itemModel.GetComponentsInChildren <Collider>(true);
         foreach (Collider itemCollider in itemColliders)
         {
             itemCollider.enabled = true;
         }
         itemHilight = Instantiate(ItemHilight, itemModel.transform);
     }
 }
        /// <summary>
        /// Make a new item icon for the given item
        /// </summary>
        /// <returns></returns>
        public static ItemIconController Make(
            Item item,
            Transform parent        = null,
            bool loadShapedIcon     = false,
            bool isDraggable        = false,
            int stackIndex          = GridBasedInventory.EmptyGridSlot,
            Coordinate gridLocation = default,
            Player.InventoryTypes parentInventory = Player.InventoryTypes.None
            )
        {
            // make the icon under the given parent, or alone if we want
            GameObject icon = parent != null
        ? Instantiate(ItemDataMapper.ItemIconPrefab, parent)
        : Instantiate(ItemDataMapper.ItemIconPrefab);

            // move to the top
            if (parent != null)
            {
                icon.transform.SetAsFirstSibling();
            }

            ItemIconController iconController = icon.GetComponent <ItemIconController>();

            iconController.item            = item;
            iconController.backgroundImage = icon.transform.Find("Icon Background").GetComponent <Image>();

            /// add the drag controller.
            if (isDraggable)
            {
                iconController.dragController = icon.AddComponent <ItemInventoryDragController>();
                iconController.dragController.initialize(iconController, stackIndex, gridLocation, parentInventory);
            }

            // try to get the sprite
            if (item != null)
            {
                Sprite itemSprite = ItemDataMapper.GetIconFor(item);
                /// if we found a sprite
                if (itemSprite != null)
                {
                    /// load the regular icon
                    iconController.defaultIconScaler = icon.transform.Find("Small Icon Scaler").gameObject;
                    GameObject sprite = Instantiate(new GameObject(), iconController.defaultIconScaler.transform);
                    sprite.layer = 5;
                    SpriteRenderer spriteRenderer = sprite.AddComponent <SpriteRenderer>();
                    spriteRenderer.sprite = itemSprite;
                    // if we didn't, use the object as an icon.
                }
                else
                {
                    iconController.defaultIconScaler = icon.transform.Find("Model Icon Scaler").gameObject;
                    GameObject itemModel = Instantiate(ItemDataMapper.GetModelFor(item), iconController.defaultIconScaler.transform);
                    iconController.itemModelRenderers = itemModel.GetComponentsInChildren <Renderer>();
                }

                /// if we're also loading the shaped icon:
                if (loadShapedIcon)
                {
                    // get the shaped scaler
                    iconController.shapedIconScaler = icon.transform.Find("Shaped Icon Scaler").gameObject;

                    // make the prototype image object
                    GameObject imageObject = new GameObject {
                        layer = 5
                    };
                    imageObject.AddComponent <Image>();
                    // resize the sprite according to it's shape size.
                    RectTransform rectTransform = imageObject.GetComponent <RectTransform>();
                    rectTransform.anchorMax = (item.type.ShapeSize - item.type.ShapePivot).Vec2;
                    rectTransform.anchorMin = ((0, 0) - item.type.ShapePivot).Vec2;
                    rectTransform.SetLTRB(0);

                    // if we need a new icon for the shaped icon than the basic square icon, get it
                    if (item.type.ShapeSize > (1, 1))
                    {
                        Sprite shapedIcon      = ItemDataMapper.GetIconFor(item, true);
                        Image  shapedIconImage = Instantiate(imageObject, iconController.shapedIconScaler.transform).GetComponent <Image>();
                        shapedIconImage.sprite = shapedIcon;
                    }

                    // add the outline
                    Image outline = Instantiate(imageObject, iconController.shapedIconScaler.transform).GetComponent <Image>();
                    outline.sprite = ItemDataMapper.GetShapedOutlineFor(item);
                    outline.color  = new Color(1, 1, 0);
                }

                // set the correct icon scaler active
                iconController.defaultIconScaler.SetActive(true);

                /// set up the stack indicator
                iconController.itemStackSizeIndicator     = icon.transform.Find("Stack Quantity Indicator").GetComponent <RectTransform>();
                iconController.itemStackSizeIndicatorText = iconController.itemStackSizeIndicator.GetComponentInChildren <Text>();
                iconController.updateStackCount();
                if (item.type.StackSize > 1)
                {
                    iconController.itemStackSizeIndicator.gameObject.SetActive(true);
                }
            }
            else
            {
                iconController.itemStackSizeIndicator     = icon.transform.Find("Stack Quantity Indicator").GetComponent <RectTransform>();
                iconController.itemStackSizeIndicatorText = iconController.itemStackSizeIndicator.GetComponentInChildren <Text>();
                iconController.updateStackCount();
            }

            return(iconController);
        }