Пример #1
0
        private void BindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if (!this.ignore.ContainsKey((BindingSource)sender))
            {
                return;
            }

            this.ignore[(BindingSource)sender] = true;
            DataRefresh?.Invoke(this, new EventArgs());
        }
Пример #2
0
        public void Update(TimeSpan elapsed)
        {
            _elapsedSinceLastVegetableGeneration += elapsed;
            _elapsedSinceLastDataRefreshTime     += elapsed;

            foreach (var specie in Population)
            {
                var newGeneration = new List <Animal>();

                foreach (var animal in specie.Value)
                {
                    // He could die due to another animal attack
                    if (_evaluator.IsDead(animal))
                    {
                        continue;
                    }

                    // * Movement
                    bool moved = false;

                    // Hunger is always the priority
                    if (animal.IsHungry)
                    {
                        if (animal.Diet == Diet.Herbivore)
                        {
                            var vegetableNearby = _evaluator.FirstVegetableInLineOfSight(animal);
                            if (vegetableNearby != null)
                            {
                                moved = _evaluator.Approach(animal, vegetableNearby, elapsed);

                                if (_evaluator.IsEnoughCloseToInteract(animal, vegetableNearby))
                                {
                                    _evaluator.Eat(animal, vegetableNearby);
                                }
                            }
                        }
                        else if (animal.Diet == Diet.Carnivorous)
                        {
                            var enemyNearby = _evaluator.FirstEnemyInLineOfSight(animal);
                            if (enemyNearby != null)
                            {
                                moved = _evaluator.Approach(animal, enemyNearby, elapsed);

                                if (_evaluator.IsEnoughCloseToInteract(animal, enemyNearby))
                                {
                                    _evaluator.Attack(animal, enemyNearby);

                                    if (_evaluator.IsDead(enemyNearby))
                                    {
                                        _evaluator.Eat(animal, enemyNearby);
                                    }

                                    if (_evaluator.IsDead(animal))
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (animal.Diet == Diet.Herbivore)
                        {
                            var enemyNearby = _evaluator.FirstEnemyInLineOfSight(animal);
                            if (enemyNearby != null)
                            {
                                moved = _evaluator.Flee(animal, enemyNearby, elapsed);
                            }
                        }

                        // If it is in love, find a partner
                        if (!moved && _evaluator.NeedsToReproduce(animal))
                        {
                            var otherGenderAllyNearbyThatNeedsToReproduce = _evaluator.AllayInLineOfSight(animal, true);
                            if (otherGenderAllyNearbyThatNeedsToReproduce != null)
                            {
                                moved = _evaluator.Approach(animal, otherGenderAllyNearbyThatNeedsToReproduce, elapsed);

                                if (_evaluator.IsEnoughCloseToInteract(animal, otherGenderAllyNearbyThatNeedsToReproduce))
                                {
                                    var father = animal.Gender == Gender.Male ? animal : otherGenderAllyNearbyThatNeedsToReproduce;
                                    var mother = animal.Gender == Gender.Female ? animal : otherGenderAllyNearbyThatNeedsToReproduce;

                                    var newAnimal = _evaluator.Copulate(father: father, mother: mother);
                                    newGeneration.Add(newAnimal);
                                    if (animal.Diet == Diet.Carnivorous)
                                    {
                                        TotalBornCarnivorousNumber++;
                                    }
                                    else
                                    {
                                        TotalBornHerbivoreNumber++;
                                    }
                                }
                            }
                        }

                        if (!moved)
                        {
                            // Every animal tends to stay in group with his specie
                            var allyNearby = _evaluator.AllayInLineOfSight(animal);
                            if (allyNearby != null)
                            {
                                moved = _evaluator.Approach(animal, allyNearby, elapsed);
                            }
                        }
                    }

                    // Move randomly to feel alive
                    _evaluator.RandomMove(animal, elapsed);
                    _evaluator.EvaluateMoveEnergyCost(animal);
                    _evaluator.EvaluateAge(animal);
                }

                for (int a = specie.Value.Count - 1; a >= 0; --a)
                {
                    if (_evaluator.IsDead(specie.Value[a]))
                    {
                        if (specie.Value[a].Diet == Diet.Carnivorous)
                        {
                            TotalDeadCarnivorousNumber++;
                        }
                        else
                        {
                            TotalDeadHerbivoreNumber++;
                        }

                        _evaluator.Kill(specie.Value[a]);
                        specie.Value.RemoveAt(a);
                    }
                }

                specie.Value.AddRange(newGeneration);
            }

            for (int v = Vegetables.Count - 1; v >= 0; --v)
            {
                _evaluator.EvaluateAge(Vegetables[v]);
                if (Vegetables[v].IsEaten)
                {
                    Vegetables.RemoveAt(v);
                }
            }

            if (_elapsedSinceLastVegetableGeneration > StartingValues.IntervalForVegetablesGeneration)
            {
                Vegetables.AddRange(_generator.SpawnVegetables(StartingValues.NumberOfVegetables - Vegetables.Count));
                _elapsedSinceLastVegetableGeneration = TimeSpan.Zero;
            }

            if (_elapsedSinceLastDataRefreshTime > _intervalForRaiseDataRefreshTime)
            {
                DataRefresh?.Invoke(this, EventArgs.Empty);
                _elapsedSinceLastDataRefreshTime = TimeSpan.Zero;
            }
        }