예제 #1
0
        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            boardState = new BoardState(ScreenManager.ComputerColorArray.Current, this);

            if (content == null)
            {
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            }

            camera = new Camera(ScreenManager.GraphicsDevice.Viewport.Width /
                                (float)ScreenManager.GraphicsDevice.Viewport.Height);

            #region Load chessboard and figures

            // Load all of the models that will appear on the chessboard:
            for (var i = 0; i < BoardState.FiguresNumber; i++)
            {
                if (boardState.CheckFigureExistence(i))
                {
                    // Load the actual model, using BoardState to determine what
                    // file to load.
                    figureModels[i] =
                        content.Load <Model>(string.Format(@"Models\{0}", boardState.GetFigure(i).GetModelName()));

                    // Create an array of matrices to hold the absolute bone transforms,
                    // calculate them, and copy them in.
                    figureModelAbsoluteBoneTransforms[i] =
                        new Matrix[figureModels[i].Bones.Count];
                    figureModels[i].CopyAbsoluteBoneTransformsTo(
                        figureModelAbsoluteBoneTransforms[i]);
                }
            }

            // Now that we've loaded in the models that will sit on the chessboard, go
            // through the same procedure for the chessboard itself.
            chessboardModel = content.Load <Model>(@"Models\Chessboard");
            chessboardAbsoluteBoneTransforms = new Matrix[chessboardModel.Bones.Count];
            chessboardModel.CopyAbsoluteBoneTransformsTo(chessboardAbsoluteBoneTransforms);

            #endregion

            #region Cell highlight

            cellBasicEffect = new BasicEffect(ScreenManager.GraphicsDevice);

            cellVertices = new VertexPositionTexture[9];

            cellTexture = content.Load <Texture2D>(@"Textures\Cell");

            #endregion

            // once the load has finished, we use ResetElapsedTime to tell the game's
            // timing mechanism that we have just finished a very long frame, and that
            // it should not try to catch up.
            ScreenManager.Game.ResetElapsedTime();
        }
예제 #2
0
        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ScreenManager.GraphicsDevice.Clear(Color.BurlyWood);

            spriteBatch = ScreenManager.SpriteBatch;

            #region Draw chessboard and figures

            // For correct drawing 3D models
            ScreenManager.GraphicsDevice.BlendState        = BlendState.Opaque;
            ScreenManager.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            // Draw the table. DrawModel is a function defined below draws a model using
            // a world matrix and the model's bone transforms.
            DrawModel(chessboardModel, camera.World, chessboardAbsoluteBoneTransforms);

            // Calculate current position of figures, except moving figure, and
            // use the same DrawModel function to draw all of the models on the table.
            for (var i = 0; i < BoardState.FiguresNumber; i++)
            {
                if (boardState.CheckFigureExistence(i))
                {
                    if (i != movingFigureIndex)
                    {
                        // Set position of figure in game world
                        figureModelWorldTransforms[i] =
                            Matrix.CreateTranslation(new Vector3(
                                                         (boardState.FigurePosition(i).X - 'a' - 3.5f) * cellSize,
                                                         0.0f,
                                                         -(boardState.FigurePosition(i).Y - 4.5f) * cellSize));
                    }
                    DrawModel(figureModels[i], figureModelWorldTransforms[i] * camera.World,
                              figureModelAbsoluteBoneTransforms[i]);
                }
            }

            #endregion

            #region Cell highlight

            DrawGameInfo(gameTime); //TODO Message logic - add if in check, players name etc

            // Where we want to draw?
            CellHighlightPosition();
            // Let's draw!
            if (movingFigureIndex.HasValue)
            {
                ScreenManager.GraphicsDevice.BlendState = BlendState.AlphaBlend;

                ScreenManager.GraphicsDevice.RasterizerState = new RasterizerState()
                {
                    CullMode = CullMode.None
                };
                cellBasicEffect.World = camera.World *
                                        Matrix.CreateTranslation(Vector3.Up / 200);
                cellBasicEffect.View           = camera.View;
                cellBasicEffect.Projection     = camera.Projection;
                cellBasicEffect.Texture        = cellTexture;
                cellBasicEffect.TextureEnabled = true;

                foreach (var pass in cellBasicEffect.CurrentTechnique.Passes)
                {
                    pass.Apply();

                    ScreenManager.GraphicsDevice.DrawUserPrimitives <VertexPositionTexture>(
                        PrimitiveType.TriangleList,
                        cellVertices,
                        0,
                        2);
                }

                ScreenManager.GraphicsDevice.BlendState = BlendState.Opaque;
            }

            #endregion

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0)
            {
                ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
            }
        }