Last active
October 30, 2019 08:14
-
-
Save verborghs/9d6a7b66c754adee0936bc14246b61c0 to your computer and use it in GitHub Desktop.
Using TimedQueue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
private TimedQueue<string> _messageQueue; | |
void Start() | |
{ | |
_messageQueue = new TimedQueue<string>(ShowMessage, ShowDefaultMessage); | |
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() | |
{ | |
_messageQueue.Update(Time.deltaTime); | |
} | |
public void InsertCoin() | |
{ | |
_numbers[0] = 0; | |
_numbers[1] = 0; | |
_coins++; | |
_messageQueue.Enqueue("You entered " + _coins + " coins"); | |
} | |
public void EnterNumber(int number) | |
{ | |
_numbers[0] = _numbers[1]; | |
_numbers[1] = number; | |
_messageQueue.Enqueue(string.Join("", _numbers)); | |
} | |
public void Select() | |
{ | |
if (_products.TryGetValue(string.Join("", _numbers), out Product product)) | |
{ | |
if (product.Available > 0) | |
{ | |
if (product.Coins <= _coins) | |
{ | |
_coins -= product.Coins; | |
product.Available--; | |
_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() | |
{ | |
_messageQueue.Enqueue("Returning " + _coins + " coins"); | |
_coins = 0; | |
} | |
private void ShowMessage(string message) | |
{ | |
_message.text = message; | |
} | |
private void ShowDefaultMessage() | |
{ | |
_message.text = "Select a product"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment