32. 一例の紹介:ReactiveProperty<T>の利用
private void Start()
{
button.OnClickAsObservable()
.Where(_ => !player.IsDead)
.Subscribe(_ => player.CurrentHp.Value --);
player.CurrentHp.SubscribeToText(text);
}
public class Player
{
public IReactiveProperty<int> CurrentHp {get; private set;}
public bool IsDead {get; private set;}
public Player()
{
CurrentHp = new ReactiveProperty<int>(10);
CurrentHp.Where(x => x <= 0)
.Subscribe(_ => IsDead = true);
}
}
Presenter Model
33. 一例の紹介:ReactiveProperty<T>の利用
public class Player
{
public IReactiveProperty<int> CurrentHp {get; private set;}
public bool IsDead {get; private set;}
public Player()
{
CurrentHp = new ReactiveProperty<int>(10);
CurrentHp.Where(x => x <= 0)
.Subscribe(_ => IsDead = true);
}
}
ReactivePropertyを定義
Model
34. 一例の紹介:ReactiveProperty<T>の利用
public class Player
{
public IReactiveProperty<int> CurrentHp {get; private set;}
public bool IsDead {get; private set;}
public Player()
{
CurrentHp = new ReactiveProperty<int>(10);
CurrentHp.Where(x => x <= 0)
.Subscribe(_ => IsDead = true);
}
}
ReactivePropertyは、値が更新された時にメッセージを発行する。
Model