Example #1
0
        /// <summary>
        /// This method steps through one unit of time in the pigWorld. It loops over
        /// every LifeForm in the pigWorld and calls the DoSomething() method on each
        /// LifeForm once. You can use the Step method while the pigWorld is stopped to
        /// see how the pigWorld changes after one unit of time.
        /// To make multiple steps, this method can be called repeatedly,
        /// e.g. by the periodic tick of a timer.
        /// </summary>
        public virtual void Step()
        {
            now += 1;

            // Make any sounds in the air dampen over time.
            if (now % 3 == 0)
            {
                for (int row = 0; row < numOfRows; row++)
                {
                    for (int column = 0; column < numOfColumns; column++)
                    {
                        cells[row, column].Air.DecrementSoundLevel();
                    }
                }
            }

            // Update each Thing in the pigWorld, for this moment in time.

            // Items can be added to, and removed from, the list of Things as each LifeForm gets
            // its slice of time. E.g. pigs can be born, or eaten by a wolf.
            // To make sure that only existing Things get their time-slice on the current Step,
            // we make a copy of the list first.
            List <Thing> localListOfThings = new List <Thing>(Things);

            for (int i = 0; i < localListOfThings.Count; i++)
            {
                // The Things update themselves, so we can't use "foreach" here.
                Thing thing = localListOfThings[i];

                if (thing is LifeForm)
                {
                    LifeForm lifeForm = (LifeForm)thing;
                    if (lifeForm.Exists())
                    {
                        lifeForm.HandleTime();
                    }
                }
                else
                {
                    // Refresh the display of NonlivingThings periodically.
                    if (now % 6 == 0)
                    {
                        Cell cell = thing.Cell;
                        if (cell != null)
                        {
                            cell.Pulse();
                        }
                    }
                }
            }
        }