Ejemplo n.º 1
0
        /// <summary>
        /// Event-handler for when a NonlivingThing is picked up from this CellView's Cell.
        /// </summary>
        /// <param name="nonlivingThing"> the NonlivingThing being picked up from the Cell</param>
        public void NonlivingThingPickedUpEvent(NonlivingThing nonlivingThing)
        {
            NonlivingThingView nonLivingThingView = nonLivingThingViews[nonlivingThing];

            nonLivingThingViews.Remove(nonlivingThing);

            if (nonLivingThingView.DisplayObjectInGridWithinCell)
            {
                numOfObjectsInGrid -= 1;
            }

            Invalidate();  // Cause the OnPaint method to repaint this CellView.
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Event-handler for when a NonlivingThing is put down on this CellView's Cell.
        /// </summary>
        /// <param name="nonlivingThing"> the NonlivingThing being put down on the Cell</param>
        public void NonlivingThingPutDownEvent(NonlivingThing nonlivingThing)
        {
            NonlivingThingView nonLivingThingView = (NonlivingThingView)pigWorldView.CreateView(nonlivingThing);

            nonLivingThingViews.Add(nonlivingThing, nonLivingThingView);

            if (nonLivingThingView.DisplayObjectInGridWithinCell)
            {
                numOfObjectsInGrid += 1;
            }

            Invalidate();  // Cause the OnPaint method to repaint this CellView.
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This method is used to pick up a NonlivingThing off the ground of the cell/square.
        /// In general, you will need to cast the object returned from this method, e.g.
        ///     PigFood food = (PigFood)Cell.PickUp(typeof(PigFood));
        ///
        /// The NonlivingThing will continue to exist, so it may be put down again using
        /// the PutDown() method. To stop a NonlivingThing from existing, you need to
        /// pick it up, and then destroy it, using its Delete() method.
        /// The Eat method (in the Animal class) calls Delete().
        ///
        /// It is an error to attempt to pick up a nonlivingThing that does not exist on
        /// this Cell. You should check that it exists, by using the Exists() method of
        /// this class first.
        /// </summary>
        /// <param name="type"> the type of nonlivingThing to pickup. </param>
        /// <returns> the NonlivingThing that was picked up. </returns>
        public NonlivingThing PickUp(Type type)
        {
            List <NonlivingThing> list;
            bool listExists = nonLivingThings.TryGetValue(type, out list);

            if (!listExists || list.Count == 0)
            {
                throw new Exception(
                          "There is no " + type.Name + " on this cell. "
                          + "You should check with Exists() first before trying to pick one up.");
            }

            NonlivingThing nonlivingThing = list[0];

            return(PickUp(nonlivingThing));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Pick up a specific nonlivingThing. The NonlivingThing will continue to exist, so it
        /// may be put down again using the PutDown() method. To stop a NonlivingThing
        /// from existing, you need to pick it up, and then destroy it, using the
        /// Delete() method.
        ///
        /// It is an error to attempt to pick up a nonlivingThing that does not exist on
        /// this Cell.
        /// </summary>
        /// <param name="nonlivingThing"> the type of nonlivingThing to pick up. </param>
        /// <returns> the nonlivingThing that was picked up. </returns>
        public NonlivingThing PickUp(NonlivingThing nonlivingThing)
        {
            Type type = nonlivingThing.GetType();
            List <NonlivingThing> list;
            bool listExists = nonLivingThings.TryGetValue(type, out list);

            if (!listExists || list.Count == 0)
            {
                throw new Exception("Can't pick that nonlivingThing up. It's not there!");
            }

            if (!list.Remove(nonlivingThing))
            {
                throw new Exception("Can't pick that nonlivingThing up. It's not there!");
            }

            nonlivingThing.Cell = null;

            nonlivingThingPickedUpEvent(nonlivingThing);

            return(nonlivingThing);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// This method will attempt to put a NonlivingThing down on the ground of a cell.
        /// See rules 2-4 (at the top of this file) for restrictions on the number
        /// of each type of NonlivingThing in the cell, e.g. there can be no more
        /// than one PigFood object in the cell.
        ///
        /// If you attempt to break that rule, then this method will return false.
        /// But when the PutDown succeeds, the method will return true.
        /// </summary>
        /// <param name="nonlivingThing"> the nonlivingThing to put down on the ground. </param>
        /// <returns> true if the nonlivingThing was successfully put down, or false
        /// otherwise. </returns>
        public bool PutDown(NonlivingThing nonlivingThing)
        {
            // Don't allow when occupant is a Plant (e.g. a tree) -- see rule 4, at top of this file.
            if (lifeFormOccupant != null && lifeFormOccupant is Plant)
            {
                return(false);
            }

            // Don't allow when the cell already has a NonlivingThing of the same type (e.g. pig-food),
            // unless OnlyOneObjectOfThisTypePerCell is false -- see rule 2, at top of this file.
            Type type = nonlivingThing.GetType();

            if (Exists(type) && nonlivingThing.OnlyOneObjectOfThisTypePerCell)
            {
                return(false);
            }

            List <NonlivingThing> list;
            bool listExists = nonLivingThings.TryGetValue(type, out list);

            if (!listExists)
            {
                list = new List <NonlivingThing>();
                nonLivingThings.Add(type, list);
            }

            // If the cell already contains the same object that is trying to be added, then complain!
            if (list.Contains(nonlivingThing))
            {
                throw new Exception("You can't put that " + type.Name + " there. It's already there!");
            }

            list.Add(nonlivingThing);
            nonlivingThing.Cell = this;
            nonlivingThingPutDownEvent(nonlivingThing);
            return(true);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Constructs a NonLivingThingView.
 ///
 /// NonlivingThing model the nonlivingThing to view.
 /// </summary>
 protected NonlivingThingView(NonlivingThing nonlivingThing)
 {
     this.nonlivingThing = nonlivingThing;
 }