Guest User

Dave input xna

a guest
Mar 27th, 2012
37
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //// This is your GamePlayScreen class, followed by my input class:
  2.  
  3.  
  4. #region File Description
  5. //-----------------------------------------------------------------------------
  6. // GameplayScreen.cs
  7. //
  8. // Microsoft XNA Community Game Platform
  9. // Copyright (C) Microsoft Corporation. All rights reserved.
  10. //-----------------------------------------------------------------------------
  11. #endregion
  12.  
  13. #region Using Statements
  14. using System;
  15. using System.Threading;
  16. using Microsoft.Xna.Framework;
  17. using Microsoft.Xna.Framework.Content;
  18. using Microsoft.Xna.Framework.Graphics;
  19. using Microsoft.Xna.Framework.Input;
  20. using Microsoft.Xna.Framework.Media;
  21. using Drawing;
  22. #endregion
  23.  
  24. namespace MyXnaGame
  25. {
  26.     /// <summary>
  27.     /// This screen implements the actual game logic. It is just a
  28.     /// placeholder to get the idea across: you'll probably want to
  29.     /// put some more interesting gameplay in here!
  30.     /// </summary>
  31.     class GameplayScreen : GameScreen
  32.     {
  33.         #region Fields
  34. //        Sprite mySprite;
  35.        
  36.         private Ball myBall;
  37.         private Bat rightBat;
  38.         private Bat leftBat;
  39.        
  40.  
  41.         HudContainer hudContainer = new HudContainer();
  42.        
  43.         SafeArea safeArea;
  44.         Vector2 safeTopLeft = Vector2.Zero; // (2.0f * (safeArea.dx), 1.5f * (safeArea.dy));
  45.         ContentManager content;
  46.         SpriteFont gameFont;
  47.         SpriteFont hudFont;
  48.         private Texture2D backgroundTexture;
  49.         public Song PlayingSong { get; private set; }
  50.  
  51.         Vector2 enemyPosition = new Vector2(100, 100);
  52.  
  53.         Random random = new Random();
  54.  
  55.         float pauseAlpha;
  56.  
  57.         #endregion
  58.  
  59.         #region Initialization
  60.  
  61.  
  62.         /// <summary>
  63.         /// Constructor.
  64.         /// </summary>
  65.         public GameplayScreen()
  66.         {
  67.             //
  68.             safeArea = new SafeArea();
  69. //            mySprite = new Sprite();
  70.             TransitionOnTime = TimeSpan.FromSeconds(1.5);
  71.             TransitionOffTime = TimeSpan.FromSeconds(0.5);
  72.         }
  73.  
  74.  
  75.  
  76.         public override void LoadContent()
  77.         {
  78.             if (content == null)
  79.                 content = new ContentManager(ScreenManager.Game.Services, "Content");
  80.             DrawingHelper.Initialize(ScreenManager.GraphicsDevice);
  81.             //
  82.             PlayingSong = content.Load<Song>(@"sfx/boomer");
  83.             MediaPlayer.Play(PlayingSong);
  84.             //
  85.             backgroundTexture = content.Load<Texture2D>(@"gfx/mm7Background");
  86.             //
  87.             gameFont = content.Load<SpriteFont>("gamefont");
  88.             hudFont = content.Load<SpriteFont>("menufont");
  89.             //
  90.             safeArea.LoadGraphicsContent(ScreenManager.GraphicsDevice);
  91.             //
  92.             hudContainer.LoadGraphicsContent(ScreenManager.GraphicsDevice, hudFont, safeArea);
  93.             var hudScore = new HudTextComponent("SCORE: 100", HudComponent.PresetPosition.TopLeft);
  94.             var hudHealth = new HudTextComponent("HEALTH: 300", HudComponent.PresetPosition.TopRight);
  95.             var hudLives = new HudTextComponent("LIVES: 3", HudComponent.PresetPosition.MiddleLeft);
  96.             var hudAmmo = new HudTextComponent("Ammo: 447", HudComponent.PresetPosition.MiddleRight);
  97.             var hudName = new HudTextComponent("Angry Zombie...", HudComponent.PresetPosition.BottomLeft);
  98.             var hudInfo = new HudTextComponent("...Ninja Cats!", HudComponent.PresetPosition.BottomRight);
  99.  
  100.            
  101. /*          I've commented out the hud variables -DV
  102.             hudContainer.Add(hudScore);
  103.             hudContainer.Add(hudHealth);
  104.             hudContainer.Add(hudLives);
  105.             hudContainer.Add(hudAmmo); ;
  106.             hudContainer.Add(hudName);
  107.             hudContainer.Add(hudInfo);
  108.  */          //
  109.            
  110. //            mySprite.LoadContent(content);
  111.  
  112.              myBall = new Ball(content, new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width,
  113.                 ScreenManager.GraphicsDevice.Viewport.Height));        
  114.             rightBat = new Bat(content, new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width,
  115.                 ScreenManager.GraphicsDevice.Viewport.Height), true);
  116.             leftBat = new Bat(content, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width,
  117.                 ScreenManager.GraphicsDevice.Viewport.Height), false);
  118.          
  119.            
  120.  
  121.             // A real game would probably have more content than this sample, so
  122.             // it would take longer to load. We simulate that by delaying for a
  123.             // while, giving you a chance to admire the beautiful loading screen.
  124.             Thread.Sleep(1000);
  125.  
  126.             // once the load has finished, we use ResetElapsedTime to tell the game's
  127.             // timing mechanism that we have just finished a very long frame, and that
  128.             // it should not try to catch up.
  129.             ScreenManager.Game.ResetElapsedTime();
  130.         }
  131.  
  132.  
  133.         public override void UnloadContent()
  134.         {
  135.             content.Unload();
  136.         }
  137.  
  138.  
  139.         /// <summary>
  140.         /// Updates the state of the game. This method checks the GameScreen.IsActive
  141.         /// property, so the game will stop updating when the pause menu is active,
  142.         /// or if you tab away to a different application.
  143.         /// </summary>
  144.         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  145.                                                        bool coveredByOtherScreen)
  146.         {
  147.             base.Update(gameTime, otherScreenHasFocus, false);
  148.  
  149.             // Gradually fade in or out depending on whether we are covered by the pause screen.
  150.             if (coveredByOtherScreen)
  151.                 pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
  152.             else
  153.                 pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
  154.  
  155.             if (IsActive)
  156.             {
  157.                 // Apply some random jitter to make the enemy move around.
  158.                 const float randomization = 10;
  159.  
  160.                 enemyPosition.X += (float)(random.NextDouble() - 0.5) * randomization;
  161.                 enemyPosition.Y += (float)(random.NextDouble() - 0.5) * randomization;
  162.  
  163.                 // Apply a stabilizing force to stop the enemy moving off the screen.
  164.                 Vector2 targetPosition = new Vector2(
  165.                     ScreenManager.GraphicsDevice.Viewport.Width / 2 - gameFont.MeasureString("XNA Basic Starter Kit!").X / 2,
  166.                     200);
  167.  
  168.                 enemyPosition = Vector2.Lerp(enemyPosition, targetPosition, 0.05f);
  169.  
  170. //                mySprite.Update(gameTime);
  171.                 myBall.UpdatePosition();
  172.                
  173.       //          rightBat.Update();
  174.  
  175.             }
  176.         }
  177.  
  178.  
  179.         /// <summary>
  180.         /// Lets the game respond to player input. Unlike the Update method,
  181.         /// this will only be called when the gameplay screen is active.
  182.         /// </summary>
  183.         public override void HandleInput(InputState input) //, GameTime gameTime)
  184.         {
  185.             if (input == null)
  186.                 throw new ArgumentNullException("input");
  187.  
  188.             // Look up inputs for the active player profile.
  189.             int playerIndex = (int)ControllingPlayer.Value;
  190.  
  191.             KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
  192.             GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
  193.  
  194.             // The game pauses either if the user presses the pause button, or if
  195.             // they unplug the active gamepad. This requires us to keep track of
  196.             // whether a gamepad was ever plugged in, because we don't want to pause
  197.             // on PC if they are playing with a keyboard and have no gamepad at all!
  198.             bool gamePadDisconnected = !gamePadState.IsConnected &&
  199.                                        input.GamePadWasConnected[playerIndex];
  200.  
  201.             if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
  202.             {
  203.                 ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
  204.             }
  205.             /*            else
  206.                         {
  207.                             mySprite.HandleInput(keyboardState, gamePadState, input, safeArea);
  208.                         }
  209.              */
  210.             else
  211.             {
  212.                
  213.             }
  214.         }
  215.  
  216.  
  217.         public override void Draw(GameTime gameTime)
  218.         {
  219.  
  220.  
  221.             // Our player and enemy are both actually just text strings.
  222.             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  223.  
  224.             spriteBatch.Begin();
  225.  
  226.             spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, 800, 600), Color.White);
  227.  
  228.             // draw hud
  229.             hudContainer.Draw(spriteBatch);
  230.  
  231.             spriteBatch.DrawString(gameFont, "XNA Basic Starter Kit!",
  232.                                    enemyPosition, Color.Black);
  233.  
  234. #if DEBUG     // I've temporarily commented out the safeArea debug -DV  
  235. //            safeArea.Draw(spriteBatch);
  236. #endif
  237.  
  238.             // debug info
  239.             var safeAreaInfo = "dx=" + safeArea.dx + ",dy=" + safeArea.dy
  240.                 + ",w=" + safeArea.width + ",h=" + safeArea.height;
  241.                 //+ ", H = " + playerHorizDirection
  242.                 //+ ", V = " + playerVertDirection;
  243.             spriteBatch.DrawString(gameFont, safeAreaInfo, safeTopLeft, Color.Green);
  244.  
  245.             //
  246. //            mySprite.Draw(spriteBatch);
  247.             //
  248.             myBall.Draw(spriteBatch);
  249.            
  250.             rightBat.Draw(spriteBatch);
  251.             leftBat.Draw(spriteBatch);
  252.  
  253.            
  254.             spriteBatch.End();
  255.  
  256.            // If the game is transitioning on or off, fade it out to black.
  257.             if (TransitionPosition > 0 || pauseAlpha > 0)
  258.             {
  259.                 float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
  260.  
  261.                 ScreenManager.FadeBackBufferToBlack(alpha);
  262.             }
  263.         }
  264.  
  265.  
  266.         #endregion
  267.     }
  268. }
  269.  
  270.  
  271. ----------------------------------------------------------
  272.  
  273.  
  274. using System;
  275. using System.Collections.Generic;
  276. using System.Linq;
  277. using System.Text;
  278. using Microsoft.Xna.Framework.Input;  
  279.  
  280. namespace MyXnaGame
  281. {
  282.     class Input
  283.     {
  284.         private KeyboardState keyboardState;
  285.         private KeyboardState lastState;
  286.  
  287.         public Input()
  288.         {
  289.             keyboardState = Keyboard.GetState();
  290.             lastState = keyboardState;
  291.         }
  292.  
  293.         public void Update()
  294.         {
  295.             lastState = keyboardState;
  296.             keyboardState = Keyboard.GetState();
  297.         }
  298.  
  299.         public bool RightUp
  300.         {
  301.             get
  302.             {
  303.                 return keyboardState.IsKeyDown(Keys.Up);
  304.             }
  305.         }
  306.  
  307.         public bool RightDown
  308.         {
  309.             get
  310.             {
  311.                 return keyboardState.IsKeyDown(Keys.Down);
  312.             }
  313.         }
  314.  
  315.         public bool LeftUp
  316.         {
  317.             get
  318.             {
  319.                 return keyboardState.IsKeyDown(Keys.W);
  320.             }
  321.         }
  322.  
  323.         public bool LeftDown
  324.         {
  325.             get
  326.             {
  327.                 return keyboardState.IsKeyDown(Keys.S);
  328.             }
  329.         }
  330.     }
  331. }
RAW Paste Data