예제 #1
0
 /// <summary>
 /// Sets one of each type of plant
 /// </summary>
 private void SetPlants()
 {
     _plants[0] = new Tree(-1, -1, new Rectangle());
     _plants[1] = new MoonFlower(-1, -1, new Rectangle());
     _plants[2] = new PeaShooter(-1, -1, new Rectangle());
     _plants[3] = new WallNut(-1, -1, new Rectangle());
     _plants[4] = new SnowPea(-1, -1, new Rectangle());
     _plants[5] = new PotatoMine(-1, -1, new Rectangle());
     _plants[6] = new CherryBomb(-1, -1, new Rectangle());
 }
        /// <summary>
        /// Buy and place a plant
        /// </summary>
        /// <param name="row">The row to place</param>
        /// <param name="col">The column to place</param>
        /// <param name="bounds">The bounds of the tile</param>
        private void BuyAndPlace(int row, int col, Rectangle bounds)
        {
            //Check if the mouse is pressed and a plant is grabbed
            if (!_mouseDown && _plantGrabbed[_plantGrabbedIndex])
            {
                //Get the grabbed plant
                Plant grabbedPlant = _wrapper.GetPlants()[_plantGrabbedIndex];

                //Create a plant
                Plant plant = null;

                //Check if the grabbed plant is a tree
                if (grabbedPlant is Tree)
                {
                    //Create the new plant
                    plant = new Tree(row, col, bounds);
                }
                ////Check if the grabbed plant is a moonflower
                else if (grabbedPlant is MoonFlower)
                {
                    //Create the new plant
                    plant = new MoonFlower(row, col, bounds);
                }
                //Check if the grabbed plant is a cherrybomb
                else if (grabbedPlant is CherryBomb)
                {
                    //Create the new plant
                    plant = new CherryBomb(row, col, bounds);
                }
                //Check if the grabbed plant is a potatomine
                else if (grabbedPlant is PotatoMine)
                {
                    //Create the new plant
                    plant = new PotatoMine(row, col, bounds);
                }
                //Check if the grabbed plant is a peashooter
                else if (grabbedPlant is PeaShooter)
                {
                    //Create the new plant
                    plant = new PeaShooter(row, col, bounds);
                }
                //Check if the grabbed plant is a snowpea
                else if (grabbedPlant is SnowPea)
                {
                    //Create the new plant
                    plant = new SnowPea(row, col, bounds);
                }
                //Check if the grabbed plant is a wallnut
                else if (grabbedPlant is WallNut)
                {
                    //Create the new plant
                    plant = new WallNut(row, col, bounds);
                }

                //Store the reason that a player can either place or not place a plant
                ModelWrapper.Reason reason = _wrapper.GetReason(row, col, plant);

                //Check if the tile is occupied by a plant
                if (reason == ModelWrapper.Reason.Occupied)
                {
                    //Set the note accordingly
                    _note = OCCUPIED;
                }
                //Check if there is no trees in bounds
                else if (reason == ModelWrapper.Reason.NoTreesNearby)
                {
                    //Set the note accordingly
                    _note = NO_TREES_NEARBY;
                }
                //Check if the player doesnt have enough moons
                else if (reason == ModelWrapper.Reason.InsufficientMoons)
                {
                    //Set the note accordingly
                    _note = INSUFFICIENT_MOONS;
                }

                //Buy and place the plant
                _wrapper.BuyAndPlace(row, col, plant);

                //Release the plant
                _plantGrabbed[_plantGrabbedIndex] = false;
            }
        }