Created
October 30, 2019 09:37
-
-
Save verborghs/a199316c9f43d0faa9a5244039090459 to your computer and use it in GitHub Desktop.
This file contains 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 AvailabilityChangedArgs : EventArgs | |
{ | |
public bool SoldOut { get; } | |
public AvailabilityChangedArgs(bool soldOut) | |
{ | |
SoldOut = soldOut; | |
} | |
} |
This file contains 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 Product | |
{ | |
private int _available; | |
public event EventHandler<AvailabilityChangedArgs> AvailabilityChanged; | |
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 | |
{ | |
return _available; | |
} | |
set | |
{ | |
_available = value; | |
AvailabilityChanged?.Invoke(this, new AvailabilityChangedArgs(Available <= 0)); | |
} | |
} | |
} |
This file contains 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; | |
private Product _product; | |
public Product Product | |
{ | |
set | |
{ | |
RemoveListeners(); | |
_product = value; | |
AddListener(); | |
_name.text = _product.Name; | |
_coins.text = "" + _product.Coins; | |
UpdateAvailabilityIndicator(_product.Available <= 0); | |
} | |
} | |
public string Code | |
{ | |
set { _code.text = value; } | |
} | |
public ProductView Create(Transform parent) | |
{ | |
GameObject go = Instantiate(this.gameObject, parent); | |
return go.GetComponent<ProductView>(); | |
} | |
private void AddListener() | |
{ | |
_product.AvailabilityChanged += OnAvailabilityChanged; | |
} | |
private void RemoveListeners() | |
{ | |
if (_product != null) | |
_product.AvailabilityChanged -= OnAvailabilityChanged; | |
} | |
private void OnAvailabilityChanged(object sender, AvailabilityChangedArgs evt) | |
{ | |
UpdateAvailabilityIndicator(evt.SoldOut); | |
} | |
private void UpdateAvailabilityIndicator(bool soldOut) | |
{ | |
_panel.sprite = (soldOut) ? _productUnavailableTexture : _productAvailableTexture; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment