This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
sota/RunningLateGame/Assets/Scripts/VendingMachine.cs

50 lines
1.3 KiB
C#
Raw Normal View History

2024-08-15 11:04:52 +00:00
/*
* author: ryan lin
* date: 15/8/2024
* description: script to handle vending machine behaviour
*/
2024-08-15 09:00:31 +00:00
using UnityEngine;
using Random = System.Random;
public class VendingMachine : CommonInteractable
{
/// <summary>
/// prefab to spawn when the player interacts with the vending machine
/// </summary>
public GameObject coffee;
2024-08-15 11:04:52 +00:00
2024-08-15 09:00:31 +00:00
/// <summary>
2024-08-15 11:04:52 +00:00
/// the instance of the coffee prefab
2024-08-15 09:00:31 +00:00
/// </summary>
private GameObject _instance;
2024-08-15 11:04:52 +00:00
/// <summary>
/// the amount of stock the vending machine has
/// </summary>
private int _stock;
/// <summary>
/// to get the number of stocks the vending machine has
/// </summary>
2024-08-15 09:00:31 +00:00
public void Start()
{
var rand = new Random();
2024-08-15 11:04:52 +00:00
_stock = rand.Next(0, 4);
interactionPrompt = _stock == 0 ? "Out of stock" : "Press [E] to buy coffee";
2024-08-15 09:00:31 +00:00
}
/// <summary>
/// to spawn the coffee prefab if there is stock
/// </summary>
public override void Interact()
{
2024-08-15 11:04:52 +00:00
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";
2024-08-15 09:00:31 +00:00
}
}