From 7f35404c9647887199d5e8f3df61e079e7de641e Mon Sep 17 00:00:00 2001 From: Devin Date: Tue, 16 Nov 2021 19:47:54 +0100 Subject: [PATCH] Add price modifier to view --- Assets/Resources/InventoryStart.asset | 16 +++++++++ Assets/Resources/InventoryStart.asset.meta | 8 +++++ Assets/Scenes/NewShop.unity | 19 +++++++++-- .../Components/InventoryModelComponent.cs | 13 ++++++++ .../InventoryModelComponent.cs.meta | 11 +++++++ .../ShopModelComponent.cs | 0 .../ShopModelComponent.cs.meta | 0 Assets/Scripts/Shop/Model/SellModel.cs | 33 +++++++++++++++++++ Assets/Scripts/Shop/Model/SellModel.cs.meta | 11 +++++++ Assets/Scripts/Shop/Model/ShopModel.cs | 5 ++- Assets/Scripts/Shop/View/ShopViewGrid.cs | 2 +- Assets/Scripts/Shop/View/ShopViewList.cs | 4 +-- Assets/Scripts/Shop/View/ViewItemContainer.cs | 6 ++-- .../Shop/View/ViewItemContainerGrid.cs | 4 +-- .../Shop/View/ViewItemContainerList.cs | 4 +-- Assets/Scripts/Shop/View/ViewItemInfoPanel.cs | 5 +-- 16 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 Assets/Resources/InventoryStart.asset create mode 100644 Assets/Resources/InventoryStart.asset.meta create mode 100644 Assets/Scripts/Shop/Components/InventoryModelComponent.cs create mode 100644 Assets/Scripts/Shop/Components/InventoryModelComponent.cs.meta rename Assets/Scripts/Shop/{Model => Components}/ShopModelComponent.cs (100%) rename Assets/Scripts/Shop/{Model => Components}/ShopModelComponent.cs.meta (100%) create mode 100644 Assets/Scripts/Shop/Model/SellModel.cs create mode 100644 Assets/Scripts/Shop/Model/SellModel.cs.meta diff --git a/Assets/Resources/InventoryStart.asset b/Assets/Resources/InventoryStart.asset new file mode 100644 index 0000000..ca7ba63 --- /dev/null +++ b/Assets/Resources/InventoryStart.asset @@ -0,0 +1,16 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3145dd14dac0cf84a909862b61ec2893, type: 3} + m_Name: InventoryStart + m_EditorClassIdentifier: + Money: 6500 + PriceModifier: 0.75 diff --git a/Assets/Resources/InventoryStart.asset.meta b/Assets/Resources/InventoryStart.asset.meta new file mode 100644 index 0000000..e6436cd --- /dev/null +++ b/Assets/Resources/InventoryStart.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5832c828db6d00c42a09217f7b743233 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/NewShop.unity b/Assets/Scenes/NewShop.unity index cc6bbd2..d518e72 100644 --- a/Assets/Scenes/NewShop.unity +++ b/Assets/Scenes/NewShop.unity @@ -1289,7 +1289,7 @@ MonoBehaviour: m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] - m_text: Buy + m_text: Sell m_isRightToLeft: 0 m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} @@ -9797,6 +9797,7 @@ GameObject: m_Component: - component: {fileID: 1237921496} - component: {fileID: 1237921497} + - component: {fileID: 1237921498} m_Layer: 5 m_Name: Inventory m_TagString: Untagged @@ -9839,6 +9840,20 @@ MonoBehaviour: m_EditorClassIdentifier: factory: {fileID: 11400000, guid: aed79a62cfb941741bd92ea0a643b752, type: 2} shop: {fileID: 11400000, guid: 366ca79a0c4a2df4fac53ddd28f65f1a, type: 2} +--- !u!114 &1237921498 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1237921495} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8e211938370fd3d43a99aae3848bdd1c, type: 3} + m_Name: + m_EditorClassIdentifier: + factory: {fileID: 11400000, guid: aed79a62cfb941741bd92ea0a643b752, type: 2} + shop: {fileID: 11400000, guid: 5832c828db6d00c42a09217f7b743233, type: 2} --- !u!1 &1287258904 GameObject: m_ObjectHideFlags: 0 @@ -12380,7 +12395,7 @@ MonoBehaviour: m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] - m_text: Buy + m_text: Sell m_isRightToLeft: 0 m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} diff --git a/Assets/Scripts/Shop/Components/InventoryModelComponent.cs b/Assets/Scripts/Shop/Components/InventoryModelComponent.cs new file mode 100644 index 0000000..f121d98 --- /dev/null +++ b/Assets/Scripts/Shop/Components/InventoryModelComponent.cs @@ -0,0 +1,13 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class InventoryModelComponent : ModelComponent +{ + [SerializeField] private ShopObject shop; + protected override void CreateModel() + { + Debug.Assert(shop != null,"Shop model initial object never assigned!",this); + model = new SellModel(shop); + } +} diff --git a/Assets/Scripts/Shop/Components/InventoryModelComponent.cs.meta b/Assets/Scripts/Shop/Components/InventoryModelComponent.cs.meta new file mode 100644 index 0000000..912ad01 --- /dev/null +++ b/Assets/Scripts/Shop/Components/InventoryModelComponent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8e211938370fd3d43a99aae3848bdd1c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Shop/Model/ShopModelComponent.cs b/Assets/Scripts/Shop/Components/ShopModelComponent.cs similarity index 100% rename from Assets/Scripts/Shop/Model/ShopModelComponent.cs rename to Assets/Scripts/Shop/Components/ShopModelComponent.cs diff --git a/Assets/Scripts/Shop/Model/ShopModelComponent.cs.meta b/Assets/Scripts/Shop/Components/ShopModelComponent.cs.meta similarity index 100% rename from Assets/Scripts/Shop/Model/ShopModelComponent.cs.meta rename to Assets/Scripts/Shop/Components/ShopModelComponent.cs.meta diff --git a/Assets/Scripts/Shop/Model/SellModel.cs b/Assets/Scripts/Shop/Model/SellModel.cs new file mode 100644 index 0000000..27b091e --- /dev/null +++ b/Assets/Scripts/Shop/Model/SellModel.cs @@ -0,0 +1,33 @@ +using System; +using System.Configuration; + +/// +/// This is a concrete, empty model for the sell state of the shop for you to implement +/// +public class SellModel : ShopModel +{ + public SellModel(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. + public SellModel(ShopObject pShopInitials) : this(pShopInitials.PriceModifier, 0, pShopInitials.Money) + { + } + + //------------------------------------------------------------------------------------------------------------------------ + // 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() + { + OnRemove(GetSelectedItem()); // 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 + SelectItemByIndex(selectedItemIndex >= inventory.GetItemCount() ? --selectedItemIndex : selectedItemIndex); + } + +} diff --git a/Assets/Scripts/Shop/Model/SellModel.cs.meta b/Assets/Scripts/Shop/Model/SellModel.cs.meta new file mode 100644 index 0000000..aec9b99 --- /dev/null +++ b/Assets/Scripts/Shop/Model/SellModel.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cb5965a2261e8144cb8f7323d65e465e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Shop/Model/ShopModel.cs b/Assets/Scripts/Shop/Model/ShopModel.cs index fb056c6..42b2a5e 100644 --- a/Assets/Scripts/Shop/Model/ShopModel.cs +++ b/Assets/Scripts/Shop/Model/ShopModel.cs @@ -12,6 +12,8 @@ public abstract class ShopModel : IModelObservable protected float priceModifier; //Modifies the item's price based on its base price protected int selectedItemIndex = 0; //selected item index + public float PriceModifier => priceModifier; + private List> observers = new List>(); //------------------------------------------------------------------------------------------------------------------------ @@ -19,7 +21,8 @@ public abstract class ShopModel : IModelObservable //------------------------------------------------------------------------------------------------------------------------ public ShopModel(float pPriceModifier, int pItemCount, int pMoney) { - inventory = new Inventory(pItemCount, pMoney); + inventory = new Inventory(pItemCount, pMoney); + priceModifier = pPriceModifier; } //------------------------------------------------------------------------------------------------------------------------ diff --git a/Assets/Scripts/Shop/View/ShopViewGrid.cs b/Assets/Scripts/Shop/View/ShopViewGrid.cs index 3f81178..1f44a57 100644 --- a/Assets/Scripts/Shop/View/ShopViewGrid.cs +++ b/Assets/Scripts/Shop/View/ShopViewGrid.cs @@ -50,7 +50,7 @@ public class ShopViewGrid : ShopView Debug.Assert(itemContainer != null); //bool isSelected = (item == model.GetSelectedItem()); var unsub = model.RegisterObserver(itemContainer); - itemContainer.Initialize(item,unsub); + itemContainer.Initialize(item,model,unsub); //print("Attempt to add item " + item.name + " to view"); } diff --git a/Assets/Scripts/Shop/View/ShopViewList.cs b/Assets/Scripts/Shop/View/ShopViewList.cs index 6bc984a..606a98f 100644 --- a/Assets/Scripts/Shop/View/ShopViewList.cs +++ b/Assets/Scripts/Shop/View/ShopViewList.cs @@ -59,7 +59,7 @@ public class ShopViewList : ShopView, IShopModelObserver Debug.Assert(itemContainer != null); //bool isSelected = (item == model.GetSelectedItem()); var unsub = model.RegisterObserver(itemContainer); - itemContainer.Initialize(item,unsub); + itemContainer.Initialize(item,model,unsub); //print("Attempt to add item " + item.name + " to view"); } @@ -105,7 +105,7 @@ public class ShopViewList : ShopView, IShopModelObserver public void OnSelected(Item item) { if(item != null) infoPanel.gameObject.SetActive(true); - infoPanel?.SetItemInfo(item); // ? works here as it's meant to live through the whole lifetime of this gameobject anyway + infoPanel?.SetItemInfo(item,model); // ? works here as it's meant to live through the whole lifetime of this gameobject anyway } public void OnRemoved(Item item) diff --git a/Assets/Scripts/Shop/View/ViewItemContainer.cs b/Assets/Scripts/Shop/View/ViewItemContainer.cs index dd9cbc1..5a48e38 100644 --- a/Assets/Scripts/Shop/View/ViewItemContainer.cs +++ b/Assets/Scripts/Shop/View/ViewItemContainer.cs @@ -43,7 +43,7 @@ public abstract class ViewItemContainer : MonoBehaviour, IItemContainer, IShopMo //------------------------------------------------------------------------------------------------------------------------ // Initialize() //------------------------------------------------------------------------------------------------------------------------ - public void Initialize(Item item, IDisposable unsubscriber = null){ // pass unsubscriber to this object to make it manage its own lifetime + public void Initialize(Item item, ShopModel owner, IDisposable unsubscriber = null){ // pass unsubscriber to this object to make it manage its own lifetime //Stores the item this.item = item; @@ -57,10 +57,10 @@ public abstract class ViewItemContainer : MonoBehaviour, IItemContainer, IShopMo } IsSelected = false; - OnInitialize(item); + OnInitialize(item,owner); } - protected abstract void OnInitialize(Item item); + protected abstract void OnInitialize(Item item, ShopModel owner = null); // When the observable fires, check if we're the view corresponding to the selected item. Select if we are! public void OnSelected(Item item) diff --git a/Assets/Scripts/Shop/View/ViewItemContainerGrid.cs b/Assets/Scripts/Shop/View/ViewItemContainerGrid.cs index 4a5d4cb..d5b4970 100644 --- a/Assets/Scripts/Shop/View/ViewItemContainerGrid.cs +++ b/Assets/Scripts/Shop/View/ViewItemContainerGrid.cs @@ -21,8 +21,8 @@ public class ViewItemContainerGrid : ViewItemContainer [SerializeField] private ViewItemInfoPanel infoPanel; - protected override void OnInitialize(Item item) + protected override void OnInitialize(Item item,ShopModel owner = null) { - infoPanel.SetItemInfo(item); // Grid info comes with a panel, so we set its info just once + infoPanel.SetItemInfo(item, owner); // Grid info comes with a panel, so we set its info just once } } diff --git a/Assets/Scripts/Shop/View/ViewItemContainerList.cs b/Assets/Scripts/Shop/View/ViewItemContainerList.cs index d9f2150..3904ea8 100644 --- a/Assets/Scripts/Shop/View/ViewItemContainerList.cs +++ b/Assets/Scripts/Shop/View/ViewItemContainerList.cs @@ -19,10 +19,10 @@ public class ViewItemContainerList : ViewItemContainer private ViewItemInfoPanel itemInfo; - protected override void OnInitialize(Item item) + protected override void OnInitialize(Item item, ShopModel owner) { // We just use the automatic component based item info panel stuff per-item here! itemInfo = GetComponent(); - itemInfo?.SetItemInfo(item); + itemInfo?.SetItemInfo(item,owner); } } diff --git a/Assets/Scripts/Shop/View/ViewItemInfoPanel.cs b/Assets/Scripts/Shop/View/ViewItemInfoPanel.cs index 03c23ef..485b84c 100644 --- a/Assets/Scripts/Shop/View/ViewItemInfoPanel.cs +++ b/Assets/Scripts/Shop/View/ViewItemInfoPanel.cs @@ -38,8 +38,9 @@ public class ViewItemInfoPanel : MonoBehaviour } // When this is set, it updates all relevant game objects to reflect the item's propertiese. - public void SetItemInfo(Item item) + public void SetItemInfo(Item item,ShopModel owner = null) { + var multiplier = owner?.PriceModifier ?? 1; if (item == null) { gameObject.SetActive(false); // If the seelcted item doesn't exist, disable the whole panel @@ -64,7 +65,7 @@ public class ViewItemInfoPanel : MonoBehaviour foreach (var price in prices) { - price.SetPrice(item.basePrice); // TODO: Price modifier here! + price.SetPrice((int) (item.basePrice * multiplier)); } foreach (var descriptionDisplay in descr)