Ecrofirt

GameScreen

Apr 5th, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.34 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.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13. namespace Rings
  14. {
  15.  
  16.     class GameScreen : Screen
  17.     {
  18.  
  19.         //TEMPORARY VARIABLE DECLARATIONS AND ASSIGNMENTS
  20.         List<Ring> rings = new List<Ring>();    //A list of all of the rings in the game screen
  21.         Texture2D myWedge;                      //A texture for all wedges. Will be completely redone later
  22.         Vector2 textureOrigin;                  //A Vector2 that represents the origin of the myWedge texture
  23.         Vector2 screenCenter;                   //A Vector2 that represents the center of the screen
  24.         Factory factory;                        //A factory which will be responsible for generating rings, wedges, and balls
  25.  
  26.         Random nextRand = new Random();
  27.  
  28.         bool drawWedges = true;                 //A bool that represents whether or not anything should be drawn on screen
  29.  
  30.         public int Level { get; set; }          //A property that represents the current level in the game
  31.        
  32.         /// <summary>
  33.         /// The 'standard' constructor
  34.         /// </summary>
  35.         /// <param name="graphics">The game's graphics device manage</param>
  36.         /// <param name="content">The game's content pipeline</param>
  37.         /// <param name="spriteBatch">The game's spritebatch</param>
  38.         /// <param name="theGame">The game itself (used for exiting the application</param>
  39.         public GameScreen(GraphicsDeviceManager graphics, ContentManager content, SpriteBatch spriteBatch, Game1 theGame) : base(graphics, content, spriteBatch, theGame)
  40.         {
  41.             //TEMPORARY CODE
  42.  
  43.             Initialize();
  44.             LoadContent();
  45.            
  46.         }
  47.  
  48.         /// <summary>
  49.         /// This function initializes the variables used by the GameScreen class
  50.         /// </summary>
  51.         protected override void Initialize()
  52.         {
  53.             base.Initialize();
  54.  
  55.             //Sets the initial level to 1
  56.             this.Level = 1;
  57.  
  58.             //Creates a new Factory, which is responsible for creating balls, wedges, and rings
  59.             factory = new Factory(this);
  60.  
  61.             //Creates the list of rings which will be drawn to the screen
  62.             rings = factory.CreateRings();
  63.  
  64.             //A Vector2 that represents the center of the screen
  65.             screenCenter = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
  66.  
  67.         }
  68.  
  69.  
  70.         /// <summary>
  71.         /// This function loads the GameScreen content into the game's ContentManager
  72.         /// </summary>
  73.         protected override void LoadContent()
  74.         {
  75.             base.LoadContent();
  76.  
  77.             myWedge = Content.Load<Texture2D>("wedge6");
  78.             textureOrigin = new Vector2(myWedge.Width, myWedge.Height / 2);
  79.         }
  80.  
  81.  
  82.         /// <summary>
  83.         /// This function updates the game logic for the GameScreen class
  84.         /// </summary>
  85.         /// <param name="gameTime">Provides a snapshot of timing values</param>
  86.         public override void Update(GameTime gameTime)
  87.         {
  88.             //return;
  89.             //Grabs the current keyboard state
  90.             KeyboardState currentKeyboardState = Keyboard.GetState();
  91.  
  92.             //Updates the rotation on all visible rings
  93.             for (int i = 0; i < rings.Count; i++)
  94.             {
  95.                 //rings[i].Rotation++;
  96.                 if (rings[i].Visible == true)
  97.                 {
  98.                     rings[i].Rotation -= 2;
  99.                 }
  100.             }
  101.  
  102.             //Quits the game if the escape key is pressed
  103.             if (currentKeyboardState.IsKeyDown(Keys.Escape))
  104.                 theGame.Exit();
  105.  
  106.             //toggles whether or not to draw wedges
  107.             if (currentKeyboardState.IsKeyDown(Keys.Space) && !oldKeyboardState.IsKeyDown(Keys.Space))
  108.                 drawWedges = !drawWedges;
  109.  
  110.             //Updates the old keyboard state to what the keyboard state was for this update
  111.             oldKeyboardState = currentKeyboardState;
  112.            
  113.  
  114.            
  115.         }
  116.  
  117.         /// <summary>
  118.         /// Draws the GameScreen
  119.         /// </summary>
  120.         /// <param name="gameTime"></param>
  121.         public override void Draw(GameTime gameTime)
  122.         {
  123.             base.Draw(gameTime);
  124.             //return;
  125.  
  126.             //TEMPORARY CODE
  127.             //These variables  will be used so that values don't have to constantly be retrieved from objects
  128.             Position wedgePosition;
  129.             int ringCount;
  130.             int wedgeCount;
  131.             //END TEMPORARY CODE
  132.  
  133.             //If wedges should be drawn, do this
  134.             if (drawWedges)
  135.             {
  136.                 //Grabs the count of rings from the rings list
  137.                 ringCount = rings.Count;
  138.  
  139.                 //loops through all of the rings
  140.                 for (int i = 0; i < ringCount; i++)
  141.                 {
  142.                     //Grabs the number of wedges in each ring
  143.                     wedgeCount = rings[i].Wedges.Count;
  144.  
  145.                     //Loops through all of the wedges in the ring
  146.                     for (int j = 0; j < wedgeCount; j++)
  147.                     {
  148.  
  149.  
  150.                         //If the wedges is visible, draw it
  151.                         if (rings[i].Wedges[j].Visible)
  152.                         {
  153.                            
  154.                             //Grabs the wedge's current position once, so it doesn't have to be repeatedly retrieved
  155.                             wedgePosition = rings[i].getWedgePosition(j);
  156.  
  157.                             //draw the wedge to the screen at the proper rotation
  158.                             spriteBatch.Draw(myWedge, screenCenter + wedgePosition.LocationVector, null, Color.White, wedgePosition.Radians, textureOrigin, 1f, SpriteEffects.None, 0f);
  159.  
  160.                         }//ends the if (rings[i].Wedges[j].Visible) block
  161.  
  162.                     }//ends the for (int j = 0; j < wedgeCount; j++) loop
  163.  
  164.                 }//ends the for (int i = 0; i < ringCount; i++) loop
  165.  
  166.             }//ends the if (drawWedges) block
  167.            
  168.         }
  169.  
  170.  
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment