Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Storage;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- namespace CatTetris
- {
- class Block
- {
- Vector2 origin;
- Vector2 position;
- public Sprite sprite1;
- public Sprite sprite2;
- public Sprite sprite3;
- public Sprite sprite4;
- public Vector2 mDirection = Vector2.Zero;
- public Vector2 mSpeed = Vector2.Zero;
- //Amount of Incremental movement
- public int increment = 50;
- //Limits of the play area
- public int LEFT_LIMIT = 150;
- public int RIGHT_LIMIT = 450;
- public int BOTTOM_LIMIT = 600;
- //Collision booleans
- public bool pressAgain = true;
- public bool leftWallCollide = false;
- public bool rightWallCollide = false;
- public bool dead = false;
- //So I don't have to call every single sprite at once
- public Sprite[] SpriteList;
- //Matrix for rotation on the sprite batch
- public Matrix rotationMatrix = Matrix.CreateRotationZ(0);
- //radians for the rotation
- public float radians = 0f;
- public double elapsedTime = 0;
- //a tetrimo is made up of 4 sprites, here we load them individually
- public void LoadSprites(Sprite sprite1, Sprite sprite2, Sprite sprite3, Sprite sprite4,
- Vector2 pos1, Vector2 pos2, Vector2 pos3, Vector2 pos4)
- {
- this.sprite1 = sprite1;
- this.sprite2 = sprite2;
- this.sprite3 = sprite3;
- this.sprite4 = sprite4;
- SpriteList = new Sprite[4] { sprite1, sprite2, sprite3, sprite4 };
- this.position = pos1;
- sprite1.Position = pos1;
- sprite2.Position = pos2;
- sprite3.Position = pos3;
- sprite4.Position = pos4;
- sprite2.offset = new Vector2(0,-50);
- sprite3.offset = new Vector2(-50,0);
- sprite4.offset = new Vector2(50,0);
- }
- public void Draw(SpriteBatch theSpriteBatch)
- {
- for (int i = 0; i < SpriteList.Length; i++ )
- {
- SpriteList[i].Draw(theSpriteBatch);
- }
- }
- private void UpdateSprite()
- {
- for (int i = 0; i < SpriteList.Length; i++ )
- {
- SpriteList[i].Update(position, SpriteList[i].offset);
- }
- }
- private void UpdateOrigin()
- {
- origin = new Vector2((int)(position.X + sprite1.mSpriteTexture.Width / 2),
- (int)(position.Y + sprite1.mSpriteTexture.Height / 2));
- }
- //This updates the rotation matrix
- private Matrix UpdateMatrix()
- {
- Vector3 matrixOrigin = new Vector3(origin, 0);
- rotationMatrix = Matrix.CreateTranslation(-matrixOrigin)
- * Matrix.CreateRotationZ(radians)
- * Matrix.CreateTranslation(matrixOrigin);
- return rotationMatrix;
- }
- public Vector2 AngleToVector(float angle)
- {
- return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
- }
- //Method to update all of the sprites
- public void updateSprites(GameTime theGameTime)
- {
- KeyboardState theCurrentKeyboardState = Keyboard.GetState();
- testCollision();
- if (!dead)
- Move(theCurrentKeyboardState, theGameTime);
- UpdateSprite();
- UpdateOrigin();
- UpdateMatrix();
- waitToPress(theGameTime, theCurrentKeyboardState);
- }
- //Method for moving the sprites around
- public void Move(KeyboardState aCurrentKeyboardState, GameTime theGameTime)
- {
- elapsedTime += theGameTime.ElapsedGameTime.TotalMilliseconds;
- if (aCurrentKeyboardState.IsKeyDown(Keys.Down) && elapsedTime >= 200)
- {
- MoveBlockY(increment);
- elapsedTime -= 100;
- }
- else if(elapsedTime >= 1000)
- {
- MoveBlockY(increment);
- Console.WriteLine("Sprite " + 1 + " at " + SpriteList[0].Position.X + " , " + SpriteList[0].Position.Y);
- Console.WriteLine("Sprite " + 2 + " at " + SpriteList[1].Position.X + " , " + SpriteList[1].Position.Y);
- Console.WriteLine("Sprite " + 3 + " at " + SpriteList[2].Position.X + " , " + SpriteList[2].Position.Y);
- Console.WriteLine("Sprite " + 4 + " at " + SpriteList[3].Position.X + " , " + SpriteList[3].Position.Y);
- Console.WriteLine("-----------------------------------------");
- elapsedTime -= 1000;
- }
- if (aCurrentKeyboardState.IsKeyDown(Keys.Left) && pressAgain && !leftWallCollide)
- {
- MoveBlockX(-increment);
- pressAgain = false;
- }
- else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) && pressAgain && !rightWallCollide)
- {
- MoveBlockX(increment);
- pressAgain = false;
- }
- if (aCurrentKeyboardState.IsKeyDown(Keys.Up) && pressAgain)
- {
- radians += (float)Math.PI / 2;
- pressAgain = false;
- }
- }
- //Methods to move all sprites in X or Y directions
- private void MoveBlockY(int Y)
- {
- position.Y += Y;
- }
- private void MoveBlockX(int X)
- {
- position.X += X;
- }
- private void waitToPress(GameTime theGameTime, KeyboardState theCurrentKeyboardState) {
- if (pressAgain == false)
- {
- if (theCurrentKeyboardState.IsKeyUp(Keys.Left)
- && theCurrentKeyboardState.IsKeyUp(Keys.Right)
- && theCurrentKeyboardState.IsKeyUp(Keys.Up))
- {
- pressAgain = true;
- }
- }
- }// end waitToPress()
- private void testCollision()
- {
- //check wall collision
- for (int i = 0; i < SpriteList.Length; i++)
- {
- if (SpriteList[i].BoundingBox().Intersects(GCat.wallLeft.BoundingBox()))
- {
- leftWallCollide = true;
- break;
- }
- else
- leftWallCollide = false;
- if (SpriteList[i].BoundingBox().Intersects(GCat.wallRight.BoundingBox()))
- {
- rightWallCollide = true;
- break;
- }
- else
- rightWallCollide = false;
- }
- //check floor collision
- for (int i = 0; i < SpriteList.Length; i++)
- {
- if (SpriteList[i].Position.Y >= BOTTOM_LIMIT)
- {
- position.Y = (BOTTOM_LIMIT - SpriteList[i].offset.Y - 50);
- dead = true;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment