コード例 #1
0
        public override bool Eat()
        {
            Cell          cell    = Enviroment.GetCellByCoords(PositionY, PositionX);
            List <Animal> animals = cell.Animals;
            int           food    = cell.Food;

            if (CheckCellForEat(cell))
            {
                if (IsPredator)
                {
                    foreach (Animal animal in animals)
                    {
                        if (animal != this)
                        {
                            if (animal is Bird)
                            {
                                Bird bird = (Bird)animal;
                                if (bird.IsFlying)
                                {
                                    break;
                                }
                            }
                            if (averageWeight > animal.AverageWeight)
                            {
                                cell.RemoveAnimal(animal);
                                Enviroment.RemoveAnimal(animal);
                                currentPortionOfFood += AverageWeight;
                            }
                            if (currentPortionOfFood >= RequiredPortionOfFood)
                            {
                                currentPortionOfFood = 0;
                                return(true);
                            }
                        }
                    }
                }
                else
                {
                    if (food >= RequiredPortionOfFood)
                    {
                        food -= RequiredPortionOfFood;
                        Enviroment.GetCellByCoords(PositionY, PositionX).Food = food;
                        return(true);
                    }
                }
            }

            currentPortionOfFood = 0;
            if (currentStepsWithoutFood >= PossibleStepsWithoutFood)
            {
                cell.RemoveAnimal(this);
                Enviroment.RemoveAnimal(this);
                return(false);
            }
            return(false);
        }
コード例 #2
0
        public override bool Eat()
        {
            Cell cell = Enviroment.GetCellByCoords(PositionY, PositionX);

            if (cell != null)
            {
                List <Animal> animals = cell.Animals;
                int           food    = cell.Food;
                if (IsPredator)
                {
                    foreach (Animal animal in animals)
                    {
                        if (animal != this && (animal is Fish))
                        {
                            if (averageWeight > animal.AverageWeight)
                            {
                                cell.RemoveAnimal(animal);
                                Enviroment.RemoveAnimal(animal);
                                currentPortionOfFood += AverageWeight;
                            }
                            if (currentPortionOfFood >= RequiredPortionOfFood)
                            {
                                currentPortionOfFood = 0;
                                return(true);
                            }
                        }
                    }
                }
                else
                {
                    if (food >= RequiredPortionOfFood)
                    {
                        food -= RequiredPortionOfFood;
                        Enviroment.GetCellByCoords(PositionY, PositionX).Food = food;
                        return(true);
                    }
                }

                currentPortionOfFood = 0;
                if (currentStepsWithoutFood >= PossibleStepsWithoutFood)
                {
                    cell.RemoveAnimal(this);
                    Enviroment.RemoveAnimal(this);
                    return(false);
                }
            }
            return(false);
        }