MVC shop project for software architecture, in Unity.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

94 lines
4.0 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Permissions;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;
public class ShopViewGrid : ShopView
{
private GridLayoutGroup _gridLayoutGroup; // This is essentially just a reference to ShopView's layout group but with the correct type
protected ViewConfig viewConfig; //To set up the grid view, we need to know how many columns the grid view has, in the current setup,
//this information can be found in a ViewConfig scriptable object, which serves as a configuration file for
//views.
// Start is called before the first frame update
protected override void Awake()
{
viewConfig = Resources.Load<ViewConfig>("ViewConfig");//Load the ViewConfig scriptable object from the Resources folder
Debug.Assert(viewConfig != null);
Debug.Assert(_gridLayoutGroup != null);
base.Awake();
print("ShopView Grid Initialised");
}
protected override void SetupItemIconView()
{
_gridLayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;//Set the constraint mode of the GridLayoutGroup
_gridLayoutGroup.constraintCount = viewConfig.gridViewColumnCount; //Set the column count according to the ViewConfig object
}
protected override void ClearIconView()
{
Transform[] allIcons = layoutGroup.transform.GetComponentsInChildren<Transform>();
foreach (Transform child in allIcons) {
if (child != layoutGroup.transform) {
Destroy(child.gameObject);
}
}
}
protected override void AddItemToView(Item item)
{
GameObject newItemIcon = GameObject.Instantiate(itemPrefab);
newItemIcon.transform.SetParent(layoutGroup.transform);
newItemIcon.transform.localScale = Vector3.one;//The scale would automatically change in Unity so we set it back to Vector3.one.
ViewItemContainer itemContainer = newItemIcon.GetComponent<ViewItemContainer>();
Debug.Assert(itemContainer != null);
//bool isSelected = (item == model.GetSelectedItem());
var unsub = model.RegisterObserver(itemContainer);
itemContainer.Initialize(item,unsub);
//print("Attempt to add item " + item.name + " to view");
}
protected override void RemoveItemFromView(Item item)
{
var items = layoutGroup.transform.GetComponentsInChildren<ViewItemContainer>();
foreach (var itemView in items)
{
if (itemView.Item == item)
{
model.RemoveObserver(itemView);
Destroy(itemView.gameObject);
return;
}
}
}
protected override void SwitchToKeyboardControl()
{
Destroy(shopController);//Remove the current controller component
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.";
buyButton.gameObject.SetActive(false);//Hide the buy button because we only use keyboard
}
protected override void SwitchToMouseControl()
{
Destroy(shopController);//Remove the current controller component
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.";
buyButton.gameObject.SetActive(true);//Show the buy button for the mouse controler
}
private void OnValidate()
{
_gridLayoutGroup = (GridLayoutGroup) layoutGroup;
bool correctLayout = _gridLayoutGroup != null;
if(!correctLayout) Debug.LogError("Layout group is not of type Grid!",this);
//else Debug.Log("Grid shop view validated",this);
}
}