Browse Source

Add item filtering

Devin Braune 5 years ago
parent
commit
728ac7b6dc
  1. 60
      Assets/Scenes/NewShop.unity
  2. 19
      Assets/Scripts/Shop/Components/ShopViewFilterButton.cs
  3. 11
      Assets/Scripts/Shop/Components/ShopViewFilterButton.cs.meta
  4. 3
      Assets/Scripts/Shop/Model/Item.cs
  5. 10
      Assets/Scripts/Shop/View/ShopView.cs

60
Assets/Scenes/NewShop.unity

@ -7466,6 +7466,7 @@ GameObject:
- component: {fileID: 1763223521} - component: {fileID: 1763223521}
- component: {fileID: 1763223520} - component: {fileID: 1763223520}
- component: {fileID: 1763223519} - component: {fileID: 1763223519}
- component: {fileID: 1763223522}
m_Layer: 5 m_Layer: 5
m_Name: ArmorButton m_Name: ArmorButton
m_TagString: Untagged m_TagString: Untagged
@ -7573,6 +7574,20 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1763223517} m_GameObject: {fileID: 1763223517}
m_CullTransparentMesh: 0 m_CullTransparentMesh: 0
--- !u!114 &1763223522
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1763223517}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a5001818e381b2c4890a9f8b1d8c943e, type: 3}
m_Name:
m_EditorClassIdentifier:
view: {fileID: 2131558515}
filter: 1
--- !u!1 &1778605631 --- !u!1 &1778605631
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -7742,6 +7757,7 @@ GameObject:
- component: {fileID: 1779774930} - component: {fileID: 1779774930}
- component: {fileID: 1779774929} - component: {fileID: 1779774929}
- component: {fileID: 1779774928} - component: {fileID: 1779774928}
- component: {fileID: 1779774931}
m_Layer: 5 m_Layer: 5
m_Name: AllButton m_Name: AllButton
m_TagString: Untagged m_TagString: Untagged
@ -7849,6 +7865,20 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1779774926} m_GameObject: {fileID: 1779774926}
m_CullTransparentMesh: 0 m_CullTransparentMesh: 0
--- !u!114 &1779774931
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1779774926}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a5001818e381b2c4890a9f8b1d8c943e, type: 3}
m_Name:
m_EditorClassIdentifier:
view: {fileID: 2131558515}
filter: 100
--- !u!1 &1808186922 --- !u!1 &1808186922
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -8437,6 +8467,7 @@ GameObject:
- component: {fileID: 1918696020} - component: {fileID: 1918696020}
- component: {fileID: 1918696019} - component: {fileID: 1918696019}
- component: {fileID: 1918696018} - component: {fileID: 1918696018}
- component: {fileID: 1918696021}
m_Layer: 5 m_Layer: 5
m_Name: PotionButton m_Name: PotionButton
m_TagString: Untagged m_TagString: Untagged
@ -8544,6 +8575,20 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1918696016} m_GameObject: {fileID: 1918696016}
m_CullTransparentMesh: 0 m_CullTransparentMesh: 0
--- !u!114 &1918696021
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1918696016}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a5001818e381b2c4890a9f8b1d8c943e, type: 3}
m_Name:
m_EditorClassIdentifier:
view: {fileID: 2131558515}
filter: 2
--- !u!1 &1941898337 --- !u!1 &1941898337
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -9342,6 +9387,7 @@ GameObject:
- component: {fileID: 1982954965} - component: {fileID: 1982954965}
- component: {fileID: 1982954964} - component: {fileID: 1982954964}
- component: {fileID: 1982954963} - component: {fileID: 1982954963}
- component: {fileID: 1982954966}
m_Layer: 5 m_Layer: 5
m_Name: WeaponButton m_Name: WeaponButton
m_TagString: Untagged m_TagString: Untagged
@ -9449,6 +9495,20 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1982954961} m_GameObject: {fileID: 1982954961}
m_CullTransparentMesh: 0 m_CullTransparentMesh: 0
--- !u!114 &1982954966
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1982954961}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a5001818e381b2c4890a9f8b1d8c943e, type: 3}
m_Name:
m_EditorClassIdentifier:
view: {fileID: 2131558515}
filter: 0
--- !u!1 &2071906703 --- !u!1 &2071906703
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

19
Assets/Scripts/Shop/Components/ShopViewFilterButton.cs

@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Simple component that expands the button by making it set the selected filter of a selected shop view
/// </summary>
[RequireComponent(typeof(Button))]
public class ShopViewFilterButton : MonoBehaviour
{
[SerializeField] private ShopView view;
[SerializeField] private ItemType filter;
// Start is called before the first frame update
void Start()
{
Debug.Assert(view,this);
GetComponent<Button>().onClick.AddListener(delegate { view.SetItemFilter(filter); });
}
}

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

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

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

@ -40,7 +40,8 @@ public enum ItemType
{ {
Weapon, Weapon,
Armor, Armor,
Potion Potion,
All = 100
} }
[System.Serializable] [System.Serializable]
public enum ItemRarity : int // Int representing color, so that this enum not only works as an identifier, but also contains name and col in just 32 bits. C#-Magic! public enum ItemRarity : int // Int representing color, so that this enum not only works as an identifier, but also contains name and col in just 32 bits. C#-Magic!

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

@ -33,6 +33,8 @@ public abstract class ShopView : MonoBehaviour
protected ShopModel other; // Other model in MVC pattern (our own inventory) protected ShopModel other; // Other model in MVC pattern (our own inventory)
private ShopController shopController; //Controller in MVC pattern private ShopController shopController; //Controller in MVC pattern
private ItemType itemFilter = ItemType.All; // View can filter items, and this is the filter we want to use for that
// Set up the view's necessary stuff before the view is enabled // Set up the view's necessary stuff before the view is enabled
protected virtual void Awake() protected virtual void Awake()
{ {
@ -78,6 +80,7 @@ public abstract class ShopView : MonoBehaviour
//Adds one icon for each item in the shop //Adds one icon for each item in the shop
private void PopulateItemIconView() { private void PopulateItemIconView() {
foreach (Item item in model.inventory.GetItems()) { foreach (Item item in model.inventory.GetItems()) {
if(itemFilter == ItemType.All || item.GetItemType() == itemFilter)
AddItemToView(item); AddItemToView(item);
} }
} }
@ -109,6 +112,13 @@ public abstract class ShopView : MonoBehaviour
); );
} }
// Set the item filter and automatically repopulate. Could use bit flags probably...
public void SetItemFilter(ItemType filter)
{
this.itemFilter = filter;
RepopulateItemIconView();
}
private void Update() private void Update()
{ {
//RepopulateItemIconView();//Repopulate the view each frame, this is very inefficient and won't work in many scenarios and SHOULD NOT be in //RepopulateItemIconView();//Repopulate the view each frame, this is very inefficient and won't work in many scenarios and SHOULD NOT be in

Loading…
Cancel
Save