Browse Source

Make view use model component, so views can share one model

Devin Braune 5 years ago
parent
commit
43962cacf1
  1. 3
      Assets/Resources/ScriptedItems/BetterSword.asset
  2. 3
      Assets/Resources/ScriptedItems/SomeSword.asset
  3. 2
      Assets/Scenes/NewShop.unity
  4. 3
      Assets/Scripts/Shop/Scriptable Objects/ScriptedItem.cs
  5. 5
      Assets/Scripts/Shop/Scriptable Objects/ScriptedItemFactory.cs
  6. 8
      Assets/Scripts/Shop/View/ShopView.cs

3
Assets/Resources/ScriptedItems/BetterSword.asset

@ -14,4 +14,5 @@ MonoBehaviour:
m_EditorClassIdentifier:
Name: Good Sword
Description: Better than the Noob Sword
sprite: {fileID: -4063295180217184869, guid: b00993d063fbc4a87983115070f6145c, type: 3}
Sprite: {fileID: -4063295180217184869, guid: b00993d063fbc4a87983115070f6145c, type: 3}
Price: 600

3
Assets/Resources/ScriptedItems/SomeSword.asset

@ -14,4 +14,5 @@ MonoBehaviour:
m_EditorClassIdentifier:
Name: Noob Sword
Description: This sword is for noobs
sprite: {fileID: -6229739577819397656, guid: b00993d063fbc4a87983115070f6145c, type: 3}
Sprite: {fileID: -6229739577819397656, guid: b00993d063fbc4a87983115070f6145c, type: 3}
Price: 150

2
Assets/Scenes/NewShop.unity

@ -2959,6 +2959,7 @@ MonoBehaviour:
buyButton: {fileID: 1510759987}
instructionText: {fileID: 1416129356}
layoutGroup: {fileID: 1865708447}
ownModel: {fileID: 132445007}
--- !u!1 &849412832
GameObject:
m_ObjectHideFlags: 0
@ -9404,3 +9405,4 @@ MonoBehaviour:
buyButton: {fileID: 889320678}
instructionText: {fileID: 1352417142}
layoutGroup: {fileID: 180905780}
ownModel: {fileID: 132445007}

3
Assets/Scripts/Shop/Scriptable Objects/ScriptedItem.cs

@ -9,5 +9,6 @@ public class ScriptedItem : ScriptableObject
{
public string Name;
public string Description;
public Sprite sprite;
public Sprite Sprite;
public int Price;
}

5
Assets/Scripts/Shop/Scriptable Objects/ScriptedItemFactory.cs

@ -10,6 +10,9 @@ public class ScriptedItemFactory : ItemFactory
public List<ScriptedItem> Items;
public override void PopulateModel(ShopModel model)
{
foreach (var item in Items)
{
model.inventory.AddItem(new Item(item.name,item.Sprite.name,item.Price));
}
}
}

8
Assets/Scripts/Shop/View/ShopView.cs

@ -14,7 +14,7 @@ using TMPro;
/// </summary>
public abstract class ShopView : MonoBehaviour, IShopModelObserver<Item>
{
public ShopModel ShopModel => model; //A getter to access shopModel.
public ShopModel ShopModel => model; //A getter to access shopModel. Will access inventory of trade partner in sell mode!
[SerializeField]
protected GameObject itemPrefab; //A prefab to display an item in the view
@ -27,6 +27,8 @@ public abstract class ShopView : MonoBehaviour, IShopModelObserver<Item>
[SerializeField] protected LayoutGroup layoutGroup; // The layout group that represents this view visually
[SerializeField] private ModelComponent ownModel; // Reference to the model this view technically belongs to
protected ShopModel model; // Model in MVC pattern
protected ShopModel other; // Other model in MVC pattern (our own inventory)
private ShopController shopController; //Controller in MVC pattern
@ -34,7 +36,9 @@ public abstract class ShopView : MonoBehaviour, IShopModelObserver<Item>
// Set up the view's necessary stuff before the view is enabled
protected virtual void Awake()
{
model = new BuyModel(2f, 16, 500); //Right now use magic values to set up the shop
//model = new BuyModel(2f, 16, 500); //Right now use magic values to set up the shop
Debug.Assert(ownModel != null,"No shop model assigned!",this);
model = ownModel.Model;
shopController = gameObject.AddComponent<MouseController>().Initialize(model);//Set the default controller to be the mouse controller
SetupItemIconView(); //Setup the grid view's properties
InitializeButtons(); //Connect the buttons to the controller

Loading…
Cancel
Save