private void ApplyDiscountRule(List <ItemPile> basket, DiscountRule currentRule) { foreach (var basketPile in basket) { ItemPile rulePile = currentRule.Piles.FirstOrDefault(p => p.Unit == basketPile.Unit); if (rulePile != null) { basketPile.RemoveFromPile(rulePile.Quantity); } } }
private void AddGridPosition(Grid grid, ItemPile itemPile, bool isAddition) { var row = grid.RowsProportions.Count; grid.RowsProportions.Add(new Proportion()); if (isAddition) { var image = new Image { Renderable = DefaultAssets.UITextureRegionAtlas["vis-check-tick"], GridRow = row, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; grid.Widgets.Add(image); } var text = itemPile.Item.Info.Name; if (itemPile.Quantity > 1) { text = itemPile.ToString(); } var textBlock = new Label { Text = text, GridColumn = 1, GridRow = row }; grid.Widgets.Add(textBlock); var isSell = grid == _gridLeft; if (isAddition) { isSell = !isSell; } var price = GetItemPrice(itemPile.Item, 1, isSell ? SellK : 1.0f); textBlock = new Label { Text = price.ToString(), GridColumn = 2, GridRow = row }; grid.Widgets.Add(textBlock); }
public override void PanelEffect() { GameObject manager = GameObject.Find("Manager"); GameManager managerScript = manager.GetComponent <GameManager>(); GameObject itemPile = GameObject.Find("ItemCardPile"); ItemPile itemPileScript = itemPile.GetComponent <ItemPile>(); managerScript.CreateFadingSystemText("You obtained one Item Card."); managerScript.characterScript.DrawItemCards(1); managerScript.currentPhase = GameManager.TurnPhases.END; }
public bool Harvest(float amount, Inventory inventory) { if (!Harvestable) { return(false); } Harvest_Progress = Mathf.Min(MAX_HP, Harvest_Progress + amount); bool harvested = Harvest_Progress == MAX_HP; if (harvested) { Block prototype = BlockPrototypes.Instance.Get(string.IsNullOrEmpty(After_Harvest_Prototype) ? BlockPrototypes.AIR_INTERNAL_NAME : After_Harvest_Prototype); Inventory ground_drops = new Inventory(); Inventory added_items = new Inventory(); foreach (KeyValuePair <string, int> drop in Harvest_Drops) { for (int i = 0; i < drop.Value; i++) { Item item = ItemPrototypes.Instance.Get_Item(drop.Key); if (inventory.Can_Fit(item)) { inventory.Add(item); added_items.Add(item); } else { ground_drops.Add(item); } } } if (inventory == Player.Current.Inventory && !added_items.Is_Empty) { FloatingMessageManager.Instance.Show(added_items.Parse_Text(true)); } Change_To(prototype); Update_Structural_Integrity(); if (!ground_drops.Is_Empty) { Block closest = Map.Instance.Find_Closest_Passable_Block(Coordinates); if (closest != null) { ItemPile pile = new ItemPile(closest.Position, ItemPile.Prototype, Map.Instance.Entity_Container, inventory); } else { CustomLogger.Instance.Warning("Block not found"); } } } return(harvested); }
public bool IsMatchingRule(List <ItemPile> basket, DiscountRule rule) { foreach (ItemPile rulePile in rule.Piles) { //not all rules can be applied, so need to check the quantity in the basket ItemPile basketPile = basket.FirstOrDefault(p => p.Unit == rulePile.Unit && p.Quantity >= rulePile.Quantity); if (basketPile == null) { return(false); } } return(true); }
public DiscountRuleBuilder WithUnits(StockKeepingUnit unit, int quantity) { ItemPile existinPile = _itemPiles.FirstOrDefault(i => i.Unit == unit); if (existinPile != null) { existinPile.AddToPile(quantity); } else { _itemPiles.Add(new ItemPile(unit, quantity)); } return(this); }
public void DrawItemCards(int cardsToDraw) { foreach (string ability in card.abilities) { string[] splitString = ability.Split(stringSeparator, System.StringSplitOptions.None); if (splitString[0] == "draw") { cardsToDraw += AbilityScript.Draw(System.Convert.ToInt32(splitString[1])); } } ItemPile itemPileScript = GameObject.Find("ItemCardPile").GetComponent <ItemPile>(); for (int i = 0; i < cardsToDraw; i++) { itemsOwned.Add(itemPileScript.Draw()); } }
public bool Deal_Damage(float amount, bool dismantle, bool ignore_groups = false) { HP = Mathf.Max(0.0f, HP - amount); if (dismantle) { HP_Dismantled += amount; } bool broke = HP == 0.0f; if (broke) { //TODO: 0.95f bool dismantled = HP_Dismantled >= 0.95f * MAX_HP; if (Completed && dismantled && Dismantle_Drops.Count != 0) { ItemPile pile = new ItemPile(Position, ItemPile.Prototype, Map.Instance.Entity_Container, new Inventory(Dismantle_Drops)); } else if (!Completed && HP_Dismantled >= 0.95f * HP_Built && Materials_Required_To_Build.Count != 0) { ItemPile pile = new ItemPile(Position, ItemPile.Prototype, Map.Instance.Entity_Container, new Inventory(Materials_Required_To_Build)); } if (Destroy_Group_When_Removed && !ignore_groups) { foreach (BlockGroup group in Groups) { foreach (Block block in group.Blocks) { if (block.Id == Id) { continue; } block.Deal_Damage(float.MaxValue, dismantled, true); } } } Change_To(BlockPrototypes.Instance.Air); Update_Structural_Integrity(); } Update_Material(); return(broke); }
private void OnHoverIndexChanged(Grid grid, Inventory inventory, Inventory addition) { if (grid.HoverRowIndex == null) { return; } var index = grid.HoverRowIndex.Value; ItemPile itemPile = null; if (index < inventory.Items.Count) { itemPile = inventory.Items[index]; } else { itemPile = addition.Items[index - inventory.Items.Count]; } _textDescription.Text = itemPile.Item.BuildDescription(); }
public void Add(Item item, int quantity) { if (quantity == 0) { return; } ItemPile position = null; foreach (var p in Items) { if (item == p.Item) { position = p; break; } } if (position == null) { if (quantity < 0) { return; } Items.Add(new ItemPile { Item = item, Quantity = quantity }); } else { position.Quantity += quantity; if (position.Quantity <= 0) { Items.Remove(position); } } }
public void Scan(string item) { if (string.IsNullOrWhiteSpace(item)) { throw new ArgumentNullException(nameof(item)); //if this was a web api controller it should return a Bad Request } StockKeepingUnit unit = _unitRepository.GetByName(item); if (unit == null) { throw new Exception($"Item {item} does not exist in stock."); } ItemPile existingPile = _basket.FirstOrDefault(p => p.Unit == unit); if (existingPile != null) { existingPile.AddToPile(1); } else { _basket.Add(new ItemPile(unit, 1)); } }
public new void Update(float delta_time) { Stopwatch watch = Stopwatch.StartNew(); base.Update(delta_time); delta_time += (watch.ElapsedMilliseconds * 0.001f); watch.Stop(); //Movement actions = actions.Where(x => x.Type != ActionType.Moving).ToList(); if (!Collision_Data.Block_Contact) { Current_Movement = new Direction(); } if (Current_Movement.Is_Empty && Collision_Data.Block_Contact) { Rigidbody.velocity = new Vector3( 0.0f, Rigidbody.velocity.y, 0.0f ); } else if (!Current_Movement.Is_Empty) { if (Current_Movement.Y == Direction.Shift.Positive) { Vector3 v = Vector3.MoveTowards( new Vector3(), new Vector3() + (GameObject.transform.forward * (10000.0f * (int)Current_Movement.X)) + (GameObject.transform.right * (10000.0f * (int)Current_Movement.Z)) + (GameObject.transform.up * (10000.0f * (int)Current_Movement.Y)), 1.0f ); Rigidbody.AddForce(new Vector3( Jump_Strenght * VERTICAL_MOVEMENT_FORCE_MULTIPLIER * v.x, Jump_Strenght * VERTICAL_MOVEMENT_FORCE_MULTIPLIER * v.y, Jump_Strenght * VERTICAL_MOVEMENT_FORCE_MULTIPLIER * v.z ), ForceMode.Force); } else { GameObject.transform.position = Vector3.MoveTowards( GameObject.transform.position, GameObject.transform.position + (GameObject.transform.forward * (10000.0f * (int)Current_Movement.X)) + (GameObject.transform.right * (10000.0f * (int)Current_Movement.Z)), Movement_Speed * delta_time * HORIZONTAL_SPEED_MULTIPLIER ); } } if (!Current_Movement.Is_Empty) { actions.Add(new ActionData("Moving", "Moving", ActionType.Moving, false)); List <ActionData> canceled_actions = new List <ActionData>(); foreach (ActionData data in actions) { if (data.Moving_Cancels) { canceled_actions.Add(data); FloatingMessageManager.Instance.Show(string.Format("{0} canceled", data.Name)); } } foreach (ActionData canceled_action in canceled_actions) { actions.Remove(canceled_action); } } //TODO: Only 1 action per type gets progressed at time. Should for example multiple crafting action be allowed to happen at once? //Dismantling ActionData dismantling = actions.FirstOrDefault(x => x.Type == ActionType.Dismantle); if (dismantling != null) { List <Tool> tools_used = new List <Tool>(); float tool_efficency = Get_Tool_Efficency(dismantling.Target.Tools_Required_To_Dismantle, out tools_used); string verb = dismantling.Target.Dismantle_Verb.Present; if (!dismantling.Target.Deal_Damage(delta_time * Dismantling_Speed * dismantling.Target.Dismantle_Speed * tool_efficency, true)) { dismantling.Relative_Progress = 1.0f - dismantling.Target.Relative_HP; dismantling.Text = string.Format(PROGRESS_TEXT, string.Format("{0} {1}", dismantling.Target.Dismantle_Verb.Present, dismantling.Target.Name), Helper.Float_To_String(100.0f * dismantling.Relative_Progress, 0)); } else { FloatingMessageManager.Instance.Show(string.Format("{0} finished", verb)); dismantling.Finished = true; } //TODO: Tool durability } //Repairing ActionData repairing = actions.FirstOrDefault(x => x.Type == ActionType.Repair); if (repairing != null) { List <Tool> tools_used = new List <Tool>(); float tool_efficency = Get_Tool_Efficency(repairing.Target.Tools_Required_To_Build, out tools_used); if (!repairing.Target.RepairOrBuild(delta_time * Building_Speed * tool_efficency)) { repairing.Relative_Progress = repairing.Target.Relative_HP; repairing.Text = string.Format(PROGRESS_TEXT, string.Format("Repairing {0}", repairing.Target.Name), Helper.Float_To_String(100.0f * repairing.Relative_Progress, 0)); } else { FloatingMessageManager.Instance.Show("Repairing finished"); repairing.Finished = true; } //TODO: Tool durability } //Building ActionData building = actions.FirstOrDefault(x => x.Type == ActionType.Build); if (building != null) { List <Tool> tools_used = new List <Tool>(); float tool_efficency = Get_Tool_Efficency(building.Target.Tools_Required_To_Build, out tools_used); if (!building.Target.RepairOrBuild(delta_time * Building_Speed * tool_efficency)) { repairing.Relative_Progress = building.Target.Relative_HP; building.Text = string.Format(PROGRESS_TEXT, string.Format("Building {0}", building.Target.Name), Helper.Float_To_String(100.0f * repairing.Relative_Progress, 0)); } else { FloatingMessageManager.Instance.Show("Building finished"); building.Finished = true; } //TODO: Tool durability } //Harvesting ActionData harvesting = actions.FirstOrDefault(x => x.Type == ActionType.Harvest); if (harvesting != null) { List <Tool> tools_used = new List <Tool>(); float tool_efficency = Get_Tool_Efficency(harvesting.Target.Tools_Required_To_Harvest, out tools_used); if (!harvesting.Target.Harvest(delta_time * Dismantling_Speed * tool_efficency * harvesting.Target.Harvest_Speed, Inventory)) { harvesting.Relative_Progress = harvesting.Target.Harvest_Progress_Relative; harvesting.Text = string.Format(PROGRESS_TEXT, string.Format("{0} {1}", harvesting.Target.Harvest_Verb.Present, harvesting.Target.Name), Helper.Float_To_String(100.0f * harvesting.Relative_Progress, 0)); } else { FloatingMessageManager.Instance.Show("Harvesting finished"); harvesting.Finished = true; } //TODO: Tool durability } //Crafting ActionData crafting = actions.FirstOrDefault(x => x.Type == ActionType.Craft); if (crafting != null) { List <Tool> tools_used = new List <Tool>(); float tool_efficency = Get_Tool_Efficency(crafting.Recipe.Required_Tools, out tools_used); float progress = delta_time * Crafting_Speed * tool_efficency; crafting.Progress = Mathf.Clamp(crafting.Progress + progress, 0.0f, crafting.Recipe.Time); if (crafting.Progress != crafting.Recipe.Time) { crafting.Relative_Progress = crafting.Progress / crafting.Recipe.Time; crafting.Text = string.Format(PROGRESS_TEXT, crafting.Recipe.Name, Helper.Float_To_String(crafting.Relative_Progress * 100.0f, 0)); } else { bool failed = false; List <long> added_outputs = new List <long>(); foreach (KeyValuePair <string, int> output in crafting.Recipe.Outputs) { for (int i = 0; i < output.Value; i++) { Item item = ItemPrototypes.Instance.Get_Item(output.Key); if (Inventory.Can_Fit(item)) { Item added = Inventory.Add(item); added_outputs.Add(added.Id); } else { failed = true; break; } } if (failed) { break; } } if (failed) { //TODO: Refunding can push inventory over limits. Spill on ground? //TODO: Failing recipes can be used to refresh durability, this should refund exactly the same items it took foreach (KeyValuePair <string, int> input in crafting.Recipe.Inputs) { for (int i = 0; i < input.Value; i++) { Inventory.Add(ItemPrototypes.Instance.Get_Item(input.Key)); } } foreach (long id in added_outputs) { Item removed = Inventory.Remove(id); if (removed == null) { //This should not happen CustomLogger.Instance.Error(string.Format("Failed to remove item #{0} from inventory", id)); } } FloatingMessageManager.Instance.Show(string.Format("{0} failed, out of inventory space", crafting.Recipe.Name)); } else { FloatingMessageManager.Instance.Show(string.Format("{0} finished", crafting.Recipe.Name)); } if (Is_Current_Player && InventoryGUIManager.Instance.Active) { InventoryGUIManager.Instance.Update_GUI(); } crafting.Finished = true; } } //Clear actions List <ActionData> finished_actions = actions.Where(x => x.Finished).ToList(); foreach (ActionData finished_action in finished_actions) { actions.Remove(finished_action); } if (crafting != null && crafting.Finished && Is_Current_Player && CraftingMenuManager.Instance.Active) { //TODO: Move to CraftingMenuManager.Update()? CraftingMenuManager.Instance.Update_Side_Panel(); } //Pick up items foreach (Entity entity in Collision_Data.Entities) { if (!(entity is ItemPile)) { continue; } ItemPile pile = (entity as ItemPile); Inventory added_items = new Inventory(); foreach (Item item in pile.Inventory) { if (Inventory.Can_Fit(item)) { Inventory.Add(item); added_items.Add(item); } } foreach (Item item in added_items) { pile.Inventory.Remove(item); } if (pile.Inventory.Is_Empty) { pile.Delete(); Map.Instance.Remove_Entity(pile); } if (!added_items.Is_Empty) { FloatingMessageManager.Instance.Show(added_items.Parse_Text(true)); } } }
public ItemPile(Vector3 position, ItemPile prototype, GameObject container, Inventory items) : base(position, prototype, container) { Inventory = items.Clone(); }
/// <summary> /// Per frame update /// </summary> private void Update() { if (!Active) { return; } Block_Data_Container.SetActive(false); Mob_Data_Container.SetActive(false); Item_Pile_Data_Container.SetActive(false); if (Target is Block) { Block block = Target as Block; if (block.Is_Air) { Active = false; return; } Block_Data_Container.SetActive(true); Name_Text.text = block.Name; Image.sprite = SpriteManager.Instance.Get_Sprite(block.UI_Sprite, block.UI_Sprite_Type); Block_Coordinates_Text.text = block.Coordinates.Parse_Text(true, true); //Block_HP_Text.text = string.Format("HP: {0} / {1}", Helper.Float_To_String(block.HP, 0), block.MAX_HP); Block_HP_Text.text = block.Groups.Count != 0 ? block.Groups[0].Name + " #" + block.Groups[0].Id : ""; Dictionary <string, string> actions = new Dictionary <string, string>(); List <string> possible_actions = new List <string>(); string s = null; default_action = null; if (!block.Indestructible) { if (block.Harvestable) { string harvest_button = KeyboardManager.Instance.Get_Key_Bind("Harvest block"); actions.Add(harvest_button, block.Harvest_Verb.Base); if (Player.Current.Can_Harvest(block, out s, true)) { possible_actions.Add(harvest_button); if (default_action == null) { default_action = harvest_button; } } } if (block.Can_Be_Dismantled) { string dismantle_button = KeyboardManager.Instance.Get_Key_Bind("Dismantle block"); actions.Add(dismantle_button, block.Dismantle_Verb.Base); if (Player.Current.Can_Dismantle(block, out s, true)) { possible_actions.Add(dismantle_button); if (default_action == null) { default_action = dismantle_button; } } } if (block.Can_Be_Repaired) { string repair_button = KeyboardManager.Instance.Get_Key_Bind("Repair block"); actions.Add(repair_button, "Repair"); if (Player.Current.Can_Repair(block, out s, true)) { possible_actions.Add(repair_button); if (default_action == null) { default_action = repair_button; } } } } if (actions.Count != 0) { StringBuilder builder = new StringBuilder(); foreach (KeyValuePair <string, string> pair in actions) { if (!possible_actions.Contains(pair.Key)) { builder.Append("<i>"); } if (default_action == pair.Key) { builder.Append("["); } builder.Append(pair.Key).Append(" = ").Append(pair.Value); if (default_action == pair.Key) { builder.Append("]"); } if (!possible_actions.Contains(pair.Key)) { builder.Append("</i>"); } builder.Append(", "); } Block_Actions_Text.text = builder.Remove(builder.Length - 2, 2).ToString(); } else { Block_Actions_Text.text = string.Empty; } } else if (Target is Mob) { Mob_Data_Container.SetActive(true); Mob mob = Target as Mob; Name_Text.text = mob.Name; Image.sprite = SpriteManager.Instance.Get_Sprite(SpriteManager.UI_PLACEHOLDER_NAME, SpriteManager.SpriteType.UI); Block_HP_Text.text = string.Format("HP: {0} / {1}", Helper.Float_To_String(mob.HP, 0), mob.MAX_HP); } else if (Target is ItemPile) { Item_Pile_Data_Container.SetActive(true); ItemPile pile = Target as ItemPile; Name_Text.text = Helper.Plural("Item", pile.Inventory.Count); Item top_most = pile.Top_Most_Item; if (top_most != null) { Image.sprite = SpriteManager.Instance.Get_Sprite(top_most.UI_Sprite, top_most.UI_Sprite_Type); } else { Image.sprite = SpriteManager.Instance.Get_Sprite(SpriteManager.UI_PLACEHOLDER_NAME, SpriteManager.SpriteType.UI); } Item_Pile_Row_1_Text.gameObject.SetActive(false); Item_Pile_Row_2_Text.gameObject.SetActive(false); Item_Pile_Row_3_Text.gameObject.SetActive(false); Item_Pile_Row_4_Text.gameObject.SetActive(false); Item_Pile_Row_5_Text.gameObject.SetActive(false); Dictionary <string, int> counts = pile.Inventory.Count_Dictionary_Names.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value); int row = 1; int total = 0; //TODO: Messy code foreach (KeyValuePair <string, int> data in counts) { if (row == 1) { Item_Pile_Row_1_Text.gameObject.SetActive(true); Item_Pile_Row_1_Text.text = string.Format("{0}x {1}", data.Value, data.Key); total += data.Value; } else if (row == 2) { Item_Pile_Row_2_Text.gameObject.SetActive(true); Item_Pile_Row_2_Text.text = string.Format("{0}x {1}", data.Value, data.Key); total += data.Value; } else if (row == 3) { Item_Pile_Row_3_Text.gameObject.SetActive(true); Item_Pile_Row_3_Text.text = string.Format("{0}x {1}", data.Value, data.Key); total += data.Value; } else if (row == 4) { Item_Pile_Row_4_Text.gameObject.SetActive(true); Item_Pile_Row_4_Text.text = string.Format("{0}x {1}", data.Value, data.Key); total += data.Value; } else if (row == 5) { Item_Pile_Row_5_Text.gameObject.SetActive(true); Item_Pile_Row_5_Text.text = string.Format("{0}x {1}", data.Value, data.Key); } else { Item_Pile_Row_5_Text.text = string.Format("+{0} other item{1}", pile.Inventory.Count - total, Helper.Plural(pile.Inventory.Count - total)); break; } row++; } } }