MVC shop project for software architecture, in Unity.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

63 lines
2.6 KiB

using System;
using System.Configuration;
using UnityEngine;
/// <summary>
/// This is a concrete, empty model for the buy state of the shop for you to implement
/// </summary>
public class BuyModel : ShopModel
{
private Inventory tradePartner;
public BuyModel(float pPriceModifier, int pItemCount, int pMoney) : base(pPriceModifier, pItemCount, pMoney)
{
}
// Rather than modifying the whole class, we just reuse the existing stuff but don't make it create any items.
// This makes it less work to make proper functionality, without having to break any potential old functionality.
// Additionally, saves us work having to rip out the inventory's own ability to generate items.
// Edit: Nevermind, let's just allow setting the inventory in the constructor, for shared inventories between models!
public BuyModel(ShopObject pShopInitials, Inventory inventory = null) : this(pShopInitials.PriceModifier, 0, 0)
{
if (inventory != null) this.inventory = inventory;
}
//------------------------------------------------------------------------------------------------------------------------
// ConfirmSelectedItem()
//------------------------------------------------------------------------------------------------------------------------
//Currently it just removes the selected item from the shop's inventory, rewrite this function and don't forget the unit test.
public override void ConfirmSelectedItem()
{
if (tradePartner == null)
{
Debug.Assert(false,"Could not make trade because the shop has no trade partner");
throw new NoTradePartnerException();
}
// if (tradePartner.Money < GetSelectedItem().basePrice * priceModifier)
// {
// var up = new NotEnoughMoneyException();
// throw up; // If you find this, you can keep it!
// }
var item = GetSelectedItem();
tradePartner.ChangeBalance((int) (-item.basePrice * priceModifier));
OnRemove(item); // If there's a view subscribed, this will probably remove the item from it
inventory.RemoveItemByIndex(selectedItemIndex); // Before removing the item from the model's actual inventory
tradePartner.AddItem(item);
SelectItemByIndex(selectedItemIndex >= inventory.GetItemCount() ? --selectedItemIndex : selectedItemIndex);
}
public override void SetTradePartner(Inventory tradePartner)
{
this.tradePartner = tradePartner;
}
}
public class NoTradePartnerException : Exception
{
}
public class InsufficientMoneyException : Exception
{
}