コード例 #1
0
ファイル: Game.cs プロジェクト: Boddypen/Pixia
        /// <summary>
        /// Go through all components of the game (entities, tiles, etc.) and draw them onto the screen.
        /// This also checks whether or not the game is focused or not.
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Draw(GameTime gameTime)
        {
            // Set the render target to the pre-buffer
            GraphicsDevice.SetRenderTarget(target);

            #region Drawing

            // Begin the spritebatch
            spriteBatch.Begin(SpriteSortMode.Deferred,
                              BlendState.NonPremultiplied,
                              SamplerState.PointClamp,
                              DepthStencilState.Default,
                              RasterizerState.CullNone);

            switch (focusState)
            {
            case FocusState.Focused:
                switch (gameState)
                {
                case GameState.Menu:
                    #region Menu Drawing Logic

                    // Clear the screen
                    GraphicsDevice.Clear(Color.Black);
                    background(mainSheet,
                               Tile.getTileSheetRect(1),
                               Tile.tileWidth,
                               Tile.tileHeight);

                    // Menu Title
                    spriteBatch.Draw(mainSheet,
                                     new Rectangle(displacementX - (menuTitleWidth / 2),
                                                   (menuTitleHeight / 2) + 80,
                                                   menuTitleWidth,
                                                   menuTitleHeight),
                                     new Rectangle(0, 208, 48, 16),
                                     Color.White);
                    spriteBatch.Draw(mainSheet,
                                     new Rectangle(displacementX - (menuTitleWidth / 2),
                                                   (menuTitleHeight / 2) + 80,
                                                   menuTitleWidth,
                                                   menuTitleHeight),
                                     new Rectangle(48, 208, 48, 16),
                                     Color.White);

                    // Menu Options
                    String playOption       = "Press ENTER to play.";
                    int    playOptionWidth  = (int)uiFont.MeasureString(playOption).X;
                    int    playOptionHeight = (int)uiFont.MeasureString(playOption).Y;
                    String quitOption       = "Press Q to quit.";
                    int    quitOptionWidth  = (int)uiFont.MeasureString(quitOption).X;
                    int    quitOptionHeight = (int)uiFont.MeasureString(quitOption).Y;
                    drawShadowString(spriteBatch,
                                     uiFont, playOption,
                                     new Vector2(displacementX - (playOptionWidth / 2),
                                                 displacementY - (playOptionHeight / 2)),
                                     Color.White);
                    drawShadowString(spriteBatch,
                                     uiFont, quitOption,
                                     new Vector2(displacementX - (quitOptionWidth / 2),
                                                 displacementY - (quitOptionHeight / 2) + 32),
                                     Color.White);

                    #endregion
                    break;

                case GameState.Exploring:
                    #region Exploring Drawing Logic

                    // Clear the screen
                    GraphicsDevice.Clear(skyColor);

                    // Draw the level
                    level.draw(spriteBatch,
                               displacementX,
                               displacementY,
                               cameraPosition,
                               mainSheet,
                               level);

                    level.player.draw(spriteBatch,
                                      level,
                                      displacementX,
                                      displacementY,
                                      cameraPosition,
                                      mainSheet);

                    // Draw the focus
                    spriteBatch.Draw(mainSheet,
                                     new Rectangle(displacementX + (int)Math.Round(focusPosition.X) - (int)cameraPosition.X,
                                                   displacementY + (int)Math.Round(focusPosition.Y) - (int)cameraPosition.Y,
                                                   Tile.tileWidth, Tile.tileHeight),
                                     new Rectangle(112, 240, 16, 16),
                                     Color.White);

                    #endregion
                    break;

                case GameState.Inventory:
                    #region Inventory Drawing Logic

                    // Clear the screen
                    GraphicsDevice.Clear(skyColor);

                    // Draw the level, as a background
                    level.draw(spriteBatch,
                               displacementX,
                               displacementY,
                               cameraPosition,
                               mainSheet,
                               level);

                    // Draw the inventory menu
                    spriteBatch.Draw(mainSheet,
                                     new Rectangle(inventoryX + 4,
                                                   inventoryY + 4,
                                                   inventoryWidth * Tile.tileWidth,
                                                   inventoryHeight * Tile.tileHeight),
                                     blankTextureRect,
                                     Color.Black);
                    for (int x = inventoryWidth - 1; x >= 0; x--)
                    {
                        for (int y = inventoryHeight - 1; y >= 0; y--)
                        {
                            Rectangle rect = new Rectangle(inventoryX + (x * Tile.tileWidth),
                                                           inventoryY + (y * Tile.tileHeight),
                                                           Tile.tileWidth,
                                                           Tile.tileHeight);

                            spriteBatch.Draw(mainSheet,
                                             rect,
                                             inventorySlotTextureRect,
                                             Color.White);

                            inventory[x, y].draw(spriteBatch,
                                                 mainSheet,
                                                 uiFont,
                                                 rect);

                            // If this slot is the currently selected one
                            if (inventorySelectionX == x && inventorySelectionY == y)
                            {
                                spriteBatch.Draw(mainSheet,
                                                 rect,
                                                 blankTextureRect,
                                                 new Color(1.0F, 1.0F, 1.0F, 0.3F));
                            }
                        }
                    }

                    #endregion
                    break;

                case GameState.Generating:
                    #region Generating Drawing Logic

                    // Clear the screen
                    GraphicsDevice.Clear(Color.Black);
                    background(mainSheet,
                               Tile.getTileSheetRect(1),
                               Tile.tileWidth,
                               Tile.tileHeight);

                    //TODO: Create a method that draws a string to the center of the screen.

                    // Message
                    String genMessage       = "Generating level...";
                    int    genMessageWidth  = (int)uiFont.MeasureString(genMessage).X;
                    int    genMessageHeight = (int)uiFont.MeasureString(genMessage).Y;
                    drawShadowString(spriteBatch,
                                     uiFont, genMessage,
                                     new Vector2(displacementX - (genMessageWidth / 2),
                                                 displacementY - (genMessageHeight / 2)),
                                     Color.White);

                    #endregion
                    break;
                }
                break;

            case FocusState.Unfocused:
                #region Unfocused Drawing Logic

                // Clear the screen
                GraphicsDevice.Clear(new Color(0.3F, 0.3F, 0.3F));

                // Warn the player that the game is unfocused
                String warnMessage       = "Paused: Click to Focus!";
                int    warnMessageWidth  = (int)uiFont.MeasureString(warnMessage).X;
                int    warnMessageHeight = (int)uiFont.MeasureString(warnMessage).Y;
                drawShadowString(spriteBatch,
                                 uiFont,
                                 warnMessage,
                                 new Vector2(displacementX - (warnMessageWidth / 2),
                                             displacementY - (warnMessageHeight / 2)),
                                 Color.White);

                #endregion
                break;
            }

            // Draw debug info
            drawShadowString(spriteBatch,
                             uiFont, GAMENAME + " " + GAMEVERSION + "  " + framesDisplay + " FPS (" + updatesDisplay + ")",
                             new Vector2(5, 5),
                             Color.White);
            if (showDebug)
            {
                drawShadowString(spriteBatch,
                                 uiFont, "T: " + focusTargetTileX + ", " + focusTargetTileY,
                                 new Vector2(5, 25),
                                 Color.White);
                drawShadowString(spriteBatch,
                                 uiFont, "FS: " + focusState.ToString(),
                                 new Vector2(5, 45),
                                 Color.White);
                drawShadowString(spriteBatch,
                                 uiFont, "GS: " + gameState.ToString(),
                                 new Vector2(5, 65),
                                 Color.White);
                drawShadowString(spriteBatch,
                                 uiFont, "IS: " + inventorySelectionX + ", " + inventorySelectionY,
                                 new Vector2(5, 85),
                                 Color.White);
                drawShadowString(spriteBatch,
                                 uiFont, "OG: " + (level.player.onGround > 0),
                                 new Vector2(5, 105),
                                 Color.White);
            }

            // Draw the mouse cursor
            if (handStack.items.Count > 0)
            {
                handStack.draw(spriteBatch,
                               mainSheet,
                               uiFont,
                               new Rectangle((m.X / scale) - (Tile.tileWidth / 2),
                                             (m.Y / scale) - (Tile.tileHeight / 2),
                                             Tile.tileWidth,
                                             Tile.tileHeight));
            }
            spriteBatch.Draw(cursorTexture,
                             new Rectangle(m.X / scale, m.Y / scale,
                                           cursorWidth, cursorHeight),
                             Color.White);

            // End the spritebatch
            spriteBatch.End();

            #endregion

            // Set the render target to the back buffer
            GraphicsDevice.SetRenderTarget(null);

            // Clear the screen
            GraphicsDevice.Clear(Color.Black);

            // Begin the spritebatch
            spriteBatch.Begin(SpriteSortMode.Deferred,
                              BlendState.NonPremultiplied,
                              SamplerState.PointClamp,
                              DepthStencilState.Default,
                              RasterizerState.CullNone);

            spriteBatch.Draw(target,
                             new Rectangle(0, 0, gameWidth, gameHeight),
                             Color.White);

            // End the spritebatch
            spriteBatch.End();

            frames++;
            base.Draw(gameTime);
        }