예제 #1
0
 /// <summary>
 /// Move the entity to the specified position.
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="x"></param>
 /// <param name="y"></param>
 public void MoveEntity(ForestEntity entity, int x, int y)
 {
     // Remove from its current position
     RemoveEntity(entity);
     _entities[x, y] = entity;
     entity.Location = new System.Drawing.Point(x, y);
 }
예제 #2
0
        /// <summary>
        /// Remove an entity from the forest.
        /// </summary>
        /// <param name="entity"></param>
        public void RemoveEntity(ForestEntity entity)
        {
            // Replace the existing entity with an empty one.
            Meadow m = new Meadow();

            m.Location = entity.Location;
            _entities[entity.Location.X, entity.Location.Y] = m;
        }
예제 #3
0
        public override void ProcessRule(Forest forest, long currentTick)
        {
            _ruleAge++;
            if (_ruleAge % Forest.YEAR_LENGTH == 0)
            {
                // Get all lumberjacks
                var lumberJacks =
                    from e in forest.Entities
                    where typeof(LumberJack).IsAssignableFrom(e.GetType())
                    select e as LumberJack;

                // Get the total number of logs collected from all lumberjacks
                int logSum = 0;
                lumberJacks.ToList().ForEach((e) => logSum += e.Logs);

                // If more logs have been collected than we have lumberjacks, we
                // might need to hire some people
                if (logSum >= lumberJacks.Count())
                {
                    int newHires = (logSum - lumberJacks.Count()) / 10;
                    for (int i = 0; i < newHires; i++)
                    {
                        ForestEntity randomEmpty = forest.GetRandomEmptyPosition();
                        LumberJack   l           = new LumberJack();
                        l.Location = randomEmpty.Location;
                        forest.AddEntity(l);
                    }
                }
                else
                {
                    // They're not working hard enough! Fire someone!
                    // (But make sure we have at least one person working for us...)
                    if (lumberJacks.Count() > 1)
                    {
                        forest.RemoveEntity(lumberJacks.First());
                    }
                }

                if (lumberJacks.Count() == 0)
                {
                    // No more lumberjacks. We have to make sure there's someone working.
                    LumberJack nl = new LumberJack();
                    nl.Location = forest.GetRandomEmptyPosition().Location;
                    forest.AddEntity(nl);
                }

                // Start of a new year... reset log counts for each lumberjack.
                lumberJacks.ToList().ForEach((e) => e.Logs = 0);
            }
        }
예제 #4
0
 /// <summary>
 /// Determine the starting position for the specified entity in the specified forest.
 /// </summary>
 /// <param name="forest"></param>
 /// <param name="entity"></param>
 protected virtual ForestEntity DetermineStartLocation(Forest forest, ForestEntity entity)
 {
     // Probably not the best algorithm.. potential for infinite loop if the forest is
     // already full.
     do
     {
         int x = r.Next(forest.Width),
             y = r.Next(forest.Height);
         // Look for an existing entity in the chosen random position.
         ForestEntity existingInPosition = forest.GetEntityAtPosition(x, y);
         if (existingInPosition == null || existingInPosition.IsEmpty)
         {
             // If position is empty, assign it and return.
             entity.Location = new System.Drawing.Point(x, y);
             return(entity);
         }
     }while (true);
 }
예제 #5
0
        /// <summary>
        /// Draw the forest to the specified graphics surface.
        /// </summary>
        /// <param name="g"></param>
        public void Draw(Graphics g)
        {
            float pX = 0, pY = 0;
            long  monthsRun = _forest.GetMonths(),
                  yearsRun  = monthsRun / Forest.YEAR_LENGTH;

            monthsRun = monthsRun % Forest.YEAR_LENGTH + 1;
            string timeMsg = string.Format("Year: {0}, Month: {1}", yearsRun, monthsRun);
            SizeF  txtSize = g.MeasureString(timeMsg, _txtFont);

            g.DrawString(timeMsg,
                         _txtFont,
                         Brushes.Black,
                         pX, pY);
            pY += txtSize.Height;

            for (int x = 0; x < _forest.Width; x++)
            {
                for (int y = 0; y < _forest.Height; y++)
                {
                    Color        c;
                    ForestEntity entity = _forest.GetEntityAtPosition(x, y);
                    c = entity.Color;

                    RectangleF bounds = new RectangleF(
                        pX + x * SQUARE_SCALE,
                        pY + y * SQUARE_SCALE,
                        1 * SQUARE_SCALE,
                        1 * SQUARE_SCALE);

                    using (SolidBrush brush = new SolidBrush(c))
                        g.FillRectangle(brush, bounds);
                    g.DrawRectangle(Pens.Black, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                }
            }
        }
예제 #6
0
 /// <summary>
 /// Add an entity to the forest.
 /// </summary>
 /// <param name="entity"></param>
 public void AddEntity(ForestEntity entity)
 {
     _entities[entity.Location.X, entity.Location.Y] = entity;
 }