Created
October 30, 2019 08:51
-
-
Save verborghs/7f56effcb6cc9656731246fe88591795 to your computer and use it in GitHub Desktop.
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
public class ProductView : MonoBehaviour | |
{ | |
[SerializeField] | |
private Text _code; | |
[SerializeField] | |
private Text _name; | |
[SerializeField] | |
private Text _coins; | |
[SerializeField] | |
private Image _panel; | |
[SerializeField] | |
private Sprite _productAvailableTexture; | |
[SerializeField] | |
private Sprite _productUnavailableTexture; | |
public Product Product | |
{ | |
set | |
{ | |
_name.text = value.Name; | |
_coins.text = "" + value.Coins; | |
_panel.sprite = (value.Available <= 0) ? _productUnavailableTexture : _productAvailableTexture; | |
} | |
} | |
public string Code | |
{ | |
set { _code.text = value; } | |
} | |
public ProductView Create(Transform parent) | |
{ | |
GameObject go = Instantiate(this.gameObject, parent); | |
return go.GetComponent<ProductView>(); | |
} | |
} |
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
public class VendingMachineBehaviour : MonoBehaviour | |
{ | |
#region Serialized Fields | |
[SerializeField] | |
private Text _message; | |
[SerializeField] | |
private ProductView _productUIPrefab; | |
[SerializeField] | |
private Transform _productsListUI; | |
#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) | |
{ | |
ProductView view = _productUIPrefab.Create(_productsListUI); | |
view.Code = product.Key; | |
view.Product = product.Value; | |
} | |
} | |
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