public static GameObject CreateGameObjectFromItem(Item item) { GameObject obj = new GameObject(item.Name); //Sprite renderer SpriteRenderer sprite = obj.AddComponent <SpriteRenderer>(); sprite.sprite = item.InGameSprite; sprite.sortingLayerID = item.SortingLayer; //BoxCollider BoxCollider2D collider = obj.AddComponent <BoxCollider2D>(); collider.size = sprite.sprite.bounds.size; MonoItem monoItem = obj.AddComponent <MonoItem>(); monoItem.thisItem = (Item)item.Clone(); monoItem.thisItem.gameObject = obj; monoItem.Type = item.Type; monoItem.isJustSpawned = true; monoItem.spriteRenderer.enabled = true; return(obj); }
public void InventoryItemClick(int index) { if (Inventory.Items[index] == null) { ClearText(); _lastSelectedItem = -1; UseButton.gameObject.SetActive(false); return; } //Removing item: if (Input.GetMouseButton(1)) { GameObject item = null; if (Input.GetKey(KeyCode.LeftShift)) { GlobalEventText.AddMessage(string.Format("You just throw away \"{0}\" (x1)", Inventory.Items[index].Name)); item = MonoItem.CreateGameObjectFromItem(Inventory.Items[index]); item.GetComponent <MonoItem>().thisItem.Count = 1; Inventory.DecreaseCount(Inventory.Items[index], 1); } else { GlobalEventText.AddMessage(string.Format("You just throw away \"{0}\" (x{1})", Inventory.Items[index].Name, Inventory.Items[index].Count)); item = MonoItem.CreateGameObjectFromItem(Inventory.Items[index]); Inventory.RemoveItemFromInventory(Inventory.Items[index]); //Hide the infos popup OnHoovering = false; InventoryPopupInfos.Hide(); } //Check if the playe tile contains items if (Scene._grid[Player.CurrentIndexPosition].TileItem != null) { //Check if the existing item is not a container Item[] items; Item existing = Scene._grid[Player.CurrentIndexPosition].TileItem.GetComponent <MonoItem>().thisItem; if (existing.GType != GlobalType.Container) { Debug.Log("Wrap items"); //Wrap all items in a bag: Bag01 bag = new Bag01(Scene._grid[Player.CurrentIndexPosition].TileItem); items = new Item[2]; items[0] = existing; items[1] = item.GetComponent <MonoItem>().thisItem; bag.SetContent(items); Scene._grid[Player.CurrentIndexPosition].TileItem.GetComponent <MonoItem>().thisItem = bag; Scene._grid[Player.CurrentIndexPosition].ItemValue = bag.ItemValue; Scene._grid[Player.CurrentIndexPosition].TileItem.GetComponent <SpriteRenderer>().sprite = bag.InGameSprite; Destroy(item); } else { Debug.Log("Add content"); ItemContainer container = (ItemContainer)existing; items = new Item[container.Content.Length + 1]; //Get the existing items for (int i = 0; i < container.Content.Length; i++) { items[i] = container.Content[i]; } items[container.Content.Length] = (Item)item.GetComponent <MonoItem>().thisItem.Clone(); //Add the new item container.SetContent(items); //Set the new content Destroy(item); } } else { //Create the object in the player tile item.transform.position = Scene._grid[Player.CurrentIndexPosition].position; item.transform.SetParent(Scene._grid[Player.CurrentIndexPosition].TileObject.transform); Scene._grid[Player.CurrentIndexPosition].TileItem = item; Scene._grid[Player.CurrentIndexPosition].ItemValue = item.GetComponent <MonoItem>().thisItem.ItemValue; } return; } _lastSelectedItem = index; NameText.text = string.Format("Name: {0}", Inventory.Items[index].Name); MassText.text = string.Format("Mass: {0}", Inventory.Items[index].Mass); CountText.text = string.Format("Quantity: {0}", Inventory.Items[index].Count); DescriptionText.text = string.Format("Description: {0}", Inventory.Items[index].Description); if (Inventory.Items[index].isUsable) { switch (Inventory.Items[index].GType) { case GlobalType.Fruits: case GlobalType.Vegetables: UseButton.transform.GetChild(0).GetComponent <Text>().text = "Eat"; break; case GlobalType.Container: UseButton.transform.GetChild(0).GetComponent <Text>().text = "Open"; break; default: UseButton.transform.GetChild(0).GetComponent <Text>().text = "Use"; break; } UseButton.gameObject.SetActive(true); } else { UseButton.gameObject.SetActive(false); } }