Last active
December 19, 2020 13:54
-
-
Save nucleartide/135f1d22660d5c4f72100f5b78be9b9c 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
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