TheGadgetCatTumblr

Block Class (Update 1/26/12)

Jan 26th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         Vector2 origin;
  16.         Vector2 position;
  17.  
  18.         public Sprite sprite1;
  19.         public Sprite sprite2;
  20.         public Sprite sprite3;
  21.         public Sprite sprite4;
  22.  
  23.         public Vector2 mDirection = Vector2.Zero;
  24.         public Vector2 mSpeed = Vector2.Zero;
  25.  
  26.         //Amount of Incremental movement
  27.         public int increment = 50;
  28.  
  29.         //Limits of the play area
  30.         public int LEFT_LIMIT = 150;
  31.         public int RIGHT_LIMIT = 450;
  32.         public int BOTTOM_LIMIT = 600;
  33.  
  34.         //Collision booleans
  35.         public bool pressAgain = true;
  36.         public bool leftWallCollide = false;
  37.         public bool rightWallCollide = false;
  38.         public bool dead = false;
  39.  
  40.         //So I don't have to call every single sprite at once
  41.         public Sprite[] SpriteList;
  42.  
  43.         //Matrix for rotation on the sprite batch
  44.         public Matrix rotationMatrix = Matrix.CreateRotationZ(0);
  45.  
  46.         //radians for the rotation
  47.         public float radians = 0f;
  48.  
  49.         public double elapsedTime = 0;
  50.  
  51.         //a tetrimo is made up of 4 sprites, here we load them individually
  52.         public void LoadSprites(Sprite sprite1, Sprite sprite2, Sprite sprite3, Sprite sprite4,
  53.             Vector2 pos1, Vector2 pos2, Vector2 pos3, Vector2 pos4)
  54.         {
  55.             this.sprite1 = sprite1;
  56.             this.sprite2 = sprite2;
  57.             this.sprite3 = sprite3;
  58.             this.sprite4 = sprite4;
  59.  
  60.             SpriteList = new Sprite[4] { sprite1, sprite2, sprite3, sprite4 };
  61.  
  62.             this.position = pos1;
  63.  
  64.             sprite1.Position = pos1;
  65.             sprite2.Position = pos2;
  66.             sprite3.Position = pos3;
  67.             sprite4.Position = pos4;
  68.  
  69.             sprite2.offset = new Vector2(0,-50);
  70.             sprite3.offset = new Vector2(-50,0);
  71.             sprite4.offset = new Vector2(50,0);
  72.         }
  73.  
  74.         public void Draw(SpriteBatch theSpriteBatch)
  75.         {
  76.  
  77.             for (int i = 0; i < SpriteList.Length; i++ )
  78.             {
  79.                 SpriteList[i].Draw(theSpriteBatch);
  80.             }
  81.  
  82.         }
  83.  
  84.         private void UpdateSprite()
  85.         {
  86.             for (int i = 0; i < SpriteList.Length; i++ )
  87.             {
  88.                 SpriteList[i].Update(position, SpriteList[i].offset);
  89.             }
  90.         }
  91.  
  92.         private void UpdateOrigin()
  93.         {
  94.             origin = new Vector2((int)(position.X + sprite1.mSpriteTexture.Width / 2),
  95.                 (int)(position.Y + sprite1.mSpriteTexture.Height / 2));
  96.         }
  97.        
  98.         //This updates the rotation matrix
  99.         private Matrix UpdateMatrix()
  100.         {
  101.             Vector3 matrixOrigin = new Vector3(origin, 0);
  102.             rotationMatrix = Matrix.CreateTranslation(-matrixOrigin)
  103.                     * Matrix.CreateRotationZ(radians)
  104.                     * Matrix.CreateTranslation(matrixOrigin);
  105.  
  106.             return rotationMatrix;
  107.         }
  108.  
  109.         public Vector2 AngleToVector(float angle)
  110.         {
  111.             return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
  112.         }
  113.  
  114.         //Method to update all of the sprites
  115.         public void updateSprites(GameTime theGameTime)
  116.         {
  117.  
  118.             KeyboardState theCurrentKeyboardState = Keyboard.GetState();
  119.  
  120.             testCollision();
  121.  
  122.             if (!dead)
  123.                 Move(theCurrentKeyboardState, theGameTime);
  124.  
  125.             UpdateSprite();
  126.             UpdateOrigin();
  127.             UpdateMatrix();
  128.  
  129.             waitToPress(theGameTime, theCurrentKeyboardState);
  130.         }
  131.  
  132.  
  133.         //Method for moving the sprites around
  134.         public void Move(KeyboardState aCurrentKeyboardState, GameTime theGameTime)
  135.         {
  136.             elapsedTime += theGameTime.ElapsedGameTime.TotalMilliseconds;
  137.  
  138.             if (aCurrentKeyboardState.IsKeyDown(Keys.Down) && elapsedTime >= 200)
  139.             {
  140.                 MoveBlockY(increment);
  141.                 elapsedTime -= 100;
  142.             }
  143.             else if(elapsedTime >= 1000)
  144.             {
  145.                 MoveBlockY(increment);
  146.  
  147.                 Console.WriteLine("Sprite " + 1 + " at " + SpriteList[0].Position.X + " , " + SpriteList[0].Position.Y);
  148.                 Console.WriteLine("Sprite " + 2 + " at " + SpriteList[1].Position.X + " , " + SpriteList[1].Position.Y);
  149.                 Console.WriteLine("Sprite " + 3 + " at " + SpriteList[2].Position.X + " , " + SpriteList[2].Position.Y);
  150.                 Console.WriteLine("Sprite " + 4 + " at " + SpriteList[3].Position.X + " , " + SpriteList[3].Position.Y);
  151.                 Console.WriteLine("-----------------------------------------");
  152.  
  153.                 elapsedTime -= 1000;
  154.             }
  155.  
  156.             if (aCurrentKeyboardState.IsKeyDown(Keys.Left) && pressAgain && !leftWallCollide)
  157.             {
  158.                 MoveBlockX(-increment);
  159.                 pressAgain = false;
  160.             }
  161.  
  162.             else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) && pressAgain && !rightWallCollide)
  163.             {
  164.                 MoveBlockX(increment);
  165.                 pressAgain = false;
  166.             }
  167.  
  168.             if (aCurrentKeyboardState.IsKeyDown(Keys.Up) && pressAgain)
  169.             {
  170.                 radians += (float)Math.PI / 2;
  171.                 pressAgain = false;
  172.             }
  173.  
  174.         }
  175.  
  176.        
  177.         //Methods to move all sprites in X or Y directions
  178.         private void MoveBlockY(int Y)
  179.         {
  180.                 position.Y += Y;
  181.         }
  182.  
  183.         private void MoveBlockX(int X)
  184.         {
  185.                 position.X += X;
  186.         }
  187.  
  188.         private void waitToPress(GameTime theGameTime, KeyboardState theCurrentKeyboardState) {
  189.  
  190.             if (pressAgain == false)
  191.             {
  192.                 if (theCurrentKeyboardState.IsKeyUp(Keys.Left)
  193.                     && theCurrentKeyboardState.IsKeyUp(Keys.Right)
  194.                     && theCurrentKeyboardState.IsKeyUp(Keys.Up))
  195.                 {
  196.                     pressAgain = true;
  197.                 }
  198.             }
  199.  
  200.         }// end waitToPress()
  201.  
  202.         private void testCollision()
  203.         {
  204.             //check wall collision
  205.             for (int i = 0; i < SpriteList.Length; i++)
  206.             {
  207.                 if (SpriteList[i].BoundingBox().Intersects(GCat.wallLeft.BoundingBox()))
  208.                 {
  209.                     leftWallCollide = true;
  210.                     break;
  211.                 }
  212.                 else
  213.                     leftWallCollide = false;
  214.  
  215.                 if (SpriteList[i].BoundingBox().Intersects(GCat.wallRight.BoundingBox()))
  216.                 {
  217.                     rightWallCollide = true;
  218.                     break;
  219.                 }
  220.                 else
  221.                     rightWallCollide = false;
  222.             }
  223.  
  224.             //check floor collision
  225.             for (int i = 0; i < SpriteList.Length; i++)
  226.             {
  227.                 if (SpriteList[i].Position.Y >= BOTTOM_LIMIT)
  228.                 {
  229.                     position.Y = (BOTTOM_LIMIT - SpriteList[i].offset.Y - 50);
  230.                     dead = true;
  231.                     break;
  232.                 }
  233.             }
  234.         }
  235.  
  236.     }
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment