public BlockManager(TextureManager manager, double rightSide, double topSide, Vector scaleVector) { _textureManager = manager; _scalingVector = scaleVector; _currentBlock = new Block(_textureManager, "pickle_block", _scalingVector); _blockHeight = _currentBlock.GetHeight(); _blockWidth = _currentBlock.GetWidth(); _previewBottomBound = (topSide / 2) - _blockHeight * 6; _previewLeftBound = (rightSide / 2) - _blockWidth * 5; _previewTetro = new Tetromino(); _currentTetro = new Tetromino(); _shadowTetro = new Tetromino(); }
// Spawns a new tetromino just for preview. Stored in _previewTetro public void SpawnNewPreviewTetro() { Random rand = new Random(); Color color = new Color(1, 1, 1, 1); int tetro = rand.Next(0, 7); string blockType = ""; if (tetro == 0) { _previewTetro = new Tetromino(TetroType.LongBlock); } if (tetro == 1) { _previewTetro = new Tetromino(TetroType.Square); } if (tetro == 2) { _previewTetro = new Tetromino(TetroType.LeftZ); } if (tetro == 3) { _previewTetro = new Tetromino(TetroType.RightZ); } if (tetro == 4) { _previewTetro = new Tetromino(TetroType.LeftHook); } if (tetro == 5) { _previewTetro = new Tetromino(TetroType.RightHook); } if (tetro == 6) { _previewTetro = new Tetromino(TetroType.TBlock); } switch (_previewTetro.Type) { case TetroType.LongBlock: { /* * * * * * * * * **/ color.Red = 1.0f; color.Green = 0.2f; color.Blue = 0.0f; blockType = "breadBot_block"; SpawnNewBlock(blockType, "preview"); _previewTetro.Blocks.Add(_currentBlock); blockType = "breadMid_block"; SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_previewLeftBound + ((_blockWidth * 5) /2), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_previewLeftBound + ((_blockWidth * 5) /2), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _previewTetro.Blocks.Add(_currentBlock); blockType = "breadTop_block"; SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_previewLeftBound + ((_blockWidth * 5) /2), _currentBlock.GetPosition().Y + (3 * _currentBlock.GetHeight())); _previewTetro.Blocks.Add(_currentBlock); break; } case TetroType.Square: { /* * ** * ** **/ color.Red = 1.0f; color.Green = 0.0f; color.Blue = 0.0f; blockType = "pickle_block"; SpawnNewBlock(blockType, "preview"); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_previewLeftBound + ((_blockWidth * 5) /2), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); break; } case TetroType.LeftHook: { /* * ** * * * * **/ color.Red = 0.0f; color.Green = 1.0f; color.Blue = 0.0f; blockType = "cucumber_block"; SpawnNewBlock(blockType, "preview"); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_previewLeftBound + ((_blockWidth * 5) /2), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_previewLeftBound + ((_blockWidth * 5) /2), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _previewTetro.Blocks.Add(_currentBlock); break; } case TetroType.RightHook: { /* * ** * * * * **/ color.Red = 0.0f; color.Green = 0.0f; color.Blue = 1.0f; blockType = "tomato_block"; SpawnNewBlock(blockType, "preview"); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_previewLeftBound + ((_blockWidth * 5) /2), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_previewLeftBound + ((_blockWidth * 5) /2), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _previewTetro.Blocks.Add(_currentBlock); break; } case TetroType.RightZ: { /* * ** * ** **/ color.Red = 0.5f; color.Green = 0.0f; color.Blue = 0.5f; blockType = "olive_block"; SpawnNewBlock(blockType, "preview"); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + (2 * _currentBlock.GetWidth()), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); break; } case TetroType.LeftZ: { /* * ** * ** **/ color.Red = 1.0f; color.Green = 1.0f; color.Blue = 0f; blockType = "lettuce_block"; SpawnNewBlock(blockType, "preview"); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - (2 * _currentBlock.GetWidth()), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); break; } case TetroType.TBlock: { /* * *** * * **/ color.Red = 0.5f; color.Green = 0.5f; color.Blue = 0.5f; blockType = "pepper_block"; SpawnNewBlock(blockType, "preview"); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X, _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "preview"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _previewTetro.Blocks.Add(_currentBlock); break; } default: { break; } } _previewTetro.Moving = false; }
// Spawns a new tetromino just for holding. Stored in _holdingTetro public void SpawnNewHoldingTetro(TetroType type) { Color color = new Color(1, 1, 1, 1); _holdingTetro = new Tetromino(type); string blockType = ""; switch (_holdingTetro.Type) { case TetroType.LongBlock: { /* * * * * * * * * **/ color.Red = 1.0f; color.Green = 0.2f; color.Blue = 0.0f; blockType = "breadBot_block"; SpawnNewBlock(blockType, "holding"); _holdingTetro.Blocks.Add(_currentBlock); blockType = "breadMid_block"; SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(-_previewLeftBound - ((_blockWidth * 5) / 2), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(-_previewLeftBound - ((_blockWidth * 5) / 2), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _holdingTetro.Blocks.Add(_currentBlock); blockType = "breadTop_block"; SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(-_previewLeftBound - ((_blockWidth * 5) / 2), _currentBlock.GetPosition().Y + (3 * _currentBlock.GetHeight())); _holdingTetro.Blocks.Add(_currentBlock); break; } case TetroType.Square: { /* * ** * ** **/ color.Red = 1.0f; color.Green = 0.0f; color.Blue = 0.0f; blockType = "pickle_block"; SpawnNewBlock(blockType, "holding"); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(-_previewLeftBound - ((_blockWidth * 5) / 2), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); break; } case TetroType.LeftHook: { /* * ** * * * * **/ color.Red = 0.0f; color.Green = 1.0f; color.Blue = 0.0f; blockType = "cucumber_block"; SpawnNewBlock(blockType, "holding"); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(-_previewLeftBound - ((_blockWidth * 5) / 2), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(-_previewLeftBound - ((_blockWidth * 5) / 2), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _holdingTetro.Blocks.Add(_currentBlock); break; } case TetroType.RightHook: { /* * ** * * * * **/ color.Red = 0.0f; color.Green = 0.0f; color.Blue = 1.0f; blockType = "tomato_block"; SpawnNewBlock(blockType, "holding"); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(-_previewLeftBound - ((_blockWidth * 5) / 2), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(-_previewLeftBound - ((_blockWidth * 5) / 2), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + (2 * _currentBlock.GetHeight())); _holdingTetro.Blocks.Add(_currentBlock); break; } case TetroType.RightZ: { /* * ** * ** **/ color.Red = 0.5f; color.Green = 0.0f; color.Blue = 0.5f; blockType = "olive_block"; SpawnNewBlock(blockType, "holding"); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + (2 * _currentBlock.GetWidth()), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); break; } case TetroType.LeftZ: { /* * ** * ** **/ color.Red = 1.0f; color.Green = 1.0f; color.Blue = 0f; blockType = "lettuce_block"; SpawnNewBlock(blockType, "holding"); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - (2 * _currentBlock.GetWidth()), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); break; } case TetroType.TBlock: { /* * *** * * **/ color.Red = 0.5f; color.Green = 0.5f; color.Blue = 0.5f; blockType = "pepper_block"; SpawnNewBlock(blockType, "holding"); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X, _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X - _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); SpawnNewBlock(blockType, "holding"); _currentBlock.SetPosition(_currentBlock.GetPosition().X + _currentBlock.GetWidth(), _currentBlock.GetPosition().Y + _currentBlock.GetHeight()); _holdingTetro.Blocks.Add(_currentBlock); break; } default: { break; } } _holdingTetro.Moving = false; }
// Resets the _block List, _gamOver bool, _currentBlock, and _currentTetro public void Reset() { _gameOver = false; _blocks.Clear(); _completedRows = 0; _currentBlock = new Block(); _currentTetro = new Tetromino(); }
// Checks to see if the tetro is colliding with the sides of play area private bool BlockSideCollisionCheck(Tetromino tetro) { foreach (Block testBlock in tetro.Blocks) { if (testBlock != null) { if (testBlock.GetBoundingBox().Left < _leftBound) { // Gets the difference in space between where the colliding block is and the left. double position = -(testBlock.GetPosition().X + (_blockWidth / 2) - _leftBound); foreach (Block block in tetro.Blocks) { block.Readjust(new Vector(1, 0, 0), position); } return false; } if (testBlock.GetBoundingBox().Right > _rightBound) { // Gets the difference in space between where the colliding block is and the right. double position = -(testBlock.GetPosition().X - (_blockWidth / 2) - _rightBound); foreach (Block block in tetro.Blocks) { block.Readjust(new Vector(1, 0, 0), position); } return false; } } } return true; }
// Checks to see if the tetro it hit another block private bool BlockCollisionCheck(Tetromino tetro, bool fallingBlock) { foreach (Block testBlock in tetro.Blocks) { if (_blocks.Count != 0 && testBlock != null) { foreach (Block block in _blocks) { if (testBlock.GetBoundingBox().IntersectsWith(block.GetBoundingBox())) { if (fallingBlock) { // Gets the difference in space between where the colliding block is and the block it hit. Moves the whole tetro back // by that amount to set it in the correct place. double position = -1 * ((testBlock.GetPosition().Y - (testBlock.GetHeight() / 2)) - (block.GetPosition().Y + (block.GetHeight() / 2))); foreach (Block tetroBlock in tetro.Blocks) { tetroBlock.Readjust(new Vector(0, 1, 0), position); } } return false; } } } } return true; }
// Checks to see if the tetro hit the bottom of the play area private bool BlockBottomCollisionCheck(Tetromino tetro) { foreach (Block testBlock in tetro.Blocks) { if (testBlock != null) { if (testBlock.GetBoundingBox().Bottom < _bottomBound + testBlock.GetHeight()) { // Gets the difference in space between where the colliding block is and the bottom. Moves the whole tetro back // by that amount to set it in the correct place. double position = -1 * ((testBlock.GetPosition().Y - (testBlock.GetHeight() /2)) - _bottomBound); foreach (Block block in tetro.Blocks) { block.Readjust(new Vector(0, 1, 0), position); } return false; } } } return true; }
// Spawns a new tetromino just for shadow. Stored in _shadowTetrp public void SpawnNewShadowTetro() { _shadowTetro = new Tetromino(_currentTetro.Type); foreach(Block block in _currentTetro.Blocks) { _shadowTetro.Blocks.Add(new Block(block.GetTexture(), _scalingVector, new Vector(block.GetPosition().X, block.GetPosition().Y, 0))); } _shadowTetro.MakeShadow(); DropBlock(new Vector(0, -1, 0), _shadowTetro); }
// Drops a specified Tetro public void DropBlock(Vector movement, Tetromino tetro) { while (BlockCollisionCheck(tetro, true) && BlockBottomCollisionCheck(tetro)) { foreach (Block block in tetro.Blocks) { block.Move(movement); } } }