コード例 #1
0
ファイル: Herbavore.cs プロジェクト: JohnLamontagne/Ecology
        public Herbavore(World world) :
            base(world)
        {
            this.DoEat = (gameTime) =>
            {
                // Find suitable plantlife to consume
                // This would of course be the closest plant (will add other qualifiers in future)
                if (_target == null)
                {
                    _target = (Plant)this.World.LivingThings.Where(l => l is Plant)
                              .OrderBy(l => Vector2.Distance(this.Position, l.Position)).FirstOrDefault();

                    if (_target != null)
                    {
                        this.MoveTo(_target.Position);
                    }
                }
                else if (Vector2.Distance(_target.Position, this.Position) <= 5f)
                {
                    // Consume the plant for its energy
                    this.Energy += _target.Energy;

                    _target.Kill();

                    _target = null;
                }
            };

            this.DoReproduce = (gameTime) =>
            {
                _lastReproduceTime += gameTime.ElapsedGameTime.TotalSeconds;

                if (_lastReproduceTime >= REPRODUCE_COOLDOWN)
                {
                    var newHerbavore = new Herbavore(this.World)
                    {
                        Name          = this.Name,
                        Health        = 100,
                        Energy        = 100,
                        Speed         = this.Speed,
                        MetabolicRate = this.MetabolicRate,
                        GrowthRate    = this.GrowthRate,
                        Size          = this.Size
                    };
                    newHerbavore.Position = new Vector2((float)(this.World.Random.NextDouble() * this.World.Size.X),
                                                        (float)(this.World.Random.NextDouble() * this.World.Size.Y));

                    newHerbavore.Color = this.Color;

                    world.LivingThings.Add(newHerbavore);

                    _lastReproduceTime = 0;
                }
            };
        }
コード例 #2
0
ファイル: World.cs プロジェクト: JohnLamontagne/Ecology
        private void GenerateAnimals(int amount)
        {
            for (int i = 0; i < amount; i++)
            {
                var animal = new Herbavore(this)
                {
                    Name          = "Zebra",
                    Health        = 100,
                    Energy        = 100,
                    Speed         = .2f,
                    MetabolicRate = 5f,
                    GrowthRate    = .0f,
                    Size          = new Vector2(10, 10)
                };

                animal.Position = new Vector2((float)(this.Random.NextDouble() * this.Size.X),
                                              (float)(this.Random.NextDouble() * this.Size.Y));

                animal.Color = new Color((int)((float)this.Random.NextDouble() * 255), (int)((float)this.Random.NextDouble() * 255), (int)((float)this.Random.NextDouble() * 255));

                this.LivingThings.Add(animal);
            }
        }