#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using XnaTetris.Graphics;
using XnaTetris.Helpers;
#endregion
namespace XnaTetris
{
///
/// This is a game component that implements IUpdateable.
///
public partial class NextBlock : Microsoft.Xna.Framework.DrawableGameComponent
{
#region Constants
///
/// Width and height of the preview grid
///
const int PreviewGridWidth = 5,
PreviewGridHeight = 5;
#endregion
#region Variables
///
/// Remember game for accessing textures and sprites
///
TetrisGame game = null;
///
/// Next block type we are currently displaying
///
TetrisGrid.BlockTypes nextBlockType = TetrisGrid.BlockTypes.Empty;
///
/// Set new random block and return the last block type.
///
///
public TetrisGrid.BlockTypes SetNewRandomBlock()
{
// Generate first next block if not done yet
if (nextBlockType == TetrisGrid.BlockTypes.Empty)
nextBlockType = (TetrisGrid.BlockTypes)
(1+RandomHelper.GetRandomInt(TetrisGrid.NumOfBlockTypes - 1));
// Copy over current next and use it for current block
TetrisGrid.BlockTypes currentBlockType = nextBlockType;
// Generate next random block type
nextBlockType = (TetrisGrid.BlockTypes)
(1 + RandomHelper.GetRandomInt(TetrisGrid.NumOfBlockTypes - 1));
return currentBlockType;
} // SetNewRandomBlock()
///
/// Rectangle for this element. Set in constructor, used in Draw.
///
Rectangle nextBlockRect;
#endregion
#region Constructor
public NextBlock(TetrisGame setGame, Rectangle setNextBlockRect)
: base(setGame)
{
game = setGame;
nextBlockRect = setNextBlockRect;
} // NextBlock(game)
#endregion
#region Draw
public override void Draw(GameTime gameTime)
{
TextureFont.WriteText(nextBlockRect.X + 5, nextBlockRect.Y + 10, "Next:");
Rectangle gridRect = new Rectangle(
nextBlockRect.X + 5, nextBlockRect.Y + 43,
nextBlockRect.Width - 15, nextBlockRect.Height - 46);
int blockWidth = gridRect.Width / PreviewGridWidth;
int blockHeight = gridRect.Height / PreviewGridHeight;
for (int x = 0; x < PreviewGridWidth; x++)
for (int y = 0; y < PreviewGridHeight; y++)
{
int[,] blockData = TetrisGrid.BlockTypeShapesNormal[(int)nextBlockType];
bool isFilled = x > 0 && y > 0 &&
x - 1 < blockData.GetLength(0) &&
y - 1 < blockData.GetLength(1) &&
blockData[x - 1, y - 1] != 0;
game.BlockSprite.Render(new Rectangle(
gridRect.X + x * blockWidth,
gridRect.Y + y * blockHeight,
blockWidth - 1, blockHeight - 1),
TetrisGrid.BlockColor[isFilled ? (int)nextBlockType : 0]);
} // for for
} // Draw(gameTime)
#endregion
} // class NextBlock
} // namespace XnaTetris