Browse Source

Make grid view aware of available items

Devin Braune 5 years ago
parent
commit
52df504331
  1. 12
      Assets/Scripts/Shop/Controller/GridViewKeyboardController.cs
  2. 3
      Assets/Scripts/Shop/Controller/ListKeyboardController.cs
  3. 22
      Assets/Scripts/Shop/Controller/ShopController.cs
  4. 8
      Assets/Scripts/Shop/View/ShopViewGrid.cs

12
Assets/Scripts/Shop/Controller/GridViewKeyboardController.cs

@ -47,9 +47,9 @@ public class GridViewKeyboardController : ShopController
if (Input.GetKeyDown(KeyCode.D)) if (Input.GetKeyDown(KeyCode.D))
{ {
currentItemIndex++; 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 //Move the focus down if possible
if (Input.GetKeyDown(KeyCode.S)) if (Input.GetKeyDown(KeyCode.S))
{ {
; if (currentItemIndex < this.Model.inventory.GetItemCount() - columnCount) ; if (currentItemIndex < items.Count - columnCount)
currentItemIndex += columnCount; currentItemIndex += columnCount;
} }
//Select the item //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 //Confirm the selected item when K is pressed
if (Input.GetKeyDown(KeyCode.K)) if (Input.GetKeyDown(KeyCode.K))
{ {
ConfirmSelectedItem(); ConfirmSelectedItem();
--currentItemIndex;
} }
} }
} }

3
Assets/Scripts/Shop/Controller/ListKeyboardController.cs

@ -73,6 +73,8 @@ public class ListKeyboardController : ShopController
} }
} }
// List view does not support item filtering at the moment so we don't use the list at all
//Select the item //Select the item
SelectItemByIndex(currentItemIndex); SelectItemByIndex(currentItemIndex);
@ -80,6 +82,7 @@ public class ListKeyboardController : ShopController
if (Input.GetKeyDown(KeyCode.K)) if (Input.GetKeyDown(KeyCode.K))
{ {
ConfirmSelectedItem(); ConfirmSelectedItem();
currentItemIndex--;
} }
} }
} }

22
Assets/Scripts/Shop/Controller/ShopController.cs

@ -11,6 +11,14 @@ public abstract class ShopController : MonoBehaviour
public ShopModel Model => model;//Public getter for the model public ShopModel Model => model;//Public getter for the model
protected ShopModel model; //Ties this controller to a ShopModel protected ShopModel model; //Ties this controller to a ShopModel
protected List<Item> items; // List of items we know we can select. The view may only show some items, so this is important
public List<Item> Items
{
get => items;
set => items = value;
}
public abstract void HandleInput(); //Concrete controllers override this method and handle input in different ways. 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) public virtual ShopController Initialize(ShopModel pModel)
{ {
model = pModel; model = pModel;
items = new List<Item>();
return this; return this;
} }
@ -51,4 +60,17 @@ public abstract class ShopController : MonoBehaviour
{ {
model.ConfirmSelectedItem(); 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<Item> AvailableItems { get; set; }
} }

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

@ -38,6 +38,8 @@ public class ShopViewGrid : ShopView
Destroy(child.gameObject); Destroy(child.gameObject);
} }
} }
shopController.Items = new List<Item>(); // Reset controller's item list
} }
protected override void AddItemToView(Item item) protected override void AddItemToView(Item item)
@ -51,6 +53,7 @@ public class ShopViewGrid : ShopView
//bool isSelected = (item == model.GetSelectedItem()); //bool isSelected = (item == model.GetSelectedItem());
var unsub = model.RegisterObserver(itemContainer); var unsub = model.RegisterObserver(itemContainer);
itemContainer.Initialize(item,model,unsub); 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"); //print("Attempt to add item " + item.name + " to view");
} }
@ -62,6 +65,7 @@ public class ShopViewGrid : ShopView
if (itemView.Item == item) if (itemView.Item == item)
{ {
model.RemoveObserver(itemView); model.RemoveObserver(itemView);
shopController.RemoveSelectableItem(itemView.Item); // Notify the controller that this item is no longer selectable
Destroy(itemView.gameObject); Destroy(itemView.gameObject);
return; return;
} }
@ -70,18 +74,22 @@ public class ShopViewGrid : ShopView
protected override void SwitchToKeyboardControl() protected override void SwitchToKeyboardControl()
{ {
var availableItems = shopController.Items;
Destroy(shopController);//Remove the current controller component Destroy(shopController);//Remove the current controller component
shopController = gameObject.AddComponent<GridViewKeyboardController>().Initialize(model);//Create and add a keyboard controller shopController = gameObject.AddComponent<GridViewKeyboardController>().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."; 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 buyButton.gameObject.SetActive(false);//Hide the buy button because we only use keyboard
shopController.Items = availableItems;
} }
protected override void SwitchToMouseControl() protected override void SwitchToMouseControl()
{ {
var availableItems = shopController.Items;
Destroy(shopController);//Remove the current controller component Destroy(shopController);//Remove the current controller component
shopController = gameObject.AddComponent<MouseController>().Initialize(model);//Create and add a mouse controller shopController = gameObject.AddComponent<MouseController>().Initialize(model);//Create and add a mouse controller
instructionText.text = "The current control mode is: Mouse Control, press 'K' to switch to Keyboard Control."; 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 buyButton.gameObject.SetActive(true);//Show the buy button for the mouse controler
shopController.Items = availableItems;
} }
private void OnValidate() private void OnValidate()

Loading…
Cancel
Save