예제 #1
0
 /// <summary>
 /// Updates the world
 /// </summary>
 /// <param name="fractionElapsed">A fraction of the simulation time</param>
 public void Update(double fractionElapsed)
 {
     //All the fields grow
     Parallel.ForEach <Field[]>(Fields, column => {
         foreach (var field in column)
         {
             field.GrowCalories(fractionElapsed);
         }
     });
     //Creatures update, if they split newborns are added and if they die, they are removed
     creaturesToDie.Clear();
     creaturesToBirth.Clear();
     Parallel.ForEach <Creature.Creature>(Creatures, creature =>
     {
         var newBorn = creature.Update(fractionElapsed);
         if (creature.ShouldDie())
         {
             lock (creaturesToDie)
             {
                 creaturesToDie.Add(creature);
             }
         }
         else if (newBorn != null)
         {
             lock (creaturesToBirth)
             {
                 creaturesToBirth.Add(newBorn);
             }
         }
     });
     foreach (var deadCreature in creaturesToDie)
     {
         Creatures.Remove(deadCreature);
         if (deadCreature == SelectedCreature)
         {
             SelectedCreature = null;
         }
     }
     foreach (var newBorn in creaturesToBirth)
     {
         Creatures.AddFirst(newBorn);
     }
     //New creatures replace the dead ones
     if (Creatures.Count < MinCreatures)
     {
         for (int i = 0; i < MinCreatures - Creatures.Count; i++)
         {
             AddCreature(new Creature.Creature(this));
         }
     }
     //Old creatures over the max die
     for (int i = 0; i < Creatures.Count - maxCreatures; i++)
     {
         if (Creatures.Last.Value == SelectedCreature)
         {
             SelectedCreature = null;
         }
         Creatures.RemoveLast();
     }
 }
예제 #2
0
 /// <summary>
 /// Registers a creature into the world
 /// </summary>
 /// <param name="creature">The creature</param>
 public void AddCreature(Creature.Creature creature)
 {
     Creatures.AddFirst(creature);
 }