TheGadgetCatTumblr

Block Class

Jan 23rd, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Storage;
  7. using Microsoft.Xna.Framework.Input;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.Graphics;
  10.  
  11. namespace CatTetris
  12. {
  13.     class Block
  14.     {
  15.  
  16.         public Sprite sprite1;
  17.         public Sprite sprite2;
  18.         public Sprite sprite3;
  19.         public Sprite sprite4;
  20.  
  21.         public Vector2 mDirection = Vector2.Zero;
  22.         public Vector2 mSpeed = Vector2.Zero;
  23.  
  24.         //Amount of Incremental movement
  25.         public int increment = 50;
  26.  
  27.         //Collision booleans
  28.         public bool pressAgain = true;
  29.         public bool leftWallCollide = false;
  30.         public bool rightWallCollide = false;
  31.         public bool dead = false;
  32.  
  33.         //So I don't have to call every single sprite at once
  34.         public Sprite[] SpriteList;
  35.  
  36.         //Matrix for rotation on the sprite batch
  37.         public Matrix rotationMatrix = Matrix.CreateRotationZ(0);
  38.  
  39.         //radians for the rotation
  40.         public float radians = 0f;
  41.  
  42.         public double elapsedTime = 0;
  43.  
  44.         //a tetrimo is made up of 4 sprites, here we load them individually
  45.         public void LoadSprites(Sprite sprite1, Sprite sprite2, Sprite sprite3, Sprite sprite4,
  46.             Vector2 pos1, Vector2 pos2, Vector2 pos3, Vector2 pos4)
  47.         {
  48.             this.sprite1 = sprite1;
  49.             this.sprite2 = sprite2;
  50.             this.sprite3 = sprite3;
  51.             this.sprite4 = sprite4;
  52.  
  53.             SpriteList = new Sprite[4] { sprite1, sprite2, sprite3, sprite4 };
  54.  
  55.             sprite1.Position = pos1;
  56.             sprite2.Position = pos2;
  57.             sprite3.Position = pos3;
  58.             sprite4.Position = pos4;
  59.         }
  60.  
  61.         public void Draw(SpriteBatch theSpriteBatch)
  62.         {
  63.  
  64.             for (int i = 0; i < SpriteList.Length; i++ )
  65.             {
  66.                 SpriteList[i].Draw(theSpriteBatch);
  67.             }
  68.  
  69.         }
  70.  
  71.         //This updates the rotation matrix
  72.         private Matrix UpdateMatrix(Vector2 origin, float deltaRadians)
  73.         {
  74.             radians += deltaRadians;
  75.  
  76.             Vector3 matrixOrigin = new Vector3(origin, 0);
  77.             rotationMatrix = Matrix.CreateTranslation(-matrixOrigin)
  78.                     * Matrix.CreateRotationZ(radians)
  79.                     * Matrix.CreateTranslation(matrixOrigin);
  80.  
  81.             return rotationMatrix;
  82.         }
  83.  
  84.         //Method to update all of the sprites
  85.         public void updateSprites(GameTime theGameTime)
  86.         {
  87.  
  88.             KeyboardState theCurrentKeyboardState = Keyboard.GetState();
  89.  
  90.             if (!dead)
  91.                 Move(theCurrentKeyboardState, theGameTime);
  92.  
  93.             testCollision();
  94.  
  95.             for (int i = 0; i < SpriteList.Length; i++)
  96.             {
  97.                 SpriteList[i].Update(theGameTime, mSpeed, mDirection);
  98.             }
  99.  
  100.             waitToPress(theGameTime, theCurrentKeyboardState);
  101.         }
  102.  
  103.  
  104.         //Method for moving the sprites around
  105.         public void Move(KeyboardState aCurrentKeyboardState, GameTime theGameTime)
  106.         {
  107.             elapsedTime += theGameTime.ElapsedGameTime.TotalMilliseconds;
  108.  
  109.             if (aCurrentKeyboardState.IsKeyDown(Keys.Down) && elapsedTime >= 200)
  110.             {
  111.                 MoveBlockY(increment);
  112.                 elapsedTime -= 100;
  113.             }
  114.             else if(elapsedTime >= 1000)
  115.             {
  116.                 MoveBlockY(increment);
  117.                 elapsedTime -= 1000;
  118.             }
  119.  
  120.             if (aCurrentKeyboardState.IsKeyDown(Keys.Left) && pressAgain && !leftWallCollide)
  121.             {
  122.                 MoveBlockX(-increment);
  123.                 pressAgain = false;
  124.             }
  125.  
  126.             else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) && pressAgain && !rightWallCollide)
  127.             {
  128.                 MoveBlockX(increment);
  129.                 pressAgain = false;
  130.             }
  131.  
  132.             if (aCurrentKeyboardState.IsKeyDown(Keys.Up) && pressAgain)
  133.             {
  134.                 UpdateMatrix(sprite1.origin, (float)Math.PI / 2);
  135.                 pressAgain = false;
  136.             }
  137.  
  138.         }
  139.  
  140.        
  141.         //Methods to move all sprites in X or Y directions
  142.         private void MoveBlockY(int Y)
  143.         {
  144.             for (int i = 0; i < SpriteList.Length; i++)
  145.             {
  146.                 SpriteList[i].Position.Y += Y;
  147.             }
  148.         }
  149.  
  150.         private void MoveBlockX(int X)
  151.         {
  152.             for (int i = 0; i < SpriteList.Length; i++)
  153.             {
  154.                 SpriteList[i].Position.X += X;
  155.             }
  156.         }
  157.  
  158.         private void waitToPress(GameTime theGameTime, KeyboardState theCurrentKeyboardState) {
  159.  
  160.             if (pressAgain == false)
  161.             {
  162.                 if (theCurrentKeyboardState.IsKeyUp(Keys.Left)
  163.                     && theCurrentKeyboardState.IsKeyUp(Keys.Right)
  164.                     && theCurrentKeyboardState.IsKeyUp(Keys.Up))
  165.                 {
  166.                     pressAgain = true;
  167.                 }
  168.             }
  169.  
  170.         }// end waitToPress()
  171.  
  172.         private void testCollision()
  173.         {
  174.             for (int i = 0; i < SpriteList.Length; i++)
  175.             {
  176.                 if (SpriteList[i].BoundingBox().Intersects(GCat.wallLeft.BoundingBox()))
  177.                 {
  178.                     leftWallCollide = true;
  179.                     break;
  180.                 }
  181.                 else
  182.                     leftWallCollide = false;
  183.  
  184.                 if (SpriteList[i].BoundingBox().Intersects(GCat.wallRight.BoundingBox()))
  185.                 {
  186.                     rightWallCollide = true;
  187.                     break;
  188.                 }
  189.                 else
  190.                     rightWallCollide = false;
  191.                 if (SpriteList[i].Position.Y + 50 >= 600)
  192.                 {
  193.                     dead = true;
  194.                     break;
  195.                 }
  196.             }
  197.         }
  198.  
  199.     }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment