Browse Source

Add price modifier to view

master
Devin 4 years ago
parent
commit
7f35404c96
  1. 16
      Assets/Resources/InventoryStart.asset
  2. 8
      Assets/Resources/InventoryStart.asset.meta
  3. 19
      Assets/Scenes/NewShop.unity
  4. 13
      Assets/Scripts/Shop/Components/InventoryModelComponent.cs
  5. 11
      Assets/Scripts/Shop/Components/InventoryModelComponent.cs.meta
  6. 0
      Assets/Scripts/Shop/Components/ShopModelComponent.cs
  7. 0
      Assets/Scripts/Shop/Components/ShopModelComponent.cs.meta
  8. 33
      Assets/Scripts/Shop/Model/SellModel.cs
  9. 11
      Assets/Scripts/Shop/Model/SellModel.cs.meta
  10. 5
      Assets/Scripts/Shop/Model/ShopModel.cs
  11. 2
      Assets/Scripts/Shop/View/ShopViewGrid.cs
  12. 4
      Assets/Scripts/Shop/View/ShopViewList.cs
  13. 6
      Assets/Scripts/Shop/View/ViewItemContainer.cs
  14. 4
      Assets/Scripts/Shop/View/ViewItemContainerGrid.cs
  15. 4
      Assets/Scripts/Shop/View/ViewItemContainerList.cs
  16. 5
      Assets/Scripts/Shop/View/ViewItemInfoPanel.cs

16
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

8
Assets/Resources/InventoryStart.asset.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5832c828db6d00c42a09217f7b743233
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

19
Assets/Scenes/NewShop.unity

@ -1289,7 +1289,7 @@ MonoBehaviour:
m_OnCullStateChanged: m_OnCullStateChanged:
m_PersistentCalls: m_PersistentCalls:
m_Calls: [] m_Calls: []
m_text: Buy m_text: Sell
m_isRightToLeft: 0 m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
@ -9797,6 +9797,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 1237921496} - component: {fileID: 1237921496}
- component: {fileID: 1237921497} - component: {fileID: 1237921497}
- component: {fileID: 1237921498}
m_Layer: 5 m_Layer: 5
m_Name: Inventory m_Name: Inventory
m_TagString: Untagged m_TagString: Untagged
@ -9839,6 +9840,20 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
factory: {fileID: 11400000, guid: aed79a62cfb941741bd92ea0a643b752, type: 2} factory: {fileID: 11400000, guid: aed79a62cfb941741bd92ea0a643b752, type: 2}
shop: {fileID: 11400000, guid: 366ca79a0c4a2df4fac53ddd28f65f1a, 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 --- !u!1 &1287258904
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -12380,7 +12395,7 @@ MonoBehaviour:
m_OnCullStateChanged: m_OnCullStateChanged:
m_PersistentCalls: m_PersistentCalls:
m_Calls: [] m_Calls: []
m_text: Buy m_text: Sell
m_isRightToLeft: 0 m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}

13
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);
}
}

11
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:

0
Assets/Scripts/Shop/Model/ShopModelComponent.cs → Assets/Scripts/Shop/Components/ShopModelComponent.cs

0
Assets/Scripts/Shop/Model/ShopModelComponent.cs.meta → Assets/Scripts/Shop/Components/ShopModelComponent.cs.meta

33
Assets/Scripts/Shop/Model/SellModel.cs

@ -0,0 +1,33 @@
using System;
using System.Configuration;
/// <summary>
/// This is a concrete, empty model for the sell state of the shop for you to implement
/// </summary>
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);
}
}

11
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:

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

@ -12,6 +12,8 @@ public abstract class ShopModel : IModelObservable<Item>
protected float priceModifier; //Modifies the item's price based on its base price protected float priceModifier; //Modifies the item's price based on its base price
protected int selectedItemIndex = 0; //selected item index protected int selectedItemIndex = 0; //selected item index
public float PriceModifier => priceModifier;
private List<IShopModelObserver<Item>> observers = new List<IShopModelObserver<Item>>(); private List<IShopModelObserver<Item>> observers = new List<IShopModelObserver<Item>>();
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------
@ -19,7 +21,8 @@ public abstract class ShopModel : IModelObservable<Item>
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------
public ShopModel(float pPriceModifier, int pItemCount, int pMoney) public ShopModel(float pPriceModifier, int pItemCount, int pMoney)
{ {
inventory = new Inventory(pItemCount, pMoney); inventory = new Inventory(pItemCount, pMoney);
priceModifier = pPriceModifier;
} }
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------

2
Assets/Scripts/Shop/View/ShopViewGrid.cs

@ -50,7 +50,7 @@ public class ShopViewGrid : ShopView
Debug.Assert(itemContainer != null); Debug.Assert(itemContainer != null);
//bool isSelected = (item == model.GetSelectedItem()); //bool isSelected = (item == model.GetSelectedItem());
var unsub = model.RegisterObserver(itemContainer); var unsub = model.RegisterObserver(itemContainer);
itemContainer.Initialize(item,unsub); itemContainer.Initialize(item,model,unsub);
//print("Attempt to add item " + item.name + " to view"); //print("Attempt to add item " + item.name + " to view");
} }

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

@ -59,7 +59,7 @@ public class ShopViewList : ShopView, IShopModelObserver<Item>
Debug.Assert(itemContainer != null); Debug.Assert(itemContainer != null);
//bool isSelected = (item == model.GetSelectedItem()); //bool isSelected = (item == model.GetSelectedItem());
var unsub = model.RegisterObserver(itemContainer); var unsub = model.RegisterObserver(itemContainer);
itemContainer.Initialize(item,unsub); itemContainer.Initialize(item,model,unsub);
//print("Attempt to add item " + item.name + " to view"); //print("Attempt to add item " + item.name + " to view");
} }
@ -105,7 +105,7 @@ public class ShopViewList : ShopView, IShopModelObserver<Item>
public void OnSelected(Item item) public void OnSelected(Item item)
{ {
if(item != null) infoPanel.gameObject.SetActive(true); 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) public void OnRemoved(Item item)

6
Assets/Scripts/Shop/View/ViewItemContainer.cs

@ -43,7 +43,7 @@ public abstract class ViewItemContainer : MonoBehaviour, IItemContainer, IShopMo
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------
// Initialize() // 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 //Stores the item
this.item = item; this.item = item;
@ -57,10 +57,10 @@ public abstract class ViewItemContainer : MonoBehaviour, IItemContainer, IShopMo
} }
IsSelected = false; 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! // When the observable fires, check if we're the view corresponding to the selected item. Select if we are!
public void OnSelected(Item item) public void OnSelected(Item item)

4
Assets/Scripts/Shop/View/ViewItemContainerGrid.cs

@ -21,8 +21,8 @@ public class ViewItemContainerGrid : ViewItemContainer
[SerializeField] [SerializeField]
private ViewItemInfoPanel infoPanel; 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
} }
} }

4
Assets/Scripts/Shop/View/ViewItemContainerList.cs

@ -19,10 +19,10 @@ public class ViewItemContainerList : ViewItemContainer
private ViewItemInfoPanel itemInfo; 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! // We just use the automatic component based item info panel stuff per-item here!
itemInfo = GetComponent<ViewItemInfoPanel>(); itemInfo = GetComponent<ViewItemInfoPanel>();
itemInfo?.SetItemInfo(item); itemInfo?.SetItemInfo(item,owner);
} }
} }

5
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. // 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) if (item == null)
{ {
gameObject.SetActive(false); // If the seelcted item doesn't exist, disable the whole panel 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) foreach (var price in prices)
{ {
price.SetPrice(item.basePrice); // TODO: Price modifier here! price.SetPrice((int) (item.basePrice * multiplier));
} }
foreach (var descriptionDisplay in descr) foreach (var descriptionDisplay in descr)

Loading…
Cancel
Save