Browse Source

Add basic upgrading system with unfinished UI

master
Devin 4 years ago
parent
commit
86c7e486d0
  1. 3
      Assets/Resources/UpgradeShop.asset
  2. 2
      Assets/Scenes/NewShop.unity
  3. 13
      Assets/Scripts/Shop/Components/UpgradeModelComponent.cs
  4. 11
      Assets/Scripts/Shop/Components/UpgradeModelComponent.cs.meta
  5. 6
      Assets/Scripts/Shop/Model/Item.cs
  6. 19
      Assets/Scripts/Shop/Model/ItemArmor.cs
  7. 10
      Assets/Scripts/Shop/Model/ItemPotion.cs
  8. 10
      Assets/Scripts/Shop/Model/ItemWeapon.cs
  9. 8
      Assets/Scripts/Shop/Model/ShopModel.cs
  10. 61
      Assets/Scripts/Shop/Model/UpgradeModel.cs
  11. 11
      Assets/Scripts/Shop/Model/UpgradeModel.cs.meta
  12. 1
      Assets/Scripts/Shop/Scriptable Objects/ShopObject.cs
  13. 3
      Assets/Scripts/Shop/View/ShopViewList.cs
  14. 13
      Assets/Scripts/Shop/View/ViewItemInfoPanel.cs

3
Assets/Resources/UpgradeShop.asset

@ -12,4 +12,5 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3145dd14dac0cf84a909862b61ec2893, type: 3}
m_Name: UpgradeShop
m_EditorClassIdentifier:
PriceModifier: 2
PriceModifier: 5
UpgradeFactor: 1.5

2
Assets/Scenes/NewShop.unity

@ -8577,7 +8577,7 @@ MonoBehaviour:
m_GameObject: {fileID: 842666262}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8e211938370fd3d43a99aae3848bdd1c, type: 3}
m_Script: {fileID: 11500000, guid: c0435c4334f16f74d9eb1c7fbd973641, type: 3}
m_Name:
m_EditorClassIdentifier:
inventory: {fileID: 1237921497}

13
Assets/Scripts/Shop/Components/UpgradeModelComponent.cs

@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UpgradeModelComponent : ModelComponent
{
[SerializeField] private ShopObject shop;
protected override void CreateModel()
{
Debug.Assert(shop != null,"Shop model initial object never assigned!",this);
model = new UpgradeModel(shop);
}
}

11
Assets/Scripts/Shop/Components/UpgradeModelComponent.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c0435c4334f16f74d9eb1c7fbd973641
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: -10
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

6
Assets/Scripts/Shop/Model/Item.cs

@ -11,7 +11,7 @@ public abstract class Item
public readonly string description;
public readonly ItemRarity rarity;
public int basePrice { get; private set; } // This is the base price for the item, the buying and selling prices can be
public int basePrice { get; protected set; } // This is the base price for the item, the buying and selling prices can be
// generated based on this value.
//------------------------------------------------------------------------------------------------------------------------
@ -31,6 +31,10 @@ public abstract class Item
// Returns a nicely formatted text about the item's stats. Implementation-specific, because different types of items generate these differently
public abstract string GetStats();
public abstract void Upgrade(float upgradeFactor); // Each type of item will upgrade differently
public abstract int GetUpgradeCosts(float upgradeFactor); // How much would it cost to upgrade this item by that amount? Base price
}
/// <summary>

19
Assets/Scripts/Shop/Model/ItemArmor.cs

@ -4,8 +4,10 @@ using UnityEngine;
public class ItemArmor : Item
{
public readonly int Defense;
public readonly int Block;
public int Defense { get; private set; }
public int Block { get; private set; }
public ItemArmor(string name, string iconName, int pbasePrice,int pDefense, int pBlock, string descr = "", ItemRarity rarity = ItemRarity.Common) : base(name, iconName, pbasePrice, descr,rarity)
{
Block = pBlock;
@ -22,4 +24,17 @@ public class ItemArmor : Item
{
return "Defense: " + Defense + "\tBlock: " + Block;
}
public override void Upgrade(float upgradeFactor)
{
Block = (int) (Block * upgradeFactor); // Don't wanna randomise upgrades for now, to be honest
Defense = (int) (Defense * upgradeFactor);
// We increase value, but only by half as much as the upgrade improved stats. No one wants to buy a bad item upgraded to be passable. Cost to do so is higher by as much as the upgrade shop charges extra
basePrice += (int)(GetUpgradeCosts(upgradeFactor) * 0.5); // The goal is to make upgrading an endless money sink, and incentivise buying better base items
}
public override int GetUpgradeCosts(float upgradeFactor)
{
return (int) (((basePrice * upgradeFactor) - basePrice));
}
}

10
Assets/Scripts/Shop/Model/ItemPotion.cs

@ -27,6 +27,16 @@ public class ItemPotion : Item
? "Strength: " + Effect + "\tType: " + Type + "\tTime: " + Time
: "Strength: " + Effect + "\tType: " + Type;
}
public override void Upgrade(float upgradeFactor)
{
throw new NotImplementedException();
}
public override int GetUpgradeCosts(float upgradeFactor)
{
return 0;
}
}
[Serializable]

10
Assets/Scripts/Shop/Model/ItemWeapon.cs

@ -21,4 +21,14 @@ public class ItemWeapon : Item
{
return "Attack: " + Attack + "\tDamage: " + Damage; // Empty placeholder
}
public override void Upgrade(float upgradeFactor)
{
throw new System.NotImplementedException();
}
public override int GetUpgradeCosts(float upgradeFactor)
{
return 0;
}
}

8
Assets/Scripts/Shop/Model/ShopModel.cs

@ -18,7 +18,7 @@ public abstract class ShopModel : IModelObservable<Item>
public float PriceModifier => priceModifier;
private List<IShopModelObserver<Item>> observers = new List<IShopModelObserver<Item>>();
protected List<IShopModelObserver<Item>> observers = new List<IShopModelObserver<Item>>();
//------------------------------------------------------------------------------------------------------------------------
// ShopModel()
@ -101,6 +101,12 @@ public abstract class ShopModel : IModelObservable<Item>
public abstract void SetTradePartner(Inventory tradePartner);
// So, certain models may charge something fairly unrelated to the base price. Keyword upgrade shops. Let's make it possible to figure out what a shop would charge for an item
public virtual int GetPriceForItem(Item item)
{
return (int) (item.basePrice * priceModifier); // Default to the logical thing here
}
// Observer pattern. Anything that wants to know what happens regarding item selection subscribes to this.
// This could mean all items of this model and its views do it, or all views do it.
public IDisposable RegisterObserver(IShopModelObserver<Item> observer)

61
Assets/Scripts/Shop/Model/UpgradeModel.cs

@ -0,0 +1,61 @@
using System;
using System.Configuration;
using UnityEngine;
/// <summary>
/// The upgrade model differs a little from the other two. We do not care for a trade partner, only for ourselves.
/// On that note, we do use the price modifier to determine how expensive an upgrade is gonna be.
/// We do not upgrade by class, we merely improve the stats until it's no longer affordable. Self-balancing, right?
/// Still, upgrading a wooden sword to the point it's as good as a diamond sword (but 3x more expensive) technically does not make it more rare. Just impressive, really...
/// </summary>
public class UpgradeModel : ShopModel
{
private float upgradeFactor; // How much would an upgrade improve stats?
public UpgradeModel(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 UpgradeModel(ShopObject pShopInitials, Inventory inventory = null) : this(pShopInitials.PriceModifier, 0, 0)
{
if (inventory != null) this.inventory = inventory;
upgradeFactor = pShopInitials.UpgradeFactor;
}
//------------------------------------------------------------------------------------------------------------------------
// 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()
{
var item = GetSelectedItem();
inventory.ChangeBalance(-GetPriceForItem(item)); // Expensive upgrading!
// If no exception was thrown, we just assume we continue the upgrade!
item.Upgrade(upgradeFactor);
triggerTransaction();
}
public override void SetTradePartner(Inventory tradePartner)
{
//this.tradePartner = tradePartner; No need!
}
public override int GetPriceForItem(Item item)
{
return (int) (item.GetUpgradeCosts(upgradeFactor) * priceModifier);
}
// This thing triggers transaction events for each upgrade!
private void triggerTransaction()
{
foreach (var observer in observers)
{
observer.OnTransaction(inventory.Money);
}
}
}

11
Assets/Scripts/Shop/Model/UpgradeModel.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 32a48e31df172e24b8d2aa672369a274
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

1
Assets/Scripts/Shop/Scriptable Objects/ShopObject.cs

@ -9,4 +9,5 @@ public class ShopObject : ScriptableObject
{
//public int Money;
public float PriceModifier; // How much more expensive than retail price does this shop sell stuff?
public float UpgradeFactor = 1; // Also default this one to 1.0. Used by anything that can upgrade stuff, to determine how much of an upgrade (or downgrade) it is
}

3
Assets/Scripts/Shop/View/ShopViewList.cs

@ -129,6 +129,7 @@ public class ShopViewList : ShopView, IShopModelObserver<Item>
public void OnTransaction(int moneyDelta)
{
// Well we bought something so let's update the panel in any case
infoPanel?.Refresh();
}
}

13
Assets/Scripts/Shop/View/ViewItemInfoPanel.cs

@ -18,6 +18,9 @@ public class ViewItemInfoPanel : MonoBehaviour
private List<ItemDescriptionDisplay> descr;
private List<ItemClassDisplay> rarity;
private List<ItemIconDisplay> icons;
private Item lastItem; // Should we want to refresh for whatever reason
private ShopModel lastShop;
private void Awake()
{
UpdateComponentInfo();
@ -37,9 +40,16 @@ public class ViewItemInfoPanel : MonoBehaviour
// TODO: Create and add all other types in here as components!
}
public void Refresh()
{
SetItemInfo(lastItem,lastShop);
}
// When this is set, it updates all relevant game objects to reflect the item's propertiese.
public void SetItemInfo(Item item,ShopModel owner = null)
{
lastShop = owner;
lastItem = item;
var multiplier = owner?.PriceModifier ?? 1;
if (item == null)
{
@ -65,7 +75,8 @@ public class ViewItemInfoPanel : MonoBehaviour
foreach (var price in prices)
{
price.SetPrice((int) (item.basePrice * multiplier));
var cost = owner?.GetPriceForItem(item);
price.SetPrice(cost ?? (int) (item.basePrice * multiplier)); // If the shop has any special pricing, show that here
}
foreach (var descriptionDisplay in descr)

Loading…
Cancel
Save