@@ -67,6 +67,8 @@ class Time
6767template <typename D>
6868class Region
6969{
70+ Time<D>& theTime;
71+
7072public:
7173 USING_REACTIVE_DOMAIN (D)
7274
@@ -83,21 +85,17 @@ class Region
8385
8486 SignalT<int > FoodPerDay = MakeSignal(
8587 theTime.Season,
86- [] (int season) {
87- return season == summer ? 20 : 10 ;
88- });
88+ calculateFoodPerDay);
8989
9090 SignalT<int > FoodOutputPerDay = MakeSignal(
9191 With (FoodPerDay,AnimalCount),
92- [] (int food, int count) {
93- return count > 0 ? food/count : 0 ;
94- });
92+ calculateFoodOutputPerDay);
9593
9694 EventsT<int > FoodOutput = Pulse(theTime.NewDay, FoodOutputPerDay);
9795
9896 Region (Time<D>& time, int x, int y) :
99- Bounds ( x* 10 , x* 10 + 9 , y* 10 , y* 10 + 9 ),
100- theTime ( time )
97+ theTime ( time ),
98+ Bounds ( x* 10 , x* 10 + 9 , y* 10 , y* 10 + 9 )
10199 {}
102100
103101 PositionT Center () const
@@ -126,7 +124,16 @@ class Region
126124 }
127125
128126private:
129- Time<D>& theTime;
127+ static int calculateFoodPerDay (int season)
128+ {
129+ return season == summer ? 20 : 10 ;
130+ }
131+
132+ static int calculateFoodOutputPerDay (int food, int count)
133+ {
134+ return count > 0 ? food/count : 0 ;
135+ }
136+
130137};
131138
132139// /////////////////////////////////////////////////////////////////////////////////////////////////
@@ -138,7 +145,7 @@ class World
138145public:
139146 USING_REACTIVE_DOMAIN (D)
140147
141- vector<unique_ptr<Region<D>>> Regions;
148+ vector<unique_ptr<Region<D>>> Regions;
142149
143150 World (Time<D>& time, int w) :
144151 w_ ( w )
@@ -199,14 +206,7 @@ class Animal
199206 EventsT<Region<D>*> RegionChanged = Monitor(NewRegion);
200207
201208 SignalT<int > Age = Iterate(theTime.NewDay, 0 , Incrementer<int >());
202-
203- SignalT<int > Health = Iterate(
204- FoodReceived,
205- 100 ,
206- [] (int food, int health) {
207- auto newHealth = health + food - 10 ;
208- return newHealth < 0 ? 0 : newHealth > 10000 ? 10000 : newHealth;
209- });
209+ SignalT<int > Health = Iterate(FoodReceived, 100 , calculateHealth);
210210
211211 Animal (Time<D>& time, World<D>& world, Region<D>* initRegion, unsigned seed) :
212212 theTime ( time ),
@@ -239,20 +239,29 @@ class Animal
239239 [this ] (PositionT pos) {
240240 return theWorld.GetRegion (pos);
241241 })
242+ ),
243+ regionChangeContinuation_
244+ (
245+ MakeContinuation (RegionChanged, With(CurrentRegion),
246+ [this ] (Region<D>* newRegion, Region<D>* oldRegion) {
247+ oldRegion->EnterOrLeave (leave);
248+ newRegion->EnterOrLeave (enter);
249+
250+ // Change region in continuation
251+ CurrentRegion <<= newRegion;
252+ })
242253 )
243254 {
244255 initRegion->EnterOrLeave (enter);
256+ }
245257
246- Observe (
247- RegionChanged,
248- With (CurrentRegion),
249- [this ] (Region<D>* newRegion, Region<D>* oldRegion) {
250- oldRegion->EnterOrLeave (leave);
251- newRegion->EnterOrLeave (enter);
258+ private:
259+ Continuation<D> regionChangeContinuation_;
252260
253- // Change region in continuation
254- CurrentRegion <<= newRegion;
255- });
261+ static int calculateHealth (int food, int health)
262+ {
263+ auto newHealth = health + food - 10 ;
264+ return newHealth < 0 ? 0 : newHealth > 10000 ? 10000 : newHealth;
256265 }
257266};
258267
0 commit comments