/*
* author: ryan lin
* date: 15/8/2024
* description: script to handle vending machine behaviour
*/
using UnityEngine;
using Random = System.Random;
public class VendingMachine : CommonInteractable
{
///
/// prefab to spawn when the player interacts with the vending machine
///
public GameObject coffee;
///
/// the instance of the coffee prefab
///
private GameObject _instance;
///
/// the amount of stock the vending machine has
///
private int _stock;
///
/// to get the number of stocks the vending machine has
///
public void Start()
{
var rand = new Random();
_stock = rand.Next(0, 4);
interactionPrompt = _stock == 0 ? "Out of stock" : "Press [E] to buy coffee";
}
///
/// to spawn the coffee prefab if there is stock
///
public override void Interact()
{
if (_stock <= 0) return;
_instance = Instantiate(coffee, transform.position, transform.rotation);
_stock--;
//to change the interaction prompt if the vending machine is out of stock
if (_stock == 0)
interactionPrompt = "Out of stock";
}
}