Skip to content

Instantly share code, notes, and snippets.

@verborghs
Created October 29, 2019 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verborghs/6e9ec26af1bb845871b4fa8814563b76 to your computer and use it in GitHub Desktop.
Save verborghs/6e9ec26af1bb845871b4fa8814563b76 to your computer and use it in GitHub Desktop.
VendingMachineBehaviour Start
namespace VendingMachines
{
public class Product
{
public Product(string name, int coins, int available)
{
Name = name;
Coins = coins;
Available = available;
}
public string Name { get; }
public int Coins { get; }
public int Available { get; set; }
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace VendingMachines
{
public class VendingMachineBehaviour : MonoBehaviour
{
#region Serialized Fields
[SerializeField]
private Text _message;
[SerializeField]
private GameObject _productUIPrefab;
[SerializeField]
private Transform _productsListUI;
[SerializeField]
private Sprite _productAvailableTexture;
[SerializeField]
private Sprite _productUnavailableTexture;
#endregion
#region Vending Machine Data
private Dictionary<string, Product> _products = new Dictionary<string, Product>()
{
{ "01", new Product("Product 01", 3, 5)},
{ "02", new Product("Product 02", 2, 1)},
{ "03", new Product("Product 03", 1, 0)},
{ "11", new Product("Product 04", 4, 0)},
{ "12", new Product("Product 05", 1, 5)},
{ "13", new Product("Product 06", 1, 5)},
{ "21", new Product("Product 07", 2, 3)},
{ "22", new Product("Product 08", 2, 2)},
{ "23", new Product("Product 09", 3, 5)},
{ "31", new Product("Product 10", 4, 0)},
{ "32", new Product("Product 11", 2, 1)},
{ "33", new Product("Product 12", 1, 1)},
};
private int[] _numbers = new[] { 0, 0 };
private int _coins = 0;
#endregion
#region Display Delay
private Queue<string> _messageQueue = new Queue<string>();
private float _messageTimer = 0.51f;
private float _messageDelay = 0.5f;
private float _defaultMessageTimer = 0;
private float _defaultMessageDelay = 5;
#endregion
void Start()
{
foreach (var product in _products)
{
GameObject go = Instantiate(_productUIPrefab, _productsListUI);
Transform t = go.transform;
t.Find("Code").GetComponent<Text>().text = product.Key;
t.Find("Name").GetComponent<Text>().text = product.Value.Name;
t.Find("Coins").GetComponent<Text>().text = "" + product.Value.Coins;
t.GetComponent<Image>().sprite = (product.Value.Available <= 0) ? _productUnavailableTexture : _productAvailableTexture;
}
}
void Update()
{
if (_messageQueue.Count > 0)
{
_defaultMessageTimer = 0;
if (_messageTimer > _messageDelay)
{
_message.text = _messageQueue.Dequeue();
_messageTimer = 0;
}
_messageTimer += Time.deltaTime;
}
else
{
if (_defaultMessageTimer < _defaultMessageDelay)
{
_defaultMessageTimer += Time.deltaTime;
if (_defaultMessageTimer > _defaultMessageDelay)
{
_message.text = "Select Product please";
}
}
}
}
public void InsertCoin()
{
_defaultMessageTimer = 0;
_numbers[0] = 0;
_numbers[1] = 0;
_coins++;
_messageQueue.Enqueue("You entered " + _coins + " coins");
}
public void EnterNumber(int number)
{
_defaultMessageTimer = 0;
_numbers[0] = _numbers[1];
_numbers[1] = number;
_messageQueue.Enqueue(string.Join("", _numbers));
}
public void Select()
{
_defaultMessageTimer = 0;
if (_products.TryGetValue(string.Join("", _numbers), out Product product))
{
if (product.Available > 0)
{
if (product.Coins <= _coins)
{
_coins -= product.Coins;
_messageQueue.Enqueue("Selling " + product.Name);
_messageQueue.Enqueue("Returning " + _coins + " coins");
_coins = 0;
}
else
{
_messageQueue.Enqueue("Need " + product.Coins + " coins for " + product.Name);
}
}
else
{
_messageQueue.Enqueue("Sorry, " + product.Name + "sold out");
}
}
else
{
_messageQueue.Enqueue("Invalid choice");
}
}
public void Cancel()
{
_defaultMessageTimer = 0;
_messageQueue.Enqueue("Returning " + _coins + " coins");
_coins = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment