Game Development Reference
In-Depth Information
Tip
If you wish, you can define
ShopManager
as a singleton. However, unlike the
singletons used so far, this shop will need to be destroyed when the scene is unloaded.
Otherwise, you will always get the same shop. This is unless you also change how we
load the shop.
With the
ShopManager
set up, we can now create the missing definition for
ShopSlot.
This will define the slots in the shop that remember what is being stored on
the shelf. Create a new script in
Assets\Scripts\Shop
and name it
ShopSlot
re-
placing its contents with the following code:
using UnityEngine;
public class ShopSlot : MonoBehaviour {
public InventoryItem Item;
public ShopManager Manager;
}
Now, to add other functions for the shop slots that will be used by the manager, add the
following functions to the
ShopSlot
script:
public void AddShopItem(InventoryItem item)
{
var spriteRenderer = GetComponent<SpriteRenderer>();
spriteRenderer.sprite = item.Sprite;
spriteRenderer.transform.localScale = item.Scale;
Item = item;
}
public void PurchaseItem()
{
GameState.currentPlayer.Inventory.Add(Item);
Item = null;
var spriteRenderer = GetComponent<SpriteRenderer>();
spriteRenderer.sprite = null;
Manager.ClearSelectedItem();
}