Advertisement
PatrickAupperle

Block.cs

Oct 24th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5.  
  6. namespace Tetris
  7. {
  8.     public enum Direction
  9.     {
  10.         None,
  11.         Up,
  12.         Down,
  13.         Right,
  14.         Left
  15.     }
  16.  
  17.     internal class Block
  18.     {
  19.         private readonly Texture2D _texture;
  20.         public EventHandler Froze;
  21.         private int _blockNumber;
  22.         private Vector2 _location;
  23.         private readonly Color _color;
  24.         private bool[,] _shape;
  25.         private bool _stopped;
  26.  
  27.         public Block(Vector2 location, Texture2D texture, List<bool[,]> blockArray)
  28.         {
  29.             _stopped = false;
  30.             _location = location;
  31.             _color = Color.RoyalBlue;
  32.             _blockNumber = new Random().Next(0, blockArray.Count);
  33.             _blockNumber = 0;
  34.             _shape = blockArray[_blockNumber];
  35.             _texture = texture;
  36.         }
  37.  
  38.         public Block(Vector2 location, bool[,] shape, Color color, Texture2D texture)
  39.         {
  40.             _stopped = false;
  41.             _location = location;
  42.             _color = color;
  43.             _shape = shape;
  44.             _texture = texture;
  45.         }
  46.  
  47.         public virtual void Turn(IEnumerable<Block> blocks, List<bool[,]> blockArray)
  48.         {
  49.             int nextBlock = _blockNumber;
  50.             if ((_blockNumber + 1)%4 != 0)
  51.                 ++nextBlock;
  52.             else
  53.                 nextBlock -= 3;
  54.             _shape = blockArray[nextBlock];
  55.             if (NoCollisions(Direction.None, blocks))
  56.                 _blockNumber = nextBlock;
  57.             _shape = blockArray[_blockNumber];
  58.         }
  59.  
  60.         public void Drop(List<Block> blocks)
  61.         {
  62.             if (NoCollisions(Direction.Down, blocks))
  63.                 _location.Y += 1;
  64.             else if (_stopped == false)
  65.                 _stopped = true;
  66.             else
  67.             {
  68.                 Froze(this, null);
  69.             }
  70.         }
  71.  
  72.         private bool NoCollisions(Direction direction, IEnumerable<Block> blocks)
  73.         {
  74.             int changeInX;
  75.             if (direction == Direction.Left)
  76.                 changeInX = -1;
  77.             else if (direction == Direction.Right)
  78.                 changeInX = 1;
  79.             else
  80.                 changeInX = 0;
  81.             int changeInY = direction == Direction.Down ? 1 : 0;
  82.             bool allowDrop = true;
  83.             for (int i = 0; i < _shape.GetLength(0); ++i)
  84.             {
  85.                 for (int j = 0; j < _shape.GetLength(1); ++j)
  86.                 {
  87.                     if (_shape[i, j])
  88.                     {
  89.                         foreach (Block block in blocks)
  90.                             if (block.Contains(new Vector2(j + _location.X + changeInX, i + _location.Y + changeInY)))
  91.                             {
  92.                                 allowDrop = false;
  93.                             }
  94.                     }
  95.                 }
  96.             }
  97.             return allowDrop;
  98.         }
  99.  
  100.         public void Move(Direction direction, IEnumerable<Block> blockList)
  101.         {
  102.             switch (direction)
  103.             {
  104.                 case Direction.None:
  105.                     break;
  106.                 case Direction.Left:
  107.                     if (NoCollisions(Direction.Left, blockList))
  108.                         _location.X -= 1;
  109.                     break;
  110.                 case Direction.Right:
  111.                     if (NoCollisions(Direction.Right, blockList))
  112.                         _location.X += 1;
  113.                     break;
  114.             }
  115.             _stopped = false;
  116.         }
  117.  
  118.         public void Draw(SpriteBatch batch)
  119.         {
  120.             for (int i = 0; i < _shape.GetLength(0); ++i)
  121.             {
  122.                 for (int j = 0; j < _shape.GetLength(1); ++j)
  123.                 {
  124.                     if (_shape[i, j])
  125.                         batch.Draw(_texture,
  126.                                    new Rectangle((int) (_location.X + j)*10, (int) (_location.Y + i)*10, 10, 10),
  127.                                    _color);
  128.                 }
  129.             }
  130.         }
  131.  
  132.         public bool Contains(Vector2 point)
  133.         {
  134.             if (point.Y < _location.Y)
  135.                 return false;
  136.             if (point.X < _location.X)
  137.                 return false;
  138.             if (point.Y >= _location.Y + _shape.GetLength(0))
  139.                 return false;
  140.             if (point.X >= _location.X + _shape.GetLength(1))
  141.                 return false;
  142.             return _shape[(int) (point.Y - _location.Y), (int) (point.X - _location.X)];
  143.         }
  144.  
  145.         public bool IsEmpty()
  146.         {
  147.             bool empty = true;
  148.             foreach (bool location in _shape)
  149.                 if (location)
  150.                     empty = false;
  151.             return empty;
  152.         }
  153.  
  154.         /// <summary>
  155.         /// Deletes a square in the block
  156.         /// </summary>
  157.         /// <param name="point">The location of the point to delete</param>
  158.         /// <returns>True if a point was deleted</returns>
  159.         public bool Delete(Vector2 point)
  160.         {
  161.             if (point.Y < _location.Y)
  162.                 return false;
  163.             if (point.X < _location.X)
  164.                 return false;
  165.             if (point.Y >= _location.Y + _shape.GetLength(0))
  166.                 return false;
  167.             if (point.X >= _location.X + _shape.GetLength(1))
  168.                 return false;
  169.             _shape[(int) (point.Y - _location.Y), (int) (point.X - _location.X)] = false;
  170.             return true;
  171.         }
  172.     }
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement