Skip to content

Instantly share code, notes, and snippets.

@nucleartide
Last active December 19, 2020 13:54
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 nucleartide/135f1d22660d5c4f72100f5b78be9b9c to your computer and use it in GitHub Desktop.
Save nucleartide/135f1d22660d5c4f72100f5b78be9b9c to your computer and use it in GitHub Desktop.
class EnemyModel
{
public ReactiveProperty<long> CurrentHp { get; private set; }
public ReactiveProperty<bool> IsDed { get; private set; }
public EnemyModel(int initialHp)
{
CurrentHp = new ReactiveProperty<long>(initialHp);
IsDead = CurrentHp.Select(x => x <= 0).ToReactiveProperty();
}
}
class ReactivePresenter : MonoBehaviour
{
// Inject these dependencies in the Inspector.
public Button MyButton;
public Toggle MyToggle;
// State-change events from model.
Enemy enemy = new Enemy(1000);
void SubscribeToModel()
{
enemy.CurrentHp.SubscribeToText(MyText);
enemy.IsDead.Where(isDead => isDead).Subscribe(HandleIsDeadChange);
}
void HandleIsDeadChange()
{
MyToggle.interactable = false;
MyButton.interactable = false;
}
void SubscribeToUI()
{
MyButton.OnClickAsObservable().Subscribe(HandleButtonClick);
MyToggle.OnValueChangedAsObservable().SubscribeToInteractable(MyButton);
}
void HandleButtonClick()
{
enemy.CurrentHp.Value -= 99;
}
void Start()
{
SubscribeToModel();
SubscribeToUI();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment