Example #1
0
        /// <summary>
        /// Teken alle visuele objecten.
        /// </summary>
        /// <param name="spriteBatch">SpriteBatch instance van MonoGame</param>
        /// <seealso cref="SpriteBatch"/>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (!IsActive)
            {
                return;
            }

            int menuWidth  = graphicsDevice.Viewport.Width - 100;
            int menuHeight = graphicsDevice.Viewport.Height - 100;

            spriteBatch.Draw(dummyTexture, new Rectangle(50, 50,
                                                         graphicsDevice.Viewport.Width - 100, graphicsDevice.Viewport.Height - 100), Color.White);

            // Een lijn om verschillende producten te onderscheiden.
            for (int i = 1; i < 4; i++)
            {
                spriteBatch.Draw(dummyTexture,
                                 new Rectangle(50 + (int)menuWidth / 4 * i, 50, 1, menuHeight), Color.LightGray);
            }
//
//            for (int i = 1; i < 3; i++)
//            {
//                spriteBatch.Draw(dummyTexture,
//                    new Rectangle(50, 50 + (int) menuHeight / 3 * i, menuWidth, 1), Color.LightGray);
//            }

            int index      = 0;
            int maxIndex   = PlantFactory.Plants.Count;
            int cellWidth  = menuWidth / 4;
            int cellHeigth = menuHeight / 3;

            // Ga over alle planten.
            for (int row = 0; row < 3; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    String text = "";

                    // Laat zien hoeveel geld de gebruiker heeft.
                    if (row == 0 && column == 0)
                    {
                        text = "Geld: $" + Money;
                        spriteBatch.DrawString(titleFont, text,
                                               new Vector2(50 + (int)cellWidth / 2 - (int)(titleFont.MeasureString(text).X / 2),
                                                           50 + (int)cellHeigth / 2 - (int)(titleFont.MeasureString(text).Y / 2)), Color.Black);
                    }

                    // Sla de eerste een laatste hokje over op de eerste rij.
                    if (row == 0 && (column < 1 || column > 2))
                    {
                        continue;
                    }

                    // Schrijf over welke plant het gaat.
                    text = PlantFactory.GetPlant(PlantFactory.Plants[index]).Name;
                    spriteBatch.DrawString(spriteFont, text,
                                           new Vector2(50 + (int)cellWidth / 2 - (int)(spriteFont.MeasureString(text).X / 2) + cellWidth * column, 50 + 10 + cellHeigth * row), Color.Black);

                    // Hoeveel zaadjes heeft de gebruiker over deze plant.
                    text = "Zaadjes: " + GetSeedInventoryAmount(PlantFactory.Plants[index]);
                    spriteBatch.DrawString(spriteFont, text,
                                           new Vector2(50 + (int)cellWidth / 2 - (int)(spriteFont.MeasureString(text).X / 2) + cellWidth * column, 50 + 30 + cellHeigth * row), Color.Black);

                    // Hoeveel vruchten heeft de gebruiker over deze plant.
                    text = "Vruchten: " + GetInventoryAmount(PlantFactory.Plants[index]);
                    spriteBatch.DrawString(spriteFont, text,
                                           new Vector2(50 + (int)cellWidth / 2 - (int)(spriteFont.MeasureString(text).X / 2) + cellWidth * column, 50 + 50 + cellHeigth * row), Color.Black);

                    // Laat een plaatje zien van de plant.
                    spriteBatch.Draw(PlantFactory.GetPlant(PlantFactory.Plants[index]).Texture,
                                     new Rectangle(50 + (int)cellWidth / 2 - 25 + cellWidth * column, 80 + 50 + (int)cellHeigth * row, 50, 50),
                                     Color.White);

                    // Teken een koop knop.
                    text = "Kopen $" + PlantFactory.GetCostPrice(PlantFactory.Plants[index]);
                    spriteBatch.Draw(dummyTexture,
                                     new Rectangle(50 + 5 + cellWidth * column, 50 + (int)cellHeigth - 30 + cellHeigth * row, (int)cellWidth / 2 - 18,
                                                   (int)(spriteFont.MeasureString(text).Y) + 4),
                                     Color.LightBlue);
                    spriteBatch.DrawString(spriteFont, text, new Vector2(50 + 10 + cellWidth * column, 50 + (int)cellHeigth - 28 + cellHeigth * row), Color.Black);

                    // Teken een verkoop knop.
                    text = "Verkopen $" + PlantFactory.GetSellPrice(PlantFactory.Plants[index]);
                    spriteBatch.Draw(dummyTexture,
                                     new Rectangle(45 + (int)cellWidth / 2 + cellWidth * column, 50 + (int)cellHeigth - 30 + cellHeigth * row, (int)cellWidth / 2 + 3,
                                                   (int)(spriteFont.MeasureString(text).Y) + 4),
                                     Color.LightBlue);
                    spriteBatch.DrawString(spriteFont, text, new Vector2(45 + (int)cellWidth / 2 + cellWidth * column, 50 + (int)cellHeigth - 28 + cellHeigth * row),
                                           Color.Black);

                    // Ga naar he volgende product.
                    index++;

                    if (index >= maxIndex)
                    {
                        break;
                    }
                }

                if (index >= maxIndex)
                {
                    break;
                }
            }

            // Laat een kruisje rechts bovenin het scherm zien.
            spriteBatch.Draw(closeImage,
                             new Rectangle(50 + graphicsDevice.Viewport.Width - 140, 50, 40, 40),
                             Color.White);
        }
Example #2
0
        /// <summary>
        /// Het verwerken van muis events.
        /// </summary>
        /// <param name="main">Verwijzing naar Main class</param>
        /// <param name="gameTime">GameTime instance van MonoGame</param>
        /// <param name="mouseState">MouseState instance van MonoGame</param>
        /// <param name="prevMouseState">MouseState instance van vorige MouseState update</param>
        /// <seealso cref="Main"/>
        /// <seealso cref="GameTime"/>
        /// <seealso cref="MouseState"/>
        public void Update(Main main, GameTime gameTime, MouseState mouseState, MouseState prevMouseState)
        {
            if (!IsActive)
            {
                return;
            }

            // Sluit de winkel als er buiten de winkel wordt gedrukt.
            if (mouseState.LeftButton == ButtonState.Pressed &&
                prevMouseState.LeftButton == ButtonState.Released &&
                (!Collide(mouseState) || Collide(mouseState, 50 + graphicsDevice.Viewport.Width - 140, 50 + graphicsDevice.Viewport.Width - 100, 50, 50 + 40)) &&
                IsActive)
            {
                IsActive = false;
            }

            if (mouseState.LeftButton != ButtonState.Pressed || prevMouseState.LeftButton != ButtonState.Released || !IsActive)
            {
                return;
            }

            int menuWidth  = graphicsDevice.Viewport.Width - 100;
            int menuHeight = graphicsDevice.Viewport.Height - 100;
            int index      = 0;
            int maxIndex   = PlantFactory.Plants.Count;
            int cellWidth  = menuWidth / 4;
            int cellHeigth = menuHeight / 3;

            for (int row = 0; row < 3; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    // Sla de eerst en laatste vak over van de eerste rij.
                    if (row == 0 && (column < 1 || column > 2))
                    {
                        continue;
                    }

                    PlantList plant = PlantFactory.Plants[index];
                    String    text  = "Placeholder";

                    // Creƫer een rechthoek voor de koop en verkoop knop.
                    Rectangle buyRectangle = new Rectangle(50 + 5 + cellWidth * column, 50 + (int)cellHeigth - 30 + cellHeigth * row, (int)cellWidth / 2 - 18,
                                                           (int)(spriteFont.MeasureString(text).Y) + 4);
                    Rectangle sellRectangle = new Rectangle(45 + (int)cellWidth / 2 + cellWidth * column, 50 + (int)cellHeigth - 30 + cellHeigth * row, (int)cellWidth / 2 + 3,
                                                            (int)(spriteFont.MeasureString(text).Y) + 4);

                    // De muis drukt op de koop knop.
                    if (buyRectangle.Intersects(new Rectangle(mouseState.X, mouseState.Y, 1, 1)))
                    {
                        // De gebruiker heeft genoeg geld en de plant wordt gekocht.
                        if (Money >= PlantFactory.GetCostPrice(plant))
                        {
                            SetSeedInventoryAmount(plant, GetSeedInventoryAmount(plant) + 1);
                            Money -= PlantFactory.GetCostPrice(plant);
                        }
                        else // De gebruiker heeft niet genoeg geld en zal een alert worden getoont.
                        {
                            main.ShowAlert(Constants.DangerColor, Strings.ErrorNotEnoughMoney, Strings.ErrorNotEnoughMoneyBody.Replace("%%PLANT", plant.ToString()));
                        }
                    }
                    else if (sellRectangle.Intersects(new Rectangle(mouseState.X, mouseState.Y, 1, 1))) // De muis drukt op de verkoop knop.
                    {
                        // De gebruiker heeft genoeg vruchten van de plant.
                        if (GetInventoryAmount(plant) >= 1)
                        {
                            SetInventoryAmount(plant, GetInventoryAmount(plant) - 1);
                            Money += PlantFactory.GetSellPrice(plant);
                        }
                        else // De gebruiker heeft niet genoeg vruchten en zal een alert worden getoont.
                        {
                            main.ShowAlert(Constants.DangerColor, Strings.ErrorNotEnoughInventory, Strings.ErrorNotEnoughInventoryBody.Replace("%%PLANT", plant.ToString()));
                        }
                    }

                    index++;
                    if (index >= maxIndex)
                    {
                        break;
                    }
                }

                if (index >= maxIndex)
                {
                    break;
                }
            }
        }