public static void Run() { Bag01 bag = new Bag01(); System.Console.WriteLine(bag.GetMaxValue(4, new int[] { 2, 1, 3 }, new int[] { 4, 2, 3 })); System.Console.WriteLine(bag.GetMaxValueAns(3, 4, new int[] { 2, 1, 3 }, new int[] { 4, 2, 3 })); }
public object Clone() { Bag01 clonedItem = new Bag01(null); //Set the new content items: clonedItem.Content = new Item[Content.Length]; for (int i = 0; i < Content.Length; i++) { clonedItem.Content[i] = (Item)Content[i].Clone(); } clonedItem.Mass = Mass; clonedItem.Type = Type; clonedItem.SortingLayer = SortingLayer; clonedItem.InventorySprite = InventorySprite; clonedItem.InGameSprite = InGameSprite; clonedItem.Count = Count; return(clonedItem); }
public object Clone() { Bag01 clonedItem = new Bag01(null); //Set the new content items: clonedItem.Content = new Item[Content.Length]; for (int i = 0; i < Content.Length; i++ ) { clonedItem.Content[i] = (Item)Content[i].Clone(); } clonedItem.Mass = Mass; clonedItem.Type = Type; clonedItem.SortingLayer = SortingLayer; clonedItem.InventorySprite = InventorySprite; clonedItem.InGameSprite = InGameSprite; clonedItem.Count = Count; return clonedItem; }
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); } }
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); }