private bool BuyItem(GUIComponent component, object obj) { PurchasedItem pi = obj as PurchasedItem; if (pi == null || pi.ItemPrefab == null) { return(false); } if (GameMain.Client != null && !GameMain.Client.HasPermission(Networking.ClientPermissions.ManageCampaign)) { return(false); } PriceInfo priceInfo = pi.ItemPrefab.GetPrice(campaign.Map.CurrentLocation); if (priceInfo == null || priceInfo.BuyPrice > campaign.Money) { return(false); } campaign.CargoManager.PurchaseItem(pi.ItemPrefab, 1); GameMain.Client?.SendCampaignState(); return(false); }
public void SellItem(PurchasedItem purchasedItem, int quantity = 1) { quantity = Math.Min(purchasedItem.Quantity, quantity); campaign.Money += purchasedItem.ItemPrefab.GetPrice(campaign.Map.CurrentLocation).BuyPrice *quantity; purchasedItem.Quantity -= quantity; if (purchasedItem != null && purchasedItem.Quantity <= 0) { PurchasedItems.Remove(purchasedItem); } OnItemsChanged?.Invoke(); }
private bool SellItem(GUIComponent component, object obj) { PurchasedItem pi = obj as PurchasedItem; if (pi == null || pi.itemPrefab == null) return false; if (GameMain.Client != null && !GameMain.Client.HasPermission(Networking.ClientPermissions.ManageCampaign)) { return false; } campaign.CargoManager.SellItem(pi.itemPrefab, 1); GameMain.Client?.SendCampaignState(); return false; }
public void SellItem(ItemPrefab item, int quantity = 1) { PurchasedItem purchasedItem = PurchasedItems.Find(pi => pi.itemPrefab == item); if (purchasedItem != null && purchasedItem.quantity - quantity > 0) { purchasedItem.quantity -= quantity; } else { PurchasedItems.Remove(purchasedItem); } OnItemsChanged?.Invoke(); }
public void PurchaseItem(ItemPrefab item, int quantity = 1) { PurchasedItem purchasedItem = PurchasedItems.Find(pi => pi.ItemPrefab == item); campaign.Money -= item.GetPrice(campaign.Map.CurrentLocation).BuyPrice *quantity; if (purchasedItem != null) { purchasedItem.Quantity += quantity; } else { purchasedItem = new PurchasedItem(item, quantity); purchasedItems.Add(purchasedItem); } OnItemsChanged?.Invoke(); }
public void PurchaseItem(ItemPrefab item, int Quantity = 1) { PurchasedItem purchasedItem = PurchasedItems.Find(pi => pi.itemPrefab == item); if (purchasedItem != null && Quantity == 1) { campaign.Money -= item.Price; purchasedItem.quantity += 1; } else { campaign.Money -= (item.Price * Quantity); purchasedItem = new PurchasedItem(item, Quantity); purchasedItems.Add(purchasedItem); } OnItemsChanged?.Invoke(); }
public void ModifyItemQuantityInBuyCrate(ItemPrefab itemPrefab, int changeInQuantity) { PurchasedItem itemInCrate = ItemsInBuyCrate.Find(i => i.ItemPrefab == itemPrefab); if (itemInCrate != null) { itemInCrate.Quantity += changeInQuantity; if (itemInCrate.Quantity < 1) { ItemsInBuyCrate.Remove(itemInCrate); } } else if (changeInQuantity > 0) { itemInCrate = new PurchasedItem(itemPrefab, changeInQuantity); ItemsInBuyCrate.Add(itemInCrate); } OnItemsInBuyCrateChanged?.Invoke(); }
public void PurchaseItems(List <PurchasedItem> itemsToPurchase, bool removeFromCrate) { // Check all the prices before starting the transaction // to make sure the modifiers stay the same for the whole transaction Dictionary <ItemPrefab, int> buyValues = GetBuyValuesAtCurrentLocation(itemsToPurchase.Select(i => i.ItemPrefab)); foreach (PurchasedItem item in itemsToPurchase) { // Add to the purchased items var purchasedItem = PurchasedItems.Find(pi => pi.ItemPrefab == item.ItemPrefab); if (purchasedItem != null) { purchasedItem.Quantity += item.Quantity; } else { purchasedItem = new PurchasedItem(item.ItemPrefab, item.Quantity); PurchasedItems.Add(purchasedItem); } // Exchange money var itemValue = item.Quantity * buyValues[item.ItemPrefab]; campaign.Money -= itemValue; Location.StoreCurrentBalance += itemValue; if (removeFromCrate) { // Remove from the shopping crate var crateItem = ItemsInBuyCrate.Find(pi => pi.ItemPrefab == item.ItemPrefab); if (crateItem != null) { crateItem.Quantity -= item.Quantity; if (crateItem.Quantity < 1) { ItemsInBuyCrate.Remove(crateItem); } } } } OnPurchasedItemsChanged?.Invoke(); }
public void PurchaseItems(List <PurchasedItem> itemsToPurchase, bool removeFromCrate) { foreach (PurchasedItem item in itemsToPurchase) { // Add to the purchased items var purchasedItem = PurchasedItems.Find(pi => pi.ItemPrefab == item.ItemPrefab); if (purchasedItem != null) { purchasedItem.Quantity += item.Quantity; } else { purchasedItem = new PurchasedItem(item.ItemPrefab, item.Quantity); PurchasedItems.Add(purchasedItem); } // Exchange money var itemValue = GetBuyValueAtCurrentLocation(item); campaign.Money -= itemValue; Location.StoreCurrentBalance += itemValue; if (removeFromCrate) { // Remove from the shopping crate var crateItem = ItemsInBuyCrate.Find(pi => pi.ItemPrefab == item.ItemPrefab); if (crateItem != null) { crateItem.Quantity -= item.Quantity; if (crateItem.Quantity < 1) { ItemsInBuyCrate.Remove(crateItem); } } } } OnPurchasedItemsChanged?.Invoke(); }
public int GetBuyValueAtCurrentLocation(PurchasedItem item) => item?.ItemPrefab != null && Location != null? item.Quantity *Location.GetAdjustedItemBuyPrice(item.ItemPrefab) : 0;
private GUIComponent CreateItemFrame(PurchasedItem pi, PriceInfo priceInfo, GUIListBox listBox, int width) { GUIFrame frame = new GUIFrame(new RectTransform(new Point(listBox.Content.Rect.Width, (int)(GUI.Scale * 50)), listBox.Content.RectTransform), style: "ListBoxElement") { UserData = pi, ToolTip = pi.ItemPrefab.Description }; var content = new GUILayoutGroup(new RectTransform(Vector2.One, frame.RectTransform), isHorizontal: true) { RelativeSpacing = 0.02f, Stretch = true }; ScalableFont font = listBox.Rect.Width < 280 ? GUI.SmallFont : GUI.Font; Sprite itemIcon = pi.ItemPrefab.InventoryIcon ?? pi.ItemPrefab.sprite; if (itemIcon != null) { GUIImage img = new GUIImage(new RectTransform(new Point((int)(content.Rect.Height * 0.8f)), content.RectTransform, Anchor.CenterLeft), itemIcon, scaleToFit: true) { Color = itemIcon == pi.ItemPrefab.InventoryIcon ? pi.ItemPrefab.InventoryIconColor : pi.ItemPrefab.SpriteColor }; img.RectTransform.MaxSize = img.Rect.Size; //img.Scale = Math.Min(Math.Min(40.0f / img.SourceRect.Width, 40.0f / img.SourceRect.Height), 1.0f); } GUITextBlock textBlock = new GUITextBlock(new RectTransform(new Vector2(0.6f, 1.0f), content.RectTransform), pi.ItemPrefab.Name, font: font) { ToolTip = pi.ItemPrefab.Description }; new GUITextBlock(new RectTransform(new Vector2(0.2f, 1.0f), content.RectTransform), priceInfo.BuyPrice.ToString(), font: font, textAlignment: Alignment.CenterRight) { ToolTip = pi.ItemPrefab.Description }; //If its the store menu, quantity will always be 0 GUINumberInput amountInput = null; if (pi.Quantity > 0) { amountInput = new GUINumberInput(new RectTransform(new Vector2(0.3f, 1.0f), content.RectTransform), GUINumberInput.NumberType.Int) { MinValueInt = 0, MaxValueInt = 100, UserData = pi, IntValue = pi.Quantity }; amountInput.OnValueChanged += (numberInput) => { PurchasedItem purchasedItem = numberInput.UserData as PurchasedItem; //Attempting to buy if (numberInput.IntValue > purchasedItem.Quantity) { int quantity = numberInput.IntValue - purchasedItem.Quantity; //Cap the numberbox based on the amount we can afford. quantity = Campaign.Money <= 0 ? 0 : Math.Min((int)(Campaign.Money / (float)priceInfo.BuyPrice), quantity); for (int i = 0; i < quantity; i++) { BuyItem(numberInput, purchasedItem); } numberInput.IntValue = purchasedItem.Quantity; } //Attempting to sell else { int quantity = purchasedItem.Quantity - numberInput.IntValue; for (int i = 0; i < quantity; i++) { SellItem(numberInput, purchasedItem); } } }; } listBox.RecalculateChildren(); content.Recalculate(); content.RectTransform.RecalculateChildren(true, true); amountInput?.LayoutGroup.Recalculate(); textBlock.Text = ToolBox.LimitString(textBlock.Text, textBlock.Font, textBlock.Rect.Width); content.RectTransform.IsFixedSize = true; content.RectTransform.Children.ForEach(c => c.IsFixedSize = true); return(frame); }
private void CreateItemFrame(PurchasedItem pi, GUIListBox listBox, int width) { GUIFrame frame = new GUIFrame(new Rectangle(0, 0, 0, 50), "ListBoxElement", listBox); frame.UserData = pi; frame.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f); frame.ToolTip = pi.itemPrefab.Description; ScalableFont font = listBox.Rect.Width < 280 ? GUI.SmallFont : GUI.Font; GUITextBlock textBlock = new GUITextBlock( new Rectangle(50, 0, 0, 25), pi.itemPrefab.Name, null, null, Alignment.Left, Alignment.CenterX | Alignment.Left, "", frame); textBlock.Font = font; textBlock.Padding = new Vector4(5.0f, 0.0f, 5.0f, 0.0f); textBlock.ToolTip = pi.itemPrefab.Description; if (pi.itemPrefab.sprite != null) { GUIImage img = new GUIImage(new Rectangle(0, 0, 40, 40), pi.itemPrefab.sprite, Alignment.CenterLeft, frame); img.Color = pi.itemPrefab.SpriteColor; img.Scale = Math.Min(Math.Min(40.0f / img.SourceRect.Width, 40.0f / img.SourceRect.Height), 1.0f); } textBlock = new GUITextBlock( new Rectangle(width - 160, 0, 80, 25), pi.itemPrefab.Price.ToString(), null, null, Alignment.TopLeft, Alignment.TopLeft, "", frame); textBlock.Font = font; textBlock.ToolTip = pi.itemPrefab.Description; //If its the store menu, quantity will always be 0 if (pi.quantity > 0) { var amountInput = new GUINumberInput(new Rectangle(width - 80, 0, 50, 40), "", GUINumberInput.NumberType.Int, frame); amountInput.MinValueInt = 0; amountInput.MaxValueInt = 1000; amountInput.UserData = pi; amountInput.IntValue = pi.quantity; amountInput.OnValueChanged += (numberInput) => { PurchasedItem purchasedItem = numberInput.UserData as PurchasedItem; //Attempting to buy if (numberInput.IntValue > purchasedItem.quantity) { int quantity = numberInput.IntValue - purchasedItem.quantity; //Cap the numberbox based on the amount we can afford. quantity = campaign.Money <= 0 ? 0 : Math.Min((int)(Campaign.Money / (float)purchasedItem.itemPrefab.Price), quantity); for (int i = 0; i < quantity; i++) { BuyItem(numberInput, purchasedItem); } numberInput.IntValue = purchasedItem.quantity; } //Attempting to sell else { int quantity = purchasedItem.quantity - numberInput.IntValue; for (int i = 0; i < quantity; i++) { SellItem(numberInput, purchasedItem); } } }; } }
private void CreateItemFrame(PurchasedItem pi, PriceInfo priceInfo, GUIListBox listBox, int width) { GUIFrame frame = new GUIFrame(new RectTransform(new Point(listBox.Rect.Width, 50), listBox.Content.RectTransform), style: "ListBoxElement") { UserData = pi, ToolTip = pi.ItemPrefab.Description }; ScalableFont font = listBox.Rect.Width < 280 ? GUI.SmallFont : GUI.Font; GUITextBlock textBlock = new GUITextBlock(new RectTransform(new Point(listBox.Rect.Width - (pi.Quantity > 0 ? 160 : 120), 25), frame.RectTransform, Anchor.CenterLeft) { AbsoluteOffset = new Point(40, 0), }, pi.ItemPrefab.Name, font: font) { ToolTip = pi.ItemPrefab.Description }; textBlock.Text = ToolBox.LimitString(textBlock.Text, textBlock.Font, textBlock.Rect.Width); Sprite itemIcon = pi.ItemPrefab.InventoryIcon ?? pi.ItemPrefab.sprite; if (itemIcon != null) { GUIImage img = new GUIImage(new RectTransform(new Point(40, 40), frame.RectTransform, Anchor.CenterLeft), itemIcon) { Color = itemIcon == pi.ItemPrefab.InventoryIcon ? pi.ItemPrefab.InventoryIconColor : pi.ItemPrefab.SpriteColor }; img.Scale = Math.Min(Math.Min(40.0f / img.SourceRect.Width, 40.0f / img.SourceRect.Height), 1.0f); } textBlock = new GUITextBlock(new RectTransform(new Point(60, 25), frame.RectTransform, Anchor.CenterRight) { AbsoluteOffset = new Point(pi.Quantity > 0 ? 70 : 25, 0) }, priceInfo.BuyPrice.ToString(), font: font, textAlignment: Alignment.CenterRight) { ToolTip = pi.ItemPrefab.Description }; //If its the store menu, quantity will always be 0 if (pi.Quantity > 0) { var amountInput = new GUINumberInput(new RectTransform(new Point(50, 40), frame.RectTransform, Anchor.CenterRight) { AbsoluteOffset = new Point(20, 0) }, GUINumberInput.NumberType.Int) { MinValueInt = 0, MaxValueInt = 1000, UserData = pi, IntValue = pi.Quantity }; amountInput.OnValueChanged += (numberInput) => { PurchasedItem purchasedItem = numberInput.UserData as PurchasedItem; //Attempting to buy if (numberInput.IntValue > purchasedItem.Quantity) { int quantity = numberInput.IntValue - purchasedItem.Quantity; //Cap the numberbox based on the amount we can afford. quantity = Campaign.Money <= 0 ? 0 : Math.Min((int)(Campaign.Money / (float)priceInfo.BuyPrice), quantity); for (int i = 0; i < quantity; i++) { BuyItem(numberInput, purchasedItem); } numberInput.IntValue = purchasedItem.Quantity; } //Attempting to sell else { int quantity = purchasedItem.Quantity - numberInput.IntValue; for (int i = 0; i < quantity; i++) { SellItem(numberInput, purchasedItem); } } }; } }