Exemplo n.º 1
0
        /// <summary>
        /// PigWorld already has gaps between each pair of cells,
        /// so put walls in some of them, for demonstration purposes.
        /// </summary>
        private void CreateDemoWalls()
        {
            PigWorld.FillVerticalGap(1, 2);
            PigWorld.FillVerticalGap(2, 2);
            PigWorld.FillVerticalGap(3, 2);
            PigWorld.FillVerticalGap(4, 2);
            PigWorld.FillVerticalGap(5, 2);
            PigWorld.FillVerticalGap(2, 4);
            PigWorld.FillVerticalGap(3, 4);
            PigWorld.FillVerticalGap(4, 4);
            PigWorld.FillVerticalGap(5, 4);
            PigWorld.FillHorizontalGap(6, 0);
            PigWorld.FillHorizontalGap(6, 1);
            PigWorld.FillHorizontalGap(6, 2);
            PigWorld.FillHorizontalGap(6, 3);

            PigWorld.FillVerticalGap(0, 5);
            PigWorld.FillVerticalGap(1, 5);
            PigWorld.FillVerticalGap(2, 5);
            PigWorld.FillVerticalGap(3, 5);
            PigWorld.FillVerticalGap(4, 5);
            PigWorld.FillVerticalGap(5, 5);

            PigWorld.FillVerticalGap(3, 7);
            PigWorld.FillVerticalGap(4, 7);
            PigWorld.FillVerticalGap(6, 7);
            PigWorld.FillVerticalGap(7, 7);
            PigWorld.FillVerticalGap(8, 7);
        }
Exemplo n.º 2
0
        private double sweepAngle; // the current angle of the radar's sweep

        /// <summary>
        /// Constructs a new Radar.
        /// </summary>
        /// <param name="owner">A reference to the LifeForm that owns this radar.  </param>
        /// <param name="targetType"> The type of object detected by this radar. </param>
        public Radar(LifeForm owner, Type targetType)
        {
            this.owner      = owner;
            this.pigWorld   = owner.PigWorld;
            this.targetType = targetType;

            sweepAngle = -1.0;  // negative value ensures 0 degrees will be swept first.
        }
Exemplo n.º 3
0
        /// <summary>
        /// Constructs a new Pig at, or near, the specified position.
        /// If the desired position is already occupied by another LifeForm,
        /// then the pig will be placed in a nearby Cell.
        /// If no nearby Cells are free, then the pig will not be added to the pigWorld.
        ///
        /// This constructor is protected since only Pig subclasses should be
        /// able to create a pig.
        /// </summary>
        /// <param name="pigWorld"> the pigWorld that this pig is entering </param>
        /// <param name="position"> the preferred position for the new pig </param>
        /// <param name="color"> the pig's colour (blue or pink) </param>
        /// <param name="mother"> the pig's mother (may be null)</param>
        /// <param name="father"> the pig's father (may be null)</param>
        protected Pig(PigWorld pigWorld, Position position, Color color, GirlPig mother, BoyPig father)
        {
            this.color  = color; // Must be set before AddToWorld is called.
            this.mother = mother;
            this.father = father;

            AddToWorld(pigWorld, position);  // Must be called before any sounds are played.
        }
Exemplo n.º 4
0
        /// <summary>
        /// Every subclass MUST call this method, or the above AddToWorld() method,
        /// as the last line of its constructor. This method will complete the
        /// creation of a new Thing by making it appear in the pigWorld.
        /// The Thing will be placed at, or nearby, the position specified.
        /// </summary>
        /// <param name="pigWorld"> the PigWorld in which this Thing should be created. </param>
        /// <param name="position"></param>
        protected void AddToWorld(PigWorld pigWorld, Position position)
        {
            // It is necessary for this Thing to know its pigWorld before pigWorld.AddThing() is called.
            this.pigWorld = pigWorld;

            if (!pigWorld.AddThing(this, position))
            {
                // If we failed to add ourselves to the pigWorld, set our pigWorld
                // reference back to null...
                this.pigWorld = null;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Constructs a new piece of rope.
        /// </summary>
        /// <param name="pigWorld"></param>
        /// <param name="ownerPig"></param>
        /// <param name="position"></param>
        public RopePiece(PigWorld pigWorld, Pig ownerPig, Position position)
        {
            this.ownerPig = ownerPig;

            // Allow more than one ropePiece in a cell.  (See comments at top of Cell.cs.)
            OnlyOneObjectOfThisTypePerCell = false;

            AddToWorld(pigWorld, position);
            if (this.PigWorld == null)
            {
                throw new Exception("Error: Rope could not be created!");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Set-up the third demo, by adding various objects (and walls) to PigWorld.
        /// </summary>
        public void SetupDemo3()
        {
            BoyPig  boyPig  = new BoyPig(PigWorld, new Position(4, 3));
            GirlPig girlPig = new GirlPig(PigWorld, new Position(4, 5));

            Tree tree1 = new Tree(PigWorld, new Position(0, 4));
            Tree tree2 = new Tree(PigWorld, new Position(4, 4));
            Tree tree3 = new Tree(PigWorld, new Position(8, 4));

            Wolf wolf = new Wolf(PigWorld, new Position(0, 0));

            PigFood pigFood1 = new PigFood(PigWorld, new Position(8, 0));
            PigFood pigFood2 = new PigFood(PigWorld, new Position(8, 1));
            PigFood pigFood3 = new PigFood(PigWorld, new Position(7, 0));
            PigFood pigFood4 = new PigFood(PigWorld, new Position(1, 8));
            PigFood pigFood5 = new PigFood(PigWorld, new Position(0, 8));
            PigFood pigFood6 = new PigFood(PigWorld, new Position(0, 7));

            PigWorld.FillVerticalGap(2, 5);
            PigWorld.FillVerticalGap(3, 1);
            PigWorld.FillVerticalGap(3, 6);
            PigWorld.FillVerticalGap(4, 1);
            PigWorld.FillVerticalGap(4, 6);
            PigWorld.FillVerticalGap(5, 1);
            PigWorld.FillVerticalGap(5, 6);
            PigWorld.FillVerticalGap(6, 2);

            PigWorld.FillHorizontalGap(1, 3);
            PigWorld.FillHorizontalGap(1, 4);
            PigWorld.FillHorizontalGap(1, 5);
            PigWorld.FillHorizontalGap(3, 1);
            PigWorld.FillHorizontalGap(3, 7);
            PigWorld.FillHorizontalGap(4, 0);
            PigWorld.FillHorizontalGap(4, 8);
            PigWorld.FillHorizontalGap(6, 3);
            PigWorld.FillHorizontalGap(6, 4);
            PigWorld.FillHorizontalGap(6, 5);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Constructs a new piece of PigFood. The PigFood will be placed at a random
 /// location.
 /// </summary>
 /// <param name="pigWorld"> the PigWorld in which to create the PigFood. </param>
 public PigFood(PigWorld pigWorld)
 {
     Init();
     AddToWorld(pigWorld);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Constructs a new Cell.
 /// </summary>
 /// <param name="pigWorld"></param>
 /// <param name="position"></param>
 public Cell(PigWorld pigWorld, Position position)
 {
     this.pigWorld = pigWorld;
     this.position = position;
     air           = new Air(this);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Causes this Animal to try to move one step in the direction of the specified targetCell.
 /// </summary>
 /// <param name="targetCell"> the cell to move towards. </param>
 /// <returns> true if the Animal successfully moved, or false otherwise. </returns>
 public bool Move(Cell targetCell)
 {
     return(Move(PigWorld.GetDirection(this.Cell, targetCell)));
 }
Exemplo n.º 10
0
        /// <summary>
        /// Draws the RopePiece on the screen.
        /// (Doesn't use an image from a file).
        ///
        /// Overrides the Paint method in the base class, ThingView.
        /// </summary>
        /// <param name="graphics"> the Graphics object on which the animal's image is displayed. </param>
        protected override void Paint(Graphics graphics)
        {
            PigWorld pigWorld             = ropePiece.PigWorld;
            Position thisPos              = ropePiece.Cell.Position;
            int      indexOfThisRopePiece = ropePiece.GetIndex();

            // Get RopePiece dropped just before this one (if any).
            RopePiece previousRopePiece = ropePiece.OwnerPig.ropePieces.ElementAtOrDefault(indexOfThisRopePiece - 1);
            // Is null when this element does not exist.

            // Get RopePiece dropped just after this one (if any).
            RopePiece nextRopePiece = ropePiece.OwnerPig.ropePieces.ElementAtOrDefault(indexOfThisRopePiece + 1);
            // Is null when this element does not exist.

            int viewWidth  = thingViewRectangle.Width;
            int viewHeight = thingViewRectangle.Height;

            Point midPoint = new Point(viewWidth / 2, viewHeight / 2);

            // Set the starting point for the line to be drawn.
            Point startPoint = new Point();

            // If there is a previous piece of rope, use its position.
            if (previousRopePiece != null)
            {
                Position previousPos = previousRopePiece.Cell.Position;
                startPoint.X = midPoint.X + (viewWidth / 2) * (previousPos.Column - thisPos.Column);
                startPoint.Y = midPoint.Y + (viewHeight / 2) * (previousPos.Row - thisPos.Row);
            }
            else
            {
                // Else, start in the middle of the cell.
                startPoint = midPoint;
            }

            // Set the ending point for the line to be drawn.
            Point    endPoint = new Point();
            Position nextPos;

            // If there is a next piece of rope, use its position.
            if (nextRopePiece != null)
            {
                nextPos = nextRopePiece.Cell.Position;
            }
            else
            {
                // Else, use the pig's current position.
                nextPos = ropePiece.OwnerPig.Cell.Position;
            }
            endPoint.X = midPoint.X + (viewWidth / 2) * (nextPos.Column - thisPos.Column);
            endPoint.Y = midPoint.Y + (viewHeight / 2) * (nextPos.Row - thisPos.Row);

            // Use a rope width of 2 because the default width of 1 isn't always visible in small windows.
            const float penWidth = 2f;

            using (Pen ropePen = new Pen(ropePiece.Color, penWidth)) {
                // If the line would be only along the edge of the cell, then draw two lines to make the rope clearer.
                if ((startPoint.X == endPoint.X && (startPoint.X == 0 || startPoint.X == viewWidth)) ||
                    (startPoint.Y == endPoint.Y && (startPoint.Y == 0 || startPoint.Y == viewHeight)))
                {
                    graphics.DrawLine(ropePen, startPoint.X, startPoint.Y, midPoint.X, midPoint.Y);
                    graphics.DrawLine(ropePen, midPoint.X, midPoint.Y, endPoint.X, endPoint.Y);
                }
                else
                {
                    // Else draw just one line.
                    graphics.DrawLine(ropePen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
                }
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Every subclass MUST call this method, or the second AddToWorld()
 /// method, as the last line of its constructor. This method will complete
 /// the creation of a new Thing by making it appear in the pigWorld.
 /// The Thing will be placed at a random location.
 /// </summary>
 /// <param name="pigWorld"> the PigWorld in which this Thing should be created. </param>
 protected void AddToWorld(PigWorld pigWorld)
 {
     AddToWorld(pigWorld, Position.any);
 }
Exemplo n.º 12
0
 /// <summary>
 /// Constructs a new BoyPig without parents,
 /// e.g. when a pig is added to the pigWorld by the user.
 /// See the following constructor for further details.
 /// </summary>
 /// <param name="pigWorld"> the pigWorld that this pig is entering </param>
 /// <param name="position"> the preferred position for the new pig </param>
 public BoyPig(PigWorld pigWorld, Position position)
     : this(pigWorld, position, null, null)    // In this context, "this" is a call to the constructor below.
 {
 }
Exemplo n.º 13
0
 /// <summary>
 /// Constructs a new Wolf at the specified position. If the desired position is
 /// already occupied by another LifeForm, then the Wolf will be placed in a
 /// nearby Cell. If there are no nearby Cells that are free, then the Wolf
 /// will not be added to the pigWorld.
 /// </summary>
 /// <param name="pigWorld"></param>
 /// <param name="position"></param>
 public Wolf(PigWorld pigWorld, Position position)
 {
     AddToWorld(pigWorld, position);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Constructs a new Wolf.
 /// </summary>
 /// <param name="pigWorld"> the PigWorld in which to create the Wolf. </param>
 public Wolf(PigWorld pigWorld)
     : this(pigWorld, Position.any)    // In this context, "this" is a call to the constructor below.
 {
 }
Exemplo n.º 15
0
 /// <summary>
 /// Constructs a new Tree at the specified position. If the desired position is
 /// already occupied by another Thing, then the Tree will be placed in a
 /// nearby Cell. If there are no nearby Cells that are free, then the Tree
 /// will not be added to the pigWorld.
 /// </summary>
 /// <param name="pigWorld"></param>
 /// <param name="position"></param>
 public Tree(PigWorld pigWorld, Position position)
 {
     AddToWorld(pigWorld, position);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Constructs a new piece of PigFood. The PigFood will be placed at, or
 /// nearby, the specified position.
 /// </summary>
 public PigFood(PigWorld pigWorld, Position position)
 {
     Init();
     AddToWorld(pigWorld, position);
 }
Exemplo n.º 17
0
 /// <summary>
 /// Constructs a new BoyPig at, or near, the specified position.
 /// If the desired position is already occupied by another LifeForm,
 /// then the pig will be placed in a nearby Cell.
 /// If no nearby Cells are free, then the pig will not be added to the pigWorld.
 /// </summary>
 /// <param name="pigWorld"> the pigWorld that this pig is entering </param>
 /// <param name="position"> the preferred position for the new pig </param>
 /// <param name="color"> the pig's colour (blue or pink) </param>
 /// <param name="mother"> the pig's mother (may be null)</param>
 /// <param name="father"> the pig's father (may be null)</param>
 public BoyPig(PigWorld pigWorld, Position position, GirlPig mother, BoyPig father)
     : base(pigWorld, position, BOY_COLOR, mother, father)
 {
 }
Exemplo n.º 18
0
 /// <summary>
 /// Destroys this Thing so that it ceases to exist.
 /// Example 1: the Eat() method inside Animal calls Delete() on the food
 /// after transferring its energy to the eater.
 /// Example 2: to make a NonlivingThing disappear, you need to pick it up
 /// (with Cell.pickup), then destroy it (with Thing.Delete()).
 /// </summary>
 public virtual void Delete()
 {
     thingDestroyedEvent();
     pigWorld.RemoveThing(this);
     pigWorld = null;
 }