Example #1
0
        private void OnEnable()
        {
            WhiteSheepPool.Initialize(MaxAnimalsOnField + MaxFollowers);
            WhenUserClicks =
                Observable.EveryUpdate()
                .Where(_ => IsPlaying.Value == true && Input.GetMouseButton(0) && !droppingAnimals)
                .Select(_ =>
            {
                Vector3 groundPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                groundPos.z       = GroundZ;
                return(groundPos);
            });

            var heroNavigator = new Navigator(Hero.Speed, Hero.transform, WhenUserClicks);

            this.formationNavigator = new FormationNavigator(
                FreeAnimals,
                Followers,
                FormationSlots,
                heroNavigator);

            this.WhenHeroWillDropFollowersAtPen =
                this.PenCollisionTrigger.OnTriggerEnter2DAsObservable()
                .Where(collider => collider.gameObject == Hero.gameObject)
                .Where(_ => Followers.Count > 0 && IsPlaying.Value == true)
                .Select(_ => Followers);

            this.WhenHeroCanGatherAnimal =
                Observable.EveryUpdate()
                .Where(_ => Followers.Count < MaxFollowers && IsPlaying.Value == true)
                .Select(_ => FreeAnimals.FirstOrDefault(Hero.IsAnimalInGatherRange))
                .Where(animal => animal != null);


            this.WhenAnimalSpawns =
                Observable.Interval(TimeSpan.FromSeconds(1))
                .Where(_ => FreeAnimals.Count < MaxAnimalsOnField && IsPlaying.Value == true)
                .Select(_ => WhiteSheepPool.Retain());

            WhenHeroCanGatherAnimal.Subscribe(formationNavigator.AddAnimalToFollowers);
            WhenAnimalSpawns.Subscribe(AddToFieldInRandomPosition);
            WhenHeroWillDropFollowersAtPen.Select(followers => DropAnimalsInPen(followers))
            .Switch()
            .Subscribe(_ => ReleaseFollowers());
            StartLevel();
        }