From 25a31957442822690efe7193c8c4398455a37622 Mon Sep 17 00:00:00 2001 From: Devin Date: Tue, 16 Nov 2021 19:47:57 +0100 Subject: [PATCH] Make grid view aware of available items --- .../Controller/GridViewKeyboardController.cs | 12 ++++++---- .../Shop/Controller/ListKeyboardController.cs | 3 +++ .../Scripts/Shop/Controller/ShopController.cs | 22 +++++++++++++++++++ Assets/Scripts/Shop/View/ShopViewGrid.cs | 8 +++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Shop/Controller/GridViewKeyboardController.cs b/Assets/Scripts/Shop/Controller/GridViewKeyboardController.cs index 25122e9..c1de417 100644 --- a/Assets/Scripts/Shop/Controller/GridViewKeyboardController.cs +++ b/Assets/Scripts/Shop/Controller/GridViewKeyboardController.cs @@ -47,9 +47,9 @@ public class GridViewKeyboardController : ShopController if (Input.GetKeyDown(KeyCode.D)) { currentItemIndex++; - if (currentItemIndex >= this.Model.inventory.GetItemCount()) + if (currentItemIndex >= items.Count) { - currentItemIndex = this.Model.inventory.GetItemCount() - 1; + currentItemIndex = items.Count - 1; } } @@ -63,17 +63,21 @@ public class GridViewKeyboardController : ShopController //Move the focus down if possible if (Input.GetKeyDown(KeyCode.S)) { -; if (currentItemIndex < this.Model.inventory.GetItemCount() - columnCount) +; if (currentItemIndex < items.Count - columnCount) currentItemIndex += columnCount; } //Select the item - SelectItemByIndex(currentItemIndex); + //SelectItemByIndex(currentItemIndex); + + // We now select the item by getting it from our local list of items shown in the view! + SelectItem(items[currentItemIndex]); //Confirm the selected item when K is pressed if (Input.GetKeyDown(KeyCode.K)) { ConfirmSelectedItem(); + --currentItemIndex; } } } diff --git a/Assets/Scripts/Shop/Controller/ListKeyboardController.cs b/Assets/Scripts/Shop/Controller/ListKeyboardController.cs index 6b2288a..592eb72 100644 --- a/Assets/Scripts/Shop/Controller/ListKeyboardController.cs +++ b/Assets/Scripts/Shop/Controller/ListKeyboardController.cs @@ -72,6 +72,8 @@ public class ListKeyboardController : ShopController currentItemIndex = this.Model.inventory.GetItemCount() - 1; } } + + // List view does not support item filtering at the moment so we don't use the list at all //Select the item SelectItemByIndex(currentItemIndex); @@ -80,6 +82,7 @@ public class ListKeyboardController : ShopController if (Input.GetKeyDown(KeyCode.K)) { ConfirmSelectedItem(); + currentItemIndex--; } } } diff --git a/Assets/Scripts/Shop/Controller/ShopController.cs b/Assets/Scripts/Shop/Controller/ShopController.cs index da3cb57..2d395a3 100644 --- a/Assets/Scripts/Shop/Controller/ShopController.cs +++ b/Assets/Scripts/Shop/Controller/ShopController.cs @@ -11,6 +11,14 @@ public abstract class ShopController : MonoBehaviour public ShopModel Model => model;//Public getter for the model protected ShopModel model; //Ties this controller to a ShopModel + protected List items; // List of items we know we can select. The view may only show some items, so this is important + + public List Items + { + get => items; + set => items = value; + } + public abstract void HandleInput(); //Concrete controllers override this method and handle input in different ways. //------------------------------------------------------------------------------------------------------------------------ @@ -21,6 +29,7 @@ public abstract class ShopController : MonoBehaviour public virtual ShopController Initialize(ShopModel pModel) { model = pModel; + items = new List(); return this; } @@ -51,4 +60,17 @@ public abstract class ShopController : MonoBehaviour { model.ConfirmSelectedItem(); } + + public void AddSelectableItem(Item selectable) + { + items.Add(selectable); + } + + public void RemoveSelectableItem(Item selectable) + { + items.Remove(selectable); + } + + // Sometimes there's a chance the controller needs to know which items are even selectable in the view + //public virtual List AvailableItems { get; set; } } diff --git a/Assets/Scripts/Shop/View/ShopViewGrid.cs b/Assets/Scripts/Shop/View/ShopViewGrid.cs index 1f44a57..6e0b15f 100644 --- a/Assets/Scripts/Shop/View/ShopViewGrid.cs +++ b/Assets/Scripts/Shop/View/ShopViewGrid.cs @@ -38,6 +38,8 @@ public class ShopViewGrid : ShopView Destroy(child.gameObject); } } + + shopController.Items = new List(); // Reset controller's item list } protected override void AddItemToView(Item item) @@ -51,6 +53,7 @@ public class ShopViewGrid : ShopView //bool isSelected = (item == model.GetSelectedItem()); var unsub = model.RegisterObserver(itemContainer); itemContainer.Initialize(item,model,unsub); + shopController.AddSelectableItem(item); // Inform controller that a new item is selectable //print("Attempt to add item " + item.name + " to view"); } @@ -62,6 +65,7 @@ public class ShopViewGrid : ShopView if (itemView.Item == item) { model.RemoveObserver(itemView); + shopController.RemoveSelectableItem(itemView.Item); // Notify the controller that this item is no longer selectable Destroy(itemView.gameObject); return; } @@ -70,18 +74,22 @@ public class ShopViewGrid : ShopView protected override void SwitchToKeyboardControl() { + var availableItems = shopController.Items; Destroy(shopController);//Remove the current controller component shopController = gameObject.AddComponent().Initialize(model);//Create and add a keyboard controller instructionText.text = "The current control mode is: Keyboard Control, WASD to select item, press K to buy. Press left mouse button to switch to Mouse Control."; buyButton.gameObject.SetActive(false);//Hide the buy button because we only use keyboard + shopController.Items = availableItems; } protected override void SwitchToMouseControl() { + var availableItems = shopController.Items; Destroy(shopController);//Remove the current controller component shopController = gameObject.AddComponent().Initialize(model);//Create and add a mouse controller instructionText.text = "The current control mode is: Mouse Control, press 'K' to switch to Keyboard Control."; buyButton.gameObject.SetActive(true);//Show the buy button for the mouse controler + shopController.Items = availableItems; } private void OnValidate()